-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathoptimize.js
executable file
·187 lines (166 loc) · 6.01 KB
/
optimize.js
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
#!/usr/bin/env node
/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.
3-Clause BSD License
</copyright> */
var URL = require("url2");
var Path = require("path");
var build = require("./lib/build");
var spinner = require("./lib/spinner");
var Location = require("./lib/location");
var mr = require("mr/require");
/**
* Optimize the package at the given location.
* @function
* @param {string} location An absolute path to a directory containing an app
* to optimize.
* @param {Object} [config] Configuration for optimization.
* @param {string} [config.buildLocation="builds"] An absolute or relative path for a
* directory to generate the optimized files in.
* @param {boolean} [config.minify=true] Whether to minify the files.
* @param {boolean} [config.lint=false] Whether to lint the files and output
* warnings.
* @param {boolean} [config.noCss=true] Whether to optimize CSS. Cannot handle
* some modern CSS, and so disabled by default.
* @param {string} [config.delimiter="@"] Symbol to use between the package
* name and the package hash, e.g. my-app@f7e7db2
* @param {Object} [config.out=spinner] An object to use for logging.
* @param {Function} [config.out.log] Variadic function that outputs a normal message.
* @param {Function} [config.out.warn] Variadic function that outputs a warning.
* @param {Function} [config.out.status] Variadic function that outputs a status
* message. These messages are temporary, high volume and should not be
* permanently displayed. If called with no arguments it should clear the
* displayed status.
* @return {Promise.<string>} A promise for the absolute path to the directory
* containing the built app.
*/
module.exports = optimize;
function optimize(location, config) {
config = config || {};
location = Location.fromPath(location, true);
if (config.out) {
// Fill in any missing output functions
if (!config.out.log) {
config.out.log = noop;
}
if (!config.out.warn) {
config.out.warn = noop;
}
if (!config.out.status) {
config.out.status = noop;
}
if (!config.out.error) {
config.out.error = noop;
}
}
// mainly here so that fs can be mocked out for testing
var fs = config.fs || require("q-io/fs");
function read(location) {
var path = Location.toPath(location);
return fs.read(path);
}
return mr.loadPackageLock({ location: location })
.then(function (packageLock) {
return build(location, {
// configurable
buildLocation: URL.resolve(location, (config.buildLocation || "builds") + "/"),
minify: config.minify !== void 0 ? !!config.minify : true,
lint: config.lint !== void 0 ? !!config.lint : false,
noCss: config.noCss !== void 0 ? !!config.noCss : false,
cssEmbedding: config.cssEmbedding !== void 0 ? !!config.cssEmbedding : true,
delimiter: config.delimiter !== void 0 ? config.delimiter : "@",
out: config.out || spinner,
fs: fs,
read: read,
// non-configurable
overlays: ["browser"],
production: true,
packageLock: packageLock
});
});
// Once implemented but currently disabled options:
//incremental: true,
//bundle: !!bundle,
//copyright: !!copyright,
//shared: !!shared,
//manifest: !!manifest,
//force: !!force,
}
function usage() {
console.log("Usage: mop [options] [<application> ...]");
console.log("");
//console.log(" -f --force");
//console.log(" -t --target ./builds/");
//console.log(" -s --shared for overlapping dependencies to be shared");
console.log(" -o --optimize 0 to disable optimizations");
console.log(" -l --lint to enable linter warnings");
//console.log(" -c --copyright to enable copyright message check");
//console.log(" -m --manifest to force an application cache to be made");
console.log(" -d --delimiter @ to use a different symbol");
console.log(" --no-css to disable CSS compression");
console.log(" --no-css-embedding to disable embedding of CSS in HTML");
console.log("");
}
function version() {
var config = require("./package.json");
console.log(config.title + " version " + config.version);
}
function main() {
var Options = require("optimist");
var argv = Options
.boolean([
//"f", "force",
"l", "lint",
//"c", "copyright",
//"s", "shared",
//"m", "manifest",
//"b", "bundle",
"h", "help",
"v", "version",
"css",
"css-embedding"
])
.default("optimize", "1")
.alias("o", "optimize")
.default("lint", false)
.alias("l", "lint")
.default("delimiter", "@")
.alias("d", "delimiter")
.default("css", true)
.default("css-embedding", true)
.argv;
if (argv.h || argv.help) {
return usage();
}
if (argv.v || argv.version) {
return version();
}
//var force = argv.f || argv.force;
//var shared = argv.s || argv.shared;
//var manifest = argv.m || argv.manifest;
//var copyright = argv.c || argv.copyright;
//var bundle = argv.b || argv.bundle;
var location = argv._.length ? argv._[0] : ".";
// convert path to locations
location = Path.resolve(process.cwd(), location);
// Exit code
var exitCode = 0;
return optimize(location, {
buildLocation: argv.t || argv.target,
minify: argv.optimize > 0,
lint: argv.l || argv.lint,
noCss: !argv.css,
cssEmbedding: argv["css-embedding"],
delimiter: argv.delimiter
}).catch(function (err) {
console.error(err);
exitCode = 1;
}).then(function () {
process.exit(exitCode);
});
}
function noop() {}
if (module === require.main) {
main();
}