thlorenz / browserify-shim

📩 Makes CommonJS incompatible files browserifyable.
MIT License
933 stars 87 forks source link

Script is shimmed twice #110

Closed Fauntleroy closed 9 years ago

Fauntleroy commented 9 years ago

Probably related to https://github.com/thlorenz/browserify-shim/issues/88 and https://github.com/thlorenz/browserify-shim/issues/104.

I can successfully Browserify my bundle, but the resulting script always has this error:

Uncaught TypeError: Cannot set property 'exports' of undefined

This error occurs here. Note that there are what appear to be two calls to browserify-shim:

; browserify_shim__define__module__export__(typeof jwplayer != "undefined" ? jwplayer : window.jwplayer);

}).call(global, undefined, undefined, undefined, undefined, function defineExport(ex) { module.exports = ex; });

; browserify_shim__define__module__export__(typeof jwplayer != "undefined" ? jwplayer : window.jwplayer);

}).call(global, undefined, undefined, undefined, undefined, function defineExport(ex) { module.exports = ex; });

I've gone over my configuration a few times, but after a couple hours of fiddling I haven't been able to figure out the right combination to fix this. I'm set up like this:

package.json:

...
"browser": {
  "jwplayer": "./assets/scripts/vendor/jwplayer/jwplayer.js"
},
"browserify-shim": {
  "jwplayer": "jwplayer"
}

gulpfile.js:

...
var generateBrowserifyBundler = function(){
    var args = _.extend( {
        transform: ['hbsfy', 'browserify-shim']
    }, watchify.args );
    var bundler = browserify( './assets/scripts/room.js', args );
    return bundler;
};

var generateBrowserifyStream = function( bundler ){
    var stream = bundler.bundle()
        .on( 'error', gulp_util.log.bind( gulp_util, 'Browserify Error' ) )
        .pipe( vinyl_source('room.js') )
        .pipe( gulp.dest('./assets/compiled') );
    return stream;
};

gulp.task( 'compile js', function(){
    var bundler = generateBrowserifyBundler();
    return generateBrowserifyStream( bundler );
});
...

I'm using Node.js 0.10.29, Browserify 6.3.3, and Browserify-shim 3.8.0

bendrucker commented 9 years ago

A repo that we can clone that reproduces this would be a big help.

Fauntleroy commented 9 years ago

Here's everything you need to reproduce. Tell me if you have any trouble:

https://github.com/Fauntleroy/browserify-shim-test-case

thlorenz commented 9 years ago

Please prep the repo so I don't need to install any global deps, i.e. gulp should be included as a devDep it already is.

All I wanna do to repro is:

npm install && npm test

Thanks.

Fauntleroy commented 9 years ago

@thlorenz I've updated the repo and it should now work as desired.

Fauntleroy commented 9 years ago

Looks like this is a problem with specifying transforms in the browserify configuration object vs the .transform method.

This will result in transforms running twice:

var bundler = browserify( './scripts/index.js', {
    transform: ['browserify-shim']
});

This will work properly:

var bundler = browserify('./scripts/index.js');
bundler.transform('browserify-shim');