Skip to content

Web-animations.min.js throw exception when using it with Webpack #45

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
ujeenator opened this issue Oct 8, 2015 · 10 comments
Closed

Web-animations.min.js throw exception when using it with Webpack #45

ujeenator opened this issue Oct 8, 2015 · 10 comments

Comments

@ujeenator
Copy link

Error message:
Cannot set property 'true' of undefined

Error occurred on the very first line:
function(a,b){b["true"]=a

Reason - window object is captured with incorrect code:
function(){return this}

Correct way to get window object (this code taken from jQuery source):
typeof window !== "undefined" ? window : this

@dstoc
Copy link
Contributor

dstoc commented Oct 9, 2015

What is Webpack? The capturing of window seems to be a result of uglify.

@shans
Copy link
Contributor

shans commented Oct 9, 2015

(i.e. this is not a bug in web-animations - would you mind following up with the authors of uglify?)

@shans shans closed this as completed Oct 9, 2015
@ujeenator
Copy link
Author

Okay, I have examined your Gruntfile.js

In function compress(...) at line #59 you enable option of UglifyJS:
record.options.wrap = true;

What actually this option do?...

It considers that your code is CommonJS module
and wrap it with anonymous function
for using it in browser:

(function(exports,global){
    global[""]=exports;
    /* 
        Your module code

        exports.blablabla = function() { ... };
    */
)({},function(){return this}());

Since your code is web-browser polyfill (not a CommonJS module)
and it doesn't export anything with "exports" variable
then you have no need of using "wrap = true" option.

Also this will fix compatibility problem with Webpack and Browserify.

@xseignard
Copy link

Hello,
It seems this problem is still happening, isn't it?

Any hints on fixing this?

Best regards,

Xavier

@ujeenator
Copy link
Author

@xseignard

Try replace this (in the end of web-animations.min.js file):

function(){return this}()

With this:

typeof window !== "undefined" ? window : this

@xseignard
Copy link

Hi @AmberDeBlack
Thanks for the tip! But I will not maintain my own version of the polyfill, the best would be for this polyfill to handle that case since it's a pretty standard case, isn't it?

Best regards,

Xavier

@ujeenator
Copy link
Author

@xseignard

Sure :)

But I'm not maintainer of this repo

And maintainer declined my workaround for this problem (read messages above)

@ghost
Copy link

ghost commented Jun 3, 2017

I had a similar issue, but in my instance it was caused by the module option of preset-env being set on babel-loader. When I set this option to false it worked.

@Brian-McBride
Copy link

Brian-McBride commented Aug 31, 2017

I was able to fix this by doing two things:

  1. Don't process it by babel-loader. The file is already es5 and minified. No reason to further run it through babel.
  2. Add a shim in webpack.config.js
    https://webpack.js.org/guides/shimming/#imports-loader
{
    test: /web-animations-next-lite.min.js/,
    use: 'imports-loader?this=>window',
},

You can tweak this, but basically it is setting the "this" reference to "window".

@alan-agius4
Copy link

alan-agius4 commented May 27, 2019

Hi, I really don't agree that this is a not a bug in web-animation-js.

As this issue is also present when not using any bundler nor minifier. If one loads this script using type="module", it will fail because the this context will be undefined when adding the type=module attribute on a script tag.

<script>
  alert(this);  // = window
</script>

<script type="module">
  alert(this); // = undefined
</script>

I hope, that you can reconsider the fact that this is an issue.

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants