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

Module AMD/CommonJS/Browser Global not working #610

Closed
york-xtrem opened this issue Oct 27, 2016 · 2 comments
Closed

Module AMD/CommonJS/Browser Global not working #610

york-xtrem opened this issue Oct 27, 2016 · 2 comments

Comments

@york-xtrem
Copy link

I suspect it is for Babel but not sure.

How can we reproduce this bug?

  1. Git clone https://github.com/zurb/foundation-zurb-template projectname
  2. Install:
    npm install gsap --save-dev
    bower install scrollmagic --save-dev
  3. Add text line config.yml:
    • "node_modules/gsap/src/uncompressed/TweenMax.js"
    • "bower_components/scrollmagic/scrollmagic/uncompressed/ScrollMagic.js"
    • "bower_components/scrollmagic/scrollmagic/uncompressed/plugins/animation.gsap.js"
    • "bower_components/scrollmagic/scrollmagic/uncompressed/plugins/debug.addIndicators.js"
    • "bower_components/scrollmagic/scrollmagic/uncompressed/plugins/jquery.ScrollMagic.js"
  4. npm start

What happened instead?
In console Google Chrome:
Uncaught TypeError: Cannot set property 'ScrollMagic' of undefined

Google Chrome:
captura

Firefox:
captura2

@york-xtrem
Copy link
Author

I found the problem but not solve it.

When using Babel 6 and babel-preset-es2015 (or Babel 5), Babel by default assumes that files it processes are ES6 modules. The thing that is causing you trouble is that in an ES6 module, this is undefined, whereas in the "script" case, this varies depending on the environment, like window in a browser script or exports in CommonJS code.

The problem is that I have files in ES6 and others not.
Would have to separate files here? How?

// Combine JavaScript into one file
// In production, the file is minified
function javascript() {
  return gulp.src(PATHS.javascript)
    .pipe($.sourcemaps.init())
    .pipe($.babel())
    .pipe($.concat('app.js'))
    .pipe($.if(PRODUCTION, $.uglify()
      .on('error', e => { console.log(e); })
    ))
    .pipe($.if(!PRODUCTION, $.sourcemaps.write()))
    .pipe(gulp.dest(PATHS.dist + '/assets/js'));
}

Why wrong? Explanation...

Change the file javascript.

Change this to undefined

Besides this:

} else if (typeof exports === 'object') {
}(this, function () {
    "use strict";

    var ScrollMagic = function () {

to

} else if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') {
})(undefined, function () {
    "use strict";

    var ScrollMagic = function ScrollMagic() {

Code fragment:

Before doing so:

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(factory);
    } else if (typeof exports === 'object') {
        // CommonJS
        module.exports = factory();
    } else {
        // Browser global
        root.ScrollMagic = factory();
    }
}(this, function () {
    "use strict";

    var ScrollMagic = function () {

After doing so:

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(factory);
    } else if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') {
        // CommonJS
        module.exports = factory();
    } else {
        // Browser global
        root.ScrollMagic = factory();
    }
})(undefined, function () {
    "use strict";

    var ScrollMagic = function ScrollMagic() {

@york-xtrem
Copy link
Author

Sorry for the inconvenience, it was my problem.

My solution if you want to compile SS5 SS6 and preserving sourcemaps, using Gulp-filter:

foundation/foundation-zurb-template#43

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

1 participant