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

Improve barrel optimizer with loader caching and wilcard exports #54695

Merged
merged 12 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 2 additions & 3 deletions packages/next-swc/crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,8 @@ where
None => Either::Right(noop()),
},
match &opts.optimize_barrel_exports {
Some(config) => Either::Left(optimize_barrel::optimize_barrel(
file.name.clone(),config.clone())),
None => Either::Right(noop()),
Some(config) => Either::Left(optimize_barrel::optimize_barrel(file.name.clone(), config.clone())),
_ => Either::Right(noop()),
},
opts.emotion
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/core/src/named_import_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl Fold for NamedImportTransform {
if !skip_transform {
let names = specifier_names.join(",");
let new_src = format!(
"__barrel_optimize__?names={}!=!{}?__barrel_optimize_noop__={}",
names, src_value, names,
"__barrel_optimize__?names={}!=!{}",
names, src_value,
);

// Create a new import declaration, keep everything the same except the source
Expand Down
428 changes: 260 additions & 168 deletions packages/next-swc/crates/core/src/optimize_barrel.rs

Large diffs are not rendered by default.

49 changes: 38 additions & 11 deletions packages/next-swc/crates/core/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn named_import_transform_fixture(input: PathBuf) {
);
}

#[fixture("tests/fixture/optimize-barrel/**/input.js")]
#[fixture("tests/fixture/optimize-barrel/normal/**/input.js")]
fn optimize_barrel_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
Expand All @@ -493,16 +493,43 @@ fn optimize_barrel_fixture(input: PathBuf) {

chain!(
resolver(unresolved_mark, top_level_mark, false),
optimize_barrel(
FileName::Real(PathBuf::from("/some-project/node_modules/foo/file.js")),
json(
r#"
{
"names": ["x", "y", "z"]
}
"#
)
)
optimize_barrel(FileName::Real(PathBuf::from(
"/some-project/node_modules/foo/file.js"
)), json(
r#"
{
"wildcard": false
}
"#
))
)
},
&input,
&output,
Default::default(),
);
}

#[fixture("tests/fixture/optimize-barrel/wildcard/**/input.js")]
fn optimize_barrel_wildcard_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|_tr| {
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();

chain!(
resolver(unresolved_mark, top_level_mark, false),
optimize_barrel(FileName::Real(PathBuf::from(
"/some-project/node_modules/foo/file.js"
)), json(
r#"
{
"wildcard": true
}
"#
))
)
},
&input,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { A, B, C as F } from "__barrel_optimize__?names=A,B,C!=!foo?__barrel_optimize_noop__=A,B,C";
import { A, B, C as F } from "__barrel_optimize__?names=A,B,C!=!foo";
import D from 'bar';
import E from 'baz';
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { A, B, C as F } from "__barrel_optimize__?names=A,B,C!=!foo?__barrel_optimize_noop__=A,B,C";
import { D } from "__barrel_optimize__?names=D!=!bar?__barrel_optimize_noop__=D";
import { A, B, C as F } from "__barrel_optimize__?names=A,B,C!=!foo";
import { D } from "__barrel_optimize__?names=D!=!bar";
import E from 'baz';

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { z } from './3'

export { foo, b as y } from './1'
export { x, a } from './2'
export { z }

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// De-optimize this file
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// De-optimize this file
import { a as z } from './3'
export * from 'x'
export * from 'y'

export { foo, b as y } from './1'
export { x, a } from './2'
export { z }
export { z as w }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const __next_private_export_map__ = '[["foo","./1","foo"],["y","./1","b"],["x","./2","x"],["a","./2","a"],["w","./3","a"]]';
export * from "__barrel_optimize__?names=__PLACEHOLDER__!=!x";
export * from "__barrel_optimize__?names=__PLACEHOLDER__!=!y";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use client';
export const __next_private_export_map__ = '[["x","foo","a"],["y","1","y"],["b","foo","b"],["default","foo","default"],["z","bar","default"]]';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const __next_private_export_map__ = '[["x","./icons/index.js","*"]]';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const a = 1
export { b }
export * from 'c'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const __next_private_export_map__ = '[["a","",""],["b","","b"]]';
export * from "__barrel_optimize__?names=__PLACEHOLDER__!=!c";
4 changes: 1 addition & 3 deletions packages/next/src/build/swc/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,7 @@ export function getLoaderSWCOptions({
}
}
if (optimizeBarrelExports) {
baseOptions.optimizeBarrelExports = {
names: optimizeBarrelExports,
}
baseOptions.optimizeBarrelExports = optimizeBarrelExports
}

const isNextDist = nextDistPath.test(filename)
Expand Down
24 changes: 21 additions & 3 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,14 @@ function getModularizeImportAliases(packages: string[]) {
for (const pkg of packages) {
try {
const descriptionFileData = require(`${pkg}/package.json`)
const descriptionFilePath = require.resolve(`${pkg}/package.json`)

for (const field of mainFields) {
if (descriptionFileData.hasOwnProperty(field)) {
aliases[pkg] = `${pkg}/${descriptionFileData[field]}`
aliases[pkg] = path.join(
path.dirname(descriptionFilePath),
descriptionFileData[field]
)
break
}
}
Expand Down Expand Up @@ -1966,6 +1970,7 @@ export default async function getBaseWebpackConfig(
'next-invalid-import-error-loader',
'next-metadata-route-loader',
'modularize-import-loader',
'next-barrel-loader',
].reduce((alias, loader) => {
// using multiple aliases to replace `resolveLoader.modules`
alias[loader] = path.join(__dirname, 'webpack', 'loaders', loader)
Expand All @@ -1989,12 +1994,25 @@ export default async function getBaseWebpackConfig(
resourceQuery: string
issuerLayer: string
}) => {
const names = resourceQuery.slice('?names='.length).split(',')
const names = (
resourceQuery.match(/\?names=([^&]+)/)?.[1] || ''
).split(',')
const wildcard = resourceQuery.includes('&wildcard')

return [
{
loader: 'next-barrel-loader',
options: {
names,
wildcard,
},
},
getSwcLoader({
isServerLayer:
issuerLayer === WEBPACK_LAYERS.reactServerComponents,
optimizeBarrelExports: names,
optimizeBarrelExports: {
wildcard,
},
}),
]
},
Expand Down
73 changes: 73 additions & 0 deletions packages/next/src/build/webpack/loaders/next-barrel-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type webpack from 'webpack'

const NextBarrelLoader: webpack.LoaderDefinitionFunction<{
names: string[]
wildcard: boolean
}> = function (source) {
this.cacheable()
const { names, wildcard } = this.getOptions()

const matches = source.match(
/^([^]*)export const __next_private_export_map__ = '([^']+)'/
)

if (!matches) {
// This file isn't a barrel and we can't apply any optimizations. Let's re-export everything.
return `export * from ${JSON.stringify(this.resourcePath)}`
huozhi marked this conversation as resolved.
Show resolved Hide resolved
}

const wildcardExports = [...source.matchAll(/export \* from "([^"]+)"/g)]

const prefix = matches[1]
const exportList = JSON.parse(matches[2]) as [string, string, string][]
const exportMap = new Map<string, [string, string]>()
for (const [name, path, orig] of exportList) {
exportMap.set(name, [path, orig])
}

let output = prefix
let missedNames: string[] = []
for (const name of names) {
// If the name matches
if (exportMap.has(name)) {
const decl = exportMap.get(name)!

// In the wildcard case, all exports are from the file itself.
if (wildcard) {
decl[0] = this.resourcePath
decl[1] = name
}

if (decl[1] === '*') {
output += `\nexport * as ${name} from ${JSON.stringify(decl[0])}`
} else if (decl[1] === 'default') {
output += `\nexport { default as ${name} } from ${JSON.stringify(
decl[0]
)}`
} else if (decl[1] === name) {
output += `\nexport { ${name} } from ${JSON.stringify(decl[0])}`
} else {
output += `\nexport { ${decl[1]} as ${name} } from ${JSON.stringify(
decl[0]
)}`
}
} else {
missedNames.push(name)
}
}

// These are from wildcard exports.
if (missedNames.length > 0) {
for (const match of wildcardExports) {
const path = match[1]

output += `\nexport * from ${JSON.stringify(
path.replace('__PLACEHOLDER__', missedNames.join(',') + '&wildcard')
)}`
}
}

return output
}

export default NextBarrelLoader
19 changes: 9 additions & 10 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,15 +678,6 @@ function assignDefaults(
'lodash-es': {
transform: 'lodash-es/{{member}}',
},
'@headlessui/react': {
transform: {
Transition:
'modularize-import-loader?name={{member}}&join=./components/transitions/transition!@headlessui/react',
Tab: 'modularize-import-loader?name={{member}}&join=./components/tabs/tabs!@headlessui/react',
'*': 'modularize-import-loader?name={{member}}&join=./components/{{ kebabCase member }}/{{ kebabCase member }}!@headlessui/react',
},
skipDefaultConversion: true,
},
'@heroicons/react/20/solid': {
transform: '@heroicons/react/20/solid/esm/{{member}}',
},
Expand Down Expand Up @@ -739,7 +730,15 @@ function assignDefaults(
result.experimental = {}
}
result.experimental.optimizePackageImports = [
...new Set([...userProvidedOptimizePackageImports, 'lucide-react']),
...new Set([
...userProvidedOptimizePackageImports,
'lucide-react',
'@headlessui/react',
'@fortawesome/fontawesome-svg-core',
shuding marked this conversation as resolved.
Show resolved Hide resolved
'@fortawesome/free-solid-svg-icons',
'@headlessui-float/react',
'react-hot-toast',
]),
]

return result
Expand Down