カスタム Webpack 設定
覚えておいてください: webpack 設定の変更はセマンティックバージョニングの対象ではないため、自己責任で進めてください
アプリケーションにカスタム webpack 設定を追加する前に、Next.js がすでにユースケースをサポートしていることを確認してください
よく求められる機能の一部はプラグインとして利用できます
webpack
の使用を拡張するには、次のように next.config.js
内でその設定を拡張する関数を定義できます。
next.config.js
module.exports = {
webpack: (
config,
{ buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
) => {
// Important: return the modified config
return config
},
}
webpack
関数は、サーバー (nodejs / edge ランタイム) 用に 2 回、クライアント用に 1 回の計 3 回実行されます。これにより、isServer
プロパティを使用してクライアントとサーバーの設定を区別できます。
webpack
関数の 2 番目の引数は、次のプロパティを持つオブジェクトです。
buildId
:String
- ビルド ID。ビルド間で一意の識別子として使用されますdev
:Boolean
- コンパイルが開発環境で行われるかどうかを示しますisServer
:Boolean
- サーバーサイドコンパイルの場合はtrue
、クライアントサイドコンパイルの場合はfalse
ですnextRuntime
:String | undefined
- サーバーサイドコンパイルのターゲットランタイム。"edge"
または"nodejs"
のいずれか。クライアントサイドコンパイルの場合はundefined
です。defaultLoaders
: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-mdx
module.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
は、nextRuntime
が "edge"
または "nodejs"
の場合に true
になることに注意してください。nextRuntime "edge
" は現在、ミドルウェアと edge ランタイムでのサーバーコンポーネント専用です。
お役に立ちましたか?