Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that barrel files behind wildcards are transformed into shortpath #54951

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { d } from 'd'
export const a = 1
export { b } from 'b'
export * from 'c'
export { d as e }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const __next_private_export_map__ = '[["a","",""],["b","b","b"],["e","d","d"]]';
export * from "__barrel_optimize__?names=__PLACEHOLDER__!=!c";
15 changes: 11 additions & 4 deletions packages/next/src/build/webpack/loaders/next-barrel-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<string, [string, string]>()
for (const [name, path, orig] of exportList) {
exportMap.set(name, [path, orig])
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { foo } from 'my-lib'

export default function Page() {
return <h1>{foo}</h1>
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './b'
export { foo } from './foo'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo'