[英]How to use webpack 5 configs in Next.js?
我正在尝试向 webpack 配置添加experiments
,但无法确定我做错了什么。
我的环境:
我使用npx create-next-app blog
创建了一个全新的下一个应用程序
根据我所阅读的内容,我需要向package.json添加分辨率属性,如下所示:
"dependencies": {
"next": "10.0.6",
"react": "17.0.1",
"react-dom": "17.0.1"
},
"resolutions": {
"webpack": "5.21.2"
}
然后在next.config.js我有以下内容:
const webpack = require("webpack");
console.log(webpack.version); // 5.21.2
module.exports = {
webpack: function (config, options) {
console.log(options.webpack.version); // 4.44.1
config.experiments = {};
return config;
},
};
当我使用yarn dev
时,我收到以下错误:
- configuration[0] has an unknown property 'experiments'.
如果您注意到所需的模块 webpack 版本是5.21.2
但在配置设置中使用的版本是4.44.1
。
从 Next.js v11 开始 Webpack 5现在是所有 Next.js 应用程序的默认值:
Webpack 5 现在是所有 Next.js 应用程序的默认值。 If you did not have custom webpack configuration your application is already using webpack 5. If you do have custom webpack configuration you can refer to the Next.js webpack 5 documentation for upgrading guidance.
对于 Next.js 的早期版本,您需要在next.config.js
中为其设置一个标志:
module.exports = {
future: {
webpack5: true,
},
webpack: function (config, options) {
config.experiments = {};
return config;
},
};
并且您仍然可以使用 webpack 4,同时通过添加webpack5: false
标志升级到最新版本的 Next.js
module.exports = {
// Note: no `future` key here
webpack5: false,
}
由于现在默认使用 Next.js 11 webpack 5,无需额外配置。
您仍然可以通过在webpack5
设置为false
来使用next.config.js
4。
module.exports = {
webpack5: false
}
在 Next.js 11 之前,可以在next.config.js
中为 Webpack 5 启用一个future
标志。
module.exports = {
future: {
webpack5: true
},
webpack: function (config, options) {
console.log(options.webpack.version); // 5.18.0
config.experiments = {};
return config;
}
};
官方文档: https://nextjs.org/docs/messages/webpack5
转到
next.config.js
在下面添加带有future flag
的代码段
future: {
webpack5: true,
}
我的next.config.js
const path = require("path");
module.exports = {
trailingSlash: true,
webpackDevMiddleware: (config) => {
config.watchOptions = {
poll: 1000,
aggregateTimeout: 300,
};
return config;
},
sassOptions: {
includePaths: [path.join(__dirname, "styles")],
},
future: {
webpack5: true,
},
};
特征
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.