├── .editorconfig ├── .gitignore ├── .jshintrc ├── Gruntfile.js ├── README.md ├── browserify-global-shim.js ├── package.json └── test ├── .jshintrc ├── globalShimSpec.js └── testInput.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | 10 | # Change these settings to your own preference 11 | indent_style = tab 12 | indent_size = 4 13 | 14 | # We recommend you to keep these unchanged 15 | end_of_line = lf 16 | charset = utf-8 17 | trim_trailing_whitespace = true 18 | insert_final_newline = true 19 | 20 | [*.md] 21 | trim_trailing_whitespace = false 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "devel": true, 8 | "eqeqeq": true, 9 | "esnext": true, 10 | "forin": true, 11 | "immed": true, 12 | "indent": 4, 13 | "latedef": true, 14 | "laxcomma": true, 15 | "newcap": true, 16 | "noarg": true, 17 | "quotmark": "single", 18 | "regexp": true, 19 | "smarttabs": true, 20 | "strict": true, 21 | "trailing": true, 22 | "undef": true, 23 | "unused": true 24 | } 25 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function (grunt) { 4 | // Show elapsed time at the end 5 | require('time-grunt')(grunt); 6 | // Load all grunt tasks 7 | require('load-grunt-tasks')(grunt); 8 | 9 | var testDir = 'test/'; 10 | 11 | // Project configuration. 12 | grunt.initConfig({ 13 | jshint: { 14 | options: { 15 | jshintrc: true, 16 | force: true, 17 | reporter: require('jshint-stylish') 18 | }, 19 | project: { 20 | src: [ 21 | 'Gruntfile.js' 22 | ] 23 | }, 24 | lib: { 25 | src: [ 26 | '*.js' 27 | ] 28 | }, 29 | test: { 30 | src: [testDir + '**/*.js'] 31 | } 32 | }, 33 | mochaTest: { 34 | unit: { 35 | options: { 36 | reporter: 'spec', 37 | force: true 38 | }, 39 | src: [testDir + '{,*/}*Spec.js'] 40 | }, 41 | }, 42 | watch: { 43 | gruntfile: { 44 | files: '<%= jshint.project.src %>', 45 | tasks: ['jshint:project'] 46 | }, 47 | lib: { 48 | files: '<%= jshint.lib.src %>', 49 | tasks: ['jshint:lib', 'mochaTest'] 50 | }, 51 | test: { 52 | files: '<%= jshint.test.src %>', 53 | tasks: ['jshint:test', 'mochaTest'] 54 | } 55 | } 56 | }); 57 | 58 | grunt.registerTask('test', ['jshint', 'mochaTest']); 59 | 60 | // Default task. 61 | grunt.registerTask('default', 'test'); 62 | 63 | }; 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # browserify-global-shim 2 | 3 | A [browserify](http://browserify.org) [transform](https://github.com/substack/node-browserify#btransformopts-tr) for replacing modules with global variables. 4 | 5 | Are you using browserify to convert a module that depends on [lo-dash](http://lodash.com), [jQuery](http://jquery.com), [q](http://documentup.com/kriskowal/q/) or some other omnipresent library? And you don't want to include this dependency in your browserify-build because it's already part of your web app? Use **browserify-global-shim** to replace `require('some module')` with references to a global variable. 6 | 7 | ## Getting Started 8 | Install the module with: `npm install --save-dev browserify-global-shim` 9 | 10 | Configure it via `package.json`: 11 | 12 | ``` 13 | { 14 | "name": "myProject", 15 | "version": "1.0.0", 16 | ... 17 | "browserify": { 18 | "transform": [ 19 | "browserify-global-shim" 20 | ] 21 | }, 22 | "browserify-global-shim": { 23 | "jQuery": "$" 24 | }, 25 | ... 26 | } 27 | ``` 28 | 29 | or by using the [browserify API](https://github.com/substack/node-browserify#api-example): 30 | 31 | ``` 32 | var browserify = require('browserify'); 33 | var b = browserify(); 34 | ... 35 | var globalShim = require('browserify-global-shim').configure({ 36 | 'jQuery': '$' 37 | }); 38 | b.transform(globalShim); 39 | ... 40 | ``` 41 | 42 | In both cases all references to `require('jQuery')` will be replaced with `window.$` when you run browserify. 43 | 44 | ## Similar libraries 45 | 46 | What are you saying? The `global:...` option of [*browserify-shim*](https://github.com/thlorenz/browserify-shim) does just the same? That's true! 47 | 48 | And *browserify-shim* is also configurable via an API? Also true! 49 | 50 | But unfortunately you can't have both at the same time. The API is only supported in the old 2.x versions, the `global:...` option became available in 3.x. If you need both, you're in a tough spot. 51 | 52 | 53 | ## Contributing 54 | In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). 55 | 56 | 57 | ## License 58 | Copyright (c) 2014 Raphael Luba. Licensed under the MIT license. 59 | -------------------------------------------------------------------------------- /browserify-global-shim.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var transformTools = require('browserify-transform-tools'); 4 | 5 | module.exports = transformTools.makeRequireTransform('browserify-global-shim', 6 | { 7 | evaluateArguments: true, 8 | jsFilesOnly: true 9 | }, 10 | function(args, opts, cb) { 11 | var shimmedModules = opts.config || {}; 12 | 13 | var moduleName = args[0]; 14 | var shim = shimmedModules[moduleName]; 15 | 16 | if(typeof shim === 'undefined') { 17 | return cb(); 18 | } 19 | else { 20 | return cb(null, '(window.' + shim + ')'); 21 | } 22 | } 23 | ); 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-global-shim", 3 | "version": "1.0.3", 4 | "main": "browserify-global-shim.js", 5 | "description": "Browserify transform for replacing modules with global variables", 6 | "homepage": "https://github.com/rluba/browserify-global-shim", 7 | "bugs": "https://github.com/rluba/browserify-global-shim/issues", 8 | "author": { 9 | "name": "Raphael Luba", 10 | "email": "raphael@leanbyte.com" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/rluba/browserify-global-shim" 15 | }, 16 | "license": "MIT", 17 | "devDependencies": { 18 | "grunt-contrib-jshint": "~0.7.0", 19 | "grunt-contrib-watch": "~0.5.0", 20 | "grunt-mocha-test": "~0.8.1", 21 | "jshint-stylish": "~0.1.3", 22 | "load-grunt-tasks": "~0.2.0", 23 | "mocha": "~1.16.1", 24 | "time-grunt": "~0.2.0", 25 | "assert": "~1.1.0" 26 | }, 27 | "dependencies": { 28 | "browserify-transform-tools": "^1.5.0" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /test/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "devel": true, 8 | "eqeqeq": true, 9 | "esnext": true, 10 | "forin": true, 11 | "immed": true, 12 | "indent": 4, 13 | "latedef": true, 14 | "laxcomma": true, 15 | "newcap": true, 16 | "noarg": true, 17 | "quotmark": false, 18 | "regexp": true, 19 | "smarttabs": true, 20 | "strict": true, 21 | "trailing": true, 22 | "undef": true, 23 | "unused": true, 24 | 25 | "expr": true, 26 | "globals": { 27 | "describe": false, 28 | "before": false, 29 | "after": false, 30 | "beforeEach": false, 31 | "it": false 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /test/globalShimSpec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert') 4 | , path = require('path') 5 | , globalShim = require('../') 6 | , transformTools = require('browserify-transform-tools') 7 | ; 8 | 9 | describe('global-shim', function () { 10 | var testFile = path.resolve(__dirname, './testInput.js'); 11 | 12 | it('should pass everything through if unconfigured', function () { 13 | transformTools.runTransform(globalShim, testFile, function(err, transformed) { 14 | assert(!err, "Error: " + err); 15 | assert.equal("var $ = require('jQuery'), foo = require('a' + 'Module'), bar = require('another' + 'Module');\n$(foo, bar);\n", transformed); 16 | }); 17 | }); 18 | 19 | it('should replace all configured modules', function () { 20 | var config = { 21 | 'aModule': 'globalModule', 22 | 'jQuery': '$' 23 | }; 24 | 25 | transformTools.runTransform(globalShim.configure(config), testFile, config, function(err, transformed) { 26 | assert(!err, "Error: " + err); 27 | assert.equal("var $ = (window.$), foo = (window.globalModule), bar = require('another' + 'Module');\n$(foo, bar);\n", transformed); 28 | }); 29 | }); 30 | }); -------------------------------------------------------------------------------- /test/testInput.js: -------------------------------------------------------------------------------- 1 | var $ = require('jQuery'), foo = require('a' + 'Module'), bar = require('another' + 'Module'); 2 | $(foo, bar); 3 | --------------------------------------------------------------------------------