-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
next-babel-loader.test.js
402 lines (362 loc) · 14.6 KB
/
next-babel-loader.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
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
/* eslint-env jest */
// avoid generating __source annotations in JSX during testing:
const NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
require('next/dist/build/babel/preset')
process.env.NODE_ENV = NODE_ENV
const loader = require('next/dist/build/webpack/loaders/next-babel-loader')
const os = require('os')
const path = require('path')
const dir = path.resolve(os.tmpdir())
const babel = async (
code,
{
isServer = false,
resourcePath = 'index.js',
hasModern = false,
development = false,
} = {}
) => {
let isAsync = false
return new Promise((resolve, reject) => {
function callback(err, content) {
if (err) {
reject(err)
} else {
resolve(content)
}
}
const res = loader.bind({
resourcePath,
async() {
isAsync = true
return callback
},
callback,
query: {
// babel opts
babelrc: false,
configFile: false,
sourceType: 'module',
compact: true,
caller: {
name: 'tests',
supportsStaticESM: true,
},
// loader opts
cwd: dir,
isServer,
distDir: path.resolve(dir, '.next'),
pagesDir: path.resolve(dir, 'pages'),
cache: false,
babelPresetPlugins: [],
hasModern,
development,
},
})(code, null)
if (!isAsync) {
resolve(res)
}
})
}
describe('next-babel-loader', () => {
describe('replace constants', () => {
it('should replace typeof window expression nested', async () => {
const code = await babel('function a(){console.log(typeof window)}')
expect(code).toMatchInlineSnapshot(
`"function a(){console.log(\\"object\\");}"`
)
})
it('should replace typeof window expression top level (client)', async () => {
const code = await babel('typeof window;')
expect(code).toMatchInlineSnapshot(`"\\"object\\";"`)
})
it('should replace typeof window expression top level (server)', async () => {
const code = await babel('typeof window;', { isServer: true })
expect(code).toMatchInlineSnapshot(`"\\"undefined\\";"`)
})
it('should replace typeof window expression nested', async () => {
const code = await babel(
`function a(){console.log(typeof window === 'undefined')}`
)
expect(code).toMatchInlineSnapshot(`"function a(){console.log(false);}"`)
})
it('should replace typeof window expression top level', async () => {
const code = await babel(`typeof window === 'undefined';`)
expect(code).toMatchInlineSnapshot(`"false;"`)
})
it('should replace typeof window expression top level', async () => {
const code = await babel(`typeof window === 'object';`)
expect(code).toMatchInlineSnapshot(`"true;"`)
})
it('should replace typeof window expression top level', async () => {
const code = await babel(`typeof window !== 'undefined';`)
expect(code).toMatchInlineSnapshot(`"true;"`)
})
it('should replace typeof window expression top level', async () => {
const code = await babel(`typeof window !== 'object';`)
expect(code).toMatchInlineSnapshot(`"false;"`)
})
it('should replace typeof window expression top level', async () => {
const code = await babel(`typeof window !== 'undefined';`, {
isServer: true,
})
expect(code).toMatchInlineSnapshot(`"false;"`)
})
it('should replace typeof window expression top level', async () => {
const code = await babel(`typeof window !== 'object';`, {
isServer: true,
})
expect(code).toMatchInlineSnapshot(`"true;"`)
})
it('should replace process.browser (1)', async () => {
const code = await babel(`process.browser`, {
isServer: false,
})
expect(code).toMatchInlineSnapshot(`"true;"`)
})
it('should replace process.browser (2)', async () => {
const code = await babel(`process.browser`, {
isServer: true,
})
expect(code).toMatchInlineSnapshot(`"false;"`)
})
it('should replace process.browser (3)', async () => {
const code = await babel(`process.browser == false`, {
isServer: true,
})
expect(code).toMatchInlineSnapshot(`"true;"`)
})
it('should replace process.browser (4)', async () => {
const code = await babel(`if (process.browser === false) {}`, {
isServer: true,
})
expect(code).toMatchInlineSnapshot(`"if(true){}"`)
})
it('should replace process.browser (5)', async () => {
const code = await babel(`if (process.browser) {}`, {
isServer: true,
})
expect(code).toMatchInlineSnapshot(`"if(false){}"`)
})
it('should replace NODE_ENV on client (dev)', async () => {
const code = await babel(`process.env.NODE_ENV`, {
isServer: false,
development: true,
})
expect(code).toMatchInlineSnapshot(`"\\"development\\";"`)
})
it('should replace NODE_ENV on client (prod)', async () => {
const code = await babel(`process.env.NODE_ENV`, {
isServer: false,
development: false,
})
expect(code).toMatchInlineSnapshot(`"\\"production\\";"`)
})
it('should replace NODE_ENV on server', async () => {
const code = await babel(`process.env.NODE_ENV`, {
isServer: true,
})
expect(code).toMatchInlineSnapshot(`"\\"production\\";"`)
})
it('should replace NODE_ENV in statement (dev)', async () => {
const code = await babel(
`if (process.env.NODE_ENV === 'development') {}`,
{ development: true }
)
expect(code).toMatchInlineSnapshot(`"if(true){}"`)
})
it('should replace NODE_ENV in statement (prod)', async () => {
const code = await babel(`if (process.env.NODE_ENV === 'production') {}`)
expect(code).toMatchInlineSnapshot(`"if(true){}"`)
})
it('should replace NODE_ENV in statement (prod)', async () => {
const code = await babel(`if (process.env.NODE_ENV !== 'production') {}`)
expect(code).toMatchInlineSnapshot(`"if(false){}"`)
})
it('should not drop unused exports by default', async () => {
const code = await babel(
// effectful
`import"core-js";` +
// basic
`import{foo,bar}from"a";import baz from"b";` +
// complex
`import * as React from "react";` +
`import baz2,{yeet}from"c";` +
`import baz3,{cats}from"d";` +
`import{c,d}from"e";` +
`import{e as ee,f as ff}from"f";`
)
expect(code).toMatchInlineSnapshot(
`"import\\"core-js\\";import{foo,bar}from\\"a\\";import baz from\\"b\\";import*as React from\\"react\\";import baz2,{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee,f as ff}from\\"f\\";"`
)
})
const pageFile = path.resolve(dir, 'pages', 'index.js')
const tsPageFile = pageFile.replace(/\.js$/, '.ts')
it('should not drop unused exports by default in a page', async () => {
const code = await babel(
// effectful
`import"core-js";` +
// basic
`import{foo,bar}from"a";import baz from"b";` +
// complex
`import*as React from"react";` +
`import baz2,{yeet}from"c";` +
`import baz3,{cats}from"d";` +
`import{c,d}from"e";` +
`import{e as ee,f as ff}from"f";`,
{ resourcePath: pageFile }
)
expect(code).toMatchInlineSnapshot(
`"import\\"core-js\\";import{foo,bar}from\\"a\\";import baz from\\"b\\";import*as React from\\"react\\";import baz2,{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee,f as ff}from\\"f\\";"`
)
})
it('should drop unused exports in a modern-apis page', async () => {
const code = await babel(
// effectful
`import"core-js";` +
// basic
`import{foo,bar}from"a";import baz from"b";` +
// complex
`import*as React from"react";` +
`import baz2,{yeet}from"c";` +
`import baz3,{cats}from"d";` +
`import{c,d}from"e";` +
`import{e as ee,f as ff}from"f";` +
`` +
`export function unstable_getStaticProps() {foo;bar;baz;cats;baz2;ff; return { props: {} } }`,
{ resourcePath: pageFile }
)
expect(code).toMatchInlineSnapshot(
`"import\\"core-js\\";import*as React from\\"react\\";import{yeet}from\\"c\\";import baz3 from\\"d\\";import{c,d}from\\"e\\";import{e as ee}from\\"f\\";"`
)
})
it('should keep used exports in a modern-apis page (server)', async () => {
const code = await babel(
// effectful
`import"core-js";` +
// basic
`import{foo,bar}from"a";import baz from"b";import ooo from"ooo";` +
// complex
`import*as React from"react";` +
`import baz2,{yeet}from"c";` +
`import baz3,{cats}from"d";` +
`import{c,d}from"e";` +
`import{e as ee,f as ff}from"f";` +
`` +
`export function unstable_getStaticProps() {foo();baz2();ff();ooo(); return { props: {} }}` +
`export default function () { return bar(); }`,
{ resourcePath: pageFile, isServer: true }
)
expect(code).toMatchInlineSnapshot(
`"import\\"core-js\\";import{foo,bar}from\\"a\\";import baz from\\"b\\";import ooo from\\"ooo\\";import*as React from\\"react\\";import baz2,{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee,f as ff}from\\"f\\";export function unstable_getStaticProps(){foo();baz2();ff();ooo();return{props:{}};}export default function(){return bar();}"`
)
})
it('should keep used exports in a modern-apis page (client)', async () => {
const code = await babel(
// effectful
`import"core-js";` +
// basic
`import{foo,bar}from"a";import baz from"b";import ooo from"ooo";` +
// complex
`import*as React from"react";` +
`import baz2,{yeet}from"c";` +
`import baz3,{cats}from"d";` +
`import{c,d}from"e";` +
`import{e as ee,f as ff}from"f";` +
`` +
`export function unstable_getStaticProps() {foo();baz2();ff();ooo();cats; return { props: {} }}` +
`export default function () { return cats + bar(); }`,
{ resourcePath: pageFile, isServer: false }
)
expect(code).toMatchInlineSnapshot(
`"import\\"core-js\\";import{bar}from\\"a\\";import baz from\\"b\\";import*as React from\\"react\\";import{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee}from\\"f\\";const __NEXT_COMP=function(){return cats+bar();};__NEXT_COMP.__N_SSG=true export default __NEXT_COMP;"`
)
})
it('should keep used exports and react in a modern-apis page with JSX (client)', async () => {
const code = await babel(
// effectful
`import"core-js";` +
// basic
`import{foo,bar}from"a";import baz from"b";import ooo from"ooo";` +
// complex
`import*as React from"react";` +
`import baz2,{yeet}from"c";` +
`import baz3,{cats}from"d";` +
`import{c,d}from"e";` +
`import{e as ee,f as ff}from"f";` +
`` +
`export function unstable_getStaticProps() {foo();baz2();ff();ooo(); return { props: {} }}` +
`export default function () { return <div>{cats + bar()}</div> }`,
{ resourcePath: pageFile, isServer: false }
)
expect(code).toMatchInlineSnapshot(
`"var __jsx=React.createElement;import\\"core-js\\";import{bar}from\\"a\\";import baz from\\"b\\";import*as React from\\"react\\";import{yeet}from\\"c\\";import baz3,{cats}from\\"d\\";import{c,d}from\\"e\\";import{e as ee}from\\"f\\";const __NEXT_COMP=function(){return __jsx(\\"div\\",null,cats+bar());};__NEXT_COMP.__N_SSG=true export default __NEXT_COMP;"`
)
})
it('should support optional chaining for JS file', async () => {
const code = await babel(
`let hello;` +
`export default () => hello?.world ? 'something' : 'nothing' `,
{
resourcePath: pageFile,
}
)
expect(code).toMatchInlineSnapshot(
`"var hello;export default(function(){return(hello===null||hello===void 0?void 0:hello.world)?'something':'nothing';});"`
)
})
it('should support optional chaining for TS file', async () => {
const code = await babel(
`let hello;` +
`export default () => hello?.world ? 'something' : 'nothing' `,
{
resourcePath: tsPageFile,
}
)
expect(code).toMatchInlineSnapshot(
`"var hello;export default(function(){return(hello===null||hello===void 0?void 0:hello.world)?'something':'nothing';});"`
)
})
it('should support nullish coalescing for JS file', async () => {
const code = await babel(
`const res = {
status: 0,
nullVal: null,
statusText: '',
}
const status = res.status ?? 999
const nullVal = res.nullVal ?? 'another'
const statusText = res.nullVal ?? 'not found'
export default () => 'hello'
`,
{
resourcePath: pageFile,
}
)
expect(code).toMatchInlineSnapshot(
`"var _res$status,_res$nullVal,_res$nullVal2;var res={status:0,nullVal:null,statusText:''};var status=(_res$status=res.status)!==null&&_res$status!==void 0?_res$status:999;var nullVal=(_res$nullVal=res.nullVal)!==null&&_res$nullVal!==void 0?_res$nullVal:'another';var statusText=(_res$nullVal2=res.nullVal)!==null&&_res$nullVal2!==void 0?_res$nullVal2:'not found';export default(function(){return'hello';});"`
)
})
it('should support nullish coalescing for TS file', async () => {
const code = await babel(
`const res = {
status: 0,
nullVal: null,
statusText: '',
}
const status = res.status ?? 999
const nullVal = res.nullVal ?? 'another'
const statusText = res.nullVal ?? 'not found'
export default () => 'hello'
`,
{
resourcePath: tsPageFile,
}
)
expect(code).toMatchInlineSnapshot(
`"var _res$status,_res$nullVal,_res$nullVal2;var res={status:0,nullVal:null,statusText:''};var status=(_res$status=res.status)!==null&&_res$status!==void 0?_res$status:999;var nullVal=(_res$nullVal=res.nullVal)!==null&&_res$nullVal!==void 0?_res$nullVal:'another';var statusText=(_res$nullVal2=res.nullVal)!==null&&_res$nullVal2!==void 0?_res$nullVal2:'not found';export default(function(){return'hello';});"`
)
})
})
})