thlorenz / browserify-shim

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

browserify-shim fails to process require('') in ParsleyJS #202

Closed mfursov closed 8 years ago

mfursov commented 8 years ago

Here is a first line from ParsleyJS (v2.2.0-rc4)

  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) : typeof define === 'function' && define.amd ? define(['jquery'], factory) : global.parsley = factory(global.jQuery);

The "require('jquery')" is not processed by shim module, and as the result 'jquery' sources are always included into the final bundle by browserify.

bendrucker commented 8 years ago

Can you provide your shim config please?

mfursov commented 8 years ago

Here is snippet from my package.json:

"browserify-shim": {
    "jquery": "global:$"
  },
  "browserify": {
    "transform": [
      "browserify-shim"
    ]
  }
mfursov commented 8 years ago

I tried to add "deps" & "exports" section like it shown here but it also does not work: jquery is included anyway. Tried both 'parsley' & 'parsleyjs' module names.

http://w3javascript.com/question/make-parsley-js-work-with-require-js/

mfursov commented 8 years ago

Adding

.transform('browserify-shim', {global: true})

to my gulpfile.js solves the problem: no more jquery is embedded. Looks like I missed some documentation how to set this flag in package.json without use of gulp.

Complete gulp pipeline:


gulp.task('build', function () {
    return browserify('./src/site.js')
        .transform(partialify)
        .transform('browserify-shim', {global: true})
        .bundle()
        .pipe(source('site.js'))
        .pipe(gulp.dest('./package/js/'));
});
bendrucker commented 8 years ago

how to set this flag in package.json

You can't. Running transforms on your npm dependencies based on a package flag is not something browserify supports/will support. Think of it this way: package flags are things that are essential to run your package itself, e.g. using babelify to convert ES6 to ES5.

If you want to do transforms like uglifying or shimming to your dependencies, you can only use the API.