Unverified Commit 04c3f0b2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update bundler for pre-bundling runtime (#75294)

This improves the build script quite a bit and makes dev editing much
faster. Also fixed excludes causing webpack/babel compiled packages to
be bundled into the runtime un-necessarily reducing from `10MB` ->
`1.2MB`.

Before: `Finished next_bundle in 9.67s`

After: `Finished next_bundle in 1.27s`

Validated here
https://github.com/vercel/vercel/actions/runs/12957911052/job/36147343396?pr=12914
parent 47a0cd2d
......@@ -162,7 +162,7 @@
"files": ["packages/**"],
"excludedFiles": [
"packages/next/taskfile*.js",
"packages/next/next-runtime.webpack-config.js"
"packages/next/next_runtime.config.js"
],
"rules": {
"no-shadow": ["error", { "builtinGlobals": false }],
......
const webpack = require('webpack')
const webpack = require('@rspack/core')
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const EvalSourceMapDevToolPlugin = require('./webpack-plugins/eval-source-map-dev-tool-plugin')
const DevToolsIgnoreListPlugin = require('./webpack-plugins/devtools-ignore-list-plugin')
function shouldIgnorePath(modulePath) {
function shouldIgnorePath() {
// For consumers, everything will be considered 3rd party dependency if they use
// the bundles we produce here.
// In other words, this is all library code and should therefore be ignored.
......@@ -130,6 +128,11 @@ const bundleTypes = {
module.exports = ({ dev, turbo, bundleType, experimental }) => {
const externalHandler = ({ context, request, getResolve }, callback) => {
;(async () => {
if (request.match(/next[/\\]dist[/\\]compiled[/\\](babel|webpack)/)) {
callback(null, 'commonjs ' + request)
return
}
if (request.endsWith('.external')) {
const resolve = getResolve()
const resolved = await resolve(context, request)
......@@ -156,7 +159,7 @@ module.exports = ({ dev, turbo, bundleType, experimental }) => {
return {
entry: bundleTypes[bundleType],
target: 'node',
mode: 'production',
mode: dev ? 'development' : 'production',
output: {
path: path.join(__dirname, 'dist/compiled/next-server'),
filename: `[name]${turbo ? '-turbo' : ''}${
......@@ -164,36 +167,15 @@ module.exports = ({ dev, turbo, bundleType, experimental }) => {
}.runtime.${dev ? 'dev' : 'prod'}.js`,
libraryTarget: 'commonjs2',
},
devtool: process.env.NEXT_SERVER_EVAL_SOURCE_MAPS
? // We'll use a fork in plugins
false
: 'source-map',
devtool: 'source-map',
optimization: {
moduleIds: 'named',
minimize: true,
concatenateModules: true,
minimizer: [
new TerserPlugin({
minify: TerserPlugin.swcMinify,
terserOptions: {
compress: {
dead_code: true,
// Zero means no limit.
passes: 0,
},
format: {
preamble: '',
},
mangle:
dev && !process.env.NEXT_SERVER_EVAL_SOURCE_MAPS ? false : true,
},
}),
],
minimizer: [new webpack.SwcJsMinimizerRspackPlugin()],
},
plugins: [
process.env.NEXT_SERVER_EVAL_SOURCE_MAPS
? new EvalSourceMapDevToolPlugin({ shouldIgnorePath })
: new DevToolsIgnoreListPlugin({ shouldIgnorePath }),
new DevToolsIgnoreListPlugin({ shouldIgnorePath }),
new webpack.DefinePlugin({
'typeof window': JSON.stringify('undefined'),
'process.env.NEXT_MINIMAL': JSON.stringify('true'),
......
......@@ -80,7 +80,7 @@
"next": "./dist/bin/next"
},
"scripts": {
"dev": "cross-env NEXT_SERVER_EVAL_SOURCE_MAPS=1 taskr",
"dev": "taskr",
"release": "taskr release",
"build": "pnpm release",
"prepublishOnly": "cd ../../ && turbo run build",
......@@ -92,7 +92,7 @@
},
"taskr": {
"requires": [
"./taskfile-webpack.js",
"./taskfile-rspack.js",
"./taskfile-ncc.js",
"./taskfile-swc.js",
"./taskfile-watch.js"
......@@ -171,6 +171,7 @@
"@next/swc": "15.2.0-canary.25",
"@opentelemetry/api": "1.6.0",
"@playwright/test": "1.41.2",
"@rspack/core": "1.2.2",
"@storybook/addon-essentials": "^8.4.7",
"@storybook/addon-interactions": "^8.4.7",
"@storybook/addon-onboarding": "^8.4.7",
......
......@@ -1330,9 +1330,12 @@ export default class NextNodeServer extends BaseServer<
}
protected getMiddlewareManifest(): MiddlewareManifest | null {
if (this.minimalMode) return null
const manifest: MiddlewareManifest = require(this.middlewareManifestPath)
return manifest
if (this.minimalMode) {
return null
} else {
const manifest: MiddlewareManifest = require(this.middlewareManifestPath)
return manifest
}
}
/** Returns the middleware routing item if there is one. */
......
const webpack = require('webpack')
const rspack = require('@rspack/core')
module.exports = function (task) {
// eslint-disable-next-line require-yield
task.plugin('webpack', {}, function* (_, options) {
task.plugin('rspack', {}, function* (_, options) {
options = options || {}
const compiler = webpack(options.config)
const compiler = rspack(options.config)
if (options.watch) {
return compiler.watch({}, (err, stats) => {
......@@ -21,15 +21,15 @@ module.exports = function (task) {
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
return this.emit('plugin_error', {
plugin: 'taskfile-webpack',
plugin: 'taskfile-rspack',
error: err?.message ?? stats.toString(),
})
}
if (stats.hasWarnings()) {
this.emit('plugin_warning', {
plugin: 'taskfile-webpack',
warning: `webpack compiled ${options.name} with warnings:\n${stats.toString('errors-warnings')}`,
plugin: 'taskfile-rspack',
warning: `rspack compiled ${options.name} with warnings:\n${stats.toString('errors-warnings')}`,
})
}
......
......@@ -2846,9 +2846,9 @@ export async function release(task) {
}
export async function next_bundle_app_turbo(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
turbo: true,
bundleType: 'app',
}),
......@@ -2857,9 +2857,9 @@ export async function next_bundle_app_turbo(task, opts) {
}
export async function next_bundle_app_prod(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
dev: false,
bundleType: 'app',
}),
......@@ -2868,9 +2868,9 @@ export async function next_bundle_app_prod(task, opts) {
}
export async function next_bundle_app_dev(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
dev: true,
bundleType: 'app',
}),
......@@ -2879,9 +2879,9 @@ export async function next_bundle_app_dev(task, opts) {
}
export async function next_bundle_app_turbo_experimental(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
turbo: true,
bundleType: 'app',
experimental: true,
......@@ -2891,9 +2891,9 @@ export async function next_bundle_app_turbo_experimental(task, opts) {
}
export async function next_bundle_app_prod_experimental(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
dev: false,
bundleType: 'app',
experimental: true,
......@@ -2903,9 +2903,9 @@ export async function next_bundle_app_prod_experimental(task, opts) {
}
export async function next_bundle_app_dev_experimental(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
dev: true,
bundleType: 'app',
experimental: true,
......@@ -2915,9 +2915,9 @@ export async function next_bundle_app_dev_experimental(task, opts) {
}
export async function next_bundle_pages_prod(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
dev: false,
bundleType: 'pages',
}),
......@@ -2926,9 +2926,9 @@ export async function next_bundle_pages_prod(task, opts) {
}
export async function next_bundle_pages_dev(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
dev: true,
bundleType: 'pages',
}),
......@@ -2937,9 +2937,9 @@ export async function next_bundle_pages_dev(task, opts) {
}
export async function next_bundle_pages_turbo(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
turbo: true,
bundleType: 'pages',
}),
......@@ -2948,9 +2948,9 @@ export async function next_bundle_pages_turbo(task, opts) {
}
export async function next_bundle_server(task, opts) {
await task.source('dist').webpack({
await task.source('dist').rspack({
watch: opts.dev,
config: require('./next-runtime.webpack-config')({
config: require('./next_runtime.config')({
dev: false,
bundleType: 'server',
}),
......
// Source: https://github.com/mondaychen/devtools-ignore-webpack-plugin/blob/e35ce41d9606a92a455ef247f509a1c2ccab5778/src/index.ts
// eslint-disable-next-line import/no-extraneous-dependencies -- this is a dev-only file
const webpack = require('webpack')
const webpack = require('@rspack/core')
// Following the naming conventions from
// https://tc39.es/source-map/#source-map-format
......@@ -28,12 +28,12 @@ module.exports = class DevToolsIgnorePlugin {
}
}
apply(compiler) {
const { RawSource } = compiler.webpack.sources
const { RawSource } = webpack.sources
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
stage: compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
additionalAssets: true,
},
(assets) => {
......
This diff is collapsed.
......@@ -92,11 +92,14 @@ describe.each(
expect(cliOutput).toContain(
'' +
"\n ⨯ TypeError: Cannot read properties of undefined (reading 'H')" +
'\n at c (**)' +
// location not important. Just that this is the only frame.
// TODO: Stack should start at product code. Possible React limitation.
'\n at Container (**)' +
// Will just point to original file location
'\n 2 |'
process.env.__NEXT_EXPERIMENTAL_PPR
? ''
: '\n 2 |'
)
await assertHasRedbox(browser)
......
......@@ -218,18 +218,12 @@ describe('server-side dev errors', () => {
if (isTurbopack) {
expect(stderrOutput).toContain(
' ⨯ ReferenceError: missingVar is not defined' +
'\n at handler (../../test/integration/server-side-dev-errors/pages/api/hello.js:2:2)' +
// Next.js internal frame. Feel free to adjust.
// Not ignore-listed because we're not in an isolated app and Next.js is symlinked so it's not in node_modules
'\n at async'
'\n at handler (../../test/integration/server-side-dev-errors/pages/api/hello.js:2:2)'
)
} else {
expect(stderrOutput).toStartWith(
'⨯ ReferenceError: missingVar is not defined' +
'\n at handler (../../test/integration/server-side-dev-errors/pages/api/hello.js:2:2)' +
// Next.js internal frame. Feel free to adjust.
// Not ignore-listed because we're not in an isolated app and Next.js is symlinked so it's not in node_modules
'\n at async'
'\n at handler (../../test/integration/server-side-dev-errors/pages/api/hello.js:2:2)'
)
}
expect(stderrOutput).toContain(
......
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