Skip to content

Commit 9d25194

Browse files
ijjksokra
andauthored
Ensure next/dynamic transpiles for tests (vercel#24751)
Co-authored-by: Tobias Koppers <[email protected]>
1 parent 4e8fac9 commit 9d25194

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

packages/next/build/babel/plugins/react-loadable-plugin.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,12 @@ export default function ({
151151
t.binaryExpression(
152152
'+',
153153
t.stringLiteral(
154-
relativePath(
155-
state.file.opts.caller.pagesDir,
156-
state.file.opts.filename
157-
) + ' -> '
154+
(state.file.opts.caller.pagesDir
155+
? relativePath(
156+
state.file.opts.caller.pagesDir,
157+
state.file.opts.filename
158+
)
159+
: state.file.opts.filename) + ' -> '
158160
),
159161
node
160162
)

test/unit/next-babel-loader.unit.test.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ const path = require('path')
1111

1212
const dir = path.resolve(os.tmpdir())
1313

14-
const babel = async (
15-
code,
16-
{ isServer = false, resourcePath = 'index.js', development = false } = {}
17-
) => {
14+
const babel = async (code, queryOpts = {}) => {
15+
const {
16+
isServer = false,
17+
resourcePath = 'index.js',
18+
development = false,
19+
} = queryOpts
20+
1821
let isAsync = false
1922
return new Promise((resolve, reject) => {
2023
function callback(err, content) {
@@ -47,7 +50,10 @@ const babel = async (
4750
cwd: dir,
4851
isServer,
4952
distDir: path.resolve(dir, '.next'),
50-
pagesDir: path.resolve(dir, 'pages'),
53+
pagesDir:
54+
'pagesDir' in queryOpts
55+
? queryOpts.pagesDir
56+
: path.resolve(dir, 'pages'),
5157
cache: false,
5258
development,
5359
hasReactRefresh: Boolean(!isServer && development),
@@ -196,6 +202,28 @@ describe('next-babel-loader', () => {
196202
expect(code).toMatchInlineSnapshot(`"if(false){}"`)
197203
})
198204

205+
it('should handle no pagesDir', async () => {
206+
const code = await babel(
207+
`
208+
import dynamic from 'next/dynamic'
209+
210+
const Comp = dynamic(() => import('comp'))
211+
212+
export default function Page(props) {
213+
return <Comp />
214+
}
215+
`,
216+
{
217+
pagesDir: undefined,
218+
}
219+
)
220+
expect(
221+
code.replace(/modules:\[".*?"/, 'modules:["/path/to/page"')
222+
).toMatchInlineSnapshot(
223+
`"import React from\\"react\\";var __jsx=React.createElement;import dynamic from'next/dynamic';var Comp=dynamic(function(){return import('comp');},{loadableGenerated:{webpack:function webpack(){return[require.resolveWeak('comp')];},modules:[\\"/path/to/page\\"+'comp']}});export default function Page(props){return __jsx(Comp,null);}"`
224+
)
225+
})
226+
199227
it('should not drop unused exports by default', async () => {
200228
const code = await babel(
201229
// effectful

0 commit comments

Comments
 (0)