问题描述
我使用的是 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?
推荐答案
- 为了防止创建 vendor.js 和 manifest.js,只需从
webpack.prod.conf.js
中删除以下代码
- 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
}),
为了防止在
config/index.js
中设置 sourceMaps 变量productionSourceMap
从true
到false
>
To prevent sourceMaps set in
config/index.js
the variableproductionSourceMap
fromtrue
tofalse
将app.js的名称改为build.js可以通过修改webpack.base.conf.js中的entry
和output
属性来获得:
Changing name of app.js to build.js can be obtained modifying the entry
and output
properties 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')
},
- 将 webpack.prod.conf.js 中
ExtractTextPlugin
的 css 输出文件更新选项的名称更新为filename: utils.assetsPath('css/styles.css'),
代码>
- Update name of the css output file updating options of
ExtractTextPlugin
in webpack.prod.conf.js tofilename: utils.assetsPath('css/styles.css'),
这篇关于如何为 vue 生产构建输出单个 build.js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!