Unverified Commit eb89e392 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

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 1df2f3ef
...@@ -260,7 +260,7 @@ function getFreshConfig( ...@@ -260,7 +260,7 @@ function getFreshConfig(
filename: string, filename: string,
inputSourceMap?: object | null inputSourceMap?: object | null
) { ) {
let { isServer, pagesDir, development, hasJsxRuntime, configFile, cwd } = let { isServer, pagesDir, development, hasJsxRuntime, configFile, srcDir } =
loaderOptions loaderOptions
let customConfig: any = configFile let customConfig: any = configFile
...@@ -329,7 +329,7 @@ function getFreshConfig( ...@@ -329,7 +329,7 @@ function getFreshConfig(
supportsTopLevelAwait: true, supportsTopLevelAwait: true,
isServer, isServer,
cwd, srcDir,
pagesDir, pagesDir,
isDev: development, isDev: development,
hasJsxRuntime, hasJsxRuntime,
......
...@@ -17,4 +17,5 @@ export interface NextBabelLoaderOptions { ...@@ -17,4 +17,5 @@ export interface NextBabelLoaderOptions {
caller: any caller: any
configFile: string | undefined configFile: string | undefined
cwd: string cwd: string
srcDir: string
} }
...@@ -168,9 +168,9 @@ export default function ({ ...@@ -168,9 +168,9 @@ export default function ({
t.binaryExpression( t.binaryExpression(
'+', '+',
t.stringLiteral( t.stringLiteral(
(state.file.opts.caller?.cwd (state.file.opts.caller?.srcDir
? relativePath( ? relativePath(
state.file.opts.caller.cwd, state.file.opts.caller.srcDir,
state.file.opts.filename state.file.opts.filename
) )
: state.file.opts.filename) + ' -> ' : state.file.opts.filename) + ' -> '
......
...@@ -414,6 +414,7 @@ export default async function getBaseWebpackConfig( ...@@ -414,6 +414,7 @@ export default async function getBaseWebpackConfig(
isServer: isNodeOrEdgeCompilation, isServer: isNodeOrEdgeCompilation,
distDir, distDir,
pagesDir, pagesDir,
srcDir: path.dirname((appDir || pagesDir)!),
cwd: dir, cwd: dir,
development: dev, development: dev,
hasReactRefresh: dev && isClient, 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