forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.js
More file actions
246 lines (206 loc) · 5.86 KB
/
webpack.js
File metadata and controls
246 lines (206 loc) · 5.86 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
239
240
241
242
243
244
245
246
#!/usr/bin/env node
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var path = require("path");
// Local version replace global one
try {
var localWebpack = require.resolve(path.join(process.cwd(), "node_modules", "webpack", "bin", "webpack.js"));
if(__filename != localWebpack) {
return require(localWebpack);
}
} catch(e) {}
var fs = require("fs");
var util = require("util");
var sprintf = require("sprintf").sprintf;
var argv = require("optimist")
.usage("webpack " + require("../package.json").version + "\n" +
"Usage: $0 <input> <output>")
.boolean("min")
.describe("min", "Minimize it with uglifyjs")
.default("min", false)
.boolean("filenames")
.describe("filenames", "Output Filenames Into File")
.default("filenames", false)
.string("options")
.describe("options", "Options JSON File")
.string("public-prefix")
.describe("public-prefix", "Path Prefix For JavaScript Loading")
.string("library")
.describe("library", "Stores the exports into this variable")
.boolean("colors")
.describe("colors", "Output Stats with colors")
.default("colors", false)
.boolean("single")
.describe("single", "Disable lazy loading")
.default("single", false)
.boolean("json")
.describe("json", "Output Stats as JSON")
.default("json", false)
.boolean("by-size")
.describe("by-size", "Sort modules by size in Stats")
.default("by-size", false)
.boolean("verbose")
.describe("verbose", "Output dependencies in Stats")
.default("verbose", false)
.boolean("profile")
.describe("profile", "Capture timings for modules")
.default("profile", false)
.string("alias")
.describe("alias", "Set a alias name for a module. ex. http=http-browserify")
.boolean("debug")
.describe("debug", "Prints debug info to output files")
.default("debug", false)
.boolean("watch")
.describe("watch", "Recompiles on changes (except loaders)")
.default("watch", false)
.describe("watch-delay", "Timeout to wait for the last change")
.default("watch", false)
.boolean("workers")
.describe("workers", "Use worker processes to be faster (BETA)")
.default("workers", false)
.boolean("progress")
.describe("progress", "Displays a progress while compiling")
.default("progress", false)
.demand(1) // DEPRECATED
.argv;
var input = argv._[0],
output = argv._[1];
var inputFile = input && input.split("!").pop()
if (inputFile && inputFile[0] !== '/' && inputFile[1] !== ':') {
input = input.split("!");
input.pop();
input.push(path.join(process.cwd(), inputFile));
input = input.join("!");
}
var outputFile = output && output.split("!").pop();
if (output && output[0] !== '/' && input[1] !== ':') {
output = output.split("!");
output.pop();
output.push(path.join(process.cwd(), outputFile));
output = output.join("!");
}
var options = {};
if(argv.options) {
options = require(path.join(process.cwd(), argv.options));
}
if(argv["public-prefix"]) {
options.publicPrefix = argv["public-prefix"];
}
if(argv.min) {
options.minimize = true;
}
if(argv.debug) {
options.debug = true;
}
if(argv.single) {
options.single = true;
}
if(argv.watch) {
options.watch = true;
}
if(argv.workers) {
options.workers = true;
}
if(argv.profile) {
options.profile = true;
}
if(argv["watch-delay"]) {
options.watchDelay = argv["watch-delay"];
}
if(argv.filenames) {
options.includeFilenames = true;
}
if(argv.library) {
options.library = argv.library;
}
if(argv.alias) {
if(typeof argv.alias === "string")
argv.alias = [argv.alias];
options.resolve = options.resolve || {};
options.resolve.alias = options.resolve.alias || {};
var aliasObj = options.resolve.alias;
argv.alias.forEach(function(alias) {
alias = alias.split("=");
aliasObj[alias[0]] = alias[1];
});
}
var webpack = require("../lib/webpack.js");
var formatOutput = require("../lib/formatOutput.js");
function c(str) {
return argv.colors ? str : "";
}
output = output || path.join(process.cwd(), "js", "web.js");
if(!options.outputDirectory) options.outputDirectory = path.dirname(output);
if(!options.output) options.output = path.basename(output);
if(!options.outputPostfix) options.outputPostfix = "." + path.basename(output);
if(!options.context) options.context = process.cwd();
// some listeners for the progress display
if(argv.progress) {
if(!options.events) options.events = new (require("events").EventEmitter)();
var events = options.events;
var nextUpdate = 0;
var sum = 0;
var finished = 0;
var chars = 0;
function print(force) {
if(!force && nextUpdate > new Date().getTime()) return;
nextUpdate = new Date().getTime() + 100;
var msg = "";
if(sum > 0) {
var precentage = Math.floor(finished*100/sum);
msg += "compiling... (" + c("\033[1m\033[33m");
msg += sprintf("%4s", finished+"") + "/" + sprintf("%4s", sum+"");
msg += " " + sprintf("%4s", precentage+"%");
msg += c("\033[39m\033[22m") + ")";
for(var i = 0; i < Math.floor(precentage/2); i++)
msg += "#";
}
for(var i = msg.length; i < chars; i++)
msg += " ";
for(var i = 0; i < chars; i++)
process.stderr.write("\b");
process.stderr.write(msg);
chars = msg.length;
}
events.on("task", function(name) {
sum++;
print();
});
events.on("task-end", function(name) {
finished++;
if(name) {
for(var i = 0; i < chars; i++)
process.stderr.write("\b \b");
process.stderr.write(name + " " + c("\033[1m\033[32m") + "done" + c("\033[39m\033[22m") + "\n");
chars = 0;
return print(true);
}
print();
});
events.on("bundle", function(name) {
sum = 0;
finished = 0;
for(var i = 0; i < chars; i++)
process.stderr.write("\b \b");
chars = 0;
});
}
// do the stuff
webpack(input, options, function(err, stats) {
if(err) {
console.error(err);
return;
}
if(argv.json)
console.log(JSON.stringify(stats, null, 2));
else {
console.log(formatOutput(stats, {
colors: argv.colors,
"by-size": argv["by-size"],
verbose: argv.verbose,
context: options.context
}));
}
});