thlorenz / browserify-shim

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

Does not shim dependencies of modules #150

Closed Krustal closed 9 years ago

Krustal commented 9 years ago

I'm trying to use browserify-shim to help migrate a legacy application with lots of old JavaScript attached to the global. We have rewritten some of our scripts into modules that we are bringing in with Browserify but need a way for those modules to continue using the global supporting libraries so avoid bringing those libraries in twice.

We created a browserify-shim config that shims the library (i.e. moment.js)

"browserify-shim": {
  "moment": "global:moment",
  "usagejs": {
    "depends": [ "moment" ]
  }
}

This works great for libraries in the applications script folder. However, a supporting library usagejs also requires moment.js but continues to bundle the module and not use the global.

I created a repo with an example of the issue I'm seeing here: https://github.com/Krustal/browserify_sandbox

My understanding is that browserify-shim should be shimming modules that require the shimed modules. If this is an issue with either momenjs or usagejs let me know.

pmowrer commented 9 years ago

My understanding is that browserify-shim, or any transform for that matter, won't operate on node_modules unless you pass it a global: true flag (only allowed using transform api). E.g.:

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

However, applying the browserify-shim transform globally may or may not work as it requires any package.json that it comes across to have a browserify-shim field. You might be better served using https://github.com/thlorenz/exposify instead (a dependency of browserify-shim that's only concerned with your particular use case).

Krustal commented 9 years ago

Thank you, I wasn't sure if this was encompassed in the scope of browserify-shim. I'll look into exposify.

bendrucker commented 9 years ago

exposify may be helpful in general but it's not going to solve the root issue here. You're gonna need to go with the global transform as pmowrer outlined. Your package calls require('moment') and so moment is going to be bundled. No way around that other than to use a global transform that will then go and shim your usage package itself.