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 pathwebpack.config.spec.ts
384 lines (311 loc) · 19.3 KB
/
webpack.config.spec.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
import * as proxyquire from 'proxyquire';
import * as nsWebpackIndex from '../index';
import { join } from 'path';
// With noCallThru enabled, `proxyquire` will not fall back to requiring the real module to populate properties that are not mocked.
// This allows us to mock packages that are not available in node_modules.
// In case you want to enable fallback for a specific object, just add `'@noCallThru': false`.
proxyquire.noCallThru();
class EmptyClass { };
let angularCompilerOptions: any;
class AngularCompilerStub {
constructor(options) {
angularCompilerOptions = options;
}
};
let terserOptions: any;
class TerserJsStub {
constructor(options) {
terserOptions = options;
}
};
const nativeScriptDevWebpack = {
GenerateBundleStarterPlugin: EmptyClass,
GenerateNativeScriptEntryPointsPlugin: EmptyClass,
WatchStateLoggerPlugin: EmptyClass,
PlatformFSPlugin: EmptyClass,
getAppPath: () => 'app',
getEntryModule: () => 'EntryModule',
hasRootLevelScopedModules: () => false,
getResolver: () => null,
getConvertedExternals: nsWebpackIndex.getConvertedExternals,
getSourceMapFilename: nsWebpackIndex.getSourceMapFilename,
processAppComponents: nsWebpackIndex.processAppComponents,
getUserDefinedEntries: nsWebpackIndex.getUserDefinedEntries,
};
const emptyObject = {};
const FakeAotTransformerFlag = "aot";
const FakeHmrTransformerFlag = "hmr";
const FakeLazyTransformerFlag = "lazy";
const webpackConfigAngular = proxyquire('./webpack.angular', {
'nativescript-dev-webpack': nativeScriptDevWebpack,
'nativescript-dev-webpack/nativescript-target': emptyObject,
'nativescript-dev-webpack/transformers/ns-replace-bootstrap': { nsReplaceBootstrap: () => { return FakeAotTransformerFlag } },
'nativescript-dev-webpack/transformers/ns-replace-lazy-loader': { nsReplaceLazyLoader: () => { return FakeLazyTransformerFlag } },
'nativescript-dev-webpack/transformers/ns-support-hmr-ng': { nsSupportHmrNg: () => { return FakeHmrTransformerFlag } },
'nativescript-dev-webpack/utils/ast-utils': { getMainModulePath: () => { return "fakePath"; } },
'nativescript-dev-webpack/utils/tsconfig-utils': { getNoEmitOnErrorFromTSConfig: () => { return false; } },
'nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin': { getAngularCompilerPlugin: () => { return AngularCompilerStub; } },
'@ngtools/webpack': {
AngularCompilerPlugin: AngularCompilerStub
},
'terser-webpack-plugin': TerserJsStub
});
const webpackConfigTypeScript = proxyquire('./webpack.typescript', {
'nativescript-dev-webpack': nativeScriptDevWebpack,
'nativescript-dev-webpack/nativescript-target': emptyObject,
'nativescript-dev-webpack/utils/tsconfig-utils': { getNoEmitOnErrorFromTSConfig: () => { return false; } },
'terser-webpack-plugin': TerserJsStub
});
const webpackConfigJavaScript = proxyquire('./webpack.javascript', {
'nativescript-dev-webpack': nativeScriptDevWebpack,
'nativescript-dev-webpack/nativescript-target': emptyObject,
'terser-webpack-plugin': TerserJsStub
});
const webpackConfigVue = proxyquire('./webpack.vue', {
'nativescript-dev-webpack': nativeScriptDevWebpack,
'nativescript-dev-webpack/nativescript-target': emptyObject,
'vue-loader/lib/plugin': EmptyClass,
'nativescript-vue-template-compiler': emptyObject,
'terser-webpack-plugin': TerserJsStub
});
describe('webpack.config.js', () => {
const getInput = (options: { platform: string, aot?: boolean, hmr?: boolean, externals?: string[], sourceMap?: boolean, hiddenSourceMap?: boolean | string }) => {
const input: any = Object.assign({}, options);;
input[options.platform] = true;
return input;
};
[
{ type: 'javascript', webpackConfig: webpackConfigJavaScript },
{ type: 'typescript', webpackConfig: webpackConfigTypeScript },
{ type: 'angular', webpackConfig: webpackConfigAngular },
{ type: 'vue', webpackConfig: webpackConfigVue }
].forEach(element => {
const { type, webpackConfig } = element;
[
'android',
'ios'
].forEach(platform => {
describe(`verify externals for webpack.${type}.js (${platform})`, () => {
afterEach(() => {
nativeScriptDevWebpack.getConvertedExternals = nsWebpackIndex.getConvertedExternals;
});
it('returns empty array when externals are not passed', () => {
const input = getInput({ platform });
const config = webpackConfig(input);
expect(config.externals).toEqual([]);
});
it('calls getConvertedExternals to convert externals', () => {
let isCalled = false;
nativeScriptDevWebpack.getConvertedExternals = () => {
isCalled = true;
return [];
};
const input = getInput({ platform, externals: ['nativescript-vue'] });
webpackConfig(input);
expect(isCalled).toBe(true, 'Webpack.config.js must use the getConvertedExternals method');
});
if (platform === "ios") {
it('has inspector_modules entry when tns-core-modules are not externals', () => {
const input = getInput({ platform, externals: ['nativescript-vue'] });
const config = webpackConfig(input);
expect(config.entry["tns_modules/tns-core-modules/inspector_modules"]).toEqual("inspector_modules");
});
it('does not have inspector_modules entry when tns-core-modules are externals', () => {
const input = getInput({ platform, externals: ['tns-core-modules'] });
const config = webpackConfig(input);
expect(config.entry["tns_modules/tns-core-modules/inspector_modules"]).toBeUndefined();
});
}
[
{
input: ['nativescript-vue'],
expectedOutput: [/^nativescript-vue((\/.*)|$)/]
},
{
input: ['nativescript-vue', 'nativescript-angular'],
expectedOutput: [/^nativescript-vue((\/.*)|$)/, /^nativescript-angular((\/.*)|$)/]
},
].forEach(testCase => {
const input = getInput({ platform, externals: testCase.input });
it(`are correct regular expressions, for input ${testCase.input}`, () => {
const config = webpackConfig(input);
expect(config.externals).toEqual(testCase.expectedOutput);
});
});
});
if (type === 'angular') {
describe(`angular transformers for webpack.${type}.js (${platform})`, () => {
beforeEach(() => {
angularCompilerOptions = null;
});
it("should be empty by default", () => {
const input = getInput({ platform });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(0);
});
it("should contain the AOT transformer when the AOT flag is passed", () => {
const input = getInput({ platform, aot: true });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(1);
expect(angularCompilerOptions.platformTransformers[0]).toEqual(FakeAotTransformerFlag);
});
it("should contain the HMR transformer when the HMR flag is passed", () => {
const input = getInput({ platform, hmr: true });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(1);
expect(angularCompilerOptions.platformTransformers[0]).toEqual(FakeHmrTransformerFlag);
});
it("should contain the Lazy transformer when the @angular/core is an external module", () => {
const input = getInput({ platform, externals: ["@angular/core"] });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(1);
expect(angularCompilerOptions.platformTransformers[0]).toEqual(FakeLazyTransformerFlag);
});
it("should contain the AOT + HMR transformers when the AOT and HMR flags are passed", () => {
const input = getInput({ platform, aot: true, hmr: true });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(2);
expect(angularCompilerOptions.platformTransformers).toContain(FakeAotTransformerFlag);
expect(angularCompilerOptions.platformTransformers).toContain(FakeHmrTransformerFlag);
});
it("should set the AOT transformer before the HMR one when the AOT and HMR flags are passed", () => {
const input = getInput({ platform, aot: true, hmr: true });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(2);
expect(angularCompilerOptions.platformTransformers[0]).toEqual(FakeAotTransformerFlag);
expect(angularCompilerOptions.platformTransformers[1]).toEqual(FakeHmrTransformerFlag);
});
it("should contain the AOT + Lazy transformers when the AOT flag is passed and @angular/core is an external module", () => {
const input = getInput({ platform, aot: true, externals: ["@angular/core"] });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(2);
expect(angularCompilerOptions.platformTransformers).toContain(FakeAotTransformerFlag);
expect(angularCompilerOptions.platformTransformers).toContain(FakeLazyTransformerFlag);
});
it("should contain the HMR + Lazy transformers when the HMR flag is passed and @angular/core is an external module", () => {
const input = getInput({ platform, hmr: true, externals: ["@angular/core"] });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(2);
expect(angularCompilerOptions.platformTransformers).toContain(FakeHmrTransformerFlag);
expect(angularCompilerOptions.platformTransformers).toContain(FakeLazyTransformerFlag);
});
it("should contain the AOT + HMR + Lazy transformers when the AOT and HMR flags are passed and @angular/core is an external module", () => {
const input = getInput({ platform, aot: true, hmr: true, externals: ["@angular/core"] });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(3);
expect(angularCompilerOptions.platformTransformers).toContain(FakeAotTransformerFlag);
expect(angularCompilerOptions.platformTransformers).toContain(FakeHmrTransformerFlag);
expect(angularCompilerOptions.platformTransformers).toContain(FakeLazyTransformerFlag);
});
it("should contain the AOT + HMR + Lazy transformers in the proper order when the AOT and HMR flags are passed and @angular/core is an external module", () => {
const input = getInput({ platform, aot: true, hmr: true, externals: ["@angular/core"] });
webpackConfig(input);
expect(angularCompilerOptions).toBeDefined();
expect(angularCompilerOptions.platformTransformers).toBeDefined();
expect(angularCompilerOptions.platformTransformers.length).toEqual(3);
expect(angularCompilerOptions.platformTransformers[0]).toEqual(FakeAotTransformerFlag);
expect(angularCompilerOptions.platformTransformers[1]).toEqual(FakeHmrTransformerFlag);
expect(angularCompilerOptions.platformTransformers[2]).toEqual(FakeLazyTransformerFlag);
});
});
}
describe(`source map for webpack.${type}.js (${platform})`, () => {
beforeEach(() => {
terserOptions = null;
});
it("should not set source maps without the flag", () => {
const input = getInput({ platform, sourceMap: false });
const config = webpackConfig(input);
expect(config.devtool).toEqual("none");
expect(terserOptions.sourceMap).toBeFalsy();
expect(terserOptions.terserOptions.output.semicolons).toBeTruthy();
expect(config.output.sourceMapFilename).toEqual("[file].map");
});
it("should set inline-source-map devtool", () => {
const input = getInput({ platform, sourceMap: true });
const config = webpackConfig(input);
expect(config.devtool).toEqual("inline-source-map");
expect(terserOptions.sourceMap).toBeTruthy();
expect(terserOptions.terserOptions.output.semicolons).toBeFalsy();
expect(config.output.sourceMapFilename).toEqual("[file].map");
});
});
describe(`hidden source map for webpack.${type}.js (${platform})`, () => {
beforeEach(() => {
terserOptions = null;
});
it("should not set source maps without the flag", () => {
const input = getInput({ platform, hiddenSourceMap: false });
const config = webpackConfig(input);
expect(config.devtool).toEqual("none");
expect(terserOptions.sourceMap).toBeFalsy();
expect(terserOptions.terserOptions.output.semicolons).toBeTruthy();
expect(config.output.sourceMapFilename).toEqual("[file].map");
});
it("should set hidden-source-map devtool and the default sourceMap folder", () => {
const input = getInput({ platform, hiddenSourceMap: true });
const config = webpackConfig(input);
expect(config.devtool).toEqual("hidden-source-map");
expect(terserOptions.sourceMap).toBeTruthy();
expect(terserOptions.terserOptions.output.semicolons).toBeFalsy();
expect(config.output.sourceMapFilename).toEqual(join("..", "sourceMap", "[file].map"));
});
it("should override the sourceMap property and the default sourceMap folder", () => {
const input = getInput({ platform, sourceMap: true, hiddenSourceMap: true });
const config = webpackConfig(input);
expect(config.devtool).toEqual("hidden-source-map");
expect(terserOptions.sourceMap).toBeTruthy();
expect(terserOptions.terserOptions.output.semicolons).toBeFalsy();
expect(config.output.sourceMapFilename).toEqual(join("..", "sourceMap", "[file].map"));
});
it("should set hidden-source-map devtool and override the sourceMapFilename", () => {
const newSourceMapFolder = "myCoolSourceMapFolder";
const input = getInput({ platform, sourceMap: true, hiddenSourceMap: newSourceMapFolder });
const config = webpackConfig(input);
expect(config.devtool).toEqual("hidden-source-map");
expect(terserOptions.sourceMap).toBeTruthy();
expect(terserOptions.terserOptions.output.semicolons).toBeFalsy();
expect(config.output.sourceMapFilename).toEqual(join("..", newSourceMapFolder, "[file].map"));
});
});
describe(`alias for webpack.${type}.js (${platform})`, () => {
it('should add alias when @nativescript/core is at the root of node_modules', () => {
nativeScriptDevWebpack.hasRootLevelScopedModules = () => true;
const input = getInput({ platform });
const config = webpackConfig(input);
expect(config.resolve.alias['tns-core-modules']).toBe('@nativescript/core');
if (type === 'angular') {
expect(config.resolve.alias['nativescript-angular']).toBe('@nativescript/angular');
}
});
it('shouldn\'t add alias when @nativescript/core is not at the root of node_modules', () => {
nativeScriptDevWebpack.hasRootLevelScopedModules = () => false;
const input = getInput({ platform });
const config = webpackConfig(input);
expect(config.resolve.alias['tns-core-modules']).toBeUndefined();
if (type === 'angular') {
expect(config.resolve.alias['nativescript-angular']).toBeUndefined();
}
});
});
});
});
});