thlorenz / browserify-shim

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

Ability to set an external dependency with browserify shim #135

Closed samccone closed 9 years ago

samccone commented 9 years ago

I was working on integration browserify-shim the other night and I have a fairly "unique" case here.

I have several external decencies that I want to be looked up on the window. I ended up using this solution https://github.com/marionettejs/marionette.module/commit/720df76cd2c334661ef8c5f0d74f41ca5507e461 (make me cry^)

At first i tried to use browserify shim to accomplish my goals however each time it kept trying to lookup backbone, underscore via a require ... which obviously failed since they are marked as external dependencies.

Any insight that you have would be greatly appreciated. Thanks so much for the time and this great tool.

bendrucker commented 9 years ago

Can you explain what you'd actually like to do here?

samccone commented 9 years ago

sure thing @bendrucker

this lib has 3 external dependencies backbone, backbone.marionette, and underscore

I want to be able to distribute this library without bundling those three things (in the build file).

bendrucker commented 9 years ago

So basically you want browserify-shim to do those window.* or require(*) statements for you? And then call b.exclude?

samccone commented 9 years ago

yeah I think so. in the module export from browserify I think the shim would go inside the require: method def to check if I am looking up something that is excluded, and if so simply look to the window.

If this is something out of scope for this lib, I am more than willing to roll my own, It just seemed like maybe I was using this tool wrong.

bendrucker commented 9 years ago

So then you want this?

function getDep (x) {
  try {
    require(x);
  }
  catch (e) {
    return window[x];
  }
}

Happy to help as you work on rolling your own as this is definitely out of the scope of b-shim. It's worth thinking about whether this is actually what you want to do though. I distribute stuff for people using window globals, but my policy is always require it OR use the UMD build with b-shim.

samccone commented 9 years ago

yep @bendrucker that is just it, I am using the UMD build https://github.com/marionettejs/marionette.module/commit/720df76cd2c334661ef8c5f0d74f41ca5507e461#diff-0b83f9dedf40d7356e5ca147a077acb4R4

and I wanted to use b-shim on top but I ran into this nasty issue :|

I'll ping this issue with the shim that I end up making

bendrucker commented 9 years ago

Dealing with user supplied depencies is particularly hard. I think that definitely merits its own transform.

samccone commented 9 years ago

hey @bendrucker ending up doing it tonight https://github.com/samccone/browserify-global-lookup-shim

in use at https://github.com/marionettejs/marionette.module

Thanks for the insight, let me know if you have any thoughts on my approach... some of it is a bit naive :package:

bendrucker commented 9 years ago

You could totally do this as a plugin which would be easier to implement. The plugin would handle calling external and then a transform could do the require transformations.

bendrucker commented 9 years ago

And by implement I mean for the consumer. Obviously more difficult to build than this simple shim but a whole lot more robust.