loader.js
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Copyright (c) 2014 Google Inc. All rights reserved.
Copyright (c) 2012-2013 Johannes Ewald.
Use of this source code is governed by the MIT License, available in this package's LICENSE file
or at http://opensource.org/licenses/MIT.
*/
'use strict';
var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var Module = require('module');
var originalWrapper = Module.wrapper.slice(0);
var requizzleWrappers = {
extras: require('./wrappers/extras'),
requirePaths: require('./wrappers/requirepaths'),
strict: require('./wrappers/strict')
};
function wrap(wrappers, script) {
return wrappers[0] + script + wrappers[1];
}
function replaceWrapper(wrapperObj) {
var joiner = '\n';
var before = wrapperObj.before.join(joiner);
var after = wrapperObj.after.join(joiner);
var wrappers = [
originalWrapper[0] + before,
after + originalWrapper[1]
];
Module.wrap = wrap.bind(null, wrappers);
}
function restoreWrapper() {
Module.wrap = wrap.bind(null, originalWrapper);
}
function createModule(targetPath, parentModule, moduleCache) {
moduleCache[targetPath] = moduleCache[targetPath] || new Module(targetPath, parentModule);
return moduleCache[targetPath];
}
/**
* Wrapper for `require()` to prevent the target module's dependencies from being swizzled.
*
* @param {!Module} targetModule - The module that is being swizzled.
* @param {!function} nodeRequire - The original `require()` method for the target module.
* @param {!string} filepath - The value passed to `require()`.
* @return {!Module} The requested module dependency.
*/
function requireProxy(targetModule, nodeRequire, filepath) {
restoreWrapper();
targetModule.require = nodeRequire;
return nodeRequire.call(targetModule, filepath);
}
/**
* Wrapper for `require()` to swizzle the target module's dependencies, using the same settings as
* the target module.
*
* @param {!Module} targetModule - The module that is being swizzled.
* @param {!Object} opts - The Requizzle options object.
* @param {!string} filepath - The value passed to `require()`.
* @return {!Module} The requested module dependency.
*/
function infectProxy(targetModule, cache, opts, filepath) {
var moduleExports;
// loaded here to avoid circular dependencies
var Requizzle = require('./requizzle');
var requizzle;
opts = _.clone(opts);
opts.parent = targetModule;
requizzle = new Requizzle(opts, cache);
moduleExports = requizzle.requizzle(filepath);
return moduleExports;
}
var load = exports.load = function load(targetPath, parentModule, wrapper, cache, options) {
var nodeRequire;
var targetModule;
// Handle circular requires, and avoid reloading modules unnecessarily
if (cache.module[targetPath]) {
return cache.module[targetPath];
}
targetModule = createModule(targetPath, parentModule, cache.module);
nodeRequire = targetModule.require;
if (options.infect) {
targetModule.require = function(filepath) {
return infectProxy(targetModule, cache, options, filepath);
};
} else {
targetModule.require = function(filepath) {
return requireProxy(targetModule, nodeRequire, filepath);
};
}
// update the wrapper before we load the target module
replaceWrapper(wrapper);
targetModule.load(targetModule.id);
// make sure the wrapper is restored even if the target module doesn't load any dependencies
restoreWrapper();
return targetModule;
};
/**
* Check whether the entire module includes a `'use strict'` declaration.
*
* @param {string} src - The source file to check.
* @return {boolean} Set to `true` if the module includes a `use strict` declaration.
*/
function detectStrictMode(src) {
return (/^\s*(?:["']use strict["'])[ \t]*(?:[\r\n]|;)/g).test(src);
}
function loadSource(targetPath, sourceCache) {
if (sourceCache[targetPath] === undefined) {
sourceCache[targetPath] = fs.readFileSync(targetPath, 'utf8');
}
return sourceCache[targetPath];
}
exports.createWrapper = function createWrapper(targetPath, parentModule, cache, options) {
var src;
var wrapperObject = {
before: [],
after: []
};
function add(wrapperFunctions, opts) {
var params = [targetPath, parentModule, opts];
['before', 'after'].forEach(function(item) {
var result = wrapperFunctions[item].apply(null, params);
if (result) {
wrapperObject[item].push(result);
}
});
}
// Preserve the module's `use strict` declaration if present
src = loadSource(targetPath, cache.source);
if (detectStrictMode(src) === true) {
add(requizzleWrappers.strict);
}
if (options.requirePaths) {
add(requizzleWrappers.requirePaths, options.requirePaths);
}
if (options.extras) {
add(requizzleWrappers.extras, options.extras);
}
return wrapperObject;
};