LoginSignup
6
1

More than 5 years have passed since last update.

next.config.jsのコールバック地獄を回避する

Posted at

例えば

プラグインが増えてしまう度にコールバック地獄を見たくないですよね。

next.config.js
const withA = require('a')
const withB = require('b')
const withC = require('c')
module.exports = withA(withB(withC({
  webpack(config, options) {
    return config
  }
})))

どんどん増えていきます…。
毎度プラグインが増える度にコールバックを書き直す必要があります。

解決策

reduceで解決します。

next.config.js
const plugins = [
  require('a'),
  require('b'),
  require('c')
]

const webpackConf = {
  webpack(config, options) {
    return config
  }
}

module.exports = plugins.reduce((prev, current) => current(prev), webpackConf)

結構スッキリしました。
あとは、プラグイン追加の度に、pluginsに追加していけばオッケーです。
module.exports以下は書き直す必要がありません。

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1