thlorenz / browserify-shim

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

Browserify-shim globally for all dependencies #191

Closed laxity7 closed 8 years ago

laxity7 commented 8 years ago

In my project I use gulp + browserify + browserify-shim. In the project I connect other libraries which are in the node_modules folder. There is such problem that I and one of libraries use the same dependence - bluebird. But I connect bluebird globally, and I don't want to pack. In package.json I have such a rule

"browserify": {
    "transform": ["browserify-shim"]
},
"browserify-shim": {
    "bluebird": "global:Promise",
}

But this rule doesn't extend on others to library and therefore bluebird is all the same packed.

Itself task looks standardly

gulp.task ('testjs', function () {
    var b = browserify ({
        entries: 'app.js'
    });

    return b
        .transform (babelify, { "global": "true", "compact": "false" })
        .bundle ()
        .pipe (source ('app.js'))
        .pipe (gulp.dest ('./dist'));
});

How to force to distribute this rule globally to any connected libraries and their dependences?

In an ideal it would be desirable to see settings as at babelify, you put "global":"true" and everything works, but didn't find such for Browserify-shim

bendrucker commented 8 years ago

You can do the same and specify the transform with the options.global flag. Just exclude it from your package.json.

laxity7 commented 8 years ago

you could show on the example of my code as to make it?

bendrucker commented 8 years ago

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

laxity7 commented 8 years ago

It's clear=) how to configure browserify-shim, if to delete from package.json?

bendrucker commented 8 years ago

Yes, you cannot define global transforms in your package. They must be done programmatically w/ the browserify API.

xueruini commented 8 years ago

two questions:

  1. Will the browserify API merge options in program and package.json?
  2. If there were other transforms in package.json, such babelify, should all of them be present w/ the api style in program?
bendrucker commented 8 years ago
  1. No
  2. If you want to apply a transform globally, it cannot be included in package.json
xueruini commented 8 years ago
  1. No

If option is not provided in the program, will the API read options in package.json?

  1. If you want to apply a transform globally, it cannot be included in package.json

For the given package.json:

"browserify": {
  "transform": ["x", "y", "z"]
}

I found no matter how many transform() were added in the code, the generated bundle is the same:

browserify()
  // enabling any number of the three `transform()` lead to the same result.
  // .transform('x')
  // .transform('y')
  // .transform('z')
  .bundle(myOutputStream)

Does it mean browserify() will load transforms in package.json not presented in the code?

bendrucker commented 8 years ago

Options support will come with #195. Browserify will run all transforms every time they're specified, whether via the package or via the API. Some transforms may be able to run multiple times without causing issues, others may not.

kottenator commented 6 years ago

@bendrucker:

Yes, you cannot define global transforms in your package. They must be done programmatically w/ the browserify API.

But why not?