eval-as-module
Advanced tools
Comparing version 0.1.1 to 0.1.2
25
index.js
@@ -1,7 +0,27 @@ | ||
var dirname = require('path').dirname | ||
var Module = require('module') | ||
var path = require('path') | ||
var json = JSON.stringify | ||
var originalFindPath = Module._findPath | ||
/** | ||
* monkey patch findPath to handle fake directories better | ||
* | ||
* TODO: handle more types of request | ||
* | ||
* @param {String} request | ||
* @param {Array[String]} paths | ||
* @return {String} | ||
*/ | ||
function findPath(request, paths) { | ||
if (paths.length == 1 && /^\.{2}/.test(request)) { | ||
paths[0] = path.dirname(paths[0]) | ||
request = request.slice(1) | ||
} | ||
return originalFindPath(request, paths) | ||
} | ||
/** | ||
* run `js` as if it were a module at `path` | ||
@@ -16,6 +36,9 @@ * | ||
var m = new Module(path, module) | ||
Module._findPath = findPath | ||
m.paths = Module._nodeModulePaths(dirname(path)) | ||
m.id = path | ||
m.filename = path | ||
js = 'module.return=eval(' + json(js) + ')' | ||
m._compile(js, path) | ||
Module._findPath = originalFindPath | ||
return m | ||
@@ -22,0 +45,0 @@ } |
{ | ||
"name": "eval-as-module", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Eval a JS string as if it was a file being required", | ||
@@ -5,0 +5,0 @@ "keywords": ["eval","module"], |
@@ -1,13 +0,11 @@ | ||
# eval-as-module | ||
Eval a JS string as if it was a file being required | ||
Eval a JS string as if it was a file being required | ||
## Installation | ||
With your favorite package manager: | ||
```sh | ||
npm install eval-as-module | ||
``` | ||
- [packin](//github.com/jkroso/packin): `packin add eval-as-module` | ||
- [npm](//npmjs.org/doc/cli/npm-install.html): `npm install eval-as-module` | ||
then in your app: | ||
@@ -14,0 +12,0 @@ |
3163
5
40
32