-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathwebpack-compiler-service.ts
90 lines (75 loc) · 4.85 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
import { Yok } from "../../../lib/common/yok";
import { WebpackCompilerService } from "../../../lib/services/webpack/webpack-compiler-service";
import { assert } from "chai";
const iOSPlatformName = "ios";
const androidPlatformName = "android";
const chunkFiles = ["bundle.js", "runtime.js", "vendor.js"];
function getAllEmittedFiles(hash: string) {
return ["bundle.js", "runtime.js", `bundle.${hash}.hot-update.js`, `${hash}.hot-update.json`];
}
function createTestInjector(): IInjector {
const testInjector = new Yok();
testInjector.register("webpackCompilerService", WebpackCompilerService);
testInjector.register("childProcess", {});
testInjector.register("hooksService", {});
testInjector.register("hostInfo", {});
testInjector.register("logger", {});
testInjector.register("errors", {});
testInjector.register("packageInstallationManager", {});
testInjector.register("mobileHelper", {});
testInjector.register("cleanupService", {});
return testInjector;
}
describe("WebpackCompilerService", () => {
let testInjector: IInjector = null;
let webpackCompilerService: WebpackCompilerService = null;
beforeEach(() => {
testInjector = createTestInjector();
webpackCompilerService = testInjector.resolve(WebpackCompilerService);
});
describe("getUpdatedEmittedFiles", () => {
// backwards compatibility with old versions of nativescript-dev-webpack
it("should return only hot updates when nextHash is not provided", async () => {
const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, null, iOSPlatformName);
const expectedEmittedFiles = ['bundle.hash1.hot-update.js', 'hash1.hot-update.json'];
assert.deepEqual(result.emittedFiles, expectedEmittedFiles);
});
// 2 successful webpack compilations
it("should return only hot updates when nextHash is provided", async () => {
webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2", iOSPlatformName);
const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash2"), chunkFiles, "hash3", iOSPlatformName);
assert.deepEqual(result.emittedFiles, ['bundle.hash2.hot-update.js', 'hash2.hot-update.json']);
});
// 1 successful webpack compilation, n compilations with no emitted files
it("should return all files when there is a webpack compilation with no emitted files", () => {
webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2", iOSPlatformName);
const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash4"), chunkFiles, "hash5", iOSPlatformName);
assert.deepEqual(result.emittedFiles, ['bundle.js', 'runtime.js', 'bundle.hash4.hot-update.js', 'hash4.hot-update.json']);
});
// 1 successful webpack compilation, n compilations with no emitted files, 1 successful webpack compilation
it("should return only hot updates after fixing the compilation error", () => {
webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2", iOSPlatformName);
webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash5"), chunkFiles, "hash6", iOSPlatformName);
const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash6"), chunkFiles, "hash7", iOSPlatformName);
assert.deepEqual(result.emittedFiles, ['bundle.hash6.hot-update.js', 'hash6.hot-update.json']);
});
// 1 webpack compilation with no emitted files
it("should return all files when first compilation on livesync change is not successful", () => {
(<any>webpackCompilerService).expectedHashes = {
"ios": "hash1"
};
const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2", iOSPlatformName);
assert.deepEqual(result.emittedFiles, ["bundle.hash1.hot-update.js", "hash1.hot-update.json"]);
});
it("should return correct hashes when there are more than one platform", () => {
webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2", iOSPlatformName);
webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash3"), chunkFiles, "hash4", androidPlatformName);
webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash2"), chunkFiles, "hash5", iOSPlatformName);
webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash4"), chunkFiles, "hash6", androidPlatformName);
const iOSResult = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash5"), chunkFiles, "hash7", iOSPlatformName);
assert.deepEqual(iOSResult.emittedFiles, ["bundle.hash5.hot-update.js", "hash5.hot-update.json"]);
const androidResult = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash6"), chunkFiles, "hash8", androidPlatformName);
assert.deepEqual(androidResult.emittedFiles, ["bundle.hash6.hot-update.js", "hash6.hot-update.json"]);
});
});
});