module-alias
Advanced tools
Comparing version 1.0.3 to 1.0.4
23
index.js
@@ -8,3 +8,3 @@ var Module = require('module').Module | ||
var old_nodeModulePaths = Module._nodeModulePaths | ||
Module._nodeModulePaths = function(from) { | ||
Module._nodeModulePaths = function (from) { | ||
var paths = old_nodeModulePaths.call(this, from) | ||
@@ -22,3 +22,3 @@ | ||
var old_resolveFilename = Module._resolveFilename | ||
Module._resolveFilename = function(request, self) { | ||
Module._resolveFilename = function (request, self) { | ||
for (var alias in module_aliases) { | ||
@@ -36,3 +36,3 @@ if (request.indexOf(alias) === 0) { | ||
function addPathHelper(path, targetArray) { | ||
function addPathHelper (path, targetArray) { | ||
path = nodePath.normalize(path) | ||
@@ -44,3 +44,3 @@ if (targetArray && targetArray.indexOf(path) === -1) { | ||
function removePathHelper(path, targetArray) { | ||
function removePathHelper (path, targetArray) { | ||
if (targetArray) { | ||
@@ -54,3 +54,3 @@ var index = targetArray.indexOf(path) | ||
function addPath(path) { | ||
function addPath (path) { | ||
var parent | ||
@@ -74,3 +74,3 @@ path = nodePath.normalize(path) | ||
function addAliases(aliases) { | ||
function addAliases (aliases) { | ||
for (var alias in aliases) { | ||
@@ -81,3 +81,3 @@ addAlias(alias, aliases[alias]) | ||
function addAlias(alias, target) { | ||
function addAlias (alias, target) { | ||
module_aliases[alias] = target | ||
@@ -91,5 +91,5 @@ } | ||
*/ | ||
function reset() { | ||
function reset () { | ||
// Reset all changes in paths caused by addPath function | ||
module_paths.forEach(function(path) { | ||
module_paths.forEach(function (path) { | ||
removePathHelper(path, require.main.paths) | ||
@@ -111,3 +111,3 @@ var parent = module.parent | ||
*/ | ||
function init(options) { | ||
function init (options) { | ||
if (typeof options === 'string') { | ||
@@ -149,3 +149,2 @@ options = { base: options } | ||
// | ||
@@ -156,3 +155,3 @@ // Register custom module directories (like node_modules) | ||
if (npm_package._moduleDirectories instanceof Array) { | ||
npm_package._moduleDirectories.forEach(function(dir) { | ||
npm_package._moduleDirectories.forEach(function (dir) { | ||
if (dir === 'node_modules') return | ||
@@ -159,0 +158,0 @@ |
{ | ||
"name": "module-alias", | ||
"description": "Simple module for registering aliases of directories and custom module paths", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"author": { | ||
@@ -11,4 +11,5 @@ "name": "Nikita Gavrilov", | ||
"test": "npm run lint && npm run testonly", | ||
"testonly": "mocha test/specs.js", | ||
"lint": "eslint src" | ||
"testonly": "NODE_ENV=test mocha test/specs.js", | ||
"testonly-watch": "NODE_ENV=test mocha -w test/specs.js", | ||
"lint": "standard src" | ||
}, | ||
@@ -40,5 +41,5 @@ "bugs": { | ||
"chai": "^3.5.0", | ||
"eslint": "^1.1.0", | ||
"hello-world-classic": "ilearnio/hello-world-classic", | ||
"mocha": "^2.4.5" | ||
} | ||
} |
@@ -17,6 +17,6 @@ # module-alias | ||
Add these lines to your package.json (in your application's root) | ||
Add these lines to your `package.json` (in your application's root) | ||
```js | ||
"_moduleDirectories": ["node_modules_custom"], | ||
// Aliases | ||
"_moduleAliases": { | ||
@@ -30,13 +30,19 @@ "@root" : "", // Application's root | ||
} | ||
// Custom modules directory (optional) | ||
"_moduleDirectories": ["node_modules_custom"], | ||
``` | ||
And these line at the very main file of your app, before any code | ||
Then add these line at the very main file of your app, before any code | ||
```js | ||
import 'module-alias/register' | ||
``` | ||
// And you're all set, now you can do stuff like | ||
And you're all set! Now you can do stuff like: | ||
```js | ||
import 'something' | ||
import module from '@root/some-module' | ||
import veryDeepModule from '@bar/my-module' | ||
import veryDeepModule from '@deep/my-module' | ||
import myModule from '@my_module' // module from `node_modules_custom` directory | ||
@@ -99,10 +105,6 @@ ``` | ||
In order to register a custom modules path (`addPath`) it modifies the internal `Module._nodeModulePaths` method so that the directory then acts like it's the `node_modules` directory. | ||
In order to register a custom modules path (`addPath`) it modifies the internal `Module._nodeModulePaths` method so that the given directory then acts like it's the `node_modules` directory. | ||
In order to register an alias it modifies the internal `Module._resolveFilename` method so that when you fire `require` or `import` it first checks whether the given string starts with one of the registered aliases, if so, it then replaces the alias in the string with the target path of the alias | ||
### Tags | ||
Require alias, node import alias, node custom module directory, node local require paths, register module directory in nodejs | ||
[npm-image]: https://img.shields.io/npm/v/module-alias.svg | ||
@@ -109,0 +111,0 @@ [npm-url]: https://npmjs.org/package/module-alias |
8013
111