-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
286 lines (233 loc) · 9.16 KB
/
index.test.js
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
'use strict';
const compileNodeModulesPlugin = require('./index');
const webpackOptions = {
dir: 'my-project',
isServer: false,
config: {
distDir: '.next',
target: 'server',
},
};
const createWebpackConfigLTE10 = () => ({
module: {
rules: [
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
include: ['/path/to/project'],
exclude: () => {},
use: {
loader: 'next-babel-loader',
options: {
isServer: true,
hasModern: false,
distDir: '/path/to/project/.next',
cwd: '/path/to/project',
cache: true,
},
},
},
],
},
externals: () => {},
});
const createWebpackConfigGT10 = () => ({
module: {
rules: [
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
include: ['/path/to/project'],
exclude: () => {},
use: [
'@next/react-refresh-utils/loader',
{
loader: 'next-babel-loader',
options: {
isServer: true,
hasModern: false,
distDir: '/path/to/project/.next',
cwd: '/path/to/project',
cache: true,
},
},
],
},
],
},
externals: () => {},
});
it('should duplicate the default JS rule (webpack <= 10)', () => {
const config = compileNodeModulesPlugin()().webpack(createWebpackConfigLTE10(), webpackOptions);
const rule = config.module.rules[1];
expect(rule.test.toString()).toBe('/\\.js$/');
expect(rule.include.toString()).toMatch(/\bnode_modules\b/);
expect(rule.exclude.toString()).toMatch(/\/path\/to\/project/);
expect(rule.exclude.toString()).toMatch(/\bnode-libs-browser\b/);
expect(rule.exclude.toString()).toMatch(/\bprocess\b/);
expect(rule.use).toHaveLength(1);
expect(rule.use[0].options.distDir).toMatch(/^my-project[\\/]\.next[\\/]cache[\\/]compile-node-modules-plugin$/);
expect(rule.use[0].options.caller).toEqual({ isNodeModule: true });
// Test if the first rule is untouched
expect(config.module.rules[0].include).toEqual(['/path/to/project']);
});
it('should duplicate the default JS rule (webpack > 10)', () => {
const config = compileNodeModulesPlugin()().webpack(createWebpackConfigGT10(), webpackOptions);
const rule = config.module.rules[1];
expect(rule.test.toString()).toBe('/\\.js$/');
expect(rule.include.toString()).toMatch(/\bnode_modules\b/);
expect(rule.exclude.toString()).toMatch(/\/path\/to\/project/);
expect(rule.exclude.toString()).toMatch(/\bnode-libs-browser\b/);
expect(rule.exclude.toString()).toMatch(/\bprocess\b/);
expect(rule.use).toHaveLength(1);
expect(rule.use[0].options.hasReactRefresh).toBe(false);
expect(rule.use[0].options.distDir).toMatch(/^my-project[\\/]\.next[\\/]cache[\\/]compile-node-modules-plugin$/);
expect(rule.use[0].options.caller).toEqual({ isNodeModule: true });
// Test if the first rule is untouched
expect(config.module.rules[0].include).toEqual(['/path/to/project']);
});
it('should not have cwd in excludes on the duplicated JS rule', () => {
const inputConfig = createWebpackConfigLTE10();
inputConfig.module.rules[0].include.push(process.cwd());
const config = compileNodeModulesPlugin()().webpack(inputConfig, webpackOptions);
const rule = config.module.rules[1];
expect(rule.exclude).not.toContain(process.cwd());
});
it('should not throw if JS rule has no includes', () => {
const inputConfig = createWebpackConfigLTE10();
delete inputConfig.module.rules[0].include;
const config = compileNodeModulesPlugin()().webpack(inputConfig, webpackOptions);
const rule = config.module.rules[1];
expect(rule.test.toString()).toBe('/\\.js$/');
});
it('should throw if the JS rule was not found', () => {
const noGoodRuleConfig = createWebpackConfigLTE10();
noGoodRuleConfig.module.rules[0].use.loader = 'bad-loader';
expect(() => compileNodeModulesPlugin()().webpack(noGoodRuleConfig, webpackOptions)).toThrow(/JS rule/);
});
it('should allow to override the rule\'s test & include', () => {
const options = {
test: 'my-test',
include: 'my-include',
exclude: 'my-exclude',
};
const config = compileNodeModulesPlugin(options)().webpack(createWebpackConfigLTE10(), webpackOptions);
const rule = config.module.rules[1];
expect(rule.test).toBe('my-test');
expect(rule.include).toBe('my-include');
});
it('should unshift rule\'s exclude conditions if any', () => {
const options = {
exclude: 'my-exclude',
};
const config = compileNodeModulesPlugin(options)().webpack(createWebpackConfigLTE10(), webpackOptions);
const rule = config.module.rules[1];
expect(rule.exclude.length).toBeGreaterThan(1);
expect(rule.exclude[0]).toBe('my-exclude');
});
it('should call nextConfig webpack if defined', () => {
const nextConfig = {
webpack: jest.fn(() => 'foo'),
};
const config = compileNodeModulesPlugin()(nextConfig).webpack(createWebpackConfigLTE10(), webpackOptions);
expect(nextConfig.webpack).toHaveBeenCalledTimes(1);
expect(config).toBe('foo');
});
it('should have pre-configured server externals (target = server)', () => {
const config = compileNodeModulesPlugin()().webpack(createWebpackConfigLTE10(), {
...webpackOptions,
isServer: true,
});
expect(config.externals).toMatchSnapshot();
});
it('should have pre-configured server externals (target = serverless-trace)', () => {
const config = compileNodeModulesPlugin()().webpack(createWebpackConfigLTE10(), {
...webpackOptions,
isServer: true,
config: {
...webpackOptions.config,
target: 'experimental-serverless-trace',
},
});
expect(config.externals).toMatchSnapshot();
});
it('should unshift custom server externals (target = server, single)', () => {
const options = {
serverExternals: 'added-external',
};
const config = compileNodeModulesPlugin(options)().webpack(createWebpackConfigLTE10(), {
...webpackOptions,
isServer: true,
});
expect(config.externals.length).toBeGreaterThan(1);
expect(config.externals[0]).toBe('added-external');
});
it('should unshift custom server externals (target = server, array)', () => {
const options = {
serverExternals: ['added-external-1', 'added-external-2'],
};
const config = compileNodeModulesPlugin(options)().webpack(createWebpackConfigLTE10(), {
...webpackOptions,
isServer: true,
});
expect(config.externals.length).toBeGreaterThan(2);
expect(config.externals[0]).toBe('added-external-1');
expect(config.externals[1]).toBe('added-external-2');
});
it('should unshift custom server externals (target = serverless-trace, single)', () => {
const options = {
serverExternals: 'added-external',
};
const config = compileNodeModulesPlugin(options)().webpack(createWebpackConfigLTE10(), {
...webpackOptions,
isServer: true,
config: {
...webpackOptions.config,
target: 'experimental-serverless-trace',
},
});
expect(config.externals.length).toBeGreaterThan(1);
expect(config.externals[0]).toBe('added-external');
});
it('should unshift custom server externals (target = serverless-trace, array)', () => {
const options = {
serverExternals: ['added-external-1', 'added-external-2'],
};
const config = compileNodeModulesPlugin(options)().webpack(createWebpackConfigLTE10(), {
...webpackOptions,
isServer: true,
config: {
...webpackOptions.config,
target: 'experimental-serverless-trace',
},
});
expect(config.externals.length).toBeGreaterThan(2);
expect(config.externals[0]).toBe('added-external-1');
expect(config.externals[1]).toBe('added-external-2');
});
it('should not add "critters" to server externals (target = serverless-trace, experimental.optimizeCss enabled)', () => {
const config = compileNodeModulesPlugin()().webpack(createWebpackConfigLTE10(), {
...webpackOptions,
isServer: true,
config: {
...webpackOptions.config,
target: 'experimental-serverless-trace',
experimental: {
optimizeCss: true,
},
},
});
expect(config.externals).not.toContain('critters');
});
it('should leave externals untouched when serverless', () => {
const options = {
serverExternals: ['foo', 'bar'],
};
const config = compileNodeModulesPlugin(options)().webpack(createWebpackConfigLTE10(), {
...webpackOptions,
isServer: true,
config: {
...webpackOptions.config,
target: 'serverless',
},
});
expect(typeof config.externals).toBe('function');
});