This repository was archived by the owner on Dec 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.tmpl
More file actions
238 lines (163 loc) · 6.94 KB
/
README.tmpl
File metadata and controls
238 lines (163 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<%=scriptName = pkg.name[0].toUpperCase() + pkg.name.slice(1)%>
<%=Array(scriptName.length+1).join("=")%>
> <%=pkg.description%>
Summary
-------
**<%=scriptName%>** bundles up your _node_ JavaScript files by following the `require("moduleName")` statements in your source file(s), and then bundles it all up, and _uglifies_ (minimizes) it into one JavaScript file.
In addition, **<%=pkg.name%>** can replace symbols found in the source file with JavaScript values _defined_ either with command line arguments, or through the use of another _node_ module.
Coupled with [`uglify-js`'](https://github.com/mishoo/UglifyJS) ability to remove dead code, **<%=pkg.name%>** acts as a pre-processor of sorts on your _uglified_ file when using the `--define` and `--define-module` options.
Installation
------------
~~~
% npm install -g <%=pkg.name%>
~~~
Command Line Usage
------------------
~~~
% <%=pkg.name%> -h
<%=usage%>
~~~
### Dependencies ###
These are installed when **<%=pkg.name%>** is installed.
<% var fw = Object.keys(pkg.dependencies).reduce(function (t, c) {
var len = c.length;
return len > t ? len : t;
}, 0),
deps = Object.keys(pkg.dependencies).map(function (key) {
var pad = Array(fw - key.length + 1).join(" ");
return key + ": " + pad + pkg.dependencies[key];
}).join("\n");%>
~~~
<%=deps%>
~~~
### Development Dependencies ###
Installed when you run `npm link` in the package directory.
<% var fw = Object.keys(pkg.devDependencies).reduce(function (t, c) {
var len = c.length;
return len > t ? len : t;
}, 0),
devDeps = Object.keys(pkg.devDependencies).map(function (key) {
var pad = Array(fw - key.length + 1).join(" ");
return key + ": " + pad + pkg.devDependencies[key];
}).join("\n");%>
~~~
<%=devDeps%>
~~~
Defines
-------
**<%=scriptName%>** now processes all `defines` itself, and the values returned from `defines` are translated into the appropriate AST structure. No need for your `define/define-module` to return an AST formatted array.
By having **<%=pkg.name%>** handle the `defines` in the pre-mangled/squeezed AST, if you supply the `--no-minify` flag to **<%=pkg.name%>** you can see the _beautified_ `uglify-js` generated output without any dead-code being removed (this is helpful when reviewing what your defines are returning/generating).
### Example ###
~~~js
// contents my-defines.js
var env = process.env.ENVIRONMENT || "dev",
tokens = {
appName: "My Great WebApp",
authorName: "Me"
},
isDebug = env === "dev"
;
module.exports = {
ENVIRONMENT: env,
DEBUG: isDebug,
TOKEN: function (key) {
return tokens[key];
},
MY_METHOD: function () {
return isDebug ?
function () {
return "is debug";
} :
function () {
return "is not debug";
}
},
CONFIG: {
foo: "foo",
baz: [1, 2, 3],
doSomething: function () {
return "something";
},
isDebug: isDebug
}
};
// contents of my-script.js
var appConfig = CONFIG;
if (ENVIRONMENT === "dev") {
console.log("A note to the developer...");
}
function MyClass () {}
MyClass.prototype = {
appName: TOKEN("appName"),
authorName: TOKEN("authorName"),
specialMethod: MY_METHOD(),
};
~~~
Running `<%=pkg.name%> my-script.js script.js -d ./my-defines.js -N` (assuming `ENVIRONMENT` is set to "dev") the following would be produced:
~~~js
// contents of script.js
var appConfig = {
foo: "foo",
baz: [ 1, 2, 3 ],
doSomething: function() {
return "something";
},
isDebug: true
};
if ("dev" === "dev") {
console.log("A note to the developer...");
}
function MyClass() {}
MyClass.prototype = {
appName: "My Great WebApp",
authorName: "Me",
specialMethod: function() {
return "is debug";
}
};
~~~
And the minified result (`<%=pkg.name%> my-script.js script.js -d ./my-defines.js`) would be:
~~~js
// contents of script.js
function b(){}var a={foo:"foo",baz:[1,2,3],doSomething:function(){return"something"},isDebug:true};console.log("A note to the developer..."),b.prototype={appName:"My Great WebApp",authorName:"Me",specialMethod:function(){return"is debug"}}
~~~
Note how the `if` block was removed around the `console.log` statement, since this is "dev" mode. If `ENVIRONMENT` was set to some other value, `console.log` would be removed too.
There are more examples in the "examples" directory. Install the development dependencies, and then run:
~~~
sake examples ENVIRONMENT=[dev|prod] BUILD_TYPE=[debug|release]
~~~
Embedding
---------
The **<%=pkg.name%>** `-E, --embed` option will _infuse_ required modules as `strings` and lazy-evaluate them when used in the final script. In the case of a browser, this means appending a script element to the `head` of the document temporarily. In other cases, **<%=pkg.name%>** goes to the _dark side_ and uses `eval`.
The **advantage** of embedding is that the required JavaScript modules are not evaluated until the strings are put into a script node, or `eval`'d, when needed. This facilitates faster loading of the _infused_ JavaScript file, since it is, mostly, one large string.
A Poor Man's Minifier
---------------------
You can get started quickly with **<%=pkg.name%>** by using the `-i, --infuse PATH` option:
~~~
infuse -i path/one.js -i path/two.js -i path/three.js > compiled.js
~~~
This basically **<%=pkg.name%>s** up the individual files and automatically _de-fuses_ them when the compiled script runs.
Caveats
-------
### Run-time variables ###
Currently, **<%=pkg.name%>** does not compute local variables, and can not determine require statements with variables defined at run-time.
For example, this **will not** work, and **<%=pkg.name%>** will throw an error:
~~~
// script.js
var pkgModule = require("./" + process.env.ENVIRONMENT + "-pkg.js");
~~~
This will however:
~~~
// script.js <%=pkg.name%>d with `--define PKG=prod-pkg.js`
var pkgModule = require(PKG);
~~~
### Global scope leakage ###
Since **<%=pkg.name%>** does not do any computing of local variables, it can not determine if variables leak outside of a required module's scope. In _node_ this is not so much a problem, since each module is run in it's own context and only the items explicitly exported through `module.exports` are exposed. **<%=scriptName%>** uses an anonymous function to simulate what _node_ does, however if the **<%=pkg.name%>d** module doesn't locally scope a variable with the `var` keyword, that variable will be set on the _global object_ (usually the window).
In the future, plans are to resolve the _caveats_ with more introspection of the AST parsed by _UglifyJS_. Stay tuned.
Report an Issue
---------------
* [Bugs](<%=pkg.bugs.url%>)
* Contact the author: <jhamlet@hamletink.com>
License
-------
<%=license.split("\n").map(function (line) { return "> " + line; }).join("\n")%>