diff --git a/packages/next-swc/crates/core/tests/fixture/optimize-barrel/wildcard/2/input.js b/packages/next-swc/crates/core/tests/fixture/optimize-barrel/wildcard/2/input.js new file mode 100644 index 0000000000000..56b087ebec2ea --- /dev/null +++ b/packages/next-swc/crates/core/tests/fixture/optimize-barrel/wildcard/2/input.js @@ -0,0 +1,5 @@ +import { d } from 'd' +export const a = 1 +export { b } from 'b' +export * from 'c' +export { d as e } diff --git a/packages/next-swc/crates/core/tests/fixture/optimize-barrel/wildcard/2/output.js b/packages/next-swc/crates/core/tests/fixture/optimize-barrel/wildcard/2/output.js new file mode 100644 index 0000000000000..d5c8696a4bf18 --- /dev/null +++ b/packages/next-swc/crates/core/tests/fixture/optimize-barrel/wildcard/2/output.js @@ -0,0 +1,2 @@ +export const __next_private_export_map__ = '[["a","",""],["b","b","b"],["e","d","d"]]'; +export * from "__barrel_optimize__?names=__PLACEHOLDER__!=!c"; diff --git a/packages/next/src/build/webpack/loaders/next-barrel-loader.ts b/packages/next/src/build/webpack/loaders/next-barrel-loader.ts index d55851d1dfb46..d6d8146daf244 100644 --- a/packages/next/src/build/webpack/loaders/next-barrel-loader.ts +++ b/packages/next/src/build/webpack/loaders/next-barrel-loader.ts @@ -108,7 +108,7 @@ const NextBarrelLoader = async function ( }) const matches = source.match( - /^([^]*)export const __next_private_export_map__ = '([^']+)'/ + /^([^]*)export const __next_private_export_map__ = ('[^']+'|"[^"]+")/ ) if (!matches) { @@ -125,7 +125,11 @@ const NextBarrelLoader = async function ( // It needs to keep the prefix for comments and directives like "use client". const prefix = matches[1] - const exportList = JSON.parse(matches[2]) as [string, string, string][] + const exportList = JSON.parse(matches[2].slice(1, -1)) as [ + string, + string, + string + ][] const exportMap = new Map() for (const [name, path, orig] of exportList) { exportMap.set(name, [path, orig]) @@ -138,8 +142,11 @@ const NextBarrelLoader = async function ( if (exportMap.has(name)) { const decl = exportMap.get(name)! - // In the wildcard case, all exports are from the file itself. - if (wildcard) { + // In the wildcard case, if the value is exported from another file, we + // redirect to that file (decl[0]). Otherwise, export from the current + // file itself (this.resourcePath). + if (wildcard && !decl[0]) { + // E.g. the file contains `export const a = 1` decl[0] = this.resourcePath decl[1] = name } diff --git a/test/development/basic/barrel-optimization/app/recursive/shortcut/page.js b/test/development/basic/barrel-optimization/app/recursive/shortcut/page.js new file mode 100644 index 0000000000000..0cd6d599a8357 --- /dev/null +++ b/test/development/basic/barrel-optimization/app/recursive/shortcut/page.js @@ -0,0 +1,5 @@ +import { foo } from 'my-lib' + +export default function Page() { + return

{foo}

+} diff --git a/test/development/basic/barrel-optimization/node_modules_bak/my-lib/a.js b/test/development/basic/barrel-optimization/node_modules_bak/my-lib/a.js index 35dccc1ad442a..a1641729097bc 100644 --- a/test/development/basic/barrel-optimization/node_modules_bak/my-lib/a.js +++ b/test/development/basic/barrel-optimization/node_modules_bak/my-lib/a.js @@ -1 +1,2 @@ export * from './b' +export { foo } from './foo' diff --git a/test/development/basic/barrel-optimization/node_modules_bak/my-lib/foo.js b/test/development/basic/barrel-optimization/node_modules_bak/my-lib/foo.js new file mode 100644 index 0000000000000..cb356468240d5 --- /dev/null +++ b/test/development/basic/barrel-optimization/node_modules_bak/my-lib/foo.js @@ -0,0 +1 @@ +export const foo = 'foo'