Skip to content

Commit

Permalink
ensure that barrel files behind wildcards are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Sep 3, 2023
1 parent 17da2ca commit 070dc35
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
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'

0 comments on commit 070dc35

Please sign in to comment.