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

Update bundler for pre-bundling runtime #75294

Merged
merged 4 commits into from
Jan 24, 2025
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
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 }],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully removed in #75297


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.
Expand Down Expand Up @@ -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
}

Comment on lines +131 to +134
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is breaking Next.js with custom Babel configs:

 ⨯ ./node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/client/app-call-server.js
Error: Failed to resolve "@babel/runtime/regenerator" relative to "/Users/sebbie/throwaway/nextjs-custom-babel"
Import trace for requested module:
./node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/client/app-call-server.js
./node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/client/app-index.js
 ⨯ ./node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/client/app-call-server.js
Error: Failed to resolve "@babel/runtime/regenerator" relative to "/Users/sebbie/throwaway/nextjs-custom-babel"
Import trace for requested module:
./node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/client/app-call-server.js
./node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/client/app-index.js
 ⨯ [TypeError: The "path" argument must be of type string. Received undefined] {
  code: 'ERR_INVALID_ARG_TYPE'
}
[TypeError: The "path" argument must be of type string. Received undefined] {
  code: 'ERR_INVALID_ARG_TYPE'
}
[TypeError: The "path" argument must be of type string. Received undefined] {
  code: 'ERR_INVALID_ARG_TYPE'
}
 ⨯ ./app/layout.js:1:1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that @babel/runtime is not a declared peer dependency. We relied on the bundled version.

if (request.endsWith('.external')) {
const resolve = getResolve()
const resolved = await resolve(context, request)
Expand All @@ -156,44 +159,23 @@ 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' : ''}${
experimental ? '-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'),
Expand Down
5 changes: 3 additions & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -92,7 +92,7 @@
},
"taskr": {
"requires": [
"./taskfile-webpack.js",
"./taskfile-rspack.js",
"./taskfile-ncc.js",
"./taskfile-swc.js",
"./taskfile-watch.js"
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/compiled/sass-loader/cjs.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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) => {
Expand All @@ -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')}`,
})
}

Expand Down
40 changes: 20 additions & 20 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
Expand All @@ -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',
}),
Expand All @@ -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',
}),
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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',
}),
Expand All @@ -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',
}),
Expand All @@ -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',
}),
Expand All @@ -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',
}),
Expand Down
6 changes: 3 additions & 3 deletions packages/next/webpack-plugins/devtools-ignore-list-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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
Expand All @@ -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) => {
Expand Down
Loading
Loading