tuupola / lazyload

Vanilla JavaScript plugin for lazyloading images
https://appelsiini.net/projects/lazyload/
MIT License
8.76k stars 2.23k forks source link

jquery_lazyload 2.0.0-beta.2 and Webpack UglifyJsPlugin #400

Open Stefano-Sega opened 7 years ago

Stefano-Sega commented 7 years ago

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?

Stefano-Sega commented 7 years ago

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

majkimajki commented 7 years ago

How can one bypass this error in uglifyJS?

tuupola commented 7 years ago

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

majkimajki commented 7 years ago

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.

tuupola commented 7 years ago

Not sure what you mean with append?

majkimajki commented 7 years ago

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 7 years ago

Installed uglify-es, still:

ERROR in vendor.min.js from UglifyJs
Unexpected token: name (extended)
julytian commented 6 years ago

@s0By ,me too. How to deal with it

tennox commented 6 years ago

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