-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathtransformer.ts
285 lines (274 loc) · 9.43 KB
/
transformer.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { TransformError } from "../framework/core/error.ts";
import { isLikelyHttpURL, splitBy, trimPrefix } from "../shared/util.ts";
import type { TransformResult } from "./deps.ts";
import { btoa, parseDeps, transform } from "./deps.ts";
import depGraph from "./graph.ts";
import {
builtinModuleExts,
fetchCode,
getAlephConfig,
getAlephPkgUri,
getDeploymentId,
MagicString,
regFullVersion,
regJsxFile,
restoreUrl,
toLocalPath,
} from "./helpers.ts";
import log from "./log.ts";
import { getContentType } from "./media_type.ts";
import { isRouteModule } from "./router.ts";
import { bundleCSS } from "./build.ts";
import type { ImportMap, JSXConfig, ModuleLoader, ModuleLoaderOutput } from "./types.ts";
const cache = new Map<string, [content: string, headers: Headers]>();
export type TransformerOptions = {
importMap: ImportMap;
jsxConfig: JSXConfig;
loader?: ModuleLoader;
isDev?: boolean;
};
export default {
test: (pathname: string) => {
const ext = splitBy(pathname, ".", true)[1].toLowerCase();
return (
// remote module
pathname.startsWith("/-/") ||
// css
ext === "css" ||
// builtin module
(builtinModuleExts.includes(ext) && !pathname.endsWith(".d.ts"))
);
},
fetch: async (req: Request, options: TransformerOptions): Promise<Response> => {
const { loader, jsxConfig, importMap, isDev } = options;
const { pathname, searchParams, search } = new URL(req.url);
const specifier = pathname.startsWith("/-/") ? restoreUrl(pathname + search) : `.${pathname}`;
const isRemote = isLikelyHttpURL(specifier);
const ssr = searchParams.has("ssr");
const target = isDev ? "es2022" : "es2018"; // todo: get target from user-agent header
const deployId = getDeploymentId();
const etag = deployId ? `W/${deployId}` : null;
if (etag && req.headers.get("If-None-Match") === etag) {
return new Response(null, { status: 304 });
}
const [sourceRaw, sourceContentType] = await fetchCode(specifier, target);
const alephPkgUri = getAlephPkgUri();
const config = getAlephConfig();
let source = sourceRaw;
let lang: ModuleLoaderOutput["lang"];
let inlineCSS: string | undefined;
let isCSS = false;
if (loader) {
const loaded = await loader.load(
specifier,
sourceRaw,
ssr
? { jsxConfig, importMap, ssr: true, sourceMap: true }
: { jsxConfig, importMap, isDev, spaMode: !config?.ssr, sourceMap: isDev },
);
source = loaded.code;
lang = loaded.lang;
inlineCSS = loaded.inlineCSS;
} else {
isCSS = sourceContentType.startsWith("text/css");
}
// transform module for SSR
if (ssr) {
let contentType = sourceContentType;
if (lang) {
contentType = getContentType(`file.${lang}`);
}
const deps = await parseDeps(specifier, source, {
importMap: JSON.stringify(importMap),
lang,
});
depGraph.mark(specifier, { deps, inlineCSS });
if (deps.length) {
const s = new MagicString(source);
deps.forEach((dep) => {
const { specifier: depSpecifier, importUrl, loc } = dep;
if (!loc) {
return;
}
if (!isLikelyHttpURL(depSpecifier)) {
const sep = importUrl.includes("?") ? "&" : "?";
const version = depGraph.get(depSpecifier)?.version ?? depGraph.globalVersion;
const url = `"${importUrl}${sep}ssr&v=${version.toString(36)}"`;
s.overwrite(loc.start - 1, loc.end - 1, url);
}
});
return new Response(s.toBytes(), {
headers: [["Content-Type", contentType]],
});
}
return new Response(source, { headers: [["Content-Type", contentType]] });
}
// check cache
const cacheKey = pathname + search;
if (!isDev && cache.has(cacheKey)) {
const [content, cachedHeaders] = cache.get(cacheKey)!;
const headers = new Headers(cachedHeaders);
headers.set("Cache-Hit", "true");
return new Response(content, { headers });
}
let resBody = "";
let resType = "application/javascript";
try {
if (isCSS) {
const asJsModule = searchParams.has("module");
const { code, deps } = await bundleCSS(specifier, source, {
// todo: use target from user-agent header
targets: {
android: 95,
chrome: 95,
edge: 95,
firefox: 90,
safari: 14,
},
minify: !isDev,
cssModules: asJsModule && pathname.endsWith(".module.css"),
asJsModule,
hmr: isDev,
});
depGraph.mark(specifier, {
deps: deps?.map((specifier) => ({ specifier })),
});
resBody = code;
if (!asJsModule) {
resType = "text/css";
}
} else {
let code: string;
let map: string | undefined;
let deps: TransformResult["deps"];
let hasInlineCSS = false;
if (
(isRemote && !specifier.startsWith("https://aleph/")) &&
(
/^https?:\/\/(cdn\.)?esm\.sh\//i.test(specifier) ||
/^(text|application)\/javascript/i.test(sourceContentType)
)
) {
// don't transform js modules imported from remote CDN
deps = await parseDeps(specifier, source, {
importMap: JSON.stringify(importMap),
lang: "js",
});
if (deps.length > 0) {
const s = new MagicString(source);
deps.forEach((dep) => {
const { importUrl, loc } = dep;
if (loc) {
s.overwrite(
loc.start - 1,
loc.end - 1,
`"${toLocalPath(importUrl)}"`,
);
}
});
code = s.toString();
} else {
code = source;
}
} else {
const deploymentId = getDeploymentId();
const graphVersions = deploymentId ? {} : Object.fromEntries(
depGraph.modules.filter((mod) => (
!isRemote &&
!isLikelyHttpURL(mod.specifier) &&
mod.specifier !== specifier
)).map(({ specifier, version }) => [specifier, version.toString(36)]),
);
const ret = await transform(specifier, source, {
...jsxConfig,
alephPkgUri,
target,
lang,
importMap: JSON.stringify(importMap),
graphVersions,
globalVersion: deploymentId ?? depGraph.globalVersion.toString(36),
resolveRemoteModule: true,
stripDataExport: isRouteModule(specifier),
sourceMap: isDev,
minify: isDev ? undefined : { compress: true },
reactRefresh: isDev && Boolean(Deno.env.get("REACT_REFRESH")),
isDev,
});
code = ret.code;
map = ret.map;
deps = ret.deps;
}
const styleTs = `${alephPkgUri}/framework/core/style.ts`;
// embed module unocss css in dev mode
if (isDev && config?.atomicCSS) {
const re = config.atomicCSS.test ?? regJsxFile;
if (re.test(pathname)) {
try {
const { css, matched } = await config.atomicCSS.generate(source, {
id: specifier,
preflights: false,
minify: !isDev,
});
if (matched.size > 0) {
code += `\nimport { applyUnoCSS as __applyUnoCSS } from "${toLocalPath(styleTs)}";\n__applyUnoCSS(${
JSON.stringify(specifier)
}, ${JSON.stringify(css)});\n`;
hasInlineCSS = true;
}
} catch (e) {
log.warn("[UnoCSS]", e);
}
}
}
if (inlineCSS) {
code += `\nimport { applyCSS as __applyCSS } from "${toLocalPath(styleTs)}";\n__applyCSS(${
JSON.stringify(specifier)
}, ${JSON.stringify(inlineCSS)});\n`;
hasInlineCSS = true;
}
if (hasInlineCSS) {
deps = [...(deps || []), { specifier: styleTs }] as typeof deps;
}
depGraph.mark(specifier, { deps });
if (map) {
try {
const m = JSON.parse(map);
if (!isRemote) {
m.sources = [`file://source${trimPrefix(specifier, ".")}`];
}
// todo: merge loader map
m.sourcesContent = [source];
resBody = code +
`\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(JSON.stringify(m))}\n`;
} catch (e) {
log.debug(`[dev] Failed to add source map for '${specifier}'`, e);
resBody = code;
}
} else {
resBody = code;
}
}
} catch (error) {
if (error.message === "unreachable") {
resBody = source;
resType = sourceContentType;
} else {
throw new TransformError(specifier, source, error.message, error.stack);
}
}
const headers = new Headers([["Content-Type", `${resType}; charset=utf-8`]]);
if (etag) {
headers.set("ETag", etag);
}
if (
searchParams.get("v") ||
(pathname.startsWith("/-/") && regFullVersion.test(pathname))
) {
headers.append("Cache-Control", "public, max-age=31536000, immutable");
}
if (!isDev) {
cache.set(cacheKey, [resBody, headers]);
}
return new Response(resBody, { headers });
},
};