require-from-string
Advanced tools
Comparing version 1.0.2 to 1.1.0
17
index.js
@@ -6,3 +6,13 @@ 'use strict'; | ||
module.exports = function requireFromString(code, filename) { | ||
module.exports = function requireFromString(code, filename, opts) { | ||
if (typeof filename === 'object') { | ||
opts = filename; | ||
filename = undefined; | ||
} | ||
opts = opts || {}; | ||
opts.appendPaths = opts.appendPaths || []; | ||
opts.prependPaths = opts.prependPaths || []; | ||
if (typeof code !== 'string') { | ||
@@ -12,7 +22,10 @@ throw new Error('code must be a string, not ' + typeof code); | ||
var paths = Module._nodeModulePaths(path.dirname(filename)); | ||
var m = new Module(filename, module.parent); | ||
m.filename = filename; | ||
m.paths = Module._nodeModulePaths(path.dirname(filename)); | ||
m.paths = [].concat(opts.prependPaths).concat(paths).concat(opts.appendPaths); | ||
m._compile(code, filename); | ||
return m.exports; | ||
}; |
{ | ||
"name": "require-from-string", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Require module from string", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -24,3 +24,3 @@ # require-from-string [![Build Status](https://travis-ci.org/floatdrop/require-from-string.svg?branch=master)](https://travis-ci.org/floatdrop/require-from-string) | ||
### requireFromString(code, [filename]) | ||
### requireFromString(code, [filename], [options]) | ||
@@ -40,4 +40,18 @@ #### code | ||
#### options | ||
Type: `object` | ||
##### appendPaths | ||
Type: `Array` | ||
List of `paths`, that will be appended to module `paths`. Useful, when you want | ||
to be able require modules from these paths. | ||
##### prependPaths | ||
Type: `Array` | ||
Same as `appendPath`, but paths will be prepended. | ||
## License | ||
MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3246
4
21
56