forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack-compiler-service.ts
678 lines (596 loc) · 19.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import * as path from "path";
import * as child_process from "child_process";
import * as semver from "semver";
import * as _ from "lodash";
import { EventEmitter } from "events";
import { performanceLog } from "../../common/decorators";
import {
WEBPACK_COMPILATION_COMPLETE,
WEBPACK_PLUGIN_NAME,
PackageManagers,
CONFIG_FILE_NAME_DISPLAY,
} from "../../constants";
import {
IPackageManager,
IPackageInstallationManager,
IOptions,
} from "../../declarations";
import { IPlatformData } from "../../definitions/platform";
import { IProjectData } from "../../definitions/project";
import {
IDictionary,
IErrors,
IStringDictionary,
IChildProcess,
IFileSystem,
IHooksService,
IHostInfo,
} from "../../common/declarations";
import { ICleanupService } from "../../definitions/cleanup-service";
import { injector } from "../../common/yok";
import {
resolvePackagePath,
resolvePackageJSONPath,
} from "../../helpers/package-path-helper";
// todo: move out of here
interface IWebpackMessage<T = any> {
type: "compilation" | "hmr-status";
version?: number;
hash?: string;
data?: T;
}
interface IWebpackCompilation {
emittedAssets: string[];
staleAssets: string[];
}
export class WebpackCompilerService
extends EventEmitter
implements IWebpackCompilerService
{
private webpackProcesses: IDictionary<child_process.ChildProcess> = {};
private expectedHashes: IStringDictionary = {};
constructor(
private $options: IOptions,
private $errors: IErrors,
private $childProcess: IChildProcess,
public $fs: IFileSystem,
public $hooksService: IHooksService,
public $hostInfo: IHostInfo,
private $logger: ILogger,
private $mobileHelper: Mobile.IMobileHelper,
private $cleanupService: ICleanupService,
private $packageManager: IPackageManager,
private $packageInstallationManager: IPackageInstallationManager // private $sharedEventBus: ISharedEventBus
) {
super();
}
public async compileWithWatch(
platformData: IPlatformData,
projectData: IProjectData,
prepareData: IPrepareData
): Promise<any> {
return new Promise(async (resolve, reject) => {
if (this.webpackProcesses[platformData.platformNameLowerCase]) {
resolve(void 0);
return;
}
let isFirstWebpackWatchCompilation = true;
prepareData.watch = true;
try {
const childProcess = await this.startWebpackProcess(
platformData,
projectData,
prepareData
);
childProcess.stdout.on("data", function (data) {
process.stdout.write(data);
});
childProcess.stderr.on("data", function (data) {
process.stderr.write(data);
});
childProcess.on("message", (message: string | IWebpackEmitMessage) => {
this.$logger.trace("Message from webpack", message);
// if we are on webpack5 - we handle HMR in a slightly different way
if (
typeof message === "object" &&
"version" in message &&
"type" in message
) {
// first compilation can be ignored because it will be synced regardless
// handling it here would trigger 2 syncs
if (isFirstWebpackWatchCompilation) {
isFirstWebpackWatchCompilation = false;
resolve(childProcess);
return;
}
// if ((message as IWebpackMessage).type === "hmr-status") {
// // we pass message through our event-bus to be handled wherever needed
// // in this case webpack-hmr-status-service listens for this event
// this.$sharedEventBus.emit("webpack:hmr-status", message);
// return;
// }
return this.handleHMRMessage(
message as IWebpackMessage<IWebpackCompilation>,
platformData,
projectData,
prepareData
);
}
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] =
prepareData.hmr ? message.hash : "";
return;
}
// Persist the previousHash value before calling `this.getUpdatedEmittedFiles` as it will modify the expectedHashes object with the current hash
const previousHash =
this.expectedHashes[platformData.platformNameLowerCase];
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,
this.$options.hostProjectModuleName,
file
)
);
const fallbackFiles = result.fallbackFiles.map((file: string) =>
path.join(
platformData.appDestinationDirectoryPath,
this.$options.hostProjectModuleName,
file
)
);
const data = {
files,
hasOnlyHotUpdateFiles: files.every(
(f) => f.indexOf("hot-update") > -1
),
hmrData: {
hash: result.hash,
fallbackFiles,
},
platform: platformData.platformNameLowerCase,
};
this.$logger.trace("Generated data from webpack message:", data);
// the hash of the compilation is the same as the previous one and there are only hot updates produced
if (data.hasOnlyHotUpdateFiles && previousHash === message.hash) {
return;
}
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: any = 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: any = 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]);
}
}
}
private async shouldUsePreserveSymlinksOption(): Promise<boolean> {
// pnpm does not require symlink (https://github.com/nodejs/node-eps/issues/46#issuecomment-277373566)
// and it also does not work in some cases.
// Check https://github.com/NativeScript/nativescript-cli/issues/5259 for more information
const currentPackageManager =
await this.$packageManager.getPackageManagerName();
const res = currentPackageManager !== PackageManagers.pnpm;
return res;
}
@performanceLog()
private async startWebpackProcess(
platformData: IPlatformData,
projectData: IProjectData,
prepareData: IPrepareData
): Promise<child_process.ChildProcess> {
if (!this.$fs.exists(projectData.webpackConfigPath)) {
this.$errors.fail(
`The webpack configuration file ${projectData.webpackConfigPath} does not exist. Ensure the file exists, or update the path in ${CONFIG_FILE_NAME_DISPLAY}.`
);
}
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"] : [];
if (await this.shouldUsePreserveSymlinksOption()) {
additionalNodeArgs.push("--preserve-symlinks");
}
if (process.arch === "x64") {
additionalNodeArgs.unshift("--max_old_space_size=4096");
}
const args = [
...additionalNodeArgs,
this.getWebpackExecutablePath(projectData),
this.isWebpack5(projectData) ? `build` : null,
`--config=${projectData.webpackConfigPath}`,
...envParams,
].filter(Boolean);
if (prepareData.watch) {
args.push("--watch");
}
const stdio = prepareData.watch ? ["ipc"] : "inherit";
const options: { [key: string]: any } = {
cwd: projectData.projectDir,
stdio,
};
options.env = {
NATIVESCRIPT_WEBPACK_ENV: JSON.stringify(envData),
};
if (this.$hostInfo.isWindows) {
Object.assign(options.env, { APPDATA: process.env.appData });
}
if (this.$options.hostProjectPath) {
Object.assign(options.env, {
USER_PROJECT_PLATFORMS_ANDROID: this.$options.hostProjectPath,
USER_PROJECT_PLATFORMS_ANDROID_MODULE:
this.$options.hostProjectModuleName,
USER_PROJECT_PLATFORMS_IOS: this.$options.hostProjectPath,
});
}
const childProcess = this.$childProcess.spawn(
process.execPath,
args,
options
);
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 appId = projectData.projectIdentifiers[platform];
const appPath = projectData.getAppDirectoryRelativePath();
const appResourcesPath = projectData.getAppResourcesRelativeDirectoryPath();
Object.assign(
envData,
appId && { appId },
appPath && { appPath },
appResourcesPath && { appResourcesPath },
{
nativescriptLibPath: path.resolve(
__dirname,
"..",
"..",
"nativescript-cli-lib.js"
),
}
);
envData.verbose = envData.verbose || this.$logger.isVerbose();
envData.production = envData.production || prepareData.release;
// add the config file name to the env data so the webpack process can read the
// correct config file when resolving the CLI lib and the config service
// we are explicitly setting it to false to force using the defaults
envData.config =
process.env.NATIVESCRIPT_CONFIG_NAME ?? this.$options.config ?? "false";
// explicitly set the env variable
process.env.NATIVESCRIPT_CONFIG_NAME = envData.config;
// 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;
// only set sourceMap if not explicitly set through a flag
if (typeof prepareData?.env?.sourceMap === "undefined") {
if (!prepareData.release) {
envData.sourceMap = true;
}
}
// convert string to boolean
if (envData.sourceMap === "true" || envData.sourceMap === "false") {
envData.sourceMap = envData.sourceMap === "true";
}
if (prepareData.uniqueBundle > 0) {
envData.uniqueBundle = prepareData.uniqueBundle;
}
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 ${WEBPACK_PLUGIN_NAME}@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;
const fallbackFiles = chunkFiles.concat(
emittedHotUpdatesAndAssets.filter((f) => f.indexOf("hot-update") === -1)
);
return {
emittedFiles: emittedHotUpdatesAndAssets,
fallbackFiles,
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];
}
}
private handleHMRMessage(
message: IWebpackMessage,
platformData: IPlatformData,
projectData: IProjectData,
prepareData: IPrepareData
) {
// handle new webpack hmr packets
this.$logger.trace("Received message from webpack process:", message);
if (message.type !== "compilation") {
return;
}
this.$logger.trace("Webpack build done!");
const files = message.data.emittedAssets.map((asset: string) =>
path.join(
platformData.appDestinationDirectoryPath,
this.$options.hostProjectModuleName,
asset
)
);
const staleFiles = message.data.staleAssets.map((asset: string) =>
path.join(
platformData.appDestinationDirectoryPath,
this.$options.hostProjectModuleName,
asset
)
);
// extract last hash from emitted filenames
const lastHash = (() => {
const absoluteFileNameWithLastHash = files.find((fileName: string) =>
fileName.endsWith("hot-update.js")
);
if (!absoluteFileNameWithLastHash) {
return null;
}
const fileNameWithLastHash = path.basename(absoluteFileNameWithLastHash);
const matches = fileNameWithLastHash.match(/\.(.+).hot-update\.js/);
if (matches) {
return matches[1];
}
})();
if (!files.length) {
// ignore compilations if no new files are emitted
return;
}
this.emit(WEBPACK_COMPILATION_COMPLETE, {
files,
staleFiles,
hasOnlyHotUpdateFiles: prepareData.hmr,
hmrData: {
hash: lastHash || message.hash,
fallbackFiles: [],
},
platform: platformData.platformNameLowerCase,
});
}
private getWebpackExecutablePath(projectData: IProjectData): string {
if (this.isWebpack5(projectData)) {
const packagePath = resolvePackagePath("@nativescript/webpack", {
paths: [projectData.projectDir],
});
if (packagePath) {
return path.resolve(packagePath, "dist", "bin", "index.js");
}
}
const packagePath = resolvePackagePath("webpack", {
paths: [projectData.projectDir],
});
if (!packagePath) {
return "";
}
return path.resolve(packagePath, "bin", "webpack.js");
}
private isWebpack5(projectData: IProjectData): boolean {
const packageJSONPath = resolvePackageJSONPath("@nativescript/webpack", {
paths: [projectData.projectDir],
});
if (packageJSONPath) {
const packageData = this.$fs.readJson(packageJSONPath);
const ver = semver.coerce(packageData.version);
if (semver.satisfies(ver, ">= 5.0.0")) {
return true;
}
}
return false;
}
}
injector.register("webpackCompilerService", WebpackCompilerService);