How to uglify bootstrap (ES6)
See original GitHub issueI am trying to use bootstrap partly
global.Tether = require('tether');
global.Popper = require('popper.js/dist/umd/popper');
require('bootstrap/js/src/modal');
, but I alway get
test.js from UglifyJs Unexpected token: operator (>) [test.js:1722,20]
in production mode. Dev mode is fine.
I added the following to activate ES6:
package.json
"devDependencies": {
...
"uglify-js": "https://github.com/mishoo/UglifyJS2#harmony-v2.8.22",
"uglifyjs-webpack-plugin": "^0.4.6"
}
webpack.config.js
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
...
.addPlugin(new webpack.ProvidePlugin(new UglifyJSPlugin()))
But there is no effecft. Still the same error. Any ideas?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:17 (4 by maintainers)
Top Results From Across the Web
Uglifying Bootstrap ES6 JS code causes "Modal redeclared ...
I'm getting the error "Modal redeclared" while trying to uglify Bootstrap 4 JS code using Grunt. I found out those are ES6 so...
Read more >Uglify-js errors on MDB.JS - Material Design for Bootstrap
I run uglify-js on my system and up till MDB-Pro version 4.7.4 this ran fine. After updating to mdb-pro version 4.8.2 a couple...
Read more >uglify-js - npm
UglifyJS can take multiple input files. It's recommended that you pass the input files first, then pass the options. UglifyJS will parse input ......
Read more >Webpack and bundlers · Bootstrap v5.0
Installing Bootstrap. Install bootstrap as a Node.js module using npm. Importing JavaScript. Import Bootstrap's JavaScript by adding this line to your app's ...
Read more >How to Minify JavaScript — Recommended Tools and Methods
Every major JavaScript Library developer (Angular, Bootstrap, and so on) provides a minified version of JavaScript for production deployment.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @wenmingtang,
Are you trying to transpile your code with ES6 as a target or are you requiring files from a vendor that are written using ES6?
In the first case you can try using the 1.0.0-beta version of
uglifyjs-plugin
:yarn add --dev uglifyjs-webpack-plugin@^1.0.0-beta
webpack.config.js
:In the other case try looking for an already transpiled version of the files you are requiring. If you can’t find one the best way right now would probably be to manually modify the
exclude
option of thebabel-loader
as stated above.So, looking back at it a bit more, the version of Webpack shipped with Encore currently installs
uglifyjs-webpack-plugin
v0.4.6 which usesuglify-js
v2.8.29. This version ofuglify-js
doesn’t support ES6 and, since vendors don’t go through Babel it fails if you require something from them written using ES6. It works using the files indist
since they have already been processed by Babel using the ES2015 preset.I guess you could do what you were initially trying to and modify the
uglify-js
part in order to use a more recent version such asuglify-es
(you’ll probably need to remove the plugin added by Encore before adding yours though) but be aware than the resulting file may include things that are not supported by some browsers.If you really want to import the
src
files you could modify the js rule added by Encore in order to remove theexclude: /(node_modules|bower_components)/
:Since Babel will then use the
.babelrc
file from Bootstrap you’ll also need toyarn add --dev babel-preset-es2015 transform-es2015-modules-strip
.Edit: Be aware that this last method will slow down your builds and that if another module also uses a
.babelrc
file you’ll also need to add the presets/plugins it uses (or disable.babelrc
files). In my opinion you should use thedist
files whenever it’s possible to do so, and when it isn’t slightly modify theexclude
rule to add an exception instead of removing it entirely.