<legend id='gLbjQ'><style id='gLbjQ'><dir id='gLbjQ'><q id='gLbjQ'></q></dir></style></legend>

<small id='gLbjQ'></small><noframes id='gLbjQ'>

  • <i id='gLbjQ'><tr id='gLbjQ'><dt id='gLbjQ'><q id='gLbjQ'><span id='gLbjQ'><b id='gLbjQ'><form id='gLbjQ'><ins id='gLbjQ'></ins><ul id='gLbjQ'></ul><sub id='gLbjQ'></sub></form><legend id='gLbjQ'></legend><bdo id='gLbjQ'><pre id='gLbjQ'><center id='gLbjQ'></center></pre></bdo></b><th id='gLbjQ'></th></span></q></dt></tr></i><div id='gLbjQ'><tfoot id='gLbjQ'></tfoot><dl id='gLbjQ'><fieldset id='gLbjQ'></fieldset></dl></div>

      <tfoot id='gLbjQ'></tfoot>

        • <bdo id='gLbjQ'></bdo><ul id='gLbjQ'></ul>

        UglifyJS 抛出意外的标记:keyword (const) with node_modu

        时间:2023-05-29
          • <bdo id='3APOC'></bdo><ul id='3APOC'></ul>

                  <i id='3APOC'><tr id='3APOC'><dt id='3APOC'><q id='3APOC'><span id='3APOC'><b id='3APOC'><form id='3APOC'><ins id='3APOC'></ins><ul id='3APOC'></ul><sub id='3APOC'></sub></form><legend id='3APOC'></legend><bdo id='3APOC'><pre id='3APOC'><center id='3APOC'></center></pre></bdo></b><th id='3APOC'></th></span></q></dt></tr></i><div id='3APOC'><tfoot id='3APOC'></tfoot><dl id='3APOC'><fieldset id='3APOC'></fieldset></dl></div>

                  <small id='3APOC'></small><noframes id='3APOC'>

                  <legend id='3APOC'><style id='3APOC'><dir id='3APOC'><q id='3APOC'></q></dir></style></legend><tfoot id='3APOC'></tfoot>
                    <tbody id='3APOC'></tbody>

                • 本文介绍了UglifyJS 抛出意外的标记:keyword (const) with node_modules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我开始的一个小项目使用声明 const 变量的节点模块(通过 npm 安装).运行测试这个项目很好,但是执行UglifyJS时browserify失败了.

                  A small project I started make use a node module (installed via npm) that declares const variables. Running and testing this project is well, but browserify fails when UglifyJS is executed.

                  意外标记:关键字(const)

                  Unexpected token: keyword (const)

                  这是一个通用的 Gulp 文件,我已经成功地将它用于过去的一些其他项目而没有这个问题(即没有那个特定的节点模块).

                  Here is a generic Gulp file that I have successfully been using for a few other past projects without this issue (i.e. without that particular node module).

                  'use strict';
                  
                  const browserify = require('browserify');
                  const gulp = require('gulp');
                  const source = require('vinyl-source-stream');
                  const derequire = require('gulp-derequire');
                  const buffer = require('vinyl-buffer');
                  const uglify = require('gulp-uglify');
                  const sourcemaps = require('gulp-sourcemaps');
                  const gutil = require('gulp-util');
                  const path = require('path');
                  const pkg = require('./package');
                  const upperCamelCase = require('uppercamelcase');
                  
                  const SRC_PATH = path.dirname(pkg.main);
                  const DIST_PATH = path.dirname(pkg.browser);
                  
                  const INPUT_FILE = path.basename(pkg.main);
                  const OUTPUT_FILE = path.basename(pkg.browser);
                  
                  const MODULE_NAME = upperCamelCase(pkg.name);
                  
                  
                  gulp.task('default', () => {
                    // set up the browserify instance on a task basis
                    var b = browserify({
                      entries: INPUT_FILE,
                      basedir: SRC_PATH,
                      transform: ['babelify'],
                      standalone: MODULE_NAME,
                      debug: true
                    });
                  
                    return b.bundle()
                      .pipe(source(OUTPUT_FILE))
                      .pipe(buffer())
                      .pipe(derequire())
                      .pipe(sourcemaps.init({loadMaps: true}))
                      .pipe(uglify())
                      .on('error', gutil.log)
                      .pipe(sourcemaps.write('.'))
                      .pipe(gulp.dest(DIST_PATH))
                    ;
                  });
                  

                  我已尝试通过将 npm 安装的模块中的所有 const 替换为 var 来解决此问题,一切都很好.所以我不理解失败.

                  I have tried fixing this by replace all const to var in that npm-installed module, and everything is fine. So I do not understand the failure.

                  const 有什么问题?除非有人使用 IE10,否则所有主流浏览器都支持这种语法.

                  What's wrong with const? Unless someone uses IE10, all major browsers support this syntax.

                  有没有办法解决这个问题而无需更改该节点模块?

                  Is there a way to fix this without requiring a change to that node module?

                  我已经暂时(或永久)用 Butternut 替换了 UglifyJS,并且似乎可以工作.

                  I have temporarily (or permanently) replaced UglifyJS with Butternut and seem to work.

                  推荐答案

                  由于ChrisR 提到,UglifyJS 不支持 ES6完全没有.

                  As ChrisR mentionned, UglifyJS does not support ES6 at all.

                  ES6 需要使用 terser-webpack-plugin (webpack@5会使用这个插件进行丑化)

                  You need to use terser-webpack-plugin for ES6 (webpack@5 will use this plugin for uglification)

                  npm install terser-webpack-plugin --save-dev
                  

                  然后在你的 plugins 数组中定义

                  Then define in your plugins array

                  const TerserPlugin = require('terser-webpack-plugin')
                  
                    new TerserPlugin({
                      parallel: true,
                      terserOptions: {
                        ecma: 6,
                      },
                    }),
                  

                  来源

                  这篇关于UglifyJS 抛出意外的标记:keyword (const) with node_modules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:GulpUglifyError:无法缩小 JavaScript 下一篇:如何编写一个简单的 gulp 管道函数?

                  相关文章

                  最新文章

                    • <bdo id='QZcRk'></bdo><ul id='QZcRk'></ul>
                    <i id='QZcRk'><tr id='QZcRk'><dt id='QZcRk'><q id='QZcRk'><span id='QZcRk'><b id='QZcRk'><form id='QZcRk'><ins id='QZcRk'></ins><ul id='QZcRk'></ul><sub id='QZcRk'></sub></form><legend id='QZcRk'></legend><bdo id='QZcRk'><pre id='QZcRk'><center id='QZcRk'></center></pre></bdo></b><th id='QZcRk'></th></span></q></dt></tr></i><div id='QZcRk'><tfoot id='QZcRk'></tfoot><dl id='QZcRk'><fieldset id='QZcRk'></fieldset></dl></div>

                    <small id='QZcRk'></small><noframes id='QZcRk'>

                  1. <legend id='QZcRk'><style id='QZcRk'><dir id='QZcRk'><q id='QZcRk'></q></dir></style></legend>

                      <tfoot id='QZcRk'></tfoot>