-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrawscript-sw.js
More file actions
95 lines (93 loc) · 3.11 KB
/
rawscript-sw.js
File metadata and controls
95 lines (93 loc) · 3.11 KB
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
// src/transpiler.ts
import * as esbuild from "https://unpkg.com/esbuild-wasm@0.20.2/esm/browser.js";
var initPromise = null;
async function ensureInitialized() {
if (!initPromise) {
initPromise = esbuild.initialize({
wasmURL: "https://unpkg.com/esbuild-wasm@0.20.2/esbuild.wasm",
worker: false
});
}
await initPromise;
}
async function transpile(source, filename) {
await ensureInitialized();
const loader = filename.endsWith(".tsx") ? "tsx" : "ts";
const result = await esbuild.transform(source, {
loader,
format: "esm",
target: "esnext",
sourcefile: filename,
sourcemap: "inline"
});
return result.code;
}
// src/resolver.ts
var IMPORT_RE = /\bfrom\s+(['"])((?:@[^/'"]+\/[^'"]+|[^.'"/][^'"]*))\1|import\s*\(\s*(['"])((?:@[^/'"]+\/[^'"]+|[^.'"/][^'"]*))\3\s*\)|import\s+(['"])((?:@[^/'"]+\/[^'"]+|[^.'"/][^'"]*))\5/g;
var ESM_SH = "https://esm.sh/";
function isBareSpecifier(specifier) {
if (specifier.startsWith("https://") || specifier.startsWith("http://") || specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/")) {
return false;
}
return true;
}
function neutralizeLineComments(js) {
return js.replace(/\/\/.*$/gm, (match) => " ".repeat(match.length));
}
function rewriteImports(js, importmap) {
const map = importmap ?? {};
const cleaned = neutralizeLineComments(js);
const replacements = [];
let match;
IMPORT_RE.lastIndex = 0;
while ((match = IMPORT_RE.exec(cleaned)) !== null) {
const specifier = match[2] ?? match[4] ?? match[6];
const quote = match[1] ?? match[3] ?? match[5];
if (!specifier)
continue;
if (!isBareSpecifier(specifier))
continue;
if (specifier in map)
continue;
const rewritten = ESM_SH + specifier;
const fullMatch = match[0];
const specStart = fullMatch.indexOf(quote + specifier + quote);
if (specStart === -1)
continue;
const absStart = match.index + specStart + 1;
const absEnd = absStart + specifier.length;
replacements.push({ start: absStart, end: absEnd, replacement: rewritten });
}
let result = js;
for (let i = replacements.length - 1; i >= 0; i--) {
const r = replacements[i];
result = result.slice(0, r.start) + r.replacement + result.slice(r.end);
}
return result;
}
// src/sw.ts
var knownImportmap = {};
self.addEventListener("message", (event) => {
if (event.data?.type === "IMPORTMAP") {
knownImportmap = event.data.importmap ?? {};
}
});
async function handleFetch(event) {
const url = new URL(event.request.url);
if (event.request.method === "GET" && (url.pathname.endsWith(".ts") || url.pathname.endsWith(".tsx"))) {
const response = await fetch(event.request);
if (!response.ok) {
return response;
}
const source = await response.text();
const js = await transpile(source, url.pathname);
const rewritten = rewriteImports(js, knownImportmap);
return new Response(rewritten, {
headers: { "Content-Type": "application/javascript; charset=utf-8" }
});
}
return fetch(event.request);
}
self.addEventListener("fetch", (event) => {
event.respondWith(handleFetch(event));
});