Skip to content

ES6 minify / uglify #2545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
damianobarbati opened this issue May 24, 2016 · 44 comments
Closed

ES6 minify / uglify #2545

damianobarbati opened this issue May 24, 2016 · 44 comments

Comments

@damianobarbati
Copy link

I'm pretty sure this is a duplicate, but I couldn't find another already open issue about it.

I'd like to not transpile my es6 code with babel to es5 before uglifying it: I'd rather use a minified es6 version of my codebase.
Is there any uglifier production-ready/webpack-ready for this?

Watching in the meantime:
https://gist.github.com/avdg/43002267f959210a69aa3ec0f21ed751

@bebraw
Copy link
Contributor

bebraw commented Aug 14, 2016

The problem is that UglifyJS doesn't support ES6 yet so it's not possible to avoid that transformation yet. You can follow the progress at mishoo/UglifyJS#448 .

@bebraw bebraw closed this as completed Aug 14, 2016
@nakamorichi
Copy link

Even though ES6 support for UglifyJS is still under development, I would like to know how to force Webpack to use a custom version of UglifyJS, such as the Harmony branch. Is there an easy way to do this with npm?

Btw, this seems to be related to #1471.

@bebraw
Copy link
Contributor

bebraw commented Aug 16, 2016

I can see two options:

  1. Fork webpack and point uglifyjs dependency to the branch.
  2. Extract the UglifyJs plugin from the project and point that to the branch.

I'm not actually sure why UglifyJS plugin is at the core level as it could easily be a package of its own. Extra dependency to manage then, though.

@f0rr0
Copy link

f0rr0 commented Sep 3, 2016

+1 for removing UglifyJS from the core level.

@f0rr0
Copy link

f0rr0 commented Sep 3, 2016

@bebraw Or you could use npm shrinkwrap --dev and modify the version to the harmony branch under the webpack dependency in the npm-shrinkwrap.json created. If you still want to keep webpack as dev dependency add npm-shrinkwrap.json to your .gitignore.

@bebraw
Copy link
Contributor

bebraw commented Sep 3, 2016

FYI, there's a Babel specific minifier these days. See https://github.com/boopathi/babili-webpack-plugin .

@f0rr0
Copy link

f0rr0 commented Sep 3, 2016

@bebraw I tried it in my project earlier, it produces unexpected results without any warnings. Same with uglifyjs2 but that happens less often than babili. Someone needs to write a bulletproof es6 minifier

@bebraw
Copy link
Contributor

bebraw commented Sep 3, 2016

Did you see https://prosecco.surge.sh/ ?

There is not much we can do about this issue in webpack itself.

@f0rr0
Copy link

f0rr0 commented Sep 3, 2016

@bebraw This seems very experimental. The gitlab link returns a 404. Totally agree this isn't a webpack concern. Hence the need for removal of uglifyjs from core dependencies

@fulls1z3
Copy link

fulls1z3 commented Nov 26, 2016

I completely agree with the first solution proposed by @bebraw. I try to help by providing detailed instructions:

  • Fork webpack,
  • On that fork, delete all branches except master,
  • Clone master branch to a local folder,
  • On the local folder, delete all files,
  • Commit the empty local folder,
  • Download the latest release (ex: v2.1.0-beta.27 at the moment)
  • Extract contents of zip file to the local folder,
  • On the local folder, edit forked package.json, change
"uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"

to point UglifyJs2 (harmony branch) dependency to that branch.

  • Commit changes,
  • Finally, point webpack in your package.json to your custom fork:
"webpack": "[USERNAME]/webpack#master"

OR,

  • Point webpack in your package.json to fulls1z3/webpack (ES6/ES2015 friendly webpack fork):
"webpack": "fulls1z3/webpack#v2.1.0-beta.27-harmony"

@mikeerickson
Copy link

@bebraw Is this still the case? Is there a way to get this for webpack 2.2.0 final

@bebraw
Copy link
Contributor

bebraw commented Jan 20, 2017

@mikeerickson I think the instructions above should be valid. The problem is that uglify doesn't support es6 yet and webpack bundles the plugin. So it takes quite a bit of work.

@mikeerickson
Copy link

@bebraw Which of the two items should be used

  • First solution, which [USERNAME] should be used
  • Second solution, which version should be used with fulls1z3/webpack...

@bebraw
Copy link
Contributor

bebraw commented Jan 20, 2017

