1 |
|
2 | /*
|
3 | Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
|
4 |
|
5 | Redistribution and use in source and binary forms, with or without
|
6 | modification, are permitted provided that the following conditions are met:
|
7 |
|
8 | * Redistributions of source code must retain the above copyright
|
9 | notice, this list of conditions and the following disclaimer.
|
10 | * Redistributions in binary form must reproduce the above copyright
|
11 | notice, this list of conditions and the following disclaimer in the
|
12 | documentation and/or other materials provided with the distribution.
|
13 |
|
14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 | ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
18 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
19 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
20 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
21 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
22 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
23 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
24 | */
|
25 |
|
26 | /*jslint node:true */
|
27 |
|
28 | var fs = require('fs'),
|
29 | path = require('path'),
|
30 | root = path.join(path.dirname(fs.realpathSync(__filename)), '..'),
|
31 | esprima = require('esprima'),
|
32 | escodegen = require('escodegen'),
|
33 | optimist = require('optimist'),
|
34 | esmangle,
|
35 | argv,
|
36 | post,
|
37 | passes;
|
38 |
|
39 | esmangle = require(root);
|
40 |
|
41 | argv = optimist.usage("Usage: $0 file")
|
42 | .boolean('source-map')
|
43 | .describe('source-map', 'dump source-map')
|
44 | .string('o')
|
45 | .alias('o', 'output')
|
46 | .describe('o', 'output file')
|
47 | .argv;
|
48 |
|
49 | if (argv.output && Array.isArray(argv.output) && argv.output.length > 1) {
|
50 | optimist.showHelp();
|
51 | console.error('multiple output files are specified');
|
52 | process.exit(1);
|
53 | }
|
54 |
|
55 | function output(code) {
|
56 | if (argv.output) {
|
57 | fs.writeFileSync(argv.output, code);
|
58 | } else {
|
59 | console.log(code);
|
60 | }
|
61 | }
|
62 |
|
63 | function compile(content, filename) {
|
64 | var tree;
|
65 | tree = esprima.parse(content, { loc: true });
|
66 | tree = esmangle.optimize(tree, null, {
|
67 | destructive: true,
|
68 | directive: true
|
69 | });
|
70 | tree = esmangle.mangle(tree, {
|
71 | destructive: true
|
72 | });
|
73 | return escodegen.generate(tree, {
|
74 | format: {
|
75 | renumber: true,
|
76 | hexadecimal: true,
|
77 | escapeless: true,
|
78 | compact: true,
|
79 | semicolons: false,
|
80 | parentheses: false
|
81 | },
|
82 | sourceMap: argv['source-map'] && filename,
|
83 | directive: true
|
84 | });
|
85 | }
|
86 |
|
87 | if (argv._.length === 0) {
|
88 | // no file is specified, so use stdin as input
|
89 | (function () {
|
90 | var code = '';
|
91 | process.stdin.on('data', function (data) {
|
92 | code += data;
|
93 | });
|
94 | process.stdin.on('end', function (err) {
|
95 | output(compile(code, 'stdin'));
|
96 | });
|
97 | process.stdin.resume();
|
98 | }());
|
99 | } else {
|
100 | argv._.forEach(function (filename) {
|
101 | var content, result;
|
102 | content = fs.readFileSync(filename, 'utf-8');
|
103 | result = compile(content, filename);
|
104 | output(result);
|
105 | });
|
106 | }
|
107 | /* vim: set sw=4 ts=4 et tw=80 : */
|