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

Commit 4096443

Browse files
committed
Use magic string
1 parent 9589a69 commit 4096443

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

server/serve_modules.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createGenerator } from "https://esm.sh/@unocss/[email protected]";
2-
import { fastTransform } from "../compiler/mod.ts";
2+
import MagicString from "https://esm.sh/[email protected]";
3+
import { parseDeps } from "../compiler/mod.ts";
34
import { builtinModuleExts } from "../lib/helpers.ts";
45
import log from "../lib/log.ts";
56
import { getContentType } from "../lib/mime.ts";
@@ -44,17 +45,6 @@ const esModuleLoader = async (input: { pathname: string } & ModuleLoaderContent,
4445
const contentType = lang ? getContentType(`file.${lang}`) : undefined;
4546
const serverDependencyGraph: DependencyGraph | undefined = Reflect.get(globalThis, "serverDependencyGraph");
4647
if (serverDependencyGraph) {
47-
const graphVersions = serverDependencyGraph.modules.filter((mod) =>
48-
!util.isLikelyHttpURL(specifier) && !util.isLikelyHttpURL(mod.specifier) && mod.specifier !== specifier
49-
).reduce((acc, { specifier, version }) => {
50-
acc[specifier] = version.toString(16);
51-
return acc;
52-
}, {} as Record<string, string>);
53-
const { code, deps } = await fastTransform(specifier, rawCode, {
54-
importMap: JSON.stringify(env.importMap),
55-
initialGraphVersion: serverDependencyGraph.initialVersion.toString(16),
56-
graphVersions,
57-
});
5848
let inlineCSS = input.inlineCSS;
5949
if (Boolean(config?.atomicCSS?.presets?.length) && isJSX) {
6050
const uno = createGenerator(config?.atomicCSS);
@@ -70,9 +60,26 @@ const esModuleLoader = async (input: { pathname: string } & ModuleLoaderContent,
7060
}
7161
}
7262
}
63+
const deps = await parseDeps(specifier, rawCode, { importMap: JSON.stringify(env.importMap) });
7364
serverDependencyGraph.mark(specifier, { deps, inlineCSS });
65+
const locDeps = deps.filter((dep) => !util.isLikelyHttpURL(dep.specifier));
66+
if (locDeps.length) {
67+
const s = new MagicString(rawCode);
68+
locDeps.forEach((dep) => {
69+
const { specifier, importUrl, loc } = dep;
70+
const versionStr = serverDependencyGraph.get(specifier)?.version || serverDependencyGraph.initialVersion;
71+
let url = importUrl;
72+
if (url.includes("?")) {
73+
url = `"${url}&v=${versionStr}"`;
74+
} else {
75+
url = `"${url}?v=${versionStr}"`;
76+
}
77+
s.overwrite(loc.start, loc.end, url);
78+
});
79+
return { content: s.toString(), contentType };
80+
}
7481
return {
75-
content: code,
82+
content: rawCode,
7683
contentType,
7784
};
7885
}

server/transformer.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createGenerator } from "https://esm.sh/@unocss/[email protected]";
2+
import MagicString from "https://esm.sh/[email protected]";
23
import { transform } from "../compiler/mod.ts";
34
import type { TransformOptions } from "../compiler/types.ts";
45
import { readCode } from "../lib/fs.ts";
@@ -51,8 +52,10 @@ export default {
5152
return new Response(null, { status: 304 });
5253
}
5354

