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
255 lines (208 loc) · 12.2 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
import * as proxyquire from 'proxyquire';
import * as nsWebpackIndex from '../index';
// 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;
}
};
const nativeScriptDevWebpack = {
GenerateBundleStarterPlugin: EmptyClass,
WatchStateLoggerPlugin: EmptyClass,
PlatformFSPlugin: EmptyClass,
getAppPath: () => 'app',
getEntryModule: () => 'EntryModule',
getResolver: () => null,
getEntryPathRegExp: () => null,
getConvertedExternals: nsWebpackIndex.getConvertedExternals
};
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/plugins/NativeScriptAngularCompilerPlugin': { getAngularCompilerPlugin: () => { return AngularCompilerStub; } },
'@ngtools/webpack': {
AngularCompilerPlugin: AngularCompilerStub
}
});
const webpackConfigTypeScript = proxyquire('./webpack.typescript', {
'nativescript-dev-webpack': nativeScriptDevWebpack,
'nativescript-dev-webpack/nativescript-target': emptyObject,
});
const webpackConfigJavaScript = proxyquire('./webpack.javascript', {
'nativescript-dev-webpack': nativeScriptDevWebpack,
'nativescript-dev-webpack/nativescript-target': emptyObject,
});
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
});
describe('webpack.config.js', () => {
const getInput = (options: { platform: string, aot?: boolean, hmr?: boolean, externals?: string[] }) => {
const input: any = { aot: options.aot, hmr: options.hmr, externals: options.externals };
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;
describe(`verify externals for webpack.${type}.js`, () => {
[
'android',
'ios'
].forEach(platform => {
describe(`for ${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');
});
[
{
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);
});
});
});
});
});
});
[
'android',
'ios'
].forEach(platform => {
describe(`angular transformers (${platform})`, () => {
beforeEach(() => {
angularCompilerOptions = null;
});
it("should be empty by default", () => {
const input = getInput({ platform });
webpackConfigAngular(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 });
webpackConfigAngular(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 });
webpackConfigAngular(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"] });
webpackConfigAngular(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 });
webpackConfigAngular(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 });
webpackConfigAngular(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"] });
webpackConfigAngular(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"] });
webpackConfigAngular(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"] });
webpackConfigAngular(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"] });
webpackConfigAngular(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);
});
});
});
});