thlorenz / browserify-shim

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

bundling a dep that you want to use globals with #216

Closed marr closed 8 years ago

marr commented 8 years ago

i put together a test case around browserify-shim and usign globals. My dependency react-module in this case isnt able to resolve the deps like react and moment that are defined in package.json https://github.com/kentcdodds/ama/issues/183

bendrucker commented 8 years ago

Hi, I'm not sure what you're asking here. I looked at the linked issue and still don't understand. Can you explicitly spell out what you're trying to do and what's going wrong?

marr commented 8 years ago

Hi Ben, so I want to bundle a js file that will use the React global for its dependencies' react dependencies. browserify-shim appears to properly shim when its replacing the top-level's React dependency ,but if a dependency it relies on requires react I want to make sure that it uses the global instead of bundling react in.

marr commented 8 years ago

My dependency here: https://github.com/marr/js-native-modules/blob/master/src/index.js imports react. I want to make sure that is shimmed to use global.React.

This is the parent project that imports the dependency, runs browserify and configures browserify-shim: https://github.com/marr/app/blob/master/src/index.js

Maybe i need to add some more configuration in browserify-shim's config to declare that my dep (react-module) needs its react dependencies shimmed? https://github.com/marr/app/blob/master/package.json#L24

bendrucker commented 8 years ago

Ah, I get it. You're doing it backwards. Browserify wants you to declare transforms on your dep and will execute them when run from your app. You can't implicitly add transforms in your apps that change dependencies. Browserify transforms don't run on code in node_modules unless you run them as explicitly global:

 --global-transform=MODULE, -g MODULE

    Use a transform module on all files after any ordinary transforms have run.

Your ignore on babelify is effectively a noop because Browserify's default behavior is to ignore node_modules and not even let the transform see those files.

marr commented 8 years ago

Cheers Ben! That was what I was after:

browserify -x react -x react-dom -x moment src/index.js -o bundle.js -g browserify-shim

How does the --global-transform module get called with the api? I don't see a method listed in the docs. From the looks of the source, it adds { global: true } to the transform options. Is that possible to do via package.json?