Unverified Commit 9d25194e authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure next/dynamic transpiles for tests (#24751)

Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 4e8fac93
...@@ -151,10 +151,12 @@ export default function ({ ...@@ -151,10 +151,12 @@ export default function ({
t.binaryExpression( t.binaryExpression(
'+', '+',
t.stringLiteral( t.stringLiteral(
relativePath( (state.file.opts.caller.pagesDir
state.file.opts.caller.pagesDir, ? relativePath(
state.file.opts.filename state.file.opts.caller.pagesDir,
) + ' -> ' state.file.opts.filename
)
: state.file.opts.filename) + ' -> '
), ),
node node
) )
......
...@@ -11,10 +11,13 @@ const path = require('path') ...@@ -11,10 +11,13 @@ const path = require('path')
const dir = path.resolve(os.tmpdir()) const dir = path.resolve(os.tmpdir())
const babel = async ( const babel = async (code, queryOpts = {}) => {
code, const {
{ isServer = false, resourcePath = 'index.js', development = false } = {} isServer = false,
) => { resourcePath = 'index.js',
development = false,
} = queryOpts
let isAsync = false let isAsync = false
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
function callback(err, content) { function callback(err, content) {
...@@ -47,7 +50,10 @@ const babel = async ( ...@@ -47,7 +50,10 @@ const babel = async (
cwd: dir, cwd: dir,
isServer, isServer,
distDir: path.resolve(dir, '.next'), distDir: path.resolve(dir, '.next'),
pagesDir: path.resolve(dir, 'pages'), pagesDir:
'pagesDir' in queryOpts
? queryOpts.pagesDir
: path.resolve(dir, 'pages'),
cache: false, cache: false,
development, development,
hasReactRefresh: Boolean(!isServer && development), hasReactRefresh: Boolean(!isServer && development),
...@@ -196,6 +202,28 @@ describe('next-babel-loader', () => { ...@@ -196,6 +202,28 @@ describe('next-babel-loader', () => {
expect(code).toMatchInlineSnapshot(`"if(false){}"`) expect(code).toMatchInlineSnapshot(`"if(false){}"`)
}) })
it('should handle no pagesDir', async () => {
const code = await babel(
`
import dynamic from 'next/dynamic'
const Comp = dynamic(() => import('comp'))
export default function Page(props) {
return <Comp />
}
`,
{
pagesDir: undefined,
}
)
expect(
code.replace(/modules:\[".*?"/, 'modules:["/path/to/page"')
).toMatchInlineSnapshot(
`"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);}"`
)
})
it('should not drop unused exports by default', async () => { it('should not drop unused exports by default', async () => {
const code = await babel( const code = await babel(
// effectful // effectful
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment