tuupola/lazyload

jquery_lazyload 2.0.0-beta.2 and Webpack UglifyJsPlugin

Opened this issue · 9 comments

Hello!

I'm using LazyLoad as a jQuery extension:

import $ from 'jquery';
import lazyload from "lazyload";
$("img.lazyload").lazyload();

Everything works perfectly, but when I try to bundle all the JS with webpack and its own Uglify plugin I get the error

Unexpected token: name (extended)

and the error is thrown at the last of the lines of code here below:

/**
    * Merge two or more objects. Returns a new object.
    * @private
    * @param {Boolean}  deep     If true, do a deep (or recursive) merge [optional]
    * @param {Object}   objects  The objects to merge together
    * @returns {Object}          Merged values of defaults and options
    */
    const extend = function ()  {

        let extended = {};

Seems an issue with ES6 not being uglified, could be?

Yep I can confirm is due to the fact that "lazyload.js" and "lazyload.min.js" are in ES6 and not transpilled, and since in my webpack configuration I excluded all node_modules from babel transpill (generally node_modules files come already transpiled and so devs exclude them from webpack plugins pre-processing) the ES6 file generate error with UglifyJS

How can one bypass this error in uglifyJS?

Wonders of JavaScript. However uglify-es should be able to minify ES6.

I've tried it, it does. But i causes many many problems with es5, and any other js libraries in my project. Append does the trick. Also there are problems on firefox with this lazy load.

Not sure what you mean with append?

I've worked it out like this: https://www.npmjs.com/package/gulp-append

Using uglify-es with other scripts causes them to break. I minify all to one .js file. If someone doesn't mind putting more requests to their site uglify-es works fine with your lazy load.

s0By commented

Installed uglify-es, still:

ERROR in vendor.min.js from UglifyJs
Unexpected token: name (extended)

@s0By ,me too. How to deal with it

This fixed it for me: (using uglify-es with gulp-uglify)
https://stackoverflow.com/a/47339387