thlorenz / browserify-shim

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

Usage without package.json #223

Closed unional closed 7 years ago

unional commented 7 years ago

I want to run browserify directly without package.json.

It complains when I run:

var b = browserify({
  ...
  transform: ["browserify-shim"],
  "browserify-shim": {...}
})
b.bundle(...).pipe(outputStream);
{ Error: Unable to find a browserify-shim config section in the package.json ...

Can this use case be supported?

bendrucker commented 7 years ago

It cannot. browserify-shim must be able to look up your shim configuration from a package.json file. You cannot supply the configuration as a browserify option like you have in your example.

ghost commented 7 years ago

@bendrucker I think the real question here is: why?

thlorenz commented 7 years ago

@ericmorandcross this was a decision we made in the past because we believed (and still do) that it is best to include all that configuration in the package.json. Resolving packages to shim is done relative to the package and it gets very complex very quickly if we allow to pass these options from anywhere.

Nothing is keeping you from providing a PR to try to change that. If we like the solution we'll most likely accept it, if not you'd have your own browserify-shim version that is more aligned with what you need, so nothing to loose ;). Just want to clarify that we believe it is very hard to get this right and user friendly, so again we may not accept it even if you think it's a good solution.

sgarcia-dev commented 7 years ago

So how would we go about creating multiple-build configurations for browserify-shim then, @thlorenz ?

In my humble opinion, @unional 's question seems to be about one potential solution to the (I believe still existing) problem of generating multiple bundle configurations. I too been having problems with this, and I've read up on the Docs & Wiki, as well as the conversation thread on Issue #40. And while there were some crafty solutions, none of them really addressed this problem.

I really liked your proposed:

{ 
  "browserify-shim": {
    "@bundle-foo": {
      "angular": "global:angular"
   },
   "@bundle-foo": {
      "./vendor/jquery": "$"
   }
}

However, since it's not in the Docs and you didn't talk about that ever since, I asume this never went Live, correct? If not, would it be so terrible to at least provide the option to send shim options via the transform arguments? I understand you don't like having configuration on any place that isn't the package.json, but wouldn't it be better if users could decide on this? Or is it more important to adhere to best practices than finding solving a problem?

What I'm saying is that, if it's not too much to ask, it would be nice to either enable passing options via the transform method as @unional established, or custom shim configurations as you proposed initially, or at the very least doing something a mix of both, something like:

browserify({
  transform: ['browserify-shim'],
  'browserify-shim': { 
    'config': 'shim.js'
  }
});

I'd really like to contribute to this solution, maybe even submit a PR, however, I don't think my NodeJS knowledge on streams, and JavaScript is good enough for me to even attempt this. I understand that what I'm asking is for is for you to take some free time to add this functionality, but if the solution is simple enough, could you approve a solution to the multiple build configuration problem using any solution of your preference? Thanks @thlorenz