Search code examples
vue.jsnpmwebpackvue-cli-3

Vue Cli 3 build custom directory for different type of files


I am new in using vue to build web app. By default, use npm run build, it will build the following structure:

enter image description here

But I hope to build as follow:

enter image description here

Then, how can I write the vue.config.js as output like what I want?


Solution

  • Using this GitHub issue as an example, you should be able to achieve this by adding something like this to your configuration...

    // vue.config.js
    module.exports = {
      chainWebpack: config => {
        config.module.rule('images').use('url-loader')
          .loader('file-loader') // replaces the url-loader
          .tap(options => Object.assign(options, {
            name: 'images/register/[name].[hash:8].[ext]'
          }))
        config.module.rule('svg').use('file-loader')
          .tap(options => Object.assign(options, {
            name: 'images/register/[name].[hash:8].[ext]'
          }))
      },
      css: {
        extract: {
          filename: 'css/register/[name].[hash:8].css',
          chunkFilename: 'css/register/[name].[hash:8].css'
        }
      },
      configureWebpack: {
        output: {
          filename: 'js/register/[name].[hash:8].js',
          chunkFilename: 'js/register/[name].[hash:8].js'
        }
      }
    }
    

    See https://cli.vuejs.org/config/#vue-config-js for more information and examples.