1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var _ = require('underscore');
|
11 | var path = require('path');
|
12 | var Module = require('module');
|
13 |
|
14 | var originalWrapper = Module.wrapper.slice(0);
|
15 |
|
16 | function wrap(wrappers, script) {
|
17 | return wrappers[0] + script + wrappers[1];
|
18 | }
|
19 |
|
20 | function replaceWrapper(wrapperObj) {
|
21 | var joiner = '\n';
|
22 | var before = wrapperObj.before.join(joiner);
|
23 | var after = wrapperObj.after.join(joiner);
|
24 | var wrappers = [
|
25 | originalWrapper[0] + before,
|
26 | after + originalWrapper[1]
|
27 | ];
|
28 |
|
29 | Module.wrap = wrap.bind(null, wrappers);
|
30 | }
|
31 |
|
32 | function restoreWrapper() {
|
33 | Module.wrap = wrap.bind(null, originalWrapper);
|
34 | }
|
35 |
|
36 | function createModule(targetPath, parentModule, moduleCache) {
|
37 | moduleCache[targetPath] = moduleCache[targetPath] || new Module(targetPath, parentModule);
|
38 |
|
39 | return moduleCache[targetPath];
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function requireProxy(targetModule, nodeRequire, filepath) {
|
51 | restoreWrapper();
|
52 | targetModule.require = nodeRequire;
|
53 |
|
54 | return nodeRequire.call(targetModule, filepath);
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function infectProxy(targetModule, cache, opts, filepath) {
|
67 | var moduleExports;
|
68 | var Requizzle = require('./requizzle');
|
69 | var requizzle;
|
70 |
|
71 | opts = _.clone(opts);
|
72 | opts.parent = targetModule;
|
73 | requizzle = new Requizzle(opts, cache);
|
74 |
|
75 | moduleExports = requizzle.requizzle(filepath);
|
76 |
|
77 | return moduleExports;
|
78 | }
|
79 |
|
80 | var load = exports.load = function load(targetPath, parentModule, wrapper, cache, options) {
|
81 | var nodeRequire;
|
82 | var targetModule;
|
83 |
|
84 |
|
85 | if (cache.module[targetPath]) {
|
86 | return cache.module[targetPath];
|
87 | }
|
88 |
|
89 | targetModule = createModule(targetPath, parentModule, cache.module);
|
90 | nodeRequire = targetModule.require;
|
91 |
|
92 | if (options.infect) {
|
93 | targetModule.require = function(filepath) {
|
94 | return infectProxy(targetModule, cache, options, filepath);
|
95 | };
|
96 | } else {
|
97 | targetModule.require = function(filepath) {
|
98 | return requireProxy(targetModule, nodeRequire, filepath);
|
99 | };
|
100 | }
|
101 |
|
102 |
|
103 | replaceWrapper(wrapper);
|
104 |
|
105 | targetModule.load(targetModule.id);
|
106 |
|
107 |
|
108 | restoreWrapper();
|
109 |
|
110 | return targetModule;
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function detectStrictMode(src) {
|
120 | return (/^\s*(?:["']use strict["'])[ \t]*(?:[\r\n]|;)/g).test(src);
|
121 | }
|
122 |
|
123 | function loadSource(targetPath, sourceCache) {
|
124 | var fs = require('fs');
|
125 |
|
126 | if (sourceCache[targetPath] === undefined) {
|
127 | sourceCache[targetPath] = fs.readFileSync(targetPath, 'utf8');
|
128 | }
|
129 |
|
130 | return sourceCache[targetPath];
|
131 | }
|
132 |
|
133 | exports.createWrapper = function createWrapper(targetPath, parentModule, cache, options) {
|
134 | var src;
|
135 | var wrapperObject = {
|
136 | before: [],
|
137 | after: []
|
138 | };
|
139 |
|
140 | function add(wrapperFunctions, opts) {
|
141 | var params = [targetPath, parentModule, opts];
|
142 |
|
143 | ['before', 'after'].forEach(function(item) {
|
144 | var result = wrapperFunctions[item].apply(null, params);
|
145 |
|
146 | if (result) {
|
147 | wrapperObject[item].push(result);
|
148 | }
|
149 | });
|
150 | }
|
151 |
|
152 |
|
153 | src = loadSource(targetPath, cache.source);
|
154 | if (detectStrictMode(src) === true) {
|
155 | add(require('./wrappers/strict'));
|
156 | }
|
157 |
|
158 | if (options.requirePaths) {
|
159 | add(require('./wrappers/requirepaths'), options.requirePaths);
|
160 | }
|
161 |
|
162 | if (options.extras) {
|
163 | add(require('./wrappers/extras'), options.extras);
|
164 | }
|
165 |
|
166 | return wrapperObject;
|
167 | };
|