Skip to content

Instantly share code, notes, and snippets.

@dconnolly
Created August 14, 2014 16:04
Show Gist options
  • Save dconnolly/0c0c8778becbf93c2887 to your computer and use it in GitHub Desktop.
Save dconnolly/0c0c8778becbf93c2887 to your computer and use it in GitHub Desktop.
Define a globally external module in a browserify'd bundle.

Define a globally external module in a browserify'd bundle with grunt-browserify

Ideally you would have your app architected and dependencies managed so that you don't need to do this. But say you do.

You have a dependency that can be required from an external bundle or something in your package, and you want to make sure that all levels of your browserified build use that external dependency, not just at the top level of your app at your entry point. Well browserify and transforms don't quite support this, maybe a plugin could do this, but here's a simple way of achieving this with grunt-browserify. Tested with the versions spec'd here, but probably would also work with the latest versions.

module.exports = (grunt) ->
grunt.initConfig(
browserify:
options:
external: 'my-module' # Should be available from studio-module.js on page.
postBundleCB: (err, src, next) ->
src = src.replace /\{\"my\-module\"\:[A-Z0-9]+\}/ig, '{"my-module":undefined}'
next err, src
)
require('load-grunt-tasks')(grunt)
{
"name": "global-external-browserify-package-example",
"devDependencies": {
"browserify": "^3.46.1",
"grunt": "^0.4.5",
"grunt-browserify": "~1.3.2",
"load-grunt-tasks": "^0.6.0",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment