labenamata-require-from-string 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/index.js +34 -0
  2. package/license +21 -0
  3. package/package.json +28 -0
  4. package/readme.md +56 -0
package/index.js ADDED
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ var Module = require('module');
4
+ var path = require('path');
5
+
6
+ module.exports = function requireFromString(code, filename, opts) {
7
+ if (typeof filename === 'object') {
8
+ opts = filename;
9
+ filename = undefined;
10
+ }
11
+
12
+ opts = opts || {};
13
+ filename = filename || '';
14
+
15
+ opts.appendPaths = opts.appendPaths || [];
16
+ opts.prependPaths = opts.prependPaths || [];
17
+
18
+ if (typeof code !== 'string') {
19
+ throw new Error('code must be a string, not ' + typeof code);
20
+ }
21
+
22
+ var paths = Module._nodeModulePaths(path.dirname(filename));
23
+
24
+ var parent = module.parent;
25
+ var m = new Module(filename, parent);
26
+ m.filename = filename;
27
+ m.paths = [].concat(opts.prependPaths).concat(paths).concat(opts.appendPaths);
28
+ m._compile(code, filename);
29
+
30
+ var exports = m.exports;
31
+ parent && parent.children && parent.children.splice(parent.children.indexOf(m), 1);
32
+
33
+ return exports;
34
+ };
package/license ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Vsevolod Strukchinsky <floatdrop@gmail.com> (github.com/floatdrop)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "labenamata-require-from-string",
3
+ "version": "2.0.2",
4
+ "description": "Require module from string",
5
+ "license": "MIT",
6
+ "repository": "labenamata/require-from-string",
7
+ "author": {
8
+ "name": "labenamata",
9
+ "email": "labenamata@gmail.com",
10
+ "url": "github.com/labenamata"
11
+ },
12
+ "engines": {
13
+ "node": ">=0.10.0"
14
+ },
15
+ "scripts": {
16
+ "test": "mocha"
17
+ },
18
+ "files": [
19
+ "index.js"
20
+ ],
21
+ "keywords": [
22
+ ""
23
+ ],
24
+ "dependencies": {},
25
+ "devDependencies": {
26
+ "mocha": "*"
27
+ }
28
+ }
package/readme.md ADDED
@@ -0,0 +1,56 @@
1
+ # require-from-string [![Build Status](https://travis-ci.org/floatdrop/require-from-string.svg?branch=master)](https://travis-ci.org/floatdrop/require-from-string)
2
+
3
+ Load module from string in Node.
4
+
5
+ ## Install
6
+
7
+ ```
8
+ $ npm install --save require-from-string
9
+ ```
10
+
11
+
12
+ ## Usage
13
+
14
+ ```js
15
+ var requireFromString = require('require-from-string');
16
+
17
+ requireFromString('module.exports = 1');
18
+ //=> 1
19
+ ```
20
+
21
+
22
+ ## API
23
+
24
+ ### requireFromString(code, [filename], [options])
25
+
26
+ #### code
27
+
28
+ *Required*
29
+ Type: `string`
30
+
31
+ Module code.
32
+
33
+ #### filename
34
+ Type: `string`
35
+ Default: `''`
36
+
37
+ Optional filename.
38
+
39
+
40
+ #### options
41
+ Type: `object`
42
+
43
+ ##### appendPaths
44
+ Type: `Array`
45
+
46
+ List of `paths`, that will be appended to module `paths`. Useful, when you want
47
+ to be able require modules from these paths.
48
+
49
+ ##### prependPaths
50
+ Type: `Array`
51
+
52
+ Same as `appendPaths`, but paths will be prepended.
53
+
54
+ ## License
55
+
56
+ MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop)