-
Notifications
You must be signed in to change notification settings - Fork 1
/
require_polyfill.js
149 lines (133 loc) · 4.37 KB
/
require_polyfill.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
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
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last === '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
};
function pathNormalize(path) {
var isAbsolute = path.charAt(0) === '/';
var trailingSlash = path.substr(-1) === '/';
// Normalize the path
path = normalizeArray(path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
if (path && trailingSlash) {
path += '/';
}
return (isAbsolute ? '/' : '') + path;
};
var globalEval = eval;
var currentScript = document.currentScript;
var projectRoot = currentScript.dataset['project-root'] || currentScript.dataset['projectRoot'];
if (projectRoot == null) {
throw new Error('The attribute `data-project-root` isn\'t found in the script tag. You need to provide the root (in which node_modules reside).')
}
var nodeModulesDir = projectRoot + '/node_modules/';
var modulesCache = {};
var packageJsonMainCache = {};
var ensureEndsWithJs = function(path) {
if (path.endsWith('.js')) {
return path;
} else {
return path + '.js';
}
};
let dir_sep = "/";
function concat(dirname, filename) {
if (filename[0] === dir_sep) {
return filename;
}
var l = dirname.length;
if (l === 0 || dirname[l - 1 | 0] === dir_sep) {
return dirname + filename;
} else {
return dirname + (dir_sep + filename);
}
}
function loadScript(scriptPath) {
var request = new XMLHttpRequest();
var splitp = scriptPath.split(dir_sep);
for (let i = 0; i < splitp.length; i++) {
if (splitp[i] === "node_modules") {
scriptPath = splitp.slice(i, splitp.length).join(dir_sep);
}
}
request.open("GET", scriptPath, false); // sync
request.send();
var dirSeparatorIndex = scriptPath.lastIndexOf('/');
var dir = dirSeparatorIndex === -1 ? '.' : scriptPath.slice(0, dirSeparatorIndex);
var moduleText = `
(function(module, exports, modulesCache, packageJsonMainCache, nodeModulesDir) {
function require(path) {
var __dirname = "${dir}/";
var resolvedPath;
if (path.startsWith('.')) {
// require('./foo/bar')
resolvedPath = ensureEndsWithJs(__dirname + path);
} else if (path.indexOf('/') === -1) {
// require('react')
var packageJson = pathNormalize(concat(nodeModulesDir, path + '/package.json'));
if (packageJsonMainCache[packageJson] == null) {
var jsonRequest = new XMLHttpRequest();
jsonRequest.open("GET", packageJson, false);
jsonRequest.send();
var main;
if (jsonRequest.responseText != null) {
main = JSON.parse(jsonRequest.responseText).main;
};
if (main == null) {
main = 'index.js';
} else if (!main.endsWith('.js')) {
main = main + '.js';
}
packageJsonMainCache[packageJson] = concat(nodeModulesDir, concat(path, main));
}
resolvedPath = packageJsonMainCache[packageJson];
} else {
// require('react/bar')
resolvedPath = ensureEndsWithJs(concat(nodeModulesDir, path));
};
resolvedPath = pathNormalize(resolvedPath);
if (modulesCache[resolvedPath] != null) {
return modulesCache[resolvedPath];
};
var result = loadScript(resolvedPath);
modulesCache[resolvedPath] = result;
return result;
};
var process = {env: {}, argv: []};
var global = {};
// -------Begin Require Polyfilled Module Loaded From Disk------------------------------
// file: ${scriptPath}
// root: ${projectRoot}
// ----------------------------------------------------------------------
${request.responseText}
// -------End Polyfill Loaded From Disk------------------------------
// file: ${scriptPath}
// root: ${projectRoot}
// ----------------------------------------------------------------------
return module.exports})\n//@ sourceURL=${scriptPath}`;
var module = {exports: {}};
return globalEval(moduleText)(module, module.exports, modulesCache, packageJsonMainCache, nodeModulesDir);
};
loadScript(currentScript.dataset.main)