0

I build a webpack project, to compile my .vue files.

Then I write some HTML comments in the .vue file, and I build file using webpack, while the mode is "production".

But when I browse the project in browser developer tools, I can catch sight of these HTML comments.

I think that HTML comments can shows up in "development" mode rather than "production" mode.

And their are my files:

webpack.config.js:

const {VueLoaderPlugin} = require('vue-loader');
const TerserPlugin = require('terser-webpack-plugin');
const {resolve} = require('path');
module.exports = {
    mode: 'production',
    devtool: false,
    entry: {
        main: resolve(__dirname, 'src/main.js')
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                use: [
                    {
                        loader: 'vue-loader',
                    },
                ],
            },
        ]
    },
    optimization: {
        minimize: true,
        minimizer: [new TerserPlugin({
            terserOptions: {
                ecma: undefined,
                parse: {},
                mangle: true,
                module: false,
            },
        })],
    },
    plugins: [
        new VueLoaderPlugin(),
    ]
};

App.vue: in /src

<template>
    <!--cumbrous comment-->
    <!--cumbrous comment-->
    <!--cumbrous comment-->
    <!--cumbrous comment-->
    <!--cumbrous comment-->
    <div>
        this is app
    </div>
</template>
<script>
export default {};
</script>

main.js in /src

import {createApp} from 'vue';
import App from './App.vue';

const app = createApp(App);
app.mount('#app');

And this is my package.json:

{
    "name": "delete-vue-comment",
    "version": "1.0.0",
    "main": "src/main.js",
    "license": "MIT",
    "scripts": {
        "dev": "webpack --config webpack.config.js"
    },
    "dependencies": {
        "terser-webpack-plugin": "5.3.1",
        "vue": "3.2.31",
        "vue-loader": "17.0.0",
        "webpack": "5.70.0",
        "webpack-cli": "4.9.2"
    }
}

Of course, this is the DOM element after vue.js rendered:

<div id="app" data-v-app="">
  <!--cumbrous comment-->
  <!--cumbrous comment-->
  <!--cumbrous comment-->
  <!--cumbrous comment-->
  <!--cumbrous comment-->
  <div> this is app </div>
</div>
quanxin
  • 1
  • 2
  • Not sure how this is an issue but you could maybe try [this package](https://www.npmjs.com/package/webpack-comment-remover-loader) or check [this question](https://stackoverflow.com/q/41939001/8816585). – kissu Mar 21 '22 at 11:57
  • so.......... The compiled file contains createCommentVNode. And that is cumbrous comment! – quanxin Mar 21 '22 at 11:58
  • Not sure this is a big deal, especially when comparing to any codebase. Also, if I'm not mistaken, a `v-if` can also produce a temporary comment, not a big deal by itself IMO. – kissu Mar 21 '22 at 12:00
  • I remembered that vue-CLI delete these HTML comments – quanxin Mar 21 '22 at 12:05
  • Maybe, not sure. Check my solutions and if it's not working, you may `eject` the configuration of the `vue-cli` to see it's webpack's internals/settings. There is maybe something already baked-in into Webpack tbh. – kissu Mar 21 '22 at 12:07
  • @kissu. Thank you , I will view code source of vue-cli – quanxin Mar 21 '22 at 12:09

0 Answers0