introduction
注意到改变webpack配置需要执行承担风险 …
在继续增加自定义webpack 配置到你的应用中时确保Next.js 不支持你的使用情况
某些常见的特性作为了插件
为了扩展webpack的使用,你能够定义一个函数,它扩展了next.config.js内置配置,就像是
module.exports = {webpack: (config,{ buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }) => {// Important: return the modified configreturn config},}
webpack函数它会执行两次,一次是服务器,一次是客户端,这允许你通过isServer属性判断它是客户端还是服务器 ..
webpack函数的第二个参数时具有以下属性的对象
buildId:String构建id,构建之间使用一个独一无二的标识符dev:Boolean如果编译将在开发中完成isServer:Booleantrue 表示服务端编译,否则表示客户端编译nextRuntime:String | undefined对于服务端编译的目标运行时,要么是"edge","nodejs",对于客户端编译是undefineddefaultLoaders:Object由Next.js 内部使用的默认加载器 ..babel:Object- 默认的babel-loader配置
使用defaultLoaders.babel示例
// Example config for adding a loader that depends on babel-loader// This source was taken from the @next/mdx plugin source:// https://github.com/vercel/next.js/tree/canary/packages/next-mdxmodule.exports = {webpack: (config, options) => {config.module.rules.push({test: /\.mdx/,use: [options.defaultLoaders.babel,{loader: '@mdx-js/loader',options: pluginOptions.options,},],})return config},}
nextRuntime
注意到isServer是true情况是nextRuntime为"edge"或者"nodejs",nextRuntime"edge"仅仅在仅edge 运行时种服务器组件以及中间件生效 …
