简体   繁体   English

Browserify + vue + HTML 缩小

[英]Browserify + vue + HTML minify

I'm using elixir together with browserify .我正在使用elixirbrowserify In my vue components I include templates from html files, like this:在我的 vue 组件中,我包含来自 html 文件的模板,如下所示:

Vue.extend({
    template: require('./call.html'),
    props: {
        call: {
            type: Object,
            required: true
        }
    },
    //...

It works as expected.它按预期工作。 However, if I run gulp --production , the html is not compressed in the generated file.但是,如果我运行gulp --production ,则生成的文件中不会压缩 html。

What I'd like to achieve is to remove all unneeded tab, space, newline characters, comments from the included html files.我想要实现的是从包含的 html 文件中删除所有不需要的制表符、空格、换行符、注释。

There's a package called gulp-minify-html but I don't know how if I could use this one for solving this issue.有一个名为gulp-minify-html的包,但我不知道如何使用它来解决这个问题。

Has anyone here done something similar to this?这里有人做过类似的事情吗?

Take a look at vueify .看看vueify Minification is automatically applied on template, when compiled with NODE_ENV=production.使用 NODE_ENV=production 编译时,缩小会自动应用于模板。

You won't need to place your html in separate file in that case, as well.在这种情况下,您也不需要将 html 放在单独的文件中。 But you could if needed: just omit <template> block and add template to module.exports object as usual:但如果需要,您可以:只需省略<template>块并像往常一样将模板添加到 module.exports 对象:

<script>
    module.exports = {
        template: require('./template1.html'),
    };
</script>

Research研究

So, in fact its minification is purely decorational.所以,实际上它的缩小纯粹是装饰性的。 As follows from dependencies list, vueify depends on html-minifier .从依赖列表中可以看出, vueify 依赖于html-minifier

Let's take a look at code:我们来看一下代码:

// production minifiers
if (process.env.NODE_ENV === 'production') {
  var htmlMinifier = require('html-minifier')
  // required for Vue 1.0 shorthand syntax
  var htmlMinifyOptions = {
    customAttrSurround: [[/@/, new RegExp('')], [/:/, new RegExp('')]]
  }
}

The only option here is customAttrSurround , thus, anything else will be taken from default values.这里唯一的选项是customAttrSurround ,因此,其他任何内容都将从默认值中获取。

Result结果

We have several options here:我们在这里有几个选择:

  1. Fix the source once.修复一次源。 Just add your confighere .只需在此处添加您的配置。
  2. Create an issue on github.在github上创建一个问题。 Minifier config surely must be included in vue.config.js. Minifier 配置肯定必须包含在 vue.config.js 中。
  3. Pull request.拉取请求。

Final solution by asker提问者的最终解决方案

The code above should be replaced by this:上面的代码应该替换为:

// production minifiers
if (process.env.NODE_ENV === 'production') {
  var htmlMinifier = require('html-minifier')
  // required for Vue 1.0 shorthand syntax
  var htmlMinifyOptions = {
    customAttrSurround: [[/@/, new RegExp('')], [/:/, new RegExp('')]],
    collapseWhitespace: true,
    removeComments: true
  }
}

See my pull request查看我的拉取请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM