10
6

More than 5 years have passed since last update.

Nuxt.jsでwebpack.configを拡張してaliasを登録する

Posted at

nuxt.jsのwebpack拡張方法

nuxt.config.js内のbuild内にコードを記述

nuxt.config.js
module.exports = {
  build: {
    //ここに記述
    extend (config, ctx) {
      // サンプルではpushで渡している
      config.module.rules.push({
        enforce: 'pre',
        test: /\.(js|vue)$/,
        loader: 'eslint-loader',
        exclude: /(node_modules)/
      })
    }
  }
}

aliasを登録する方法

上記のを少しカスタマイズ
*私はpathモジュールを使っているので事前にインストールしていますが必須ではありません。

nuxt.config.js
module.exports = {
  build: {
    extend (config, ctx) {
      let path = require('path');
      config.resolve.alias['@components'] = path.join(__dirname, 'components')
      config.resolve.alias['@parts'] = path.join(__dirname, 'components/parts')
    }
  }
}

hoge.vue
<script>
import Logo from '@components/Logo.vue'
</script>

悩み

nuxt.config.js
config.resolve.alias = {
  '@components' : path.join(__dirname, 'components')
  '@parts' : path.join(__dirname, 'components/parts')
}

みたいな感じで複数登録を簡単に出来ないだろうか
(上ではエラーで出来ない)

10
6
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
10
6