如何为 vue 生产构建输出单个 build.js 文件 [英] How to output single build.js file for vue production build

查看:69
本文介绍了如何为 vue 生产构建输出单个 build.js 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 vue-cli 2.9.6,并使用 vue init webpack 创建了一个 vue 项目.

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

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

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

还有像这样的 css 文件:

And also css files like this:

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

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

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. 为了防止创建 vendor.js 和 manifest.js,只需从 webpack.prod.conf.js
  2. 中删除以下代码
  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. 为了防止在 config/index.js 中设置 sourceMaps 变量 productionSourceMaptruefalse>

  1. To prevent sourceMaps set in config/index.js the variable productionSourceMap from true to false

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

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. 将 webpack.prod.conf.js 中 ExtractTextPlugin 的 css 输出文件更新选项的名称更新为 filename: utils.assetsPath('css/styles.css'),代码>
  1. Update name of the css output file updating options of ExtractTextPluginin webpack.prod.conf.js to filename: utils.assetsPath('css/styles.css'),

这篇关于如何为 vue 生产构建输出单个 build.js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