-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire.js
42 lines (35 loc) · 1.37 KB
/
require.js
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
var fs = require('fs')
, _path = require('path')
, Module = require('module')
, compile = require('./compile')
;
module.exports = function(path, parent) {
// Resolve paths based off the *parent* module's location
var resolved = Module._resolveFilename(path, parent);
// We don't do native modules
if (resolved.indexOf(_path.sep) < 0)
throw new Error('Cannot use require-internal with builtin modules or other packages');
// Look for entries in the module cache. If there's something there
// that *wasn't* imported by us, panic.
var cached = require.cache[resolved];
if (cached) {
if (!cached._sandbox)
throw new Error('Module has already been loaded without require-internal');
return cached.exports;
}
// We have to set _contextLoad to true to force the implementation to
// use the sandbox we've constructed. We'll save the previous value
// and restore.
var _contextLoad = Module._contextLoad;
Module._contextLoad = true;
// Compile the module and set it up
var m = new Module(path, module.parent);
var sandbox = {};
var options = process.version.match(/^v0\.8\./) ? 'utf8' : {encoding: 'utf8'};
compile.call(m, sandbox, fs.readFileSync(resolved, options), resolved);
m._sandbox = sandbox;
require.cache[resolved] = m;
// Restore contextLoad to the original
Module._contextLoad = _contextLoad;
return m.exports;
};