@mikeerickson The first sequence is all the steps (forking etc.). The second is just pointing to https://github.com/fulls1z3/webpack, but that seems to be a little out of date already.

@mikeerickson
Copy link

Ok, I will make it work. Just curious why there is no "official" version of UglifyJs which works with ES6 (harmony) within Webpack. We are well passed the ES5 days :)

@nakamorichi
Copy link

@mikeerickson I guess that's a question we have to ask from UglifyJS team. Although adding "uglify-js": "git://github.com/mishoo/UglifyJS2#harmony" to my package.json is not a problem, having Git repos as dependencies feels a bit hacky.

@bebraw
Copy link
Contributor

bebraw commented Jan 21, 2017

What if we did an experimental uglifyjs-webpack-plugin below the webpack-contrib org? I can set up rights etc. if someone is interested. We would basically push the Uglify bits there. It's easier to fork/patch that than webpack.

@nakamorichi
Copy link

nakamorichi commented Jan 21, 2017

@bebraw You mean extracting lib/optimize/UglifyJsPlugin.js into separate plugin project and removing UglifyJS dependency from Webpack? Or, leaving Webpack as it is and providing users the ability to have UglifyJS plugin that instead depends on the harmony branch?

@bebraw
Copy link
Contributor

bebraw commented Jan 21, 2017

@bebraw You mean extracting lib/optimize/UglifyJsPlugin.js into separate plugin project and removing UglifyJS dependency from Webpack? Or, leaving Webpack as it is and providing users the ability to have UglifyJS plugin that instead depends on the harmony branch?

lib/optimize/UglifyJsPlugin.js would literally become a project of its own, yeah. This may be a step we might want to take eventually anyway so it could be a nice idea to experiment with it a bit.

I don't mind setting up a little stub project if you are interested.

@fulls1z3
Copy link

Hi @bebraw @mikeerickson, the fork I've been built seems a lot of of date. Now the latest version of webpack is 2.2.0, and don't worry I'll update that fork in a few days :)

@nakamorichi
Copy link

@bebraw That doesn't sound too difficult. Yea, I think I could do it.

@nakamorichi
Copy link

nakamorichi commented Jan 21, 2017

@fulls1z3 I don't quite understand what you are/were trying to do with that fork. There is no need to fork Webpack in order to use the harmony branch; just add "uglify-js": "git://github.com/mishoo/UglifyJS2#harmony" to your project's package.json, rm -rf node_modules && npm install, and voilà.

@fulls1z3
Copy link

fulls1z3 commented Jan 21, 2017

@Kitanotori The aim of the fork is just to prevent dependency interference/mismatch (depending on the project structure, dev environment and npm). In other cases I agree with you, and voilà.

@bebraw
Copy link
Contributor

bebraw commented Jan 21, 2017

@Kitanotori @fulls1z3 I set up a repository. There's a basic demo that works. I didn't port the tests over, though (it's a Jest based setup).

@fulls1z3
Copy link

@bebraw seems awesome!
I'll test it and share results with you.

@damianobarbati
Copy link
Author

@Kitanotori you're right: anyway I expected the ES6-only bundle to me much smaller in size :\

@phips28
Copy link

phips28 commented May 9, 2017

I am using webpack 2.5.1 and "uglify-js": "git://github.com/mishoo/UglifyJS2#harmony",
It worked till 8th may 2017.

Now it fails with the error:

ERROR in express.js from UglifyJs
TypeError: Cannot read property 'reset' of undefined
    at filterdFiles.forEach.err (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/optimize/UglifyJsPlugin.js:81:20)
    at Array.forEach (native)
    at Compilation.compilation.plugin (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/optimize/UglifyJsPlugin.js:40:18)
    at next (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/tapable/lib/Tapable.js:140:14)
    at Compilation.compilation.plugin (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/BannerPlugin.js:67:5)
    at Compilation.applyPluginsAsyncSeries (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/tapable/lib/Tapable.js:142:13)
    at self.applyPluginsAsync.err (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/Compilation.js:635:10)
    at Compilation.applyPluginsAsyncSeries (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/tapable/lib/Tapable.js:131:46)
    at sealPart2 (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/Compilation.js:631:9)
    at Compilation.applyPluginsAsyncSeries (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/tapable/lib/Tapable.js:131:46)
    at Compilation.seal (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/Compilation.js:579:8)
    at /Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/Compiler.js:493:16
    at /Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/tapable/lib/Tapable.js:225:11
    at _addModuleChain (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/Compilation.js:481:11)
    at processModuleDependencies.err (/Library/WebServer/Documents/dvel/waves-provider/api-provider/node_modules/webpack/lib/Compilation.js:452:13)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Does anyone has similar error?

@aedart
Copy link

aedart commented May 9, 2017

@phips28 I'm facing the exact same issue - and I have no idea what's causing it :/

@ksjogo
Copy link

ksjogo commented May 10, 2017

Same here.

@nakamorichi
Copy link

UglifyJS seems to have bumped to a new major version. Try

"uglify-js": "git://github.com/mishoo/UglifyJS2#harmony-v2.8.22"

@phips28
Copy link

phips28 commented May 10, 2017

@Kitanotori thx for the hint! it works

@ksjogo
Copy link

ksjogo commented May 10, 2017

It works until I run yarn upgrade which will overwrite it with the 2.8.23 without harmony. Anybody knows a way how to pin the transitive dependencies?

@nakamorichi
Copy link

@ksjogo That's weird. Doesn't happen for me.. Try deleting yarn.lock and node_modules and run yarn again.

@ixcat
Copy link

ixcat commented May 24, 2017

Just ran into this and while investigating discovered there is a fork of the harmony branch in NPM which seems to solve my issues ..

$ npm info uglify-js-es6|grep github
url: 'git+https://github.com/paulovieira/UglifyJS2.git' },
bugs: { url: 'https://github.com/mishoo/UglifyJS2/issues' },

