olum-compiler 0.8.5 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/compiler.js +1 -1
- package/bin/copySrc.js +4 -9
- package/bin/dev.js +6 -1
- package/bin/index.js +1 -1
- package/bin/watcher.js +14 -4
- package/lib/chokidar.js +344 -0
- package/lib/cross-spawn.js +318 -0
- package/lib/getImports.js +2 -2
- package/lib/helpers.js +5 -6
- package/lib/magic-string.js +213 -0
- package/lib/sourcemap.js +1 -1
- package/lib/websocket.js +347 -0
- package/package.json +3 -7
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* spawn.js — standalone single-file port of cross-spawn@7.0.6 (MIT)
|
|
3
|
+
* with its dependencies inlined: which@2.0.2, isexe@2.0.0, path-key@3.1.1,
|
|
4
|
+
* shebang-command@2.0.0, shebang-regex@3.0.0 (all MIT).
|
|
5
|
+
* Uses only node built-ins (child_process, fs, path).
|
|
6
|
+
*
|
|
7
|
+
* API (same as cross-spawn):
|
|
8
|
+
* spawn(command, [args], [options]) → ChildProcess
|
|
9
|
+
* spawn.sync(command, [args], [options]) → SpawnSyncReturns
|
|
10
|
+
*/
|
|
11
|
+
"use strict";
|
|
12
|
+
|
|
13
|
+
const cp = require("child_process");
|
|
14
|
+
const fs = require("fs");
|
|
15
|
+
const path = require("path");
|
|
16
|
+
|
|
17
|
+
const isWin = process.platform === "win32";
|
|
18
|
+
// `which`/`isexe` treat cygwin/msys shells as windows too
|
|
19
|
+
const isWindowsEnv = isWin || process.env.OSTYPE === "cygwin" || process.env.OSTYPE === "msys";
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------- path-key
|
|
22
|
+
function pathKey(options = {}) {
|
|
23
|
+
const environment = options.env || process.env;
|
|
24
|
+
const platform = options.platform || process.platform;
|
|
25
|
+
if (platform !== "win32") return "PATH";
|
|
26
|
+
return Object.keys(environment).reverse().find(key => key.toUpperCase() === "PATH") || "Path";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ------------------------------------------------------------------- isexe
|
|
30
|
+
// windows flavor: a file is executable if its extension is in PATHEXT
|
|
31
|
+
function checkPathExt(file, options) {
|
|
32
|
+
let pathext = options.pathExt !== undefined ? options.pathExt : process.env.PATHEXT;
|
|
33
|
+
if (!pathext) return true;
|
|
34
|
+
pathext = pathext.split(";");
|
|
35
|
+
if (pathext.indexOf("") !== -1) return true;
|
|
36
|
+
for (let i = 0; i < pathext.length; i++) {
|
|
37
|
+
const p = pathext[i].toLowerCase();
|
|
38
|
+
if (p && file.substr(-p.length).toLowerCase() === p) return true;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isexeWindows(file, options) {
|
|
44
|
+
const stat = fs.statSync(file);
|
|
45
|
+
if (!stat.isSymbolicLink() && !stat.isFile()) return false;
|
|
46
|
+
return checkPathExt(file, options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isexePosix(file) {
|
|
50
|
+
const stat = fs.statSync(file);
|
|
51
|
+
if (!stat.isFile()) return false;
|
|
52
|
+
const mod = stat.mode;
|
|
53
|
+
const uid = stat.uid;
|
|
54
|
+
const gid = stat.gid;
|
|
55
|
+
const myUid = process.getuid && process.getuid();
|
|
56
|
+
const myGid = process.getgid && process.getgid();
|
|
57
|
+
const u = parseInt("100", 8);
|
|
58
|
+
const g = parseInt("010", 8);
|
|
59
|
+
const o = parseInt("001", 8);
|
|
60
|
+
const ug = u | g;
|
|
61
|
+
return (mod & o) || (mod & g && gid === myGid) || (mod & u && uid === myUid) || (mod & ug && myUid === 0);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isexeSync(file, options) {
|
|
65
|
+
return isWindowsEnv ? isexeWindows(file, options) : isexePosix(file);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ------------------------------------------------------------------- which
|
|
69
|
+
function getPathInfo(cmd, opt) {
|
|
70
|
+
const colon = opt.colon || (isWindowsEnv ? ";" : ":");
|
|
71
|
+
|
|
72
|
+
// if cmd has a slash, don't search PATH — check the file itself
|
|
73
|
+
const pathEnv = cmd.match(/\//) || (isWindowsEnv && cmd.match(/\\/))
|
|
74
|
+
? [""]
|
|
75
|
+
: [
|
|
76
|
+
// windows always checks the cwd first
|
|
77
|
+
...(isWindowsEnv ? [process.cwd()] : []),
|
|
78
|
+
...(opt.path || process.env.PATH || "").split(colon),
|
|
79
|
+
];
|
|
80
|
+
const pathExtExe = isWindowsEnv ? opt.pathExt || process.env.PATHEXT || ".EXE;.CMD;.BAT;.COM" : "";
|
|
81
|
+
const pathExt = isWindowsEnv ? pathExtExe.split(colon) : [""];
|
|
82
|
+
|
|
83
|
+
if (isWindowsEnv) {
|
|
84
|
+
if (cmd.indexOf(".") !== -1 && pathExt[0] !== "") pathExt.unshift("");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return { pathEnv, pathExt, pathExtExe };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function whichSync(cmd, opt) {
|
|
91
|
+
opt = opt || {};
|
|
92
|
+
const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
|
|
93
|
+
|
|
94
|
+
for (let i = 0; i < pathEnv.length; i++) {
|
|
95
|
+
const ppRaw = pathEnv[i];
|
|
96
|
+
const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
|
|
97
|
+
|
|
98
|
+
const pCmd = path.join(pathPart, cmd);
|
|
99
|
+
const p = !pathPart && /^\.[\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd : pCmd;
|
|
100
|
+
|
|
101
|
+
for (let j = 0; j < pathExt.length; j++) {
|
|
102
|
+
const cur = p + pathExt[j];
|
|
103
|
+
try {
|
|
104
|
+
if (isexeSync(cur, { pathExt: pathExtExe })) return cur;
|
|
105
|
+
} catch (ex) { /* Empty */ }
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (opt.nothrow) return null;
|
|
110
|
+
throw Object.assign(new Error("not found: " + cmd), { code: "ENOENT" });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// -------------------------------------------------- cross-spawn/lib/util/*
|
|
114
|
+
function resolveCommandAttempt(parsed, withoutPathExt) {
|
|
115
|
+
const env = parsed.options.env || process.env;
|
|
116
|
+
const cwd = process.cwd();
|
|
117
|
+
const hasCustomCwd = parsed.options.cwd != null;
|
|
118
|
+
// worker threads do not have process.chdir()
|
|
119
|
+
const shouldSwitchCwd = hasCustomCwd && process.chdir !== undefined && !process.chdir.disabled;
|
|
120
|
+
|
|
121
|
+
// `which` stats against the process cwd, so temporarily switch to the custom one
|
|
122
|
+
if (shouldSwitchCwd) {
|
|
123
|
+
try {
|
|
124
|
+
process.chdir(parsed.options.cwd);
|
|
125
|
+
} catch (err) { /* Empty */ }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let resolved;
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
resolved = whichSync(parsed.command, {
|
|
132
|
+
path: env[pathKey({ env })],
|
|
133
|
+
pathExt: withoutPathExt ? path.delimiter : undefined,
|
|
134
|
+
});
|
|
135
|
+
} catch (e) { /* Empty */ } finally {
|
|
136
|
+
if (shouldSwitchCwd) process.chdir(cwd);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ensure an absolute path is returned (resolved against the custom cwd if any)
|
|
140
|
+
if (resolved) resolved = path.resolve(hasCustomCwd ? parsed.options.cwd : "", resolved);
|
|
141
|
+
|
|
142
|
+
return resolved;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function resolveCommand(parsed) {
|
|
146
|
+
return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// shebang-command: "#!/usr/bin/env node" → "node", "#!/usr/bin/node -x" → "node -x"
|
|
150
|
+
function shebangCommand(string = "") {
|
|
151
|
+
const match = string.match(/^#!(.*)/);
|
|
152
|
+
if (!match) return null;
|
|
153
|
+
|
|
154
|
+
const [shebangPath, argument] = match[0].replace(/#! ?/, "").split(" ");
|
|
155
|
+
const binary = shebangPath.split("/").pop();
|
|
156
|
+
|
|
157
|
+
if (binary === "env") return argument;
|
|
158
|
+
return argument ? `${binary} ${argument}` : binary;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function readShebang(command) {
|
|
162
|
+
// read the first 150 bytes of the file
|
|
163
|
+
const size = 150;
|
|
164
|
+
const buffer = Buffer.alloc(size);
|
|
165
|
+
|
|
166
|
+
let fd;
|
|
167
|
+
try {
|
|
168
|
+
fd = fs.openSync(command, "r");
|
|
169
|
+
fs.readSync(fd, buffer, 0, size, 0);
|
|
170
|
+
fs.closeSync(fd);
|
|
171
|
+
} catch (e) { /* Empty */ }
|
|
172
|
+
|
|
173
|
+
return shebangCommand(buffer.toString());
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// see http://www.robvanderwoude.com/escapechars.php
|
|
177
|
+
const metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
|
|
178
|
+
|
|
179
|
+
function escapeCommand(arg) {
|
|
180
|
+
return arg.replace(metaCharsRegExp, "^$1");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function escapeArgument(arg, doubleEscapeMetaChars) {
|
|
184
|
+
arg = `${arg}`;
|
|
185
|
+
|
|
186
|
+
// algorithm based on https://qntm.org/cmd, altered to avoid regex backtracking
|
|
187
|
+
// sequence of backslashes followed by a double quote: double the backslashes, escape the quote
|
|
188
|
+
arg = arg.replace(/(?=(\\+?)?)\1"/g, '$1$1\\"');
|
|
189
|
+
// sequence of backslashes at the end (a double quote follows once wrapped): double them
|
|
190
|
+
arg = arg.replace(/(?=(\\+?)?)\1$/, "$1$1");
|
|
191
|
+
|
|
192
|
+
// quote the whole thing, then escape cmd.exe meta chars
|
|
193
|
+
arg = `"${arg}"`;
|
|
194
|
+
arg = arg.replace(metaCharsRegExp, "^$1");
|
|
195
|
+
|
|
196
|
+
// cmd-shims are re-parsed by a second cmd.exe, so escape twice
|
|
197
|
+
if (doubleEscapeMetaChars) arg = arg.replace(metaCharsRegExp, "^$1");
|
|
198
|
+
|
|
199
|
+
return arg;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// --------------------------------------------------- cross-spawn/lib/parse
|
|
203
|
+
const isExecutableRegExp = /\.(?:com|exe)$/i;
|
|
204
|
+
const isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
|
|
205
|
+
|
|
206
|
+
function detectShebang(parsed) {
|
|
207
|
+
parsed.file = resolveCommand(parsed);
|
|
208
|
+
|
|
209
|
+
const shebang = parsed.file && readShebang(parsed.file);
|
|
210
|
+
|
|
211
|
+
if (shebang) {
|
|
212
|
+
parsed.args.unshift(parsed.file);
|
|
213
|
+
parsed.command = shebang;
|
|
214
|
+
return resolveCommand(parsed);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return parsed.file;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function parseNonShell(parsed) {
|
|
221
|
+
if (!isWin) return parsed;
|
|
222
|
+
|
|
223
|
+
const commandFile = detectShebang(parsed);
|
|
224
|
+
|
|
225
|
+
// no shell needed if the command resolved to a real executable (.com/.exe)
|
|
226
|
+
const needsShell = !isExecutableRegExp.test(commandFile);
|
|
227
|
+
|
|
228
|
+
if (parsed.options.forceShell || needsShell) {
|
|
229
|
+
// cmd-shims in node_modules/.bin invoke a nested cmd.exe, requiring double escaping
|
|
230
|
+
const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
|
|
231
|
+
|
|
232
|
+
// normalize posix paths (foo/bar → foo\bar) or cmd.exe fails with ENOENT
|
|
233
|
+
parsed.command = path.normalize(parsed.command);
|
|
234
|
+
|
|
235
|
+
parsed.command = escapeCommand(parsed.command);
|
|
236
|
+
parsed.args = parsed.args.map(arg => escapeArgument(arg, needsDoubleEscapeMetaChars));
|
|
237
|
+
|
|
238
|
+
const shellCommand = [parsed.command].concat(parsed.args).join(" ");
|
|
239
|
+
|
|
240
|
+
parsed.args = ["/d", "/s", "/c", `"${shellCommand}"`];
|
|
241
|
+
parsed.command = process.env.comspec || "cmd.exe";
|
|
242
|
+
parsed.options.windowsVerbatimArguments = true; // args are already escaped
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return parsed;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function parse(command, args, options) {
|
|
249
|
+
// normalize arguments, similar to nodejs
|
|
250
|
+
if (args && !Array.isArray(args)) {
|
|
251
|
+
options = args;
|
|
252
|
+
args = null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
args = args ? args.slice(0) : [];
|
|
256
|
+
options = Object.assign({}, options);
|
|
257
|
+
|
|
258
|
+
const parsed = {
|
|
259
|
+
command,
|
|
260
|
+
args,
|
|
261
|
+
options,
|
|
262
|
+
file: undefined,
|
|
263
|
+
original: { command, args },
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
return options.shell ? parsed : parseNonShell(parsed);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// -------------------------------------------------- cross-spawn/lib/enoent
|
|
270
|
+
function notFoundError(original, syscall) {
|
|
271
|
+
return Object.assign(new Error(`${syscall} ${original.command} ENOENT`), {
|
|
272
|
+
code: "ENOENT",
|
|
273
|
+
errno: "ENOENT",
|
|
274
|
+
syscall: `${syscall} ${original.command}`,
|
|
275
|
+
path: original.command,
|
|
276
|
+
spawnargs: original.args,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// on windows a missing command exits cmd.exe with status 1 instead of erroring;
|
|
281
|
+
// convert that into the ENOENT error posix would have produced
|
|
282
|
+
function verifyENOENT(status, parsed, syscall) {
|
|
283
|
+
if (isWin && status === 1 && !parsed.file) return notFoundError(parsed.original, syscall);
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function hookChildProcess(child, parsed) {
|
|
288
|
+
if (!isWin) return;
|
|
289
|
+
|
|
290
|
+
const originalEmit = child.emit;
|
|
291
|
+
child.emit = function (name, arg1) {
|
|
292
|
+
if (name === "exit") {
|
|
293
|
+
const err = verifyENOENT(arg1, parsed, "spawn");
|
|
294
|
+
if (err) return originalEmit.call(child, "error", err);
|
|
295
|
+
}
|
|
296
|
+
return originalEmit.apply(child, arguments);
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// ------------------------------------------------------- cross-spawn/index
|
|
301
|
+
function spawn(command, args, options) {
|
|
302
|
+
const parsed = parse(command, args, options);
|
|
303
|
+
const spawned = cp.spawn(parsed.command, parsed.args, parsed.options);
|
|
304
|
+
hookChildProcess(spawned, parsed);
|
|
305
|
+
return spawned;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function spawnSync(command, args, options) {
|
|
309
|
+
const parsed = parse(command, args, options);
|
|
310
|
+
const result = cp.spawnSync(parsed.command, parsed.args, parsed.options);
|
|
311
|
+
result.error = result.error || verifyENOENT(result.status, parsed, "spawnSync");
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
module.exports = spawn;
|
|
316
|
+
module.exports.spawn = spawn;
|
|
317
|
+
module.exports.sync = spawnSync;
|
|
318
|
+
module.exports._parse = parse;
|
package/lib/getImports.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
const
|
|
1
|
+
const acorn = require("acorn");
|
|
2
2
|
const { isFullArr } = require("./helpers");
|
|
3
3
|
// FYI: it ignores the comments by default beacuse it's based on lexial environment not based on regex
|
|
4
4
|
function getImports(str) {
|
|
5
5
|
let finalImports = [];
|
|
6
|
-
let arr =
|
|
6
|
+
let arr = acorn.parse(str, { ecmaVersion: "latest", sourceType: "module" }).body;
|
|
7
7
|
arr.forEach((obj) => {
|
|
8
8
|
if (obj.type === "ImportDeclaration") {
|
|
9
9
|
if (obj.specifiers) {
|
package/lib/helpers.js
CHANGED
|
@@ -16,7 +16,7 @@ const hasForwardSlash = (paths) => paths.find((item) => /\//g.test(item));
|
|
|
16
16
|
const hasSlash = (paths) => paths.find((item) => /\//g.test(item) || /\\/g.test(item));
|
|
17
17
|
const stringToBase64 = (str) => Buffer.from(str).toString('base64');
|
|
18
18
|
const base64ToString = (base64) => Buffer.from(base64, 'base64').toString('ascii');
|
|
19
|
-
const
|
|
19
|
+
const acorn = require("acorn");
|
|
20
20
|
|
|
21
21
|
function isUpper(char) {
|
|
22
22
|
const upperChar = char.toUpperCase();
|
|
@@ -372,8 +372,8 @@ function createScopeHandler({ stateProps, hasState, propsTobeExcluded, jsAttrs:
|
|
|
372
372
|
|
|
373
373
|
// get methods names
|
|
374
374
|
function getGlobalMethods(str) {
|
|
375
|
-
return
|
|
376
|
-
.parse(str, { sourceType: "module" })
|
|
375
|
+
return acorn
|
|
376
|
+
.parse(str, { ecmaVersion: "latest", sourceType: "module" })
|
|
377
377
|
.body.map((obj) => {
|
|
378
378
|
if (obj.type === "FunctionDeclaration") return obj.id.name;
|
|
379
379
|
if (
|
|
@@ -395,8 +395,8 @@ function getGlobalMethods(str) {
|
|
|
395
395
|
|
|
396
396
|
// get props names
|
|
397
397
|
function getGlobalProps(str) {
|
|
398
|
-
return
|
|
399
|
-
.parse(str, { sourceType: "module" })
|
|
398
|
+
return acorn
|
|
399
|
+
.parse(str, { ecmaVersion: "latest", sourceType: "module" })
|
|
400
400
|
.body.map((obj) => {
|
|
401
401
|
if (
|
|
402
402
|
obj.type === "VariableDeclaration" &&
|
|
@@ -663,7 +663,6 @@ ${routes
|
|
|
663
663
|
];`;
|
|
664
664
|
}
|
|
665
665
|
|
|
666
|
-
// todo: move it to olum-router and bind it from there to window
|
|
667
666
|
const routerParamsParser = `<script>
|
|
668
667
|
/**
|
|
669
668
|
* Extract route params from a file path and pathname.
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* magic-string.js — standalone single-file source map generator
|
|
3
|
+
* Minimal replacement for the `magic-string` package (v0.30.21), using only
|
|
4
|
+
* node built-ins. This project (lib/sourcemap.js) only ever builds an
|
|
5
|
+
* IDENTITY map — `new MagicString(str)` with zero edits, then
|
|
6
|
+
* `generateMap({ source, file, includeContent })` — so that surface is ported
|
|
7
|
+
* faithfully (byte-identical output, including the VLQ `mappings`, JSON key
|
|
8
|
+
* order and `toString()`/`toUrl()`):
|
|
9
|
+
*
|
|
10
|
+
* const MagicString = require("./magic-string");
|
|
11
|
+
* const map = new MagicString(code).generateMap({ source, file, includeContent, hires });
|
|
12
|
+
* map.mappings / map.toString() / map.toUrl()
|
|
13
|
+
*
|
|
14
|
+
* Supported: generateMap / generateDecodedMap with options
|
|
15
|
+
* `source`, `file`, `includeContent`, `hires` (true | "boundary" | falsy).
|
|
16
|
+
*
|
|
17
|
+
* KNOWN LIMITATIONS vs the real magic-string (future maintenance):
|
|
18
|
+
* The string EDITING api (overwrite, update, appendLeft/Right, prepend,
|
|
19
|
+
* append, remove, move, indent, trim, slice, clone, addSourcemapLocation,
|
|
20
|
+
* Bundle, ...) is NOT implemented — those methods throw with a clear message
|
|
21
|
+
* instead of failing silently. If the compiler ever needs to transform code
|
|
22
|
+
* while tracking mappings (e.g. the sourcemap.js TODO about mapping html
|
|
23
|
+
* above the script tag), either extend this file's char-walk in
|
|
24
|
+
* generateDecodedMap to consume an edit list, or reinstall the real package.
|
|
25
|
+
*/
|
|
26
|
+
"use strict";
|
|
27
|
+
|
|
28
|
+
// ------------------------------------------------ base64 VLQ (sourcemap-codec)
|
|
29
|
+
const BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
30
|
+
|
|
31
|
+
function encodeVlq(num) {
|
|
32
|
+
num = num < 0 ? (-num << 1) | 1 : num << 1;
|
|
33
|
+
let out = "";
|
|
34
|
+
do {
|
|
35
|
+
let digit = num & 31;
|
|
36
|
+
num >>>= 5;
|
|
37
|
+
if (num > 0) digit |= 32; // continuation bit
|
|
38
|
+
out += BASE64[digit];
|
|
39
|
+
} while (num > 0);
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// encode decoded mappings ([[ [genCol, srcIdx, srcLine, srcCol, name?], ...], ...])
|
|
44
|
+
// into the standard delta-encoded VLQ string: fields are relative to the
|
|
45
|
+
// previous segment; generated column resets per line, the rest carry over
|
|
46
|
+
function encodeMappings(decoded) {
|
|
47
|
+
let sourceIndex = 0;
|
|
48
|
+
let sourceLine = 0;
|
|
49
|
+
let sourceColumn = 0;
|
|
50
|
+
let nameIndex = 0;
|
|
51
|
+
|
|
52
|
+
return decoded
|
|
53
|
+
.map(line => {
|
|
54
|
+
let genColumn = 0;
|
|
55
|
+
return line
|
|
56
|
+
.map(segment => {
|
|
57
|
+
let out = encodeVlq(segment[0] - genColumn);
|
|
58
|
+
genColumn = segment[0];
|
|
59
|
+
if (segment.length > 1) {
|
|
60
|
+
out += encodeVlq(segment[1] - sourceIndex) + encodeVlq(segment[2] - sourceLine) + encodeVlq(segment[3] - sourceColumn);
|
|
61
|
+
sourceIndex = segment[1];
|
|
62
|
+
sourceLine = segment[2];
|
|
63
|
+
sourceColumn = segment[3];
|
|
64
|
+
}
|
|
65
|
+
if (segment.length === 5) {
|
|
66
|
+
out += encodeVlq(segment[4] - nameIndex);
|
|
67
|
+
nameIndex = segment[4];
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
})
|
|
71
|
+
.join(",");
|
|
72
|
+
})
|
|
73
|
+
.join(";");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// path of `to` relative to `from`'s directory (magic-string's own helper)
|
|
77
|
+
function getRelativePath(from, to) {
|
|
78
|
+
const fromParts = from.split(/[/\\]/);
|
|
79
|
+
const toParts = to.split(/[/\\]/);
|
|
80
|
+
|
|
81
|
+
fromParts.pop(); // get dirname
|
|
82
|
+
|
|
83
|
+
while (fromParts[0] === toParts[0]) {
|
|
84
|
+
fromParts.shift();
|
|
85
|
+
toParts.shift();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (fromParts.length) {
|
|
89
|
+
let i = fromParts.length;
|
|
90
|
+
while (i--) fromParts[i] = "..";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return fromParts.concat(toParts).join("/");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// --------------------------------------------------------------- SourceMap
|
|
97
|
+
class SourceMap {
|
|
98
|
+
constructor(properties) {
|
|
99
|
+
this.version = 3;
|
|
100
|
+
this.file = properties.file;
|
|
101
|
+
this.sources = properties.sources;
|
|
102
|
+
this.sourcesContent = properties.sourcesContent;
|
|
103
|
+
this.names = properties.names;
|
|
104
|
+
this.mappings = encodeMappings(properties.mappings);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
toString() {
|
|
108
|
+
return JSON.stringify(this); // undefined keys (file, sourcesContent) drop out
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
toUrl() {
|
|
112
|
+
return "data:application/json;charset=utf-8;base64," + Buffer.from(this.toString(), "utf-8").toString("base64");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// -------------------------------------------------------------- MagicString
|
|
117
|
+
const wordRegex = /\w/;
|
|
118
|
+
|
|
119
|
+
class MagicString {
|
|
120
|
+
constructor(string, options = {}) {
|
|
121
|
+
this.original = string;
|
|
122
|
+
this.filename = options.filename;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
toString() {
|
|
126
|
+
return this.original;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
get length() {
|
|
130
|
+
return this.original.length;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// identity mapping: walk the original char by char, mirroring magic-string's
|
|
134
|
+
// Mappings.addUneditedChunk — a segment at the first char of every line
|
|
135
|
+
// (every char for hires: true, word boundaries for hires: "boundary");
|
|
136
|
+
// lines with no characters get no segment
|
|
137
|
+
generateDecodedMap(options = {}) {
|
|
138
|
+
const hires = options.hires;
|
|
139
|
+
const mappings = [];
|
|
140
|
+
let line = [];
|
|
141
|
+
let genColumn = 0;
|
|
142
|
+
let sourceLine = 0;
|
|
143
|
+
let sourceColumn = 0;
|
|
144
|
+
let first = true;
|
|
145
|
+
let inBoundary = false;
|
|
146
|
+
|
|
147
|
+
for (let i = 0; i < this.original.length; i++) {
|
|
148
|
+
if (this.original[i] === "\n") {
|
|
149
|
+
mappings.push(line);
|
|
150
|
+
line = [];
|
|
151
|
+
genColumn = 0;
|
|
152
|
+
sourceLine += 1;
|
|
153
|
+
sourceColumn = 0;
|
|
154
|
+
first = true;
|
|
155
|
+
inBoundary = false;
|
|
156
|
+
} else {
|
|
157
|
+
if (hires || first) {
|
|
158
|
+
const segment = [genColumn, 0, sourceLine, sourceColumn];
|
|
159
|
+
if (hires === "boundary") {
|
|
160
|
+
// one segment per word, plus one for every non-word char
|
|
161
|
+
if (wordRegex.test(this.original[i])) {
|
|
162
|
+
if (!inBoundary) {
|
|
163
|
+
line.push(segment);
|
|
164
|
+
inBoundary = true;
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
line.push(segment);
|
|
168
|
+
inBoundary = false;
|
|
169
|
+
}
|
|
170
|
+
} else {
|
|
171
|
+
line.push(segment);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
sourceColumn += 1;
|
|
175
|
+
genColumn += 1;
|
|
176
|
+
first = false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
mappings.push(line);
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
|
|
183
|
+
sources: [options.source ? getRelativePath(options.file || "", options.source) : options.file || ""],
|
|
184
|
+
sourcesContent: options.includeContent ? [this.original] : undefined,
|
|
185
|
+
names: [],
|
|
186
|
+
mappings,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
generateMap(options) {
|
|
191
|
+
return new SourceMap(this.generateDecodedMap(options));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// editing api is intentionally absent — fail loudly, not silently
|
|
196
|
+
for (const method of [
|
|
197
|
+
"addSourcemapLocation", "append", "appendLeft", "appendRight", "clone", "indent",
|
|
198
|
+
"insertLeft", "insertRight", "move", "overwrite", "prepend", "prependLeft",
|
|
199
|
+
"prependRight", "remove", "replace", "replaceAll", "reset", "slice", "snip",
|
|
200
|
+
"trim", "trimEnd", "trimStart", "trimLines", "update",
|
|
201
|
+
]) {
|
|
202
|
+
MagicString.prototype[method] = function () {
|
|
203
|
+
throw new Error(
|
|
204
|
+
`MagicString#${method}() is not implemented in the standalone lib/magic-string.js — ` +
|
|
205
|
+
"only identity source map generation (generateMap) is supported. " +
|
|
206
|
+
"See the header comment for how to extend it, or reinstall the real magic-string package."
|
|
207
|
+
);
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
module.exports = MagicString;
|
|
212
|
+
module.exports.default = MagicString;
|
|
213
|
+
module.exports.SourceMap = SourceMap;
|
package/lib/sourcemap.js
CHANGED