Commit c8b6c668 authored by Jiachi Liu's avatar Jiachi Liu

Fix next/dynamic with babel and src dir (#65177)

### What

* Fix `next/dynamic` with babel config and `src/` directory
* Separate the existing `next/dynamic` dev tests, make it easier to
cover more cases, add `.babelrc` + `src/` folder coverage case

### Why

* The new transform is using `src/` or root dir as the base directory
`next/dynamic` for generating relative module path as unique key for
`next/dynamic` modules. The babel one was always using the root dir
(`cwd`), which is incorrect. It should use the `src/` dir.

In the future we could make the different solution to generate the
unique id simpler, instead of using relative paths

Closes NEXT-3254
Closes NEXT-3251
Fixes #64741
parent 11536068
......@@ -260,7 +260,7 @@ function getFreshConfig(
filename: string,
inputSourceMap?: object | null
) {
let { isServer, pagesDir, development, hasJsxRuntime, configFile, cwd } =
let { isServer, pagesDir, development, hasJsxRuntime, configFile, srcDir } =
loaderOptions
let customConfig: any = configFile
......@@ -329,7 +329,7 @@ function getFreshConfig(
supportsTopLevelAwait: true,
isServer,
cwd,
srcDir,
pagesDir,
isDev: development,
hasJsxRuntime,
......
......@@ -17,4 +17,5 @@ export interface NextBabelLoaderOptions {
caller: any
configFile: string | undefined
cwd: string
srcDir: string
}
......@@ -168,9 +168,9 @@ export default function ({
t.binaryExpression(
'+',
t.stringLiteral(
(state.file.opts.caller?.cwd
(state.file.opts.caller?.srcDir
? relativePath(
state.file.opts.caller.cwd,
state.file.opts.caller.srcDir,
state.file.opts.filename
)
: state.file.opts.filename) + ' -> '
......
......@@ -414,6 +414,7 @@ export default async function getBaseWebpackConfig(
isServer: isNodeOrEdgeCompilation,
distDir,
pagesDir,
srcDir: path.dirname((appDir || pagesDir)!),
cwd: dir,
development: dev,
hasReactRefresh: dev && isClient,
......
This diff is collapsed.
process.env.TEST_BABEL = '1'
process.env.TEST_SRC_DIR = '1'
require('./next-dynamic.test')
process.env.TEST_BABEL = '1'
require('./next-dynamic.test')
process.env.TEST_BASE_PATH = '/docs'
require('./next-dynamic.test')
process.env.TEST_CUSTOMIZED_DOCUMENT = '1'
require('./next-dynamic.test')
This diff is collapsed.
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