forked from cockpit-project/cockpit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-make
executable file
·253 lines (206 loc) · 7.75 KB
/
webpack-make
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
247
248
249
250
251
252
253
#!/usr/bin/env node
/*
* Builds with webpack and generates a Makefile include that
* lists all dependencies, inputs, outputs, and installable files
*/
function fatal(message, code) {
console.log("webpack-make: " + message);
process.exit(code || 1);
}
var webpack, path, stdio, fs;
try {
webpack = require("webpack");
path = require("path");
stdio = require("stdio");
fs = require("fs");
} catch(ex) {
fatal(ex.message, 127); /* missing looks for this */
}
var ops = stdio.getopt({
deps: { key: "d", args: 1, description: "Output dependencies in Makefile format" }
});
if (!ops.args || ops.args.length < 1 || ops.args.length > 2) {
console.log("usage: webpack-make config [section]");
process.exit(2);
}
var srcdir = process.env.SRCDIR;
var makefile = ops.deps;
var prefix = "packages";
if (makefile) {
prefix = makefile.split("/").slice(-2, -1)[0];
process.env["ONLYDIR"] = prefix + "/";
}
var cwd = process.cwd();
var config_path = path.resolve(cwd, ops.args[0]);
var config = require(config_path);
// An alternate jshint reporter when used with make
config.jshint.reporter = function(errors) {
var loader = this;
errors.forEach(function(err) {
console.log(loader.resource + ":" + err.line + ":" + err.character + ": " + err.reason);
});
process.exit(1);
};
// The latest input file time updated and used below
var latest = fs.statSync(config_path).mtime;
webpack(config, function(err, stats) {
// process.stdout.write(stats.toString({colors: true}) + "\n");
if (err) {
console.log(JSON.stringify(err));
process.exit(1);
return;
}
// Failure exit code when compilation fails
if (stats.hasErrors() || stats.hasWarnings()) {
console.log(stats.toString("normal"));
process.exit(1);
return;
}
if (makefile)
generateDeps(makefile, stats);
});
function generateDeps(makefile, stats) {
// Note that these are cheap ways of doing a set
var inputs = { };
var po_locations = { };
var outputs = { };
var installs = { };
var tests = { };
var debugs = { };
var pkgdir = path.dirname(makefile);
stats.compilation.modules.forEach(function(module) {
var parts = module.identifier().split("!");
parts.concat(module.fileDependencies || []).forEach(function(part) {
var input = part.split("?")[0];
maybePushInput(inputs, po_locations, input);
});
});
stats.compilation.fileDependencies.forEach(function(file) {
maybePushInput(inputs, po_locations, file);
});
// All the dependent files
var asset, output;
var now = Math.floor(Date.now() / 1000);
for(asset in stats.compilation.assets) {
output = path.join(stats.compilation.outputOptions.path, asset);
fs.utimesSync(output, now, now);
/*
* The manifest.json files are installed and built by Makefile.am.
* When webpack is used on its own it *should* copy these files
* (albeit poorly without substitution) ... but now that make is
* in play lets have them generated by make
*/
if (endsWith(output, "manifest.json"))
fs.unlinkSync(output);
else
outputs[output] = output;
if (output.indexOf("/test-") !== -1 && endsWith(output, ".html")) {
tests[output] = output;
continue;
}
var install = output;
if (!endsWith(output, "manifest.json") &&
!endsWith(output, "override.json") &&
!endsWith(output, "shell/index.html") &&
!endsWith(output, "shell/stub.html") &&
!endsWith(output, "shell/simple.html") &&
!endsWith(output, ".png") &&
!endsWith(output, ".map") &&
!endsWith(output, ".gif")) {
install += ".gz";
}
// Debug output and tests gets installed separately
if (endsWith(install, ".map"))
debugs[install] = install;
else if (output.indexOf("/test-") === -1)
installs[install] = install;
}
// Finalize all the sets into arrays
inputs = Object.keys(inputs).sort();
po_locations = Object.keys(po_locations).sort();
outputs = Object.keys(outputs).sort();
installs = Object.keys(installs).sort();
tests = Object.keys(tests).sort();
debugs = Object.keys(debugs).sort();
var lines = [ "# Generated Makefile data for " + prefix, "# Stamp: " + latest, "" ];
function makeArray(name, values) {
lines.push(name + " = \\");
values.forEach(function(value) {
lines.push("\t" + value + " \\");
});
lines.push("\t$(NULL)");
lines.push("");
}
makeArray(prefix + "_INPUTS", inputs);
makeArray(prefix + "_OUTPUTS", outputs);
lines.push(prefix + "_PO = " +
path.join(pkgdir, "po.js") +
" $(patsubst %," + path.join(pkgdir, "po.%.js") + ",$(LINGUAS))");
lines.push("");
installs.push("$(addsuffix .gz,$(" + prefix + "_PO))");
makeArray(prefix + "_INSTALL", installs);
makeArray(prefix + "_DEBUG", debugs);
makeArray(prefix + "_TESTS", tests);
var filters = po_locations.map(function (l) { return "-N " + l; });
if (prefix === "shell")
filters.push("$(addprefix -N ,$(shell find pkg/ -name manifest.json))");
lines.push(path.join(pkgdir, "%.po") + ": po/%.po");
lines.push("\t$(AM_V_GEN) $(MKDIR_P) $(dir $@) && \\");
lines.push("\t$(MSGGREP) " + filters.join(" ") + " $< > $@.tmp && mv $@.tmp $@");
lines.push("");
lines.push(makefile + ": $(WEBPACK_CONFIG) $(" + prefix + "_INPUTS)");
lines.push("\t$(WEBPACK_RULE) -d " + makefile + " $(WEBPACK_CONFIG)");
lines.push("");
outputs.forEach(function(name) {
lines.push(name + ": " + makefile);
lines.push("")
});
inputs.forEach(function(name) {
lines.push(name + ":");
lines.push("")
});
lines.push("WEBPACK_INPUTS += $(" + prefix + "_INPUTS)");
lines.push("WEBPACK_OUTPUTS += $(" + prefix + "_OUTPUTS)");
lines.push("WEBPACK_PO += $(" + prefix + "_PO)");
lines.push("WEBPACK_INSTALL += $(" + prefix + "_INSTALL)");
lines.push("WEBPACK_DEBUG += $(" + prefix + "_DEBUG)");
lines.push("TESTS += $(" + prefix + "_TESTS)");
lines.push("");
lines.push(prefix + ": " + makefile + " $(" + prefix + "_OUTPUTS) $(" + prefix + "_INSTALL)");
lines.push("\t@true");
lines.push("clean-" + prefix + ": ");
lines.push("\trm -rf $(" + prefix + "_OUTPUTS) $(" + prefix + "_INSTALL) " + makefile);
lines.push("all-local:: " + prefix);
lines.push("\t@true");
lines.push("clean-local:: clean-" + prefix);
lines.push("\t@true");
data = lines.join("\n") + "\n";
fs.writeFileSync(makefile, data);
}
function maybePushInput(inputs, po_locations, input) {
var po_location;
// Don't include node_modules files or external refs
if (input.split(path.sep).indexOf("node_modules") === -1 &&
input.split(path.sep).indexOf("bower_components") === -1 &&
!endsWith(input, '/') &&
!endsWith(input, 'manifest.json.in') &&
input.indexOf("external ") !== 0 &&
input.indexOf("multi ") !== 0) {
// The latest modified date
var stats = fs.statSync(input);
if (stats.mtime > latest)
latest = stats.mtime;
// Qualify the input file and add it
input = path.relative(cwd, input);
if (srcdir && input.indexOf(srcdir) === 0) {
po_location = input.substr(srcdir.length+1);
input = "$(srcdir)" + input.substr(srcdir.length);
} else
po_location = input;
po_locations[po_location] = true;
inputs[input] = true;
}
}
function endsWith(string, suffix) {
return (string.lastIndexOf(suffix) === (string.length - suffix.length))
}