thlorenz / browserify-shim

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

multiple shims in the same project #167

Closed lukemcgregor closed 9 years ago

lukemcgregor commented 9 years ago

I would like to run a different shim for my serverside and clientside JS packages. Now that the shim is now specified in packages.json I cant work out any way to have 2 separate shims (I don't think its possible anymore). Can you add in some alternate functionality to do this?

bendrucker commented 9 years ago

No idea what this means. Can you please elaborate with a specific example?

lukemcgregor commented 9 years ago

In the browser I use CDNs for a bunch of external libraries. eg

"browserify-shim": {
    "react": "global:React",
    "moment": "global:moment"
}

this means I can get those externals from a CDN so I don't want them in my bundle.

I also run some javascript on the server to pre-render the page, my serverside bundle has some different requirements. I have React already loaded on the server side so I want to shim that away, but I want to bundle in some of my other external libraries (eg I don't want moment to be shimmed serverside)

"browserify-shim": {
   "react": "global:React"
}

As the shim config is in packages.json now I cant find a way of running these two sepearate shim configs in each scenario.

bendrucker commented 9 years ago

Still don't know what you mean. If you're rendering on the server you're not passing through Browserify and so nothing would be shimmed.

bendrucker commented 9 years ago

Happy to continue the discussion but it doesn't sound like this is a browserify-shim issue. If you want to have conditional shimming you can always use an external shim config: https://github.com/thlorenz/browserify-shim#c-config-inside-configshimjs-without-aliases

lukemcgregor commented 9 years ago

If I use an external shim config (which is fine) I still need to specify "browserify-shim": "./config/shim.js" in the packages.config which means that I cant have 2 shims doesnt it?

If I could pass the shim path into browserify-shim it would probably work ok but I cant see any way to do that.

eg something like browserify(opts).transform(shim('./shim-config.js')) via gulp.

If you're rendering on the server you're not passing through Browserify and so nothing would be shimmed.

I still need browserify on the server as its not executing in node, its running in V8 and pre-rendering the page, so I still need a bundle.

bendrucker commented 9 years ago

Use an environment variable or any other shared state. Then from your external shim config just detect that and conditionally add your production shims where appropriate.

lukemcgregor commented 9 years ago

That sounds like it would work, thanks :)