Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 7fdd066

Browse files
committed
Improve transformer
1 parent 9b031af commit 7fdd066

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

server/transformer.ts

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createGenerator } from "https://esm.sh/@unocss/[email protected]";
2-
import { transform } from "../compiler/mod.ts";
3-
import type { TransformOptions } from "../compiler/types.ts";
2+
import MagicString from "https://esm.sh/[email protected]";
3+
import { parseDeps, transform } from "../compiler/mod.ts";
4+
import type { TransformOptions, TransformResult } from "../compiler/types.ts";
45
import { readCode } from "../lib/fs.ts";
56
import { restoreUrl, toLocalPath } from "../lib/helpers.ts";
67
import log from "../lib/log.ts";
@@ -30,17 +31,17 @@ export default {
3031
let mtime: number | undefined;
3132
let lang: string | undefined;
3233
let isCSS: boolean;
33-
let enableAtomicCSS: boolean;
34+
let useUno: boolean;
3435
if (loaded) {
3536
rawCode = loaded.code;
3637
mtime = loaded.modtime;
3738
lang = loaded.lang;
3839
isCSS = loaded.lang === "css";
39-
enableAtomicCSS = !!loaded.atomicCSS;
40+
useUno = !!loaded.atomicCSS;
4041
} else {
4142
let ctype: string;
4243
[rawCode, mtime, ctype] = await readCode(specifier);
43-
enableAtomicCSS = pathname.endsWith(".jsx") || pathname.endsWith(".tsx");
44+
useUno = pathname.endsWith(".jsx") || pathname.endsWith(".tsx");
4445
isCSS = ctype.startsWith("text/css") || ctype.startsWith("text/postcss");
4546
}
4647
const etag = mtime
@@ -84,27 +85,46 @@ export default {
8485
resType = "text/css";
8586
}
8687
} else {
87-
const { atomicCSS, jsxConfig, importMap, buildTarget } = options;
8888
const alephPkgUri = getAlephPkgUri();
89-
const graphVersions = clientDependencyGraph.modules.filter((mod) =>
90-
!util.isLikelyHttpURL(specifier) && !util.isLikelyHttpURL(mod.specifier) && mod.specifier !== specifier
91-
).reduce((acc, { specifier, version }) => {
92-
acc[specifier] = version.toString(16);
93-
return acc;
94-
}, {} as Record<string, string>);
95-
let { code, deps, map } = await transform(specifier, rawCode, {
96-
...jsxConfig,
97-
lang: lang as TransformOptions["lang"],
98-
stripDataExport: isRouteFile(specifier),
99-
target: buildTarget ?? (isDev ? "es2022" : "es2015"),
100-
alephPkgUri,
101-
importMap: JSON.stringify(importMap),
102-
graphVersions,
103-
initialGraphVersion: clientDependencyGraph.initialVersion.toString(16),
104-
isDev,
105-
});
89+
const { atomicCSS, jsxConfig, importMap, buildTarget } = options;
90+
let ret: TransformResult;
91+
if (/^https?:\/\/(cdn\.)esm\.sh\//.test(specifier)) {
92+
// don't transform modules imported from esm.sh
93+
const deps = await parseDeps(specifier, rawCode, { importMap: JSON.stringify(importMap) });
94+
if (deps.length > 0) {
95+
const s = new MagicString(rawCode);
96+
deps.forEach((dep) => {
97+
const { importUrl, loc } = dep;
98+
if (loc) {
99+
s.overwrite(loc.start, loc.end, `"${toLocalPath(importUrl)}"`);
100+
}
101+
});
102+
ret = { code: s.toString(), deps };
103+
} else {
104+
ret = { code: rawCode, deps };
105+
}
106+
} else {
107+
const graphVersions = clientDependencyGraph.modules.filter((mod) =>
108+
!util.isLikelyHttpURL(specifier) && !util.isLikelyHttpURL(mod.specifier) && mod.specifier !== specifier
109+
).reduce((acc, { specifier, version }) => {
110+
acc[specifier] = version.toString(16);
111+
return acc;
112+
}, {} as Record<string, string>);
113+
ret = await transform(specifier, rawCode, {
114+
...jsxConfig,
115+
lang: lang as TransformOptions["lang"],
116+
stripDataExport: isRouteFile(specifier),
117+
target: buildTarget ?? (isDev ? "es2022" : "es2015"),
118+
alephPkgUri,
119+
importMap: JSON.stringify(importMap),
120+
graphVersions,
121+
initialGraphVersion: clientDependencyGraph.initialVersion.toString(16),
122+
isDev,
123+
});
124+
}
125+
let { code, map, deps } = ret;
106126
let inlineCSS = loaded?.inlineCSS;
107-
if (enableAtomicCSS && Boolean(atomicCSS?.presets?.length)) {
127+
if (useUno && Boolean(atomicCSS?.presets?.length)) {
108128
const uno = createGenerator(atomicCSS);
109129
const { css } = await uno.generate(rawCode, { id: specifier, minify: !isDev });
110130
if (inlineCSS) {
@@ -130,7 +150,7 @@ export default {
130150
resBody = code +
131151
`\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(JSON.stringify(m))}\n`;
132152
} catch {
133-
log.warn(`Failed to add source map for '${specifier}'`);
153+
log.warn(`[dev] Failed to add source map for '${specifier}'`);
134154
resBody = code;
135155
}
136156
} else {

0 commit comments

Comments
 (0)