54-
let clientDependencyGraph: DependencyGraph | undefined = Reflect.get(globalThis, "clientDependencyGraph");
55-
if (!clientDependencyGraph) {
55+
let clientDependencyGraph: DependencyGraph;
56+
if (Reflect.has(globalThis, "clientDependencyGraph")) {
57+
clientDependencyGraph = Reflect.get(globalThis, "clientDependencyGraph");
58+
} else {
5659
clientDependencyGraph = new DependencyGraph();
5760
Reflect.set(globalThis, "clientDependencyGraph", clientDependencyGraph);
5861
}
@@ -75,28 +78,20 @@ export default {
7578
asJsModule,
7679
hmr: isDev,
7780
});
81+
clientDependencyGraph.mark(specifier, { deps: deps?.map((specifier) => ({ specifier })) });
7882
resBody = code;
7983
if (!asJsModule) {
8084
resType = "text/css";
8185
}
82-
clientDependencyGraph.mark(specifier, { deps: deps?.map((specifier) => ({ specifier })) });
8386
} else {
8487
const { atomicCSS, jsxConfig, importMap, buildTarget } = options;
85-
const graphVersions = clientDependencyGraph.modules.filter((mod) =>
86-
!util.isLikelyHttpURL(specifier) && !util.isLikelyHttpURL(mod.specifier) && mod.specifier !== specifier
87-
).reduce((acc, { specifier, version }) => {
88-
acc[specifier] = version.toString(16);
89-
return acc;
90-
}, {} as Record<string, string>);
9188
const alephPkgUri = getAlephPkgUri();
92-
const { code, deps } = await transform(specifier, rawCode, {
89+
let { code, deps, map } = await transform(specifier, rawCode, {
9390
...jsxConfig,
9491
lang: lang as TransformOptions["lang"],
9592
stripDataExport: isRouteFile(specifier),
9693
target: buildTarget ?? (isDev ? "es2022" : "es2015"),
9794
alephPkgUri,
98-
graphVersions,
99-
initialGraphVersion: clientDependencyGraph.initialVersion.toString(16),
10095
importMap: JSON.stringify(importMap),
10196
isDev,
10297
});
@@ -110,16 +105,39 @@ export default {
110105
inlineCSS = css;
111106
}
112107
}
108+
const locDeps = deps?.filter((dep) => !util.isLikelyHttpURL(dep.specifier));
109+
if (locDeps?.length) {
110+
const s = new MagicString(code);
111+
locDeps.forEach((dep) => {
112+
const { specifier, importUrl, loc } = dep;
113+
const versionStr = clientDependencyGraph.get(specifier)?.version || clientDependencyGraph.initialVersion;
114+
let url = importUrl;
115+
if (url.includes("?")) {
116+
url = `"${url}&v=${versionStr}"`;
117+
} else {
118+
url = `"${url}?v=${versionStr}"`;
119+
}
120+
s.overwrite(loc.start, loc.end, url);
121+
});
122+
code = s.toString();
123+
}
113124
if (inlineCSS) {
125+
resBody += `\nimport { applyCSS as __applyCSS } from "${
126+
toLocalPath(alephPkgUri)
127+
}/framework/core/style.ts";\n__applyCSS(${JSON.stringify(specifier)}, ${JSON.stringify(inlineCSS)});`;
128+
deps = [...(deps || []), { specifier: alephPkgUri + "/framework/core/style.ts" }] as typeof deps;
129+
}
130+
clientDependencyGraph.mark(specifier, { deps });
131+
if (map) {
132+
const m = JSON.parse(map);
133+
if (!util.isLikelyHttpURL(specifier)) {
134+
m.sources = [`file://source/${util.trimPrefix(specifier, ".")}`];
135+
}
136+
m.sourcesContent = [rawCode];
114137
resBody = code +
115-
`\nimport { applyCSS as __applyCSS } from "${
116-
toLocalPath(alephPkgUri)
117-
}/framework/core/style.ts";\n__applyCSS(${JSON.stringify(specifier)}, ${JSON.stringify(inlineCSS)});`;
118-
deps?.push({ specifier: alephPkgUri + "/framework/core/style.ts" });
119-
clientDependencyGraph.mark(specifier, { deps });
138+
`\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(JSON.stringify(m))}`;
120139
} else {
121140
resBody = code;
122-
clientDependencyGraph.mark(specifier, { deps });
123141
}
124142
}
125143
return new Response(resBody, {

0 commit comments

Comments
 (0)