How to output single build.js file for vue product

2019-08-02 15:30发布

I'm using vue-cli 2.9.6, and created a vue project using vue init webpack <project name>.

When I call vue run build, it is creating a number of different js files (and names change each time...):

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

And also css files like this:

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

I would like the output to simply be 2 files:

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

How can I achieve this?

1条回答
冷血范
2楼-- · 2019-08-02 15:45
  1. for preventing creation of vendor.js and manifest.js just remove following code from 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. To prevent sourceMaps set in config/index.js the variable productionSourceMap from true to false

  2. Changing name of app.js to build.js can be obtained modifying the entry and outputproperties in webpack.base.conf.js this way:

entry: {
    build: './src/main.js'
},
output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].js'),
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
},
  1. Update name of the css output file updating options of ExtractTextPluginin webpack.prod.conf.js to filename: utils.assetsPath('css/styles.css'),
查看更多
登录 后发表回答