1 | var Module = require("module"),
|
2 | fs = require("fs"),
|
3 | getImportGlobalsSrc = require("./getImportGlobalsSrc.js"),
|
4 | getDefinePropertySrc = require("./getDefinePropertySrc.js"),
|
5 | detectStrictMode = require("./detectStrictMode.js"),
|
6 | moduleEnv = require("./moduleEnv.js");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function internalRewire(parentModulePath, targetPath) {
|
12 | var targetModule,
|
13 | prelude,
|
14 | appendix,
|
15 | src;
|
16 |
|
17 |
|
18 | if (typeof targetPath !== "string") {
|
19 | throw new TypeError("Filename must be a string");
|
20 | }
|
21 |
|
22 |
|
23 | targetPath = Module._resolveFilename(targetPath, parentModulePath);
|
24 |
|
25 |
|
26 | targetModule = new Module(targetPath, parentModulePath);
|
27 |
|
28 |
|
29 | prelude = getImportGlobalsSrc();
|
30 |
|
31 |
|
32 |
|
33 | prelude += "(function () { ";
|
34 |
|
35 |
|
36 | appendix = "\n" + getDefinePropertySrc();
|
37 |
|
38 |
|
39 | appendix += "})();";
|
40 |
|
41 |
|
42 |
|
43 | src = fs.readFileSync(targetPath, "utf8");
|
44 | if (detectStrictMode(src) === true) {
|
45 | prelude = ' "use strict"; ' + prelude;
|
46 | }
|
47 |
|
48 | moduleEnv.inject(prelude, appendix);
|
49 | moduleEnv.load(targetModule);
|
50 |
|
51 | return targetModule.exports;
|
52 | }
|
53 |
|
54 | module.exports = internalRewire;
|