-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequire.js
93 lines (70 loc) · 1.98 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
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
const {resolve, isAbsolute, dirname} = require('path');
const {existsSync, readFileSync} = require('fs');
const convert = require('./convert');
const run = require('./run');
const local = /^\.{0,2}\//;
const engineModule = /^express-engine-jsx(\/|$)/;
module.exports = requireJSX;
requireJSX.cache = new Map();
function requireJSX(path, currentWorkingDir) {
if (!local.test(path)) {
if (engineModule.test(path)) {
return require(path.replace(engineModule, './'));
}
const resolvedPath = resolveJSX(path);
if (!resolvedPath) {
return require(path);
}
path = resolvedPath;
}
if (isAbsolute(path)) {
currentWorkingDir = null;
}
else if (!currentWorkingDir) {
const site = require('callsites')()[1];
currentWorkingDir = site && dirname(site.getFileName());
if (!currentWorkingDir || !isAbsolute(currentWorkingDir)) throw new Error('currentWorkingDir required');
}
else if (!isAbsolute(currentWorkingDir)) {
throw new Error('currentWorkingDir must be absolute path');
}
if (currentWorkingDir) {
path = resolve(currentWorkingDir, path);
}
const {cache} = requireJSX;
if (cache.has(path)) return cache.get(path).moduleExports;
if (existsSync(path + '.js') || existsSync(resolve(path, 'index.js'))) {
return require(path);
}
let pathJSX;
if (!existsSync((pathJSX = path + '.jsx')) && !existsSync((pathJSX = resolve(path, 'index.jsx')))) {
throw new Error(`JSX file not found ${JSON.stringify(path)}`);
}
const result = convert(readFileSync(pathJSX), {
path: pathJSX
});
const code = typeof result === 'string' ? result : result.code;
const map = typeof result === 'string' ? null : result.map;
const moduleExports = run(code, {
path: pathJSX
});
cache.set(path, {
moduleExports,
map,
});
return moduleExports;
}
function resolveJSX(path) {
try {
path = require.resolve(path + '.jsx');
}
catch (e1) {
try {
path = require.resolve(resolve(path, 'index.jsx'));
}
catch (e2) {
return null;
}
}
return path.replace(/\.jsx$/, '');
}