├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── package.json ├── src └── aliasify.coffee ├── test ├── mocha.opts └── test.coffee └── testFixtures ├── react ├── includeReact.js └── package.json ├── requireishConfig ├── foobar.js └── package.json ├── test ├── package.json └── src │ ├── bar │ └── bar.js │ ├── foo │ └── foo.js │ ├── foobar │ └── foobar.js │ ├── index.js │ ├── regexp.js │ └── regexp2.js ├── testNestedPackages ├── node_modules │ └── inner-package │ │ ├── foo │ │ └── foo.js │ │ └── package.json └── package.json └── testWithRelativeConfig ├── config └── config.js ├── package.json └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /lib 3 | /coverage 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /src 3 | /test 4 | /testFixtures 5 | /CHANGELOG.md 6 | /coverage 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.12' 5 | - '4' 6 | - '5' 7 | after_success: 8 | - 'cat ./coverage/lcov.info | ./node_modules/.bin/coveralls' 9 | before_install: 10 | - "npm install amqplib" 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | * 2.0.0 2 | * Add support for `requireish` (many thanks to [kamekazemaster](https://github.com/kamekazemaster).) 3 | * BREAKING CHANGE: Calling `configure()` to set configuration programatically still works, but is considered 4 | deprecated and may be removed in a future release. `setConfig()` has been removed. We've done some slightly 5 | exciting things with how configuration gets loaded; all tests are passing, but if you run into a problem, 6 | please be sure to raise an issue. 7 | * 1.7.0 - Correct handling of absolute paths on Windows (courtesy [Ronald M. Clifford](https://github.com/roncli)) 8 | * 1.6.0 9 | * Add support for passing configuration directly via browserify (added in Browserify v3.41.0?) 10 | * Verbose messages now log to stderr (courtesy [Zaim Bakar](https://github.com/zaim)) 11 | * 1.5.0 - Use 'fromSourceFileDir' from browserify-transform-tools to correctly resolves package.json (courtesy of [Justin Howard](https://github.com/justinhoward)) 12 | * 1.4.0 - Add support for `relative:` aliases. (courtesy of [mkuklis](https://github.com/mkuklis).) 13 | * 1.3.1 - Use browserify-transform-tools to decide which files are JS or not. 14 | * 1.3.0 - Upgrade to browserify-transform-tools v1.2.1. 15 | * 1.2.3 - Fix for Windows backslash problem (courtesy of [LinuxBasic](httsp://github.com/LinuxBasic).) 16 | * 1.2.2 - Commit the lib file along with the coffee file. :( 17 | Add support for literate coffee-script and streamline files. 18 | * 1.2.1 - Only instrument .js and .coffee files (courtesy of [spenceralger](https://github.com/spenceralger).) 19 | * 1.2.0 - Support for paths after aliases (courtesy of [martinheidegger](https://github.com/martinheidegger).) 20 | * 1.1.0 - Support for programatic configuration. 21 | * 1.0.0 - Initial release. 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Benbria 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/benbria/aliasify.svg)](https://travis-ci.org/benbria/aliasify) 2 | [![Coverage Status](https://coveralls.io/repos/benbria/aliasify/badge.svg?branch=master&service=github)](https://coveralls.io/github/benbria/aliasify?branch=master) 3 | 4 | Aliasify is a [transform](https://github.com/substack/node-browserify#btransformtr) for [browserify](https://github.com/substack/node-browserify) which lets you rewrite calls to `require`. 5 | 6 | Installation 7 | ============ 8 | 9 | Install with `npm install --save-dev aliasify`. 10 | 11 | Usage 12 | ===== 13 | 14 | To use, add a section to your package.json: 15 | 16 | { 17 | "aliasify": { 18 | "aliases": { 19 | "d3": "./shims/d3.js", 20 | "underscore": "lodash" 21 | } 22 | } 23 | } 24 | 25 | Now if you have a file in src/browserify/index.js which looks like: 26 | 27 | d3 = require('d3') 28 | _ = require('underscore') 29 | ... 30 | 31 | This will automatically be transformed to: 32 | 33 | d3 = require('../../shims/d3.js') 34 | _ = require('lodash') 35 | ... 36 | 37 | Any replacement that starts with a "." will be resolved as a relative path (as "d3" above.) Replacements that start with any other character will be replaced verbatim (as with "underscore" above.) 38 | 39 | Configuration 40 | ============= 41 | 42 | Configuration can be loaded in multiple ways; You can put your configuration directly in package.json, as in the example above, or you can use an external json or js file. In your package.json: 43 | 44 | { 45 | "aliasify": "./aliasifyConfig.js" 46 | } 47 | 48 | Then in aliasifyConfig.js: 49 | 50 | module.exports = { 51 | aliases: { 52 | "d3": "./shims/d3.js" 53 | }, 54 | verbose: false 55 | }; 56 | 57 | Note that using a js file means you can change your configuration based on environment variables. 58 | 59 | Alternatively, if you're using the Browserify API, you can configure your aliasify programatically: 60 | 61 | aliasifyConfig = { 62 | aliases: { 63 | "d3": "./shims/d3.js" 64 | }, 65 | verbose: false 66 | } 67 | 68 | var b = browserify(); 69 | b.transform(aliasify, aliasifyConfig); 70 | 71 | note that using the browserify API, './shims/d3.js' will be resolved against the current working 72 | directory. 73 | 74 | Configuration options: 75 | * `aliases` - An object mapping aliases to their replacements. 76 | * `replacements` - An object mapping RegExp strings with RegExp replacements, or a function that will return a replacement. 77 | * `verbose` - If true, then aliasify will print modifications it is making to stdout. 78 | * `configDir` - An absolute path to resolve relative paths against. If you're using package.json, this will automatically be filled in for you with the directory containing package.json. If you're using a .js file for configuration, set this to `__dirname`. 79 | * `appliesTo` - Controls which files will be transformed. By default, only JS type files will be transformed ('.js', '.coffee', etc...). See [browserify-transform-tools documentation](https://github.com/benbria/browserify-transform-tools/wiki/Transform-Configuration#common-configuration) for details. 80 | 81 | Relative Requires 82 | ================= 83 | 84 | When you specify: 85 | 86 | aliases: { 87 | "d3": "./shims/d3.js" 88 | } 89 | 90 | The "./" means this will be resolved relative to the current working directory (or relative to the 91 | configuration file which contains the line, in the case where configuration is loaded from 92 | package.json.) Sometimes it is desirable to literally replace an alias; to resolve the alias 93 | relative to the file which is doing the `require` call. In this case you can do: 94 | 95 | aliases: { 96 | "d3": {"relative": "./shims/d3.js"} 97 | } 98 | 99 | This will cause all occurences of `require("d3")` to be replaced with `require("./shims/d3.js")`, 100 | regardless of where those files are in the directory tree. 101 | 102 | Regular Expression Aliasing 103 | =========================== 104 | You can use the `replacements` configuration section to create more powerful aliasing. This is useful if you 105 | have a large project but don't want to manually add an alias for every single file. It is also incredibly useful when you want to combine 106 | aliasify with other transforms, such as hbsfy, reactify, or coffeeify. 107 | 108 | replacements: { 109 | "_components/(\\w+)": "src/react/components/$1/index.jsx" 110 | } 111 | 112 | Will let you replace `require('_components/SomeCoolReactComponent')` with `require('src/react/components/SomeCoolReactComponent/index.jsx')` 113 | 114 | You can also match an alias and pass a function which can return a new file name. 115 | 116 | `require("_coffee/delicious-coffee");` 117 | 118 | Using this configuration: 119 | 120 | replacements: { 121 | "_coffee/(\\w+)": function (alias, regexMatch, regexObject) { 122 | console.log(alias); // _coffee/delicious-coffee 123 | console.log(regexMatch); // _coffee/(\\w+) 124 | return 'coffee.js'; // default behavior - won't replace 125 | } 126 | } 127 | 128 | 129 | Stubbing Out Packages 130 | ===================== 131 | 132 | You can remove a package entirely for browser builds using: 133 | 134 | aliases: { 135 | "d3": false 136 | } 137 | 138 | Now any code which tries to `require('d3')` will end up compiling to: 139 | 140 | var d3 = {}; 141 | 142 | 143 | Support aliasing requireish function calls 144 | ===================== 145 | 146 | You can tell aliasify to also replace aliases in other functions than `require`. This can become very helpful if you are planing on wrap 147 | node's require function with another one. For example in case of [proxyquireify](https://github.com/thlorenz/proxyquireify) this is very helpful. 148 | 149 | ```JavaScript 150 | var aliasify = require("aliasify").requireish(["require", "foo", "bar"]) 151 | ``` 152 | 153 | with this options: 154 | 155 | aliases: { 156 | "d3": {"relative": "./shims/d3.js"} 157 | } 158 | 159 | Now any code which tries to `require('d3')` or `foo('d3')` or even `bar('d3')` will end up compiling to: 160 | 161 | `require("./shims/d3.js")` respectively `foo("./shims/d3.js")` respectively `bar("./shims/d3.js")` 162 | 163 | The argument for `requireish()` can be either a string or an array of strings. 164 | 165 | A few things to note: first, if you specify `requireish`, you must explicitly list `require` in the list of requireish 166 | things to transform, or it won't be. 167 | 168 | Second, note that aliasify only replaces the first string parameter of the "requireish" function call. All other 169 | arguments are preserved as they were passed in. (e.g. `require('d3', 'foo')` turns into 170 | `require('./shims/d3.js', 'foo')`.) Caution! Do NOT pass in arguments that have circular references. If you need that, 171 | than just pass in an identifier for the object having circular references! 172 | 173 | Alternatives 174 | ============ 175 | 176 | `aliasify` is essentially a fancy version of the [`browser` field](https://gist.github.com/defunctzombie/4339901#replace-specific-files---advanced) from package.json, which is [interpreted](https://github.com/substack/node-browserify#packagejson) by browserify. 177 | 178 | Using the `browser` field is probably going to be faster, as it doesn't involve running a transform on each of your files. On the other hand, `aliasify` gives you a finer degree of control and can be run before other transforms (for example, you can run `aliasify` before [debowerify](https://github.com/eugeneware/debowerify), which will let you replace certain components that debowerify would otherwise replace.) 179 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aliasify", 3 | "version": "2.1.0", 4 | "description": "Rewrite require calls in browserify modules.", 5 | "main": "./lib/aliasify.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/benbria/aliasify.git" 9 | }, 10 | "keywords": [ 11 | "browserify", 12 | "alias" 13 | ], 14 | "author": "Jason Walton (https://github.com/jwalton)", 15 | "contributors": [ 16 | "Jason Walton (https://github.com/jwalton)" 17 | ], 18 | "license": "MIT", 19 | "directories": { 20 | "lib": "./lib" 21 | }, 22 | "dependencies": { 23 | "browserify-transform-tools": "~1.7.0" 24 | }, 25 | "devDependencies": { 26 | "coffee-coverage": "^0.7.0", 27 | "coffee-script": "^1.8.0", 28 | "coveralls": "^2.11.6", 29 | "es6-promise": "^3.0.2", 30 | "istanbul": "^0.4.1", 31 | "mocha": "^2.1.0", 32 | "promise-breaker": "^3.0.0" 33 | }, 34 | "scripts": { 35 | "test": "mocha && istanbul report text-summary lcov", 36 | "prepublish": "coffee -c -o lib src && mocha test", 37 | "build": "coffee -c -o lib src" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/aliasify.coffee: -------------------------------------------------------------------------------- 1 | path = require 'path' 2 | transformTools = require 'browserify-transform-tools' 3 | 4 | TRANSFORM_NAME = "aliasify" 5 | 6 | # Returns replacement require, `null` to not change require, `false` to replace require with `{}`. 7 | getReplacement = (file, aliases, regexps) -> 8 | if regexps? 9 | for key of regexps 10 | re = new RegExp(key) 11 | if re.test(file) 12 | if regexps[key] == false 13 | return false 14 | else if typeof regexps[key] == "function" 15 | return regexps[key](file, key, re) 16 | else 17 | return file.replace(re, regexps[key]) 18 | 19 | if aliases? 20 | if file of aliases 21 | return aliases[file] 22 | else 23 | fileParts = /^([^\/]*)(\/.*)$/.exec(file) 24 | if fileParts?[1] of aliases 25 | pkg = aliases[fileParts?[1]] 26 | if pkg == false 27 | return false 28 | else if pkg? 29 | return pkg+fileParts[2] 30 | 31 | return null 32 | 33 | makeTransform = (requireAliases) -> 34 | transformTools.makeFunctionTransform TRANSFORM_NAME, { 35 | jsFilesOnly: true, 36 | fromSourceFileDir: true, 37 | functionNames: requireAliases 38 | }, (functionParams, opts, done) -> 39 | if !opts.config then return done new Error("Could not find configuration for aliasify") 40 | aliases = opts.config.aliases 41 | regexps = opts.config.replacements 42 | verbose = opts.config.verbose 43 | 44 | configDir = opts.configData?.configDir or opts.config.configDir or process.cwd() 45 | 46 | result = null 47 | 48 | file = functionParams.args[0].value 49 | if file? and (aliases? or regexps?) 50 | replacement = getReplacement(file, aliases, regexps) 51 | if replacement == false 52 | result = "{}" 53 | else if replacement? 54 | if replacement.relative? 55 | replacement = replacement.relative 56 | 57 | else if /^\./.test(replacement) 58 | # Resolve the new file relative to the configuration file. 59 | replacement = path.resolve configDir, replacement 60 | fileDir = path.dirname opts.file 61 | replacement = "./#{path.relative fileDir, replacement}" 62 | 63 | if verbose 64 | console.error "aliasify - #{opts.file}: replacing #{file} with #{replacement} " + 65 | "of function #{functionParams.name}" 66 | 67 | # If this is an absolute Windows path (e.g. 'C:\foo.js') then don't convert \s to /s. 68 | if /^[a-zA-Z]:\\/.test(replacement) 69 | replacement = replacement.replace(/\\/gi, "\\\\") 70 | else 71 | replacement = replacement.replace(/\\/gi, "/") 72 | 73 | result = "'#{replacement}'" 74 | 75 | 76 | # Check if the function has more than one arg. If so preserve the remaining ones. 77 | if result? and result isnt "{}" 78 | remainingArgs = functionParams.args.slice(1) 79 | if remainingArgs.length > 0 80 | for arg in remainingArgs 81 | if arg.type is "Literal" 82 | result += ", '#{arg.value}'" 83 | else if arg.type is "ObjectExpression" 84 | try 85 | result += ", #{JSON.stringify arg.value}" 86 | catch err 87 | result += ", #{JSON.stringify {}}" 88 | else if arg.type is "ArrayExpression" 89 | try 90 | result += ", #{JSON.stringify arg.value}" 91 | catch err 92 | result += ", #{JSON.stringify []}" 93 | else 94 | result += ", #{arg.value}" 95 | result = "#{functionParams.name}(#{result})" 96 | 97 | done null, result 98 | 99 | module.exports = (file, config) -> 100 | requireish = null 101 | if config and "requireish" of config 102 | requireish = config.requireish 103 | else 104 | configData = transformTools.loadTransformConfigSync TRANSFORM_NAME, file, {fromSourceFileDir: true} 105 | if configData and configData.config and "requireish" of configData.config 106 | requireish = configData.config.requireish 107 | 108 | wrappedTransform = makeTransform(requireish or ['require']) 109 | return wrappedTransform(file, config) 110 | 111 | module.exports.configure = (config) -> 112 | return (file) -> module.exports file, config 113 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --compilers coffee:coffee-script/register 2 | --require coffee-coverage/register-istanbul 3 | --reporter spec 4 | --recursive 5 | -------------------------------------------------------------------------------- /test/test.coffee: -------------------------------------------------------------------------------- 1 | require('es6-promise').polyfill() 2 | 3 | path = require 'path' 4 | assert = require 'assert' 5 | transformTools = require 'browserify-transform-tools' 6 | Mocha = require 'mocha' 7 | pb = require 'promise-breaker' 8 | aliasify = require '../src/aliasify' 9 | testDir = path.resolve __dirname, "../testFixtures/test" 10 | testWithRelativeConfigDir = path.resolve __dirname, "../testFixtures/testWithRelativeConfig" 11 | requireishConfigDir = path.resolve __dirname, "../testFixtures/requireishConfig" 12 | 13 | runTestWithConfig = pb.make (aliasifyConfig, content=null, done) -> 14 | process.chdir testDir 15 | jsFile = path.resolve __dirname, "../testFixtures/test/src/index.js" 16 | options = {config: aliasifyConfig} 17 | if content then options.content = content 18 | transformTools.runTransform aliasify, jsFile, options, done 19 | 20 | runTestWithCustomAliases = pb.make (aliasifyConfig, content=null, done) -> 21 | process.chdir testDir 22 | jsFile = path.resolve __dirname, "../testFixtures/test/src/foobar/foobar.js" 23 | options = {config: aliasifyConfig} 24 | if content then options.content = content 25 | transformTools.runTransform aliasify, jsFile, options, done 26 | 27 | describe "aliasify", -> 28 | cwd = process.cwd() 29 | 30 | after -> 31 | process.chdir cwd 32 | 33 | it "should correctly transform a file", (done) -> 34 | process.chdir testDir 35 | jsFile = path.resolve __dirname, "../testFixtures/test/src/index.js" 36 | transformTools.runTransform aliasify, jsFile, (err, result) -> 37 | return done err if err 38 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 39 | d3 = require('./../shims/d3.js'); 40 | _ = require('lodash'); 41 | """) 42 | done() 43 | 44 | it "should correctly transform a file when the configuration is in a different directory", (done) -> 45 | process.chdir testWithRelativeConfigDir 46 | jsFile = path.resolve __dirname, "../testFixtures/testWithRelativeConfig/src/index.js" 47 | transformTools.runTransform aliasify, jsFile, (err, result) -> 48 | return done err if err 49 | assert.equal result, "d3 = require('./../shims/d3.js');" 50 | done() 51 | 52 | it "should load requireish options from config", (done) -> 53 | console.log requireishConfigDir 54 | process.chdir requireishConfigDir 55 | jsFile = path.resolve requireishConfigDir, "foobar.js" 56 | transformTools.runTransform aliasify, jsFile, (err, result) -> 57 | return done err if err 58 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 59 | var foo = foobar('./foo.js'); 60 | var qux = baz('foo'); 61 | """) 62 | done() 63 | 64 | it "should allow configuration to be specified programatically", -> 65 | runTestWithConfig {aliases: {"d3": "./foo/baz.js"}} 66 | .then (result) -> 67 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 68 | d3 = require('./../foo/baz.js'); 69 | _ = require("underscore"); 70 | """) 71 | 72 | it "should allow removal of requires", -> 73 | runTestWithConfig {aliases: {"d3": false}} 74 | .then (result) -> 75 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 76 | d3 = {}; 77 | _ = require("underscore"); 78 | """) 79 | 80 | it "should allow removal of requires from code with path after alias", -> 81 | runTestWithConfig( 82 | {aliases: {"d3": false}}, 83 | """ 84 | d3 = require("d3/suffix.js"); 85 | _ = require("underscore"); 86 | """ 87 | ) 88 | .then (result) -> 89 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 90 | d3 = {}; 91 | _ = require("underscore"); 92 | """) 93 | 94 | it "should allow removal of requires via regex", -> 95 | runTestWithConfig {replacements: {"^d3$": false}, aliases: {}} 96 | .then (result) -> 97 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 98 | d3 = {}; 99 | _ = require("underscore"); 100 | """) 101 | 102 | it "should work if there are regexes and no aliases", -> 103 | runTestWithConfig { 104 | aliases: null 105 | replacements: { 106 | "d3.*": "./foo/baz.js" 107 | } 108 | } 109 | .then (result) -> 110 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 111 | d3 = require('./../foo/baz.js'); 112 | _ = require("underscore"); 113 | """) 114 | 115 | it "should work if there are no regexes and no aliases", -> 116 | runTestWithConfig { 117 | aliases: null 118 | replacements: null 119 | } 120 | .then (result) -> 121 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 122 | d3 = require("d3"); 123 | _ = require("underscore"); 124 | """) 125 | 126 | it "should allow configuration to be specified using legacy 'configure' method", (done) -> 127 | jsFile = path.resolve __dirname, "../testFixtures/test/src/index.js" 128 | aliasifyWithConfig = aliasify.configure { 129 | aliases: { 130 | "d3": "./foo/bar.js" 131 | }, 132 | configDir: path.resolve __dirname, "../testFixtures/test" 133 | } 134 | 135 | transformTools.runTransform aliasifyWithConfig, jsFile, (err, result) -> 136 | return done err if err 137 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 138 | d3 = require('./../foo/bar.js'); 139 | _ = require("underscore"); 140 | """) 141 | done() 142 | 143 | it "should allow paths after an alias", -> 144 | runTestWithConfig( 145 | { 146 | aliases: {"d3": "./base/"} 147 | configDir: path.resolve __dirname, "../testFixtures/test" 148 | }, 149 | """ 150 | d3 = require("d3/suffix.js"); 151 | _ = require("underscore"); 152 | """ 153 | ).then (result) -> 154 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 155 | d3 = require('./../base/suffix.js'); 156 | _ = require("underscore"); 157 | """) 158 | 159 | it "should allow paths after an alias2", (done) -> 160 | jsFile = path.resolve __dirname, "../testFixtures/test/src/index.js" 161 | 162 | aliasifyWithConfig = aliasify.configure { 163 | aliases: { 164 | "d3": "./foo/" 165 | }, 166 | configDir: path.resolve __dirname, "../testFixtures/test" 167 | } 168 | 169 | content = Mocha.utils.clean(""" 170 | d3 = require("d3/bar.js"); 171 | """) 172 | expectedContent = Mocha.utils.clean(""" 173 | d3 = require('./../foo/bar.js'); 174 | """) 175 | 176 | transformTools.runTransform aliasifyWithConfig, jsFile, {content}, (err, result) -> 177 | return done err if err 178 | assert.equal result, expectedContent 179 | done() 180 | 181 | 182 | 183 | it "passes anything that isn't javascript along", (done) -> 184 | jsFile = path.resolve __dirname, "../testFixtures/test/package.json" 185 | transformTools.runTransform aliasify, jsFile, (err, result) -> 186 | return done err if err 187 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean("""{ 188 | "aliasify": { 189 | "aliases": { 190 | "d3": "./shims/d3.js", 191 | "underscore": "lodash" 192 | } 193 | } 194 | } 195 | """) 196 | done() 197 | 198 | it "passes supports relative path option", (done) -> 199 | jsFile = path.resolve __dirname, "../testFixtures/test/src/bar/bar.js" 200 | 201 | aliasifyWithConfig = aliasify.configure { 202 | aliases: { 203 | "foo": { relative: "../foo/foo.js" } 204 | } 205 | } 206 | 207 | expectedContent = Mocha.utils.clean(""" 208 | var foo = require('../foo/foo.js'); 209 | """) 210 | 211 | transformTools.runTransform aliasifyWithConfig, jsFile, (err, result) -> 212 | return done err if err 213 | assert.equal result, expectedContent 214 | done() 215 | 216 | it "supports nested packages", (done) -> 217 | jsFile = path.resolve __dirname, "../testFixtures/testNestedPackages/node_modules/inner-package/foo/foo.js" 218 | transformTools.runTransform aliasify, jsFile, (err, result) -> 219 | return done err if err 220 | assert.equal result, "d3 = require('./../shims/d3.js');" 221 | done() 222 | 223 | it "supports the react case that everyone is asking for", (done) -> 224 | jsFile = path.resolve __dirname, "../testFixtures/react/includeReact.js" 225 | transformTools.runTransform aliasify, jsFile, (err, result) -> 226 | return done err if err 227 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 228 | react1 = require('react/addons'); 229 | react2 = require('react/addons'); 230 | """) 231 | done() 232 | 233 | it "should correctly resolve Windows absolute paths", (done) -> 234 | jsFile = "c:\\foo.js" 235 | 236 | aliasifyWithConfig = aliasify.configure { 237 | aliases: { 238 | "foo": jsFile 239 | } 240 | } 241 | 242 | content = Mocha.utils.clean(""" 243 | foo = require("foo"); 244 | """) 245 | # Note the \\\\ here, because this is in "s, but this resolves to a double \\. 246 | # The double \\ is still needed, since \\ inside of ''s resolves to a single \ in the end. 247 | expectedContent = Mocha.utils.clean(""" 248 | foo = require('c:\\\\foo.js'); 249 | """) 250 | 251 | transformTools.runTransform aliasifyWithConfig, jsFile, {content}, (err, result) -> 252 | return done err if err 253 | assert.equal result, expectedContent 254 | done() 255 | 256 | it "should correctly resolve absolute paths", (done) -> 257 | jsFile = path.resolve __dirname, "../testFixtures/test/src/foo/foo.js" 258 | 259 | aliasifyWithConfig = aliasify.configure { 260 | aliases: { 261 | "foo": jsFile 262 | } 263 | } 264 | 265 | content = Mocha.utils.clean(""" 266 | foo = require("foo"); 267 | """) 268 | expectedContent = Mocha.utils.clean(""" 269 | foo = require('#{jsFile.replace(/\\/gi, '\\\\')}'); 270 | """) 271 | 272 | transformTools.runTransform aliasifyWithConfig, jsFile, {content}, (err, result) -> 273 | return done err if err 274 | assert.equal result, expectedContent 275 | done() 276 | 277 | it "should correctly replace a RexExp alias", (done) -> 278 | jsFile = path.resolve __dirname, "../testFixtures/test/src/regexp.js" 279 | aliasifyWithConfig = aliasify.configure { 280 | replacements: { 281 | "_components/(\\w+)": "src/components/$1.jsx" 282 | } 283 | } 284 | 285 | expectedContent = Mocha.utils.clean(""" 286 | SomeComponent = require('src/components/SomeComponent.jsx'); 287 | """) 288 | 289 | transformTools.runTransform aliasifyWithConfig, jsFile, (err, result) -> 290 | return done err if err 291 | assert.equal result, expectedContent 292 | done() 293 | 294 | it "should correctly replace a RexExp alias function", (done) -> 295 | jsFile = path.resolve __dirname, "../testFixtures/test/src/regexp.js" 296 | aliasifyWithConfig = aliasify.configure { 297 | replacements: { 298 | "_components/(\\w+)": (alias, regexMatcher, regexObject) -> 299 | return "src/silly.js" 300 | } 301 | } 302 | 303 | expectedContent = Mocha.utils.clean(""" 304 | SomeComponent = require('src/silly.js'); 305 | """) 306 | 307 | transformTools.runTransform aliasifyWithConfig, jsFile, (err, result) -> 308 | return done err if err 309 | assert.equal result, expectedContent 310 | done() 311 | 312 | it "should correctly replace multiple RexExp alias functions (scoping)", (done) -> 313 | jsFile = path.resolve __dirname, "../testFixtures/test/src/regexp2.js" 314 | aliasifyWithConfig = aliasify.configure { 315 | replacements: { 316 | "_component/(.*)": (alias, regexMatcher, regexObject) -> 317 | return alias.replace(regexObject, 'src/silly.js') 318 | "_store/(.*)": (alias, regexMatcher, regexObject) -> 319 | return alias.replace(regexObject, 'src/stores/$1/index.js') 320 | 321 | } 322 | } 323 | 324 | expectedContent = Mocha.utils.clean(""" 325 | SomeComponent = require('src/silly.js'); 326 | SomeStore = require('src/stores/SomeStore/index.js'); 327 | """) 328 | 329 | transformTools.runTransform aliasifyWithConfig, jsFile, (err, result) -> 330 | return done err if err 331 | assert.equal Mocha.utils.clean(result), expectedContent 332 | done() 333 | 334 | 335 | it "should support aliasing require calls by a string", -> 336 | runTestWithCustomAliases {aliases: {"foo": { relative: "../foo/foo.js" }}, requireish: 'foobar'} 337 | .then (result) -> 338 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 339 | var foo = foobar('../foo/foo.js'); 340 | var qux = baz('foo'); 341 | """) 342 | 343 | it "should support aliasing require calls by an array of strings", -> 344 | runTestWithCustomAliases {aliases: {"foo": { relative: "../foo/foo.js" }}, requireish: ['foobar', 'baz']} 345 | .then (result) -> 346 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 347 | var foo = foobar('../foo/foo.js'); 348 | var qux = baz('../foo/foo.js'); 349 | """) 350 | 351 | it "should preserve args other than the first", -> 352 | 353 | content = Mocha.utils.clean(""" 354 | var foo = foobar('foo', 'baz', bar, function (){}, {}, []); 355 | """) 356 | 357 | runTestWithCustomAliases {aliases: {"foo": { relative: "../foo/foo.js" }}, requireish: 'foobar'}, content 358 | .then (result) -> 359 | assert.equal Mocha.utils.clean(result), Mocha.utils.clean(""" 360 | var foo = foobar('../foo/foo.js', 'baz', bar, function (){}, {}, []); 361 | """) 362 | -------------------------------------------------------------------------------- /testFixtures/react/includeReact.js: -------------------------------------------------------------------------------- 1 | react1 = require('react'); 2 | react2 = require('react/addons'); -------------------------------------------------------------------------------- /testFixtures/react/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "aliasify": { 3 | "aliases": { 4 | "react": "react/addons", 5 | "react/addons": "react/addons" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /testFixtures/requireishConfig/foobar.js: -------------------------------------------------------------------------------- 1 | var foo = foobar('foo'); 2 | var qux = baz('foo'); 3 | -------------------------------------------------------------------------------- /testFixtures/requireishConfig/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "aliasify": { 3 | "aliases": { 4 | "foo": {"relative": "./foo.js"} 5 | }, 6 | "requireish": "foobar" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /testFixtures/test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "aliasify": { 3 | "aliases": { 4 | "d3": "./shims/d3.js", 5 | "underscore": "lodash" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /testFixtures/test/src/bar/bar.js: -------------------------------------------------------------------------------- 1 | var foo = require('foo'); -------------------------------------------------------------------------------- /testFixtures/test/src/foo/foo.js: -------------------------------------------------------------------------------- 1 | module.exports = { foo: true }; -------------------------------------------------------------------------------- /testFixtures/test/src/foobar/foobar.js: -------------------------------------------------------------------------------- 1 | var foo = foobar('foo'); 2 | var qux = baz('foo'); -------------------------------------------------------------------------------- /testFixtures/test/src/index.js: -------------------------------------------------------------------------------- 1 | d3 = require("d3"); 2 | _ = require("underscore"); -------------------------------------------------------------------------------- /testFixtures/test/src/regexp.js: -------------------------------------------------------------------------------- 1 | SomeComponent = require("_components/SomeComponent"); -------------------------------------------------------------------------------- /testFixtures/test/src/regexp2.js: -------------------------------------------------------------------------------- 1 | SomeComponent = require("_component/SomeComponent"); 2 | SomeStore = require("_store/SomeStore"); -------------------------------------------------------------------------------- /testFixtures/testNestedPackages/node_modules/inner-package/foo/foo.js: -------------------------------------------------------------------------------- 1 | d3 = require("d3"); -------------------------------------------------------------------------------- /testFixtures/testNestedPackages/node_modules/inner-package/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "aliasify": { 3 | "aliases": { 4 | "d3": "./shims/d3.js" 5 | } 6 | } 7 | } -------------------------------------------------------------------------------- /testFixtures/testNestedPackages/package.json: -------------------------------------------------------------------------------- 1 | { 2 | 3 | } -------------------------------------------------------------------------------- /testFixtures/testWithRelativeConfig/config/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | aliases: { 3 | "d3": "../shims/d3.js" 4 | } 5 | } -------------------------------------------------------------------------------- /testFixtures/testWithRelativeConfig/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "aliasify": "./config/config.js" 3 | } -------------------------------------------------------------------------------- /testFixtures/testWithRelativeConfig/src/index.js: -------------------------------------------------------------------------------- 1 | d3 = require("d3"); --------------------------------------------------------------------------------