-
Notifications
You must be signed in to change notification settings - Fork 117
/
require_jscore.js
167 lines (158 loc) · 5.85 KB
/
require_jscore.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// This file is part of //\ Tarp.
//
// Copyright (C) 2013-2020 Torben Haase <https://pixelsvsbytes.com>
//
// Tarp is free software: you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// Tarp is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details. You should have received a copy of the GNU Lesser General Public
// License along with Tarp. If not, see <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
// NOTE The load parameter points to the function, which prepares the
// environment for each module and runs its code. Scroll down to the end of
// the file to see the function definition.
(self.Tarp = self.Tarp || {}).require = function(config) {
// iOS changes: all loading is synchronous
// Should be config. How can I make it config?
"use strict";
function resolve(id, pwd, suffix) {
var matches = id.match(/^((\.)?.*\/|)(.[^.]*|)(\..*|)$/);
return (new URL(
// matches[1] + matches[3] + (matches[3] && (matches[4] || ".js")),
matches[1] + matches[3] + matches[4] + suffix,
pwd
)).href;
}
function load(id, pwd, suffix, asyn) {
var href, cached, request;
// NOTE resolve href from id.
href = config.resolve(id, pwd, suffix, resolve);
// NOTE create cache item if required.
cached = cache[href] = cache[href] || {
e: undefined, // error
m: undefined, // module
p: undefined, // promise
r: undefined, // request
s: undefined, // source
t: undefined, // type
u: href, // url
};
var tmp, done, source, pattern, match, loading = 0, pwd2;
var responseText = jsc.readFile(href);
if (responseText == "") {
cached.e = new Error("Empty file " + href);
return null;
}
cached.s = source = responseText;
cached.t = "application/javascript"
// iOS: ensure we get application/json set
var regex = /\.json$/;
if (regex.test(href)) {
cached.t = "application/json";
}
return cached;
}
function evaluate(cached, parent) {
var module;
if (!cached.m) {
module = cached.m = {
children: new Array(),
exports: new Object(),
filename: cached.u,
id: cached.u,
loaded: false,
parent: parent,
paths: config.paths.slice(),
require: undefined,
uri: cached.u
},
module.require = factory(module);
parent && parent.children.push(module);
if (cached.t == "application/json") {
module.exports = JSON.parse(cached.s);
} else
(new Function(
"exports,require,module,__filename,__dirname",
cached.s + "\n//# sourceURL=" + module.uri
))(module.exports, module.require, module, module.uri, module.uri.match(/.*\//)[0]);
module.loaded = true;
}
return cached.m;
}
function factory(parent) {
function requireEngine(mode, id, asyn) {
function afterLoad(cached) {
// console.log("Loaded: " + id);
var regex = /package\.json$/;
if (regex.test(cached.u) && !regex.test(id)) {
// iOS: content-type is not set, so we set it manually
var pkg = evaluate(cached, parent);
return typeof pkg.exports.main == "string" ?
(factory(pkg))(pkg.exports.main.replace(/^\.*\/*/, "./"), asyn): // */ (close comment for editor parsing)
pkg.exports;
}
else if (mode == 1)
return cached.u;
else if (mode == 2)
return [pwd.match(/.*\//)[0]];
else
return evaluate(cached, parent).exports;
}
var pwd = (new URL(id[0] == "." ? (parent ? parent.uri : config.root) : config.paths[0], config.root)).href;
var cachedModule = load(id, pwd, "", false); // no suffix at all
// "browser" modules must take priority over package.json
// This might be a larger issue: package.json contains (often) index.js
// Could be: "replace index.js with browser.js" (if it exists)
// Or: edit package.json in the few relevant cases.
if (cachedModule == null) {
cachedModule = load(id, pwd, "/browser.js", false);
}
if (cachedModule == null) {
cachedModule = load(id, pwd, "-browserify/index.js", false);
}
// package.json contains the name of the module
if (cachedModule == null) {
cachedModule = load(id, pwd, "/package.json", false);
}
if (cachedModule == null) {
cachedModule = load(id, pwd, ".js", false);
}
if (cachedModule == null) {
cachedModule = load(id, pwd, "/index.js", false);
}
if (cachedModule == null) {
cachedModule = load(id, pwd, "/" + id + ".js", false);
}
if (cachedModule == null) {
throw Error("Could not find module " + id)
}
return afterLoad(cachedModule);
/* return asyn ?
new Promise(function(res, rej) { load(id, pwd, "/index.js", asyn).p.then(afterLoad).then(res, rej); }):
afterLoad(load(id, pwd, "/index.js", asyn)); */
}
var require = requireEngine.bind(undefined, 0);
require.resolve = requireEngine.bind(require, 1);
require.resolve.paths = requireEngine.bind(require.resolve, 2);
return require;
}
var cache, require;
// NOTE Web-worker will use the origin, since location.href is not available.
cache = Object.create(null);
config = config || new Object();
config.paths = config.paths || ["./node_modules/"];
config.resolve = config.resolve || resolve;
config.root = config.root || location.href;
require = factory(null);
if (config.expose)
self.require = require;
if (config.main)
return require(config.main, !config.sync);
};