-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathwebpack-compiler-service.ts
295 lines (251 loc) · 11.6 KB
/
webpack-compiler-service.ts
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import * as path from "path";
import * as child_process from "child_process";
import * as semver from "semver";
import { EventEmitter } from "events";
import { performanceLog } from "../../common/decorators";
import { WEBPACK_COMPILATION_COMPLETE, WEBPACK_PLUGIN_NAME } from "../../constants";
export class WebpackCompilerService extends EventEmitter implements IWebpackCompilerService {
private webpackProcesses: IDictionary<child_process.ChildProcess> = {};
private expectedHashes: IStringDictionary = {};
constructor(
private $errors: IErrors,
private $childProcess: IChildProcess,
public $hooksService: IHooksService,
public $hostInfo: IHostInfo,
private $logger: ILogger,
private $mobileHelper: Mobile.IMobileHelper,
private $cleanupService: ICleanupService,
private $packageInstallationManager: IPackageInstallationManager
) { super(); }
public async compileWithWatch(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<any> {
return new Promise(async (resolve, reject) => {
if (this.webpackProcesses[platformData.platformNameLowerCase]) {
resolve();
return;
}
let isFirstWebpackWatchCompilation = true;
prepareData.watch = true;
try {
const childProcess = await this.startWebpackProcess(platformData, projectData, prepareData);
childProcess.on("message", (message: string | IWebpackEmitMessage) => {
if (message === "Webpack compilation complete.") {
this.$logger.info("Webpack build done!");
resolve(childProcess);
}
message = message as IWebpackEmitMessage;
if (message.emittedFiles) {
if (isFirstWebpackWatchCompilation) {
isFirstWebpackWatchCompilation = false;
this.expectedHashes[platformData.platformNameLowerCase] = message.hash;
return;
}
let result;
if (prepareData.hmr) {
result = this.getUpdatedEmittedFiles(message.emittedFiles, message.chunkFiles, message.hash, platformData.platformNameLowerCase);
} else {
result = { emittedFiles: message.emittedFiles, fallbackFiles: <string[]>[], hash: "" };
}
const files = result.emittedFiles
.map((file: string) => path.join(platformData.appDestinationDirectoryPath, "app", file));
const fallbackFiles = result.fallbackFiles
.map((file: string) => path.join(platformData.appDestinationDirectoryPath, "app", file));
const data = {
files,
hasOnlyHotUpdateFiles: files.every(f => f.indexOf("hot-update") > -1),
hmrData: {
hash: result.hash,
fallbackFiles
},
platform: platformData.platformNameLowerCase
};
if (data.files.length) {
this.emit(WEBPACK_COMPILATION_COMPLETE, data);
}
}
});
childProcess.on("error", (err) => {
this.$logger.trace(`Unable to start webpack process in watch mode. Error is: ${err}`);
delete this.webpackProcesses[platformData.platformNameLowerCase];
reject(err);
});
childProcess.on("close", async (arg: any) => {
await this.$cleanupService.removeKillProcess(childProcess.pid.toString());
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
this.$logger.trace(`Webpack process exited with code ${exitCode} when we expected it to be long living with watch.`);
const error = new Error(`Executing webpack failed with exit code ${exitCode}.`);
error.code = exitCode;
delete this.webpackProcesses[platformData.platformNameLowerCase];
reject(error);
});
} catch (err) {
reject(err);
}
});
}
public async compileWithoutWatch(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<void> {
return new Promise(async (resolve, reject) => {
if (this.webpackProcesses[platformData.platformNameLowerCase]) {
resolve();
return;
}
try {
const childProcess = await this.startWebpackProcess(platformData, projectData, prepareData);
childProcess.on("error", (err) => {
this.$logger.trace(`Unable to start webpack process in non-watch mode. Error is: ${err}`);
delete this.webpackProcesses[platformData.platformNameLowerCase];
reject(err);
});
childProcess.on("close", async (arg: any) => {
await this.$cleanupService.removeKillProcess(childProcess.pid.toString());
delete this.webpackProcesses[platformData.platformNameLowerCase];
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
if (exitCode === 0) {
resolve();
} else {
const error = new Error(`Executing webpack failed with exit code ${exitCode}.`);
error.code = exitCode;
reject(error);
}
});
} catch (err) {
reject(err);
}
});
}
public async stopWebpackCompiler(platform: string): Promise<void> {
if (platform) {
await this.stopWebpackForPlatform(platform);
} else {
const webpackedPlatforms = Object.keys(this.webpackProcesses);
for (let i = 0; i < webpackedPlatforms.length; i++) {
await this.stopWebpackForPlatform(webpackedPlatforms[i]);
}
}
}
@performanceLog()
private async startWebpackProcess(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<child_process.ChildProcess> {
const envData = this.buildEnvData(platformData.platformNameLowerCase, projectData, prepareData);
const envParams = await this.buildEnvCommandLineParams(envData, platformData, projectData, prepareData);
const additionalNodeArgs = semver.major(process.version) <= 8 ? ["--harmony"] : [];
const args = [
...additionalNodeArgs,
"--preserve-symlinks",
path.join(projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"),
`--config=${path.join(projectData.projectDir, "webpack.config.js")}`,
...envParams
];
if (process.arch === "x64") {
args.unshift("--max_old_space_size=4096");
}
if (prepareData.watch) {
args.push("--watch");
}
const stdio = prepareData.watch ? ["inherit", "inherit", "inherit", "ipc"] : "inherit";
const childProcess = this.$childProcess.spawn(process.execPath, args, { cwd: projectData.projectDir, stdio });
this.webpackProcesses[platformData.platformNameLowerCase] = childProcess;
await this.$cleanupService.addKillProcess(childProcess.pid.toString());
return childProcess;
}
private buildEnvData(platform: string, projectData: IProjectData, prepareData: IPrepareData) {
const { env } = prepareData;
const envData = Object.assign({},
env,
{ [platform.toLowerCase()]: true }
);
const appPath = projectData.getAppDirectoryRelativePath();
const appResourcesPath = projectData.getAppResourcesRelativeDirectoryPath();
Object.assign(envData,
appPath && { appPath },
appResourcesPath && { appResourcesPath }
);
envData.verbose = envData.verbose || this.$logger.isVerbose();
envData.production = envData.production || prepareData.release;
// The snapshot generation is wrongly located in the Webpack plugin.
// It should be moved in the Native Prepare of the CLI or a Gradle task in the Runtime.
// As a workaround, we skip the mksnapshot, xxd and android-ndk calls based on skipNativePrepare.
// In this way the plugin will prepare only the snapshot JS entry without any native prepare and
// we will able to execute cloud builds with snapshot without having any local snapshot or Docker setup.
// TODO: Remove this flag when we remove the native part from the plugin.
envData.skipSnapshotTools = prepareData.nativePrepare && prepareData.nativePrepare.skipNativePrepare;
if (prepareData.env && (prepareData.env.sourceMap === false || prepareData.env.sourceMap === 'false')) {
delete envData.sourceMap;
} else if (!prepareData.release) {
envData.sourceMap = true;
}
return envData;
}
private async buildEnvCommandLineParams(envData: any, platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData) {
const envFlagNames = Object.keys(envData);
const canSnapshot = prepareData.release && this.$mobileHelper.isAndroidPlatform(platformData.normalizedPlatformName);
if (envData && envData.snapshot) {
if (!canSnapshot) {
this.$logger.warn("Stripping the snapshot flag. " +
"Bear in mind that snapshot is only available in Android release builds.");
envFlagNames.splice(envFlagNames.indexOf("snapshot"), 1);
} else if (this.$hostInfo.isWindows) {
const minWebpackPluginWithWinSnapshotsVersion = "1.3.0";
const installedWebpackPluginVersion = await this.$packageInstallationManager.getInstalledDependencyVersion(WEBPACK_PLUGIN_NAME, projectData.projectDir);
const hasWebpackPluginWithWinSnapshotsSupport = !!installedWebpackPluginVersion ? semver.gte(semver.coerce(installedWebpackPluginVersion), minWebpackPluginWithWinSnapshotsVersion) : true;
if (!hasWebpackPluginWithWinSnapshotsSupport) {
this.$errors.fail(`In order to generate Snapshots on Windows, please upgrade your Webpack plugin version (npm i nativescript-dev-webpack@latest).`);
}
}
}
const args: any[] = [];
envFlagNames.map(item => {
let envValue = envData[item];
if (typeof envValue === "undefined") {
return;
}
if (typeof envValue === "boolean") {
if (envValue) {
args.push(`--env.${item}`);
}
} else {
if (!Array.isArray(envValue)) {
envValue = [envValue];
}
envValue.map((value: any) => args.push(`--env.${item}=${value}`));
}
});
return args;
}
public getUpdatedEmittedFiles(allEmittedFiles: string[], chunkFiles: string[], nextHash: string, platform: string) {
const currentHash = this.getCurrentHotUpdateHash(allEmittedFiles);
// This logic is needed as there are already cases when webpack doesn't emit any files physically.
// We've set noEmitOnErrors in webpack.config.js based on noEmitOnError from tsconfig.json,
// so webpack doesn't emit any files when noEmitOnErrors: true is set in webpack.config.js and
// there is a compilation error in the source code. On the other side, hmr generates new hot-update files
// on every change and the hash of the next hmr update is written inside hot-update.json file.
// Although webpack doesn't emit any files, hmr hash is still generated. The hash is generated per compilation no matter
// if files will be emitted or not. This way, the first successful compilation after fixing the compilation error generates
// a hash that is not the same as the one expected in the latest emitted hot-update.json file.
// As a result, the hmr chain is broken and the changes are not applied.
const isHashValid = nextHash ? this.expectedHashes[platform] === currentHash : true;
this.expectedHashes[platform] = nextHash;
const emittedHotUpdatesAndAssets = isHashValid ? _.difference(allEmittedFiles, chunkFiles) : allEmittedFiles;
return { emittedFiles: emittedHotUpdatesAndAssets, fallbackFiles: chunkFiles, hash: currentHash };
}
private getCurrentHotUpdateHash(emittedFiles: string[]) {
let hotHash;
const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js'));
if (hotUpdateScripts && hotUpdateScripts.length) {
// the hash is the same for each hot update in the current compilation
const hotUpdateName = hotUpdateScripts[0];
const matcher = /^(.+)\.(.+)\.hot-update/gm;
const matches = matcher.exec(hotUpdateName);
hotHash = matches[2];
}
return hotHash || "";
}
private async stopWebpackForPlatform(platform: string) {
this.$logger.trace(`Stopping webpack watch for platform ${platform}.`);
const webpackProcess = this.webpackProcesses[platform];
await this.$cleanupService.removeKillProcess(webpackProcess.pid.toString());
if (webpackProcess) {
webpackProcess.kill("SIGINT");
delete this.webpackProcesses[platform];
}
}
}
$injector.register("webpackCompilerService", WebpackCompilerService);