Next.js

next-rspack 是一个社区驱动的插件,它允许 Next.js 项目使用 Rspack 作为打包工具(实验性)。

TIP

查看 Rspack 加入 Next.js 生态 博客文章了解更多。

安装

安装 next-rspack 包:

npm
yarn
pnpm
bun
npm add next-rspack -D
TIP

如果你使用的 Next.js 版本低于 15.3.0,请先升级到 >= 15.3.0 版本,参考 Next.js - Upgrading

使用

在项目的 next.config.jsnext.config.ts 中,对现有配置进行包装:

next.config.ts
next.config.js
import withRspack from 'next-rspack';
import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  /* config options here */
};

export default withRspack(nextConfig);

示例:next.js/examples/with-rspack

自定义 Rspack 配置

得益于 Rspack 对 webpack 的兼容性,在使用 next-rspack 时,可以沿用与 webpack 相同的方式来自定义配置。

next.config.js 中,通过以下回调函数来修改 Rspack 配置:

next.config.js
module.exports = {
  webpack: (
    config,
    { buildId, dev, isServer, defaultLoaders, nextRuntime, webpack },
  ) => {
    // 重要:返回修改后的配置
    return config;
  },
};

更多信息请查看 Next.js - Custom Webpack Config

使用 next-compose-plugins

此外,你可以使用 next-compose-plugins 来快速将 next-rspack 与其他 Next.js 插件集成:

next.config.js
const withPlugins = require('next-compose-plugins');
const withRspack = require('next-rspack');

module.exports = withPlugins([
  [withRspack],
  // 其他插件写在这里
]);