patched up my local uglifyjs-webpack-plugin as follows:

$ diff -urw node_modules/uglifyjs-webpack-plugin/dist/in>
--- node_modules/uglifyjs-webpack-plugin/dist/index.js.orig Wed May 24 04:31:46 2017
+++ node_modules/uglifyjs-webpack-plugin/dist/index.js Wed May 24 04:33:14 2017
@@ -16,7 +16,16 @@
var ConcatSource = require("webpack-sources").ConcatSource;
var RequestShortener = require("webpack/lib/RequestShortener");
var ModuleFilenameHelpers = require("webpack/lib/ModuleFilenameHelpers");
-var uglify = require("uglify-js");
+var uglify;
+try {

  • uglify = require("uglify-js-es6");
    +}
    +catch (e) {
  • if (e.code !== 'MODULE_NOT_FOUND') {
  • throw e;
  • }
  • uglify = require("uglify-js");
    +}

var UglifyJsPlugin = function () {
function UglifyJsPlugin(options) {

Ideally wouldn't be relying on a fork either, but this IMHO is better than a direct github reference..

@robwierzbowski
Copy link

robwierzbowski commented Sep 28, 2017

https://github.com/webpack-contrib/babel-minify-webpack-plugin mentioned in the commit above from @trazyn seems like it has a lot of potential. Tried both beta uglify plugin and the babel plugin on my bundle; I was at 118kb for uglify-es, and 119kb for babel minify.

@trazyn
Copy link

trazyn commented Sep 28, 2017

I have the same trouble with ES6, babel-minify-webpack-plugin is work well with ES6 in my project.

@ReeganExE
Copy link

Installing "uglifyjs-webpack-plugin": "^1.0.0-beta.2" works for me (with "webpack": "^3.6.0").

const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
// ...
plugins: [
  // expose and write the allowed env vars on the compiled bundle
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify(env.NODE_ENV)
  }),
  new webpack.LoaderOptionsPlugin({
    minimize: true,
    debug: false
  }),
  new UglifyJSPlugin({
    uglifyOptions: {
      beautify: false,
      ecma: 6,
      compress: true,
      comments: false
    }
  })
]

@demonmind
Copy link

i used babel-minify-webpack-plugin and worked well finally

@Apidcloud
Copy link

In my case, the problem was related to mangling. Setting mangle to false solved it (both uglifyjs-webpack-plugin and babel-minify-webpack-plugin have that option).

@xgqfrms
Copy link

xgqfrms commented Jun 27, 2018

bug

after minify, it doesn't work. what's wrong with this?

https://github.com/gildata/RAIO/issues/350

online demo

tabs click event binding bug

https://gildata.github.io/ARTC/bugs/index.html

https://gildata.github.io/ARTC/bugs/ok/index.html

Calcaware added a commit to Calcaware/weweChat that referenced this issue Jan 19, 2022
donal-james-collins added a commit to donal-james-collins/weweChat that referenced this issue Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests