如何输出单个build.js文件以进行Vue生产构建

时间:2019-02-03 22:49:12

标签: vue.js webpack vue-cli

我正在使用vue-cli 2.9.6,并使用vue init webpack <project name>创建了一个vue项目。

当我调用vue run build时,它正在创建许多不同的js文件(名称每次都会更改...):

vendor.20d54e752692d648b42a.js
vendor.20d54e752692d648b42a.js.map
app.ed70f310595763347909.js
app.ed70f310595763347909.js.map
manifest.2ae2e69a05c33dfc65f8.js
manifest.2ae2e69a05c33dfc65f8.js.map

还有这样的css文件:

app.a670fcd1e9a699133143a2b144475068.css
app.a670fcd1e9a699133143a2b144475068.css.map

我希望输出仅是2个文件:

build.js  { for all js }
styles.css { for all css }

我该如何实现?

2 个答案:

答案 0 :(得分:2)

  1. 要防止创建vendor.js和manifest.js,只需从webpack.prod.conf.js中删除以下代码
    // split vendor js into its own file
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks (module) {
        // any required modules inside node_modules are extracted to vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(
            path.join(__dirname, '../node_modules')
          ) === 0
        )
      }
    }),
    // extract webpack runtime and module manifest to its own file in order to
    // prevent vendor hash from being updated whenever app bundle is updated
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',
      minChunks: Infinity
    }),
  1. 为防止在config/index.js中将变量productionSourceMap中设置的sourceMap从truefalse

  2. 可以通过修改webpack.base.conf.js中的entryoutput属性来获得将app.js更改为build.js的名称:

entry: {
    build: './src/main.js'
},
output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].js'),
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
},
  1. 将webpack.prod.conf.js中ExtractTextPlugin的css输出文件更新选项的名称更新为filename: utils.assetsPath('css/styles.css'),

答案 1 :(得分:0)

// vue.config.js

module.exports = {
  chainWebpack: config => {
    if(config.plugins.has('extract-css')) {
      const extractCSSPlugin = config.plugin('extract-css')
      extractCSSPlugin && extractCSSPlugin.tap(() => [{
        filename: 'styles.css',
        chunkFilename: 'styles.css'
      }])
    }
  },
  configureWebpack: {
    output: {
      filename: 'build.js',
      chunkFilename: 'build.js'
    }
  }
}

module.exports = {
  configureWebpack: {
    optimization: {
      splitChunks: false
    }
  },
  filenameHashing: false
}