Unverified Commit b24d7291 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Code refactoring for loader rules (#46277

These loader rule changes should speed up compilation a bit as regexp
matches should be way faster than function calls with loop and
`includes` inside (`isResourceInPackages` is expensive), especially when
the project is large.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 7c5aa66a
...@@ -63,7 +63,8 @@ import { FontLoaderManifestPlugin } from './webpack/plugins/font-loader-manifest ...@@ -63,7 +63,8 @@ import { FontLoaderManifestPlugin } from './webpack/plugins/font-loader-manifest
import { getSupportedBrowsers } from './utils' import { getSupportedBrowsers } from './utils'
import { METADATA_IMAGE_RESOURCE_QUERY } from './webpack/loaders/metadata/discover' import { METADATA_IMAGE_RESOURCE_QUERY } from './webpack/loaders/metadata/discover'
const EXTERNAL_PACKAGES = require('../lib/server-external-packages.json') const EXTERNAL_PACKAGES =
require('../lib/server-external-packages.json') as string[]
const NEXT_PROJECT_ROOT = path.join(__dirname, '..', '..') const NEXT_PROJECT_ROOT = path.join(__dirname, '..', '..')
const NEXT_PROJECT_ROOT_DIST = path.join(NEXT_PROJECT_ROOT, 'dist') const NEXT_PROJECT_ROOT_DIST = path.join(NEXT_PROJECT_ROOT, 'dist')
...@@ -100,14 +101,6 @@ const BABEL_CONFIG_FILES = [ ...@@ -100,14 +101,6 @@ const BABEL_CONFIG_FILES = [
'babel.config.cjs', 'babel.config.cjs',
] ]
function appDirIssuerLayer(layer: string) {
return (
layer === WEBPACK_LAYERS.client ||
layer === WEBPACK_LAYERS.server ||
layer === WEBPACK_LAYERS.appClient
)
}
export const getBabelConfigFile = async (dir: string) => { export const getBabelConfigFile = async (dir: string) => {
const babelConfigFile = await BABEL_CONFIG_FILES.reduce( const babelConfigFile = await BABEL_CONFIG_FILES.reduce(
async (memo: Promise<string | undefined>, filename) => { async (memo: Promise<string | undefined>, filename) => {
...@@ -757,12 +750,8 @@ export default async function getBaseWebpackConfig( ...@@ -757,12 +750,8 @@ export default async function getBaseWebpackConfig(
} }
} }
const getBabelOrSwcLoader = () => {
return useSWCLoader ? getSwcLoader() : getBabelLoader()
}
const defaultLoaders = { const defaultLoaders = {
babel: getBabelOrSwcLoader(), babel: useSWCLoader ? getSwcLoader() : getBabelLoader(),
} }
const swcLoaderForRSC = hasServerComponents const swcLoaderForRSC = hasServerComponents
...@@ -1128,6 +1117,11 @@ export default async function getBaseWebpackConfig( ...@@ -1128,6 +1117,11 @@ export default async function getBaseWebpackConfig(
const optOutBundlingPackages = EXTERNAL_PACKAGES.concat( const optOutBundlingPackages = EXTERNAL_PACKAGES.concat(
...(config.experimental.serverComponentsExternalPackages || []) ...(config.experimental.serverComponentsExternalPackages || [])
) )
const optOutBundlingPackageRegex = new RegExp(
`[/\\\\]node_modules[/\\\\](${optOutBundlingPackages
.map((p) => p.replace(/\//g, '[/\\\\]'))
.join('|')})[/\\\\]`
)
let resolvedExternalPackageDirs: Map<string, string> let resolvedExternalPackageDirs: Map<string, string>
...@@ -1341,7 +1335,7 @@ export default async function getBaseWebpackConfig( ...@@ -1341,7 +1335,7 @@ export default async function getBaseWebpackConfig(
if (layer === WEBPACK_LAYERS.server) { if (layer === WEBPACK_LAYERS.server) {
// All packages should be bundled for the server layer if they're not opted out. // All packages should be bundled for the server layer if they're not opted out.
// This option takes priority over the transpilePackages option. // This option takes priority over the transpilePackages option.
if (isResourceInPackages(res, optOutBundlingPackages)) { if (optOutBundlingPackageRegex.test(res)) {
return `${externalType} ${request}` return `${externalType} ${request}`
} }
...@@ -1776,7 +1770,13 @@ export default async function getBaseWebpackConfig( ...@@ -1776,7 +1770,13 @@ export default async function getBaseWebpackConfig(
? [ ? [
{ {
test: codeCondition.test, test: codeCondition.test,
issuerLayer: appDirIssuerLayer, issuerLayer: {
or: [
WEBPACK_LAYERS.server,
WEBPACK_LAYERS.client,
WEBPACK_LAYERS.appClient,
],
},
resolve: { resolve: {
alias: { alias: {
// Alias next/head component to noop for RSC // Alias next/head component to noop for RSC
...@@ -1797,17 +1797,15 @@ export default async function getBaseWebpackConfig( ...@@ -1797,17 +1797,15 @@ export default async function getBaseWebpackConfig(
{ {
exclude: [staticGenerationAsyncStorageRegex], exclude: [staticGenerationAsyncStorageRegex],
issuerLayer: WEBPACK_LAYERS.server, issuerLayer: WEBPACK_LAYERS.server,
test(req: string) { test: {
// If it's not a source code file, or has been opted out of // Resolve it if it is a source code file, and it has NOT been
// bundling, don't resolve it. // opted out of bundling.
if ( and: [
!codeCondition.test.test(req) || codeCondition.test,
isResourceInPackages(req, optOutBundlingPackages) {
) { not: [optOutBundlingPackageRegex],
return false },
} ],
return true
}, },
resolve: { resolve: {
// It needs `conditionNames` here to require the proper asset, // It needs `conditionNames` here to require the proper asset,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment