Js compress by uglify-js give error Unexpected token: name ($)

情到浓时终转凉″ 提交于 2021-02-07 08:58:35

问题


I am compress my js files by uglify-js I want to compress Bootstrap 4 js file but it give me Error like this. Can I compress all js files by only uglify-js Or how can i do it.

    Parse error at src\bootstrap\alert.js:1,7
    import $ from 'jquery'
           ^
    ERROR: Unexpected token: name ($)

my package.json

"devDependencies": {
    "node-sass": "^4.6.1",
    "nodemon": "^1.12.1"
  },
  "dependencies": {
    "autoprefixer": "^7.1.6",
    "jquery": "^3.2.1",
    "postcss-cli": "^4.1.1",
    "uglify-js": "^3.1.9"
  }

回答1:


Try using the ES6 version of uglify-js. Replace with this in your package.json

"uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"

or via command line:

npm install --save uglify-js@github:mishoo/UglifyJS2#harmony




回答2:


uglify-js does not support ES2015 syntax like import statements or arrow functions. Instead, use the uglify-es module:

-    "uglify-js": "^3.1.9"
+    "uglify-es": "^3.1.9"


来源:https://stackoverflow.com/questions/47286311/js-compress-by-uglify-js-give-error-unexpected-token-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!