首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue CLI 3用Terser删除console.log和代码注释

Vue CLI 3用Terser删除console.log和代码注释
EN

Stack Overflow用户
提问于 2019-10-09 15:49:20
回答 1查看 4.4K关注 0票数 5

我使用的是VUE CLI 3,我需要从构建的产品中删除console.log和代码注释。这是我的特塞尔设置:

webpack.config.jssrc文件夹中

代码语言:javascript
复制
    module.exports = {
mode: 'production',
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          ecma: undefined,
          warnings: false,
          parse: {},
          compress: {drop_debugger},
          mangle: true, // Note `mangle.properties` is `false` by default.
          module: false,
          output: null,
          toplevel: false,
          nameCache: null,
          ie8: false,
          keep_classnames: undefined,
          keep_fnames: false,
          safari10: false,
        },
      }),
    ],
  },
};

我的生产工作流程:运行npm run build -> cd dist -> npm run serve

生产构建仍然输出所有console.log语句,并显示代码注释(<!-- -->)。我需要改变什么才能移除它们?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-10 11:16:13

首先:确保正确配置特塞尔

代码语言:javascript
复制
terserOptions: {
    ecma: 6,
    compress: { drop_console: true },
    output: { comments: false, beautify: false }
}

npm run serve

通常是以下方面的捷径:

vue-cli-service

代码语言:javascript
复制
vue-cli-service --help

  Usage: vue-cli-service <command> [options]

  Commands:

    serve     start development server
    build     build for production
    inspect   inspect internal webpack config
    lint      lint and fix source files

因此,当您的工作流是上面提到的npm run build -> cd dist -> npm run serve时,您实际上是在启动开发服务器,它不应用Terser。

为了运行生产构建,您可以使用任何能够提供静态内容的网路伺服器

NodeJs示例:

代码语言:javascript
复制
npm install -g serve
serve -s dist

代码语言:javascript
复制
npm install -g node-static
static dist
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58307891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档