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.children && parent.children.splice(parent.children.indexOf(m), 1);
|
32 |
|
33 | return exports;
|
34 | };
|