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