vue在build之后生成的index.html多次重复载入样式表和js文件的问题

创建一个基于 webpack 模板的新项目(2019.03.14 测试)


1、进入文件目录,然后调起命令行工具,输入:

#wwwroot 为自定义项目名
$ vue init webpack wwwroot

2、运行命令后,会让用户输入几个基本选项:

? Project name wwwroot   // 项目名称
? Project description a vue.js project   // 项目描述
? Author yourname    // 作者
? Vue build standalone    // 
? Install vue-router ? Yes    // 是否安装路由
? Use ESLint to lint you code ? Yes    // 使用ESLint 到你的代码
? Pick an ESLint preset Standard    // 选择一个预置ESLint
? Setup unit tests width karma + Mocha ? Yes    // 设置单元测试Karma + Mocha
? Setup e2e tests with Nightwatch ? Yes    // 设置端到端测试,Nightwatch

3、运行项目

$ cnpm run dev

4、编译项目

$ cnpm run build

在多次编译后会发现,index.html 文件内存在了多组重复引入样式和JS。

<script type="text/javascript" src="/static/js/manifest.2ae2e69a05c33dfc65f8.js"></script>
<script type="text/javascript" src="/static/js/vendor.4e6936140256982905ea.js"></script>
<script type="text/javascript" src="/static/js/app.6cac6299ff42cd3f1d19.js"></script>
<script type="text/javascript" src="/static/js/manifest.2ae2e69a05c33dfc65f8.js"></script>
<script type="text/javascript" src="/static/js/vendor.4e6936140256982905ea.js"></script>
<script type="text/javascript" src="/static/js/app.6cac6299ff42cd3f1d19.js"></script>

查找编译设置文件

/build/webpack.prod.conf.js

new HtmlWebpackPlugin({
    filename: process.env.NODE_ENV === 'testing'
    ? 'index.html'
    : config.build.index,
    template: 'index.html',
    inject: true,
    minify: {
    removeComments: true,
    collapseWhitespace: false,
    removeAttributeQuotes: false
    // more options:
    // https://github.com/kangax/html-minifier#options-quick-reference
    },
    // necessary to consistently work with multiple chunks via CommonsChunkPlugin
    chunksSortMode: 'dependency'
}),

编译文件后我们会把 /dist/ 下的文件复制到根目录下,同时每次编译都会使用之前编译的生成的index.html,这样循环往复,就会生成多组重复引入的样式和JS。

template: 'index.html',     // 模板文件
inject: true,    // 注入资源引用
minify: {}    // 压缩
removeComments: [true/false]    // 删除注释
removeAttrbuteQuotes: [true/false]    // 删除属性引号
collapseWhitespace: [true/false]    // 折叠空白

基于这个原因,我们可以设置 template: 'template/index.html',即可解决这个问题。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值