Skip to content
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

root in strict mode refer to undefined #111

Open
SuperOl3g opened this issue Apr 6, 2016 · 19 comments
Open

root in strict mode refer to undefined #111

SuperOl3g opened this issue Apr 6, 2016 · 19 comments

Comments

@SuperOl3g
Copy link

!function(root, factory) {
    "function" == typeof define && define.amd ? // AMD. Register as an anonymous module unless amdModuleId is set
    define([], function() {
        return root.svg4everybody = factory();
    }) : "object" == typeof exports ? module.exports = factory() : root.svg4everybody = factory();
}(this, function() { ... 

My bundler (webpack + babel) add 'use sctrict' to every module automatically so that this in last line is undefined

@rich-martinez
Copy link

I am having the same problem. How can we set this up to be able to pass something else?

@SuperOl3g
Copy link
Author

If you use webpack this can help you
https://webpack.github.io/docs/shimming-modules.html

require("imports?this=>window!./file.js") or require("imports?this=>global!./file.js")

@ButuzGOL
Copy link

+1

@shawnbot
Copy link
Collaborator

Yeah, this is no good. I think it'd be better to convert lib/svg4everybody.js into a CJS module so that bundlers can do what they do best, rather than bundling the generated UMD. This would mean:

  1. Turn lib/svg4everybody.js into a CJS module, a la module.exports = svg4everybody;
  2. Update package.json with "main": "lib/svg4everybody.js" (instead of dist/)
  3. Update the build task as needed to generate the right UMD

How's this sound?

@ButuzGOL
Copy link

Temporary solution
https://github.com/webpack/imports-loader

module : {
    loaders : [
      {
        test: require.resolve("svg4everybody"),
        loader: "imports?this=>window"
      }
    ]
  }

@ButuzGOL
Copy link

ButuzGOL commented Jul 13, 2016

@shawnbot I think its good to support 3 types
But I think everybody moving to CJS

@cwackerman
Copy link

I was experiencing a similar issue with webpack + babel, where import would throw:

Uncaught TypeError: Cannot set property 'svg4everybody' of undefined

Turned out that I had forgotten to exclude the node_modules directory from the babel-loader:

loaders: [{
    test: /\.jsx?$/,
    exclude: /(node_modules)/,
    loader: 'babel'
}]

Maybe you need to do the same?

@damienmcd
Copy link

damienmcd commented Feb 15, 2017

I am seeing the same behaviour with gulp-babel. I'm not using Webpack or any other bundler.
gulp-babel is adding "use strict" to the main.min.js but getting a console error of "Cannot set property 'svg4everybody' of undefined".
Is there any update on this?

@damienmcd
Copy link

damienmcd commented Feb 15, 2017

I've got this issue fixed for now. The solution involves using a different Babel preset that does not change this to undefined. The preset I used is babel-preset-es2015 and then add the following to a .babelrc file:

{
  "presets": [ [ "es2015", { modules: false } ] ]
}

I'll continue testing on projects that are browser facing and don't require modules.

@srudolph-credera
Copy link

srudolph-credera commented Apr 24, 2017

+1

@damienmcd just tried your approach (thanks!) and it worked for some browsers (Chrome for instance), but not older ones like IE10/11, where I'm still getting this passed in as undefined. Anyone else seeing the same?

FWIW: I ended up taking the approach of including the lib JS file instead of the dist version.

@dezman
Copy link

dezman commented May 9, 2017

Can we all just say window when we mean window.

@ghost
Copy link

ghost commented Jul 25, 2017

Same issue here:

Cannot set property 'svg4everybody' of undefined

Installed svg4everybody via yarn add svg4everybody.

Webpack-config (using webpack-stream):

module: {
    rules: [
        {
            test: /.js$/,
            use: [
                {
                    loader: "babel-loader"
                }
            ]
        }
    ]
}

main.js

import svg4everybody from 'svg4everybody';

@ghost
Copy link

ghost commented Jul 25, 2017

After a little bit more research I fixed this by adding node_modules to the exclude path (as said before by @cwackerman). Although I needed some packages from the node_modules-folder to be transpiled. Fixed it by excluding them from the exclude:

module: {
    rules: [
        {
            test: /.js$/,
            exclude: /node_modules(?!\/foundation-sites|flickity)/,
            use: [
                {
                    loader: "babel-loader"
                }
            ]
        }
    ]
}

@christiansany
Copy link

Had the same issue.
I found the answer of @arjenkroeze interesting, but I actually need almost all my packages transpiled, so I excluded only svg4everybody from transpiling via babel. This worked for me :)

module: {
    loaders: [
        {
            test: /\.js?$/,
            exclude: /node_modules\/svg4everybody/,
            loader: 'babel-loader',
        },
    ],
},

@DanielRuf
Copy link

DanielRuf commented Aug 25, 2017

I have the same issue when using the script in a file with use strict.
Uncaught TypeError: Cannot set property 'svg4everybody' of undefined
module.exports = factory() : root.svg4everybody = factory();

@DanielRuf
Copy link

Possible duplicate of #162

@DanielRuf
Copy link

See #171

@eeve
Copy link

eeve commented Aug 14, 2019

{
    "presets": ["@babel/preset-env"],
    "ignore": ["./src/files/**/*.js"]
}

add ignore option

ref: https://babeljs.io/docs/en/options#ignore

@DanielRuf
Copy link

exclude is typically recommended @eeve ;-)

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