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 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
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(config.clone())),
_ => Either::Right(noop()),
},
opts.emotion
.as_ref()
Expand Down
5 changes: 1 addition & 4 deletions packages/next-swc/crates/core/src/named_import_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ 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,
);
let new_src = format!("__barrel_optimize__?names={}!=!{}", names, src_value);

// Create a new import declaration, keep everything the same except the source
let mut new_decl = decl.clone();
Expand Down
440 changes: 261 additions & 179 deletions packages/next-swc/crates/core/src/optimize_barrel.rs

Large diffs are not rendered by default.

41 changes: 32 additions & 9 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,39 @@ 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#"
optimize_barrel(json(
r#"
{
"names": ["x", "y", "z"]
"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(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
60 changes: 46 additions & 14 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,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 @@ -1178,7 +1182,7 @@ export default async function getBaseWebpackConfig(
...(reactProductionProfiling ? getReactProfilingInProduction() : {}),

// For Node server, we need to re-alias the package imports to prefer to
// resolve to the module export.
// resolve to the ESM export.
...(isNodeServer
? getModularizeImportAliases(
config.experimental.optimizePackageImports || []
Expand Down Expand Up @@ -1962,6 +1966,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 @@ -1977,24 +1982,51 @@ export default async function getBaseWebpackConfig(
module: {
rules: [
{
test: /__barrel_optimize__/,
use: ({
resourceQuery,
issuerLayer,
}: {
resourceQuery: string
issuerLayer: string
}) => {
const names = resourceQuery.slice('?names='.length).split(',')
// This loader rule passes the resource to the SWC loader with
// `optimizeBarrelExports` enabled. This option makes the SWC to
// transform the original code to be a JSON of its export map, so
// the barrel loader can analyze it and only keep the needed ones.
test: /__barrel_transform__/,
use: ({ resourceQuery }: { resourceQuery: string }) => {
const isFromWildcardExport = /[&?]wildcard/.test(resourceQuery)

return [
getSwcLoader({
isServerLayer:
issuerLayer === WEBPACK_LAYERS.reactServerComponents,
optimizeBarrelExports: names,
hasServerComponents: false,
optimizeBarrelExports: {
wildcard: isFromWildcardExport,
},
}),
]
},
},
{
// This loader rule works like a bridge between user's import and
// the target module behind a package's barrel file. It reads SWC's
// analysis result from the previous loader, and directly returns the
// code that only exports values that are asked by the user.
test: /__barrel_optimize__/,
use: ({ resourceQuery }: { resourceQuery: string }) => {
const names = (
resourceQuery.match(/\?names=([^&]+)/)?.[1] || ''
).split(',')
const isFromWildcardExport = /[&?]wildcard/.test(resourceQuery)

return [
{
loader: 'next-barrel-loader',
options: {
names,
wildcard: isFromWildcardExport,
},
// This is part of the request value to serve as the module key.
// The barrel loader are no-op re-exported modules keyed by
// export names.
ident: 'next-barrel-loader:' + resourceQuery,
},
]
},
},
...(hasAppDir
? [
{
Expand Down
Loading