This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcompiler.js
125 lines (105 loc) · 4.46 KB
/
compiler.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
const utils = require("./utils");
const { spawn } = require("child_process");
const { resolve: pathResolve } = require("path");
const { existsSync } = require("fs");
const readline = require("readline");
const { messages } = require("../plugins/WatchStateLoggerPlugin");
const { buildEnvData } = require("./utils");
let hasBeenInvoked = false;
let webpackProcess = null;
let hasLoggedSnapshotWarningMessage = false;
exports.getWebpackProcess = function getWebpackProcess() {
return webpackProcess;
}
exports.runWebpackCompiler = function runWebpackCompiler(config, $projectData, $logger, hookArgs) {
if (config.bundle) {
return new Promise(function (resolveBase, rejectBase) {
if (webpackProcess) {
return resolveBase();
}
let isResolved = false;
function resolve() {
if (isResolved) return;
isResolved = true;
resolveBase();
}
function reject(error) {
if (isResolved) return;
isResolved = true;
rejectBase(error);
}
console.log(`Running webpack for ${config.platform}...`);
const projectDir = $projectData.projectDir;
const { platform, env } = config;
const envData = buildEnvData($projectData, platform, env);
const envParams = buildEnvCommandLineParams(config, envData, $logger);
const args = [
"--preserve-symlinks",
pathResolve(projectDir, "node_modules", "webpack", "bin", "webpack.js"),
`--config=${pathResolve(projectDir, "webpack.config.js")}`,
...(config.watch ? ["--watch"] : []),
...envParams,
].filter(a => !!a);
const childProcess = spawn("node", args, {
// Watch opens IPC so we don't mess with the stdin/out/err.
// These will notify us for the webpack compilation states.
// Enables `childProcess.on("message", msg => ...)` kind of communication.
stdio: config.watch ? ["inherit", "inherit", "inherit", "ipc"] : "inherit",
cwd: projectDir
});
let isFirstWebpackWatchCompilation = true;
function resolveOnWebpackCompilationComplete(message) {
if (message === messages.compilationComplete) {
console.log("Webpack build done!");
resolve();
}
if (message.emittedFiles) {
if (isFirstWebpackWatchCompilation) {
isFirstWebpackWatchCompilation = false;
return;
}
if (hookArgs.filesToSync && hookArgs.startSyncFilesTimeout) {
hookArgs.filesToSync.push(...message.emittedFiles);
hookArgs.startSyncFilesTimeout();
}
}
}
if (config.watch) {
childProcess.on("message", resolveOnWebpackCompilationComplete);
if (webpackProcess) {
throw new Error("Webpack process already spawned.");
}
webpackProcess = childProcess;
}
childProcess.on("close", code => {
if (webpackProcess == childProcess) {
webpackProcess = null;
}
if (code === 0) {
resolve();
} else {
const error = new Error(`Executing webpack failed with exit code ${code}.`);
error.code = code;
reject(error);
}
});
});
}
}
function buildEnvCommandLineParams(config, envData, $logger) {
const envFlagNames = Object.keys(envData);
const snapshotEnvIndex = envFlagNames.indexOf("snapshot");
if (snapshotEnvIndex > -1 && !utils.shouldSnapshot(config)) {
logSnapshotWarningMessage($logger);
envFlagNames.splice(snapshotEnvIndex, 1);
}
return envFlagNames.map(item => `--env.${item}=${envData[item]}`);
}
function logSnapshotWarningMessage($logger) {
if (!hasLoggedSnapshotWarningMessage) {
$logger.warn("Stripping the snapshot flag. " +
"Bear in mind that snapshot is only available in release builds and " +
"is NOT available on Windows systems.");
hasLoggedSnapshotWarningMessage = true;
}
}