thlorenz / browserify-shim

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

Exposing global variable don't prevent from importing the library #232

Closed miniven closed 6 years ago

miniven commented 6 years ago

I use Gulp + Browserify.

In my package.json:

"browserify-shim": {
  "jquery": "global:$"
}

and gulp task is:

const scriptsServe = () => {
  const alias = [
    'all-page.js',
    'index-page.js'
  ];

  PATH.src.scripts.forEach((entry, index) => {
    const bundle = browserify({
      entries: entry,
      global: true
    });

    bundle.transform(babelify, {
      global: true
    }, "browserify-shim");
    bundle.bundle()
      .pipe(source(alias[index]))
      .pipe(gulp.dest(PATH.dest.scripts));
  });
};

And I need to import jquery in the file, where it needed as a dependency of another library:

import $ from 'jquery';
import 'selectize';

But in the final bundle Selectize still importing jQuery by its own, even if I do not import jQuery manually.

miniven commented 6 years ago

Ok, sorry. I've been struggling with my carelessness all morning. Found where the problem lies:

I should have been written this:

    bundle.transform(babelify, {
      global: true
    });
    bundle.transform("browserify-shim", {
      global: true
    });

Instead of this:

    bundle.transform(babelify, {
      global: true
    }, "browserify-shim");
bendrucker commented 6 years ago

Yup the 3 args is the issue there