├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── lib ├── debug.js └── resolve-swaps.js ├── package.json └── test ├── bundle-with-transform.js ├── node_modules └── browserify-swap ├── resolve-noswap └── package.json ├── resolve-swap ├── config.js ├── index.js ├── node_modules │ ├── hyperwatch │ │ ├── index.js │ │ └── package.json │ ├── myutil │ │ ├── index.js │ │ └── package.json │ └── test-util │ │ ├── index.js │ │ └── package.json ├── package.json ├── swap │ └── some-hyperwatch-swap.js └── util.js ├── resolve-swaps.js ├── transform.js └── util └── reset.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | bundle.js 17 | 18 | resolve-swap-copy 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.8" 4 | - "0.10" 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2013 Thorsten Lorenz. 2 | All rights reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # browserify-swap [![build status](https://secure.travis-ci.org/thlorenz/browserify-swap.png)](http://travis-ci.org/thlorenz/browserify-swap) 2 | 3 | **swap/swäp/** - *an act of exchanging one thing for another* 4 | 5 | A transform that swaps out modules according to a config in your `package.json` selected via an environment variable. 6 | 7 | #### package.json 8 | 9 | ```json 10 | { 11 | "browserify": { 12 | "transform": [ "browserify-swap" ] 13 | }, 14 | "browserify-swap": { 15 | "@packages": [ "hyperwatch" ], 16 | "dev": { 17 | ".*node_modules\/hyperwatch\/\\S+\\.js$": "./swap/some-hyperwatch-swap.js", 18 | "util.js$": "myutil" 19 | }, 20 | "test": { 21 | "util.js$": "test-util" 22 | } 23 | } 24 | } 25 | ``` 26 | 27 | - each file matcher (i.e. `'util.js$'`) is a regular expression 28 | - in order to swap files of dependencies the `browserify-swap` transform needs to be injected into its package, 29 | therefore indicate those packages via the `@packages` array 30 | 31 | ```sh 32 | BROWSERIFYSWAP_ENV='dev' browserify . -o bundle.js 33 | ``` 34 | 35 | ## Installation 36 | 37 | npm install browserify-swap 38 | 39 | ## API 40 | 41 | 42 | 43 | 44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |

browserifySwap(file) → {TransformStream}

55 |
56 |
57 |
58 |

Looks up browserify_swap configuratios specified for the given file in the environment specified via BROWSERIFYSWAP_ENV.

59 |

If found the file content is replaced with a require statement to the file to swap in for the original. 60 | Otherwise the file's content is just piped through.

61 |
62 |
Parameters:
63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 77 | 78 | 79 | 80 |
NameTypeDescription
file 75 | String 76 |

full path to file being transformed

81 |
82 |
Source:
83 |
90 |
91 |
Returns:
92 |
93 |

transform stream into which browserify will pipe the original content of the file

94 |
95 |
96 |
97 | Type 98 |
99 |
100 | TransformStream 101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 | 109 | *generated with [docme](https://github.com/thlorenz/docme)* 110 |
111 | 112 | 113 | ## License 114 | 115 | MIT 116 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var through = require('through2') 4 | , resolveSwaps = require('./lib/resolve-swaps') 5 | , debug = require('./lib/debug') 6 | , viralify = require('viralify') 7 | , cachedConfig 8 | 9 | var root = process.env.BROWSERIFYSWAP_ROOT || process.cwd(); 10 | 11 | function viralifyDeps(packages) { 12 | debug.inspect({ action: 'viralify', root: root, packages: packages }); 13 | try { 14 | viralify.sync(root, packages, 'browserify-swap', true); 15 | } catch (err) { 16 | debug(err.stack); 17 | } 18 | } 19 | 20 | function requireSwap(swapFileName) { 21 | return 'module.exports = require(\'' + swapFileName + '\');' 22 | } 23 | 24 | function swap(config, env, file) { 25 | if (!config) return; 26 | 27 | var swaps = config[env]; 28 | 29 | if (!swaps) return; 30 | 31 | var matches = Object.keys(swaps) 32 | .filter(function (k) { 33 | // Remove leading and trailing '/' since creating a RegExp will add those anyhow. 34 | // We want to support the /../ format as well just a simple string without the /s. 35 | // We will only document the format without the '/'. 36 | var stringified = k.replace(/^\/|\/$/g,''); 37 | var regex = new RegExp(stringified); 38 | return regex.test(file); 39 | }) 40 | .map(function (k) { return swaps[k]; }); 41 | 42 | // XX: what if we get more than one? For now we just use first match. 43 | return matches[0]; 44 | } 45 | 46 | var go = module.exports = 47 | 48 | /** 49 | * Looks up browserify_swap configuratios specified for the given file in the environment specified via `BROWSERIFYSWAP_ENV`. 50 | * 51 | * If found the file content is replaced with a require statement to the file to swap in for the original. 52 | * Otherwise the file's content is just piped through. 53 | * 54 | * @name browserifySwap 55 | * @function 56 | * @param {String} file full path to file being transformed 57 | * @return {TransformStream} transform stream into which `browserify` will pipe the original content of the file 58 | */ 59 | function browserifySwap(file) { 60 | var env = process.env.BROWSERIFYSWAP_ENV 61 | , data = '' 62 | , swapFile; 63 | 64 | // no stubbing desired or we already determined that we can't find a swap config => just pipe it through 65 | if (!env || cachedConfig === -1) return through(); 66 | 67 | if (cachedConfig) { 68 | swapFile = swap(cachedConfig, env, file); 69 | 70 | // early exit if we have config cached already and know we won't replace anything anyways 71 | return swapFile ? through(write, end) : through(); 72 | } else { 73 | return through(write, end) 74 | } 75 | 76 | function write(d, enc, cb) { data += d; cb(); } 77 | function end(cb) { 78 | /*jshint validthis:true */ 79 | var self = this; 80 | 81 | // if config was cached we already resolved the swapFile if we got here 82 | if (swapFile) { 83 | swapFile = swapFile.replace(/\\/g, '/'); 84 | debug.inspect({ file: file, swapFile: swapFile }); 85 | self.push(requireSwap(swapFile)); 86 | return cb(); 87 | } 88 | 89 | // we should only get here the very first time that this transform is invoked 90 | resolveSwaps(root, function (err, config) { 91 | if (config && config.packages) viralifyDeps(config.packages); 92 | var swaps = config && config.swaps; 93 | 94 | // signal with -1 that we already tried to resolve a swap config but didn't find any 95 | cachedConfig = swaps || -1; 96 | 97 | if (err) return cb(err); 98 | 99 | debug.inspect({ swaps: swaps, env: env }); 100 | swapFile = swap(swaps, env, file); 101 | debug.inspect({ file: file, swapFile: swapFile }); 102 | 103 | var src = swapFile ? requireSwap(swapFile.replace(/\\/g, '/')) : data; 104 | self.push(src); 105 | cb(); 106 | }); 107 | } 108 | } 109 | 110 | // Test 111 | function inspect(obj, depth) { 112 | console.error(require('util').inspect(obj, false, depth || 5, true)); 113 | } 114 | 115 | if (!module.parent && typeof window === 'undefined') { 116 | var pack = require('./test/resolve-swap/package.json'); 117 | inspect(pack); 118 | } 119 | -------------------------------------------------------------------------------- /lib/debug.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var diagnostics = process.env.BROWSERIFYSWAP_DIAGNOSTICS; 4 | 5 | function inspect(obj, depth) { 6 | return require('util').inspect(obj, false, depth || 5, true); 7 | } 8 | 9 | exports = module.exports = function debug() { 10 | if (diagnostics) console.error.apply(console, arguments); 11 | } 12 | 13 | exports.inspect = function(obj, depth) { 14 | if (diagnostics) console.error(inspect(obj, depth)); 15 | } 16 | -------------------------------------------------------------------------------- /lib/resolve-swaps.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var findParentDir = require('find-parent-dir') 4 | , path = require('path') 5 | , resolve = require('resolve'); 6 | 7 | function isPath(s) { 8 | return (/^[.]{0,2}[/\\]/).test(s); 9 | } 10 | 11 | function resolvePaths(swaps, root) { 12 | if (!swaps) return swaps; 13 | Object.keys(swaps) 14 | .forEach(function (env) { 15 | var swap = swaps[env]; 16 | 17 | Object.keys(swap) 18 | .forEach(function (k) { 19 | var req = swap[k]; 20 | var resolved = isPath(req) ? path.resolve(root, req) : resolve.sync(req, { basedir: root }); 21 | swap[k] = resolved; 22 | }) 23 | }) 24 | 25 | return swaps; 26 | } 27 | 28 | var go = module.exports = 29 | 30 | function (cwd, cb) { 31 | findParentDir(cwd, 'package.json', function (err, dir) { 32 | if (err) return cb(err); 33 | var pack = require(path.join(dir, 'package.json')) 34 | , config = pack['browserify-swap'] 35 | , packages; 36 | 37 | if (config && config['@packages']) { 38 | packages = config['@packages']; 39 | delete config['@packages']; 40 | if (!Array.isArray(packages)) packages = [ packages ]; 41 | } 42 | 43 | var swaps = resolvePaths(config, dir); 44 | 45 | cb(null, { swaps: swaps, packages: packages }); 46 | }) 47 | } 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-swap", 3 | "version": "0.2.2", 4 | "description": "A transform that swaps out modules according to a config in your package.json selected via an environment variable.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test-main": "tap test/*.js", 8 | "test-0.8": "nave use 0.8 npm run test-main", 9 | "test-0.10": "nave use 0.10 npm run test-main", 10 | "test-all": "npm run test-main && npm run test-0.8 && npm run test-0.10", 11 | "test": "if [ -e $TRAVIS ]; then npm run test-all; else npm run test-main; fi" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git://github.com/thlorenz/browserify-swap.git" 16 | }, 17 | "homepage": "https://github.com/thlorenz/browserify-swap", 18 | "dependencies": { 19 | "find-parent-dir": "~0.1.0", 20 | "through2": "~0.2.3", 21 | "resolve": "~0.6.1", 22 | "viralify": "~0.4.1" 23 | }, 24 | "devDependencies": { 25 | "nave": "~0.4.3", 26 | "tap": "~0.4.6", 27 | "apply-transform": "~0.1.3", 28 | "rimraf": "~2.2.5", 29 | "cpr": "~0.1.1", 30 | "browserify": "~3.14.1" 31 | }, 32 | "keywords": [ 33 | "browserify", 34 | "browserify-transform", 35 | "transform", 36 | "swap", 37 | "stub", 38 | "mock", 39 | "substitute", 40 | "test", 41 | "development" 42 | ], 43 | "author": { 44 | "name": "Thorsten Lorenz", 45 | "email": "thlorenz@gmx.de", 46 | "url": "http://thlorenz.com" 47 | }, 48 | "license": { 49 | "type": "MIT", 50 | "url": "https://github.com/thlorenz/browserify-swap/blob/master/LICENSE" 51 | }, 52 | "engine": { 53 | "node": ">=0.6" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /test/bundle-with-transform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var test = require('tap').test 5 | , browserify = require('browserify') 6 | , vm = require('vm') 7 | , reset = require('./util/reset') 8 | 9 | var original = __dirname + '/resolve-swap' 10 | , copy = __dirname + '/resolve-swap-copy' 11 | 12 | var dir = copy; 13 | 14 | // simulate we are running browserify from the resolve-swap project 15 | process.env.BROWSERIFYSWAP_ROOT = dir; 16 | 17 | process.env.BROWSERIFYSWAP_DIAGNOSTICS = 1 18 | 19 | function inspect(obj, depth) { 20 | console.error(require('util').inspect(obj, false, depth || 5, true)); 21 | } 22 | 23 | reset(original, copy, runTests); 24 | 25 | function runTests(err) { 26 | var swap = require('../') 27 | 28 | test('\nwhen I bundle a file requiring three files, two of which match the swap config', function (t) { 29 | if (err) { t.fail(err); t.end(); } 30 | 31 | process.env.BROWSERIFYSWAP_ENV = 'dev'; 32 | 33 | browserify() 34 | .require(copy + '/index.js', { expose: 'entry' }) 35 | .bundle(function (err, src) { 36 | if (err) { t.fail(err); t.end(); } 37 | 38 | var ctx = { window: {}, console: console }; 39 | ctx.self = ctx.window; 40 | var require_ = vm.runInNewContext(src, ctx); 41 | 42 | 43 | var main = require_('entry'); 44 | t.deepEqual( 45 | main 46 | , { config: { name: 'config', swapped: false }, 47 | hyperwatch: 48 | { name: 'hyperwatch', 49 | alias: 'some-hyperwatch-swap', 50 | swapped: true }, 51 | util: 52 | { name: 'util', 53 | alias: 'myutil', 54 | swapped: true } } 55 | , 'swaps out the two modules that were configured to be swapped in that environment' 56 | ) 57 | 58 | t.end() 59 | }); 60 | }) 61 | 62 | test('\nwhen I bundle a file requiring three files, one of which match the swap config', function (t) { 63 | if (err) { t.fail(err); t.end(); } 64 | 65 | process.env.BROWSERIFYSWAP_ENV = 'test'; 66 | 67 | browserify() 68 | .require(copy + '/index.js', { expose: 'entry' }) 69 | .bundle(function (err, src) { 70 | if (err) { t.fail(err); t.end(); } 71 | 72 | var ctx = { window: {}, console: console }; 73 | ctx.self = ctx.window; 74 | var require_ = vm.runInNewContext(src, ctx); 75 | 76 | 77 | var main = require_('entry'); 78 | t.deepEqual( 79 | main 80 | , { config: { name: 'config', swapped: false }, 81 | hyperwatch: { name: 'hyperwatch', swapped: false }, 82 | util: 83 | { name: 'util', 84 | alias: 'test-util', 85 | swapped: true } } 86 | , 'swaps out the module that was configured to be swapped in that environment' 87 | ) 88 | 89 | t.end() 90 | }); 91 | }) 92 | 93 | test('\nwhen I bundle a file requiring three files, but the env does not match any swap config', function (t) { 94 | if (err) { t.fail(err); t.end(); } 95 | 96 | process.env.BROWSERIFYSWAP_ENV = null; 97 | 98 | browserify() 99 | .require(copy + '/index.js', { expose: 'entry' }) 100 | .bundle(function (err, src) { 101 | if (err) { t.fail(err); t.end(); } 102 | 103 | var ctx = { window: {}, console: console }; 104 | ctx.self = ctx.window; 105 | var require_ = vm.runInNewContext(src, ctx); 106 | 107 | var main = require_('entry'); 108 | t.deepEqual( 109 | main 110 | , { config: { name: 'config', swapped: false }, 111 | hyperwatch: { name: 'hyperwatch', swapped: false }, 112 | util: { name: 'util', swapped: false } } 113 | , 'swaps out no modules' 114 | ) 115 | 116 | t.end() 117 | }); 118 | }) 119 | } 120 | -------------------------------------------------------------------------------- /test/node_modules/browserify-swap: -------------------------------------------------------------------------------- 1 | ../../ -------------------------------------------------------------------------------- /test/resolve-noswap/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "resolve-noswap", 3 | "version": "0.0.0", 4 | "description": "Has no swap config.", 5 | "main": "index.js" 6 | } 7 | -------------------------------------------------------------------------------- /test/resolve-swap/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { name: 'config', swapped: false } 2 | -------------------------------------------------------------------------------- /test/resolve-swap/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // swapped nowhere 4 | var config = require('./config'); 5 | 6 | // swapped in 'dev' only 7 | var hyperwatch = require('hyperwatch'); 8 | 9 | // swapped in 'dev' and 'test' 10 | var util = require('./util'); 11 | 12 | module.exports = { 13 | config : config 14 | , hyperwatch : hyperwatch 15 | , util : util 16 | } 17 | -------------------------------------------------------------------------------- /test/resolve-swap/node_modules/hyperwatch/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | name: 'hyperwatch' 3 | , swapped: false 4 | } 5 | -------------------------------------------------------------------------------- /test/resolve-swap/node_modules/hyperwatch/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hyperwatch", 3 | "version": "0.0.0", 4 | "description": "Some fake hyperwatch to simulate the real one for the tests.", 5 | "main": "index.js" 6 | } 7 | -------------------------------------------------------------------------------- /test/resolve-swap/node_modules/myutil/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | name: 'util' 3 | , alias: 'myutil' 4 | , swapped: true 5 | } 6 | -------------------------------------------------------------------------------- /test/resolve-swap/node_modules/myutil/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "myutil", 3 | "version": "0.0.0", 4 | "description": "The local myutil stub that fills in for util at times", 5 | "main": "index.js" 6 | } 7 | -------------------------------------------------------------------------------- /test/resolve-swap/node_modules/test-util/index.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | name: 'util' 3 | , alias: 'test-util' 4 | , swapped: true 5 | } 6 | -------------------------------------------------------------------------------- /test/resolve-swap/node_modules/test-util/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test-util", 3 | "version": "0.0.0", 4 | "description": "The test util that fills in for util during tests", 5 | "main": "index.js" 6 | } 7 | -------------------------------------------------------------------------------- /test/resolve-swap/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "resolve-swap", 3 | "version": "0.0.0", 4 | "description": "Has swap config.", 5 | "main": "index.js", 6 | "browserify": { 7 | "transform": [ "browserify-swap" ] 8 | }, 9 | "browserify-swap": { 10 | "@packages": [ "hyperwatch" ], 11 | "dev": { 12 | ".*node_modules\/hyperwatch\/\\S+\\.js$": "./swap/some-hyperwatch-swap.js", 13 | "util.js$": "myutil" 14 | }, 15 | "test": { 16 | "util.js$": "test-util" 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/resolve-swap/swap/some-hyperwatch-swap.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | name: 'hyperwatch' 3 | , alias: 'some-hyperwatch-swap' 4 | , swapped: true 5 | } 6 | -------------------------------------------------------------------------------- /test/resolve-swap/util.js: -------------------------------------------------------------------------------- 1 | module.exports = { name: 'util', swapped: false }; 2 | -------------------------------------------------------------------------------- /test/resolve-swaps.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tap').test 4 | , resolve = require('../lib/resolve-swaps') 5 | 6 | function inspect(obj, depth) { 7 | console.error(require('util').inspect(obj, false, depth || 5, true)); 8 | } 9 | 10 | test('\nwhen resolving swaps from a dir with package.json that has no swaps', function (t) { 11 | var dir = __dirname + '/resolve-noswap'; 12 | resolve(dir, function (err, res) { 13 | if (err) return (t.fail(err), t.end()); 14 | t.notOk(res.swaps, 'returns no swaps') 15 | t.end() 16 | }); 17 | }) 18 | 19 | test('\nwhen resolving swaps from a dir with package.json that has swaps', function (t) { 20 | var dir = __dirname + '/resolve-swap'; 21 | resolve(dir, function (err, res) { 22 | if (err) return (t.fail(err), t.end()); 23 | t.deepEqual( 24 | res.swaps 25 | , { dev: 26 | { '.*node_modules/hyperwatch/\\S+\\.js$': dir + '/swap/some-hyperwatch-swap.js', 27 | 'util.js$': dir + '/node_modules/myutil/index.js' }, 28 | test: { 'util.js$': dir + '/node_modules/test-util/index.js' } } 29 | , 'returns swap config with full path of local files and node_modules resolved' 30 | ) 31 | t.end() 32 | }); 33 | }) 34 | 35 | test('\nwhen resolving swaps from a dir right below package.json that has swaps', function (t) { 36 | var root = __dirname + '/resolve-swap' 37 | , dir = root + '/swap'; 38 | 39 | resolve(dir, function (err, res) { 40 | if (err) return (t.fail(err), t.end()); 41 | t.deepEqual( 42 | res.swaps 43 | , { dev: 44 | { '.*node_modules/hyperwatch/\\S+\\.js$': root + '/swap/some-hyperwatch-swap.js', 45 | 'util.js$': root + '/node_modules/myutil/index.js' }, 46 | test: { 'util.js$': root + '/node_modules/test-util/index.js' } } 47 | , 'returns swap config with full path of local files and node_modules resolved' 48 | ) 49 | t.end() 50 | }); 51 | }) 52 | -------------------------------------------------------------------------------- /test/transform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /*jshint asi: true */ 3 | 4 | var test = require('tap').test 5 | , applyTransform = require('apply-transform') 6 | , reset = require('./util/reset') 7 | 8 | var original = __dirname + '/resolve-swap' 9 | , copy = __dirname + '/resolve-swap-copy' 10 | 11 | var dir = copy; 12 | process.env.BROWSERIFYSWAP_ROOT = dir; 13 | 14 | reset(original, copy, runTests); 15 | 16 | function runTests(err) { 17 | if (err) throw err; 18 | 19 | // load this module after process.cwd was overridden since it will viralify right then 20 | var swap = require('../'); 21 | 22 | test('\nwhen current dir has package.json with swap config', function (t) { 23 | 24 | process.env.BROWSERIFYSWAP_ENV = 'dev'; 25 | test('\n# and I transform a file that matches swap selector pointing to local file', function (t) { 26 | var tx = swap('/some/path/node_modules/hyperwatch/index..js') 27 | applyTransform(tx, 'var original = this', function (err, res) { 28 | if (err) { t.fail(err); t.end(); } 29 | t.equal(res, 'module.exports = require(\'' + dir + '/swap/some-hyperwatch-swap.js\');', 'swaps it out'); 30 | t.end(); 31 | }); 32 | }) 33 | 34 | test('\n# and I transform a file that does not match swap selector', function (t) { 35 | var tx = swap('/some/path/node_modules/not-matching.js') 36 | applyTransform(tx, 'var original = this', function (err, res) { 37 | if (err) { t.fail(err); t.end(); } 38 | t.equal(res, 'var original = this', 'doe not swap it out'); 39 | t.end(); 40 | }); 41 | }) 42 | 43 | test('\n# and I transform a file that matches swap selector pointing installed module', function (t) { 44 | var tx = swap('util.js') 45 | applyTransform(tx, 'var original = this', function (err, res) { 46 | if (err) { t.fail(err); t.end(); } 47 | t.equal(res, 'module.exports = require(\'' + dir + '/node_modules/myutil/index.js\');', 'swaps it out'); 48 | t.end(); 49 | }); 50 | }) 51 | 52 | test('\n# and I transform a file that matches swap selector pointing installed module for different environment', function (t) { 53 | process.env.BROWSERIFYSWAP_ENV = 'test'; 54 | var tx = swap('util.js') 55 | applyTransform(tx, 'var original = this', function (err, res) { 56 | if (err) { t.fail(err); t.end(); } 57 | t.equal(res, 'module.exports = require(\'' + dir + '/node_modules/test-util/index.js\');', 'swaps it out'); 58 | t.end(); 59 | }); 60 | }) 61 | 62 | test('\n# and I transform a file that matches swap selector but not in current environment', function (t) { 63 | process.env.BROWSERIFYSWAP_ENV = 'test'; 64 | var tx = swap('/some/path/node_modules/hyperwatch.js') 65 | applyTransform(tx, 'var original = this', function (err, res) { 66 | if (err) { t.fail(err); t.end(); } 67 | t.equal(res, 'var original = this', 'doe not swap it out'); 68 | t.end(); 69 | }); 70 | }) 71 | 72 | t.end() 73 | }) 74 | } 75 | -------------------------------------------------------------------------------- /test/util/reset.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var rmrf = require('rimraf') 4 | , cpr = require('cpr') 5 | 6 | module.exports = function reset(original, copy, cb) { 7 | rmrf(copy, function (err) { 8 | // expecting err since may not exist 9 | if (err) return console.error(err); 10 | 11 | cpr(original, copy, cb); 12 | }); 13 | } 14 | --------------------------------------------------------------------------------