Skip to content

Commit 4b1c8b8

Browse files
committed
fix
1 parent 52e5fc4 commit 4b1c8b8

4 files changed

Lines changed: 39 additions & 18 deletions

File tree

deno.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/plugin-vite/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"imports": {
2424
"@babel/core": "npm:@babel/core@^7.28.0",
2525
"@babel/preset-react": "npm:@babel/preset-react@^7.27.1",
26-
"@deno/loader": "jsr:@deno/loader@^0.4.0",
26+
"@deno/loader": "jsr:@deno/loader@^0.5.0",
2727
"@marvinh-test/import-json": "jsr:@marvinh-test/import-json@^0.0.1",
2828
"@remix-run/node-fetch-server": "npm:@remix-run/node-fetch-server@^0.12.0",
2929
"@prefresh/vite": "npm:@prefresh/vite@^2.4.8",

packages/plugin-vite/src/plugins/deno.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Plugin } from "vite";
22
import {
33
type Loader,
44
MediaType,
5+
type ModuleLoadResponse,
56
RequestedModuleType,
67
ResolutionMode,
78
Workspace,
@@ -17,6 +18,8 @@ const { default: babelReact } = await import("@babel/preset-react");
1718

1819
const BUILTINS = new Set(builtinModules);
1920

21+
const decoder = new TextDecoder();
22+
2023
interface DenoState {
2124
type: RequestedModuleType;
2225
}
@@ -180,14 +183,15 @@ export function deno(): Plugin {
180183
return null;
181184
}
182185

183-
const code = new TextDecoder().decode(result.code);
186+
const code = decoder.decode(result.code);
184187

185188
const maybeJsx = babelTransform({
186189
ssr: this.environment.config.consumer === "server",
187190
media: result.mediaType,
188191
code,
189192
id: specifier,
190193
isDev,
194+
inputSourceMap: parseSourceMap(result.sourceMap),
191195
});
192196
if (maybeJsx !== null) {
193197
return maybeJsx;
@@ -224,14 +228,15 @@ export function deno(): Plugin {
224228
return null;
225229
}
226230

227-
const code = new TextDecoder().decode(result.code);
231+
const code = decoder.decode(result.code);
228232

229233
const maybeJsx = babelTransform({
230234
ssr: this.environment.config.consumer === "server",
231235
media: result.mediaType,
232236
id,
233237
code,
234238
isDev,
239+
inputSourceMap: parseSourceMap(result.sourceMap),
235240
});
236241
if (maybeJsx) {
237242
return maybeJsx;
@@ -279,10 +284,12 @@ export function deno(): Plugin {
279284
return;
280285
}
281286

282-
const code = new TextDecoder().decode(result.code);
287+
const code = decoder.decode(result.code);
288+
const map = parseSourceMap(result.sourceMap);
283289

284290
return {
285291
code,
292+
map,
286293
};
287294
},
288295
},
@@ -375,13 +382,14 @@ function babelTransform(
375382
code: string;
376383
id: string;
377384
isDev: boolean;
385+
inputSourceMap: babel.TransformOptions["inputSourceMap"];
378386
},
379387
) {
380388
if (!isJsMediaType(options.media)) {
381389
return null;
382390
}
383391

384-
const { ssr, code, id, isDev } = options;
392+
const { ssr, code, id, isDev, inputSourceMap } = options;
385393

386394
const presets: babel.PluginItem[] = [];
387395
if (
@@ -400,6 +408,7 @@ function babelTransform(
400408
const result = babel.transformSync(code, {
401409
filename: id,
402410
babelrc: false,
411+
inputSourceMap,
403412
sourceMaps: "both",
404413
presets: presets,
405414
plugins: [httpAbsolute(url)],
@@ -415,3 +424,10 @@ function babelTransform(
415424

416425
return null;
417426
}
427+
428+
function parseSourceMap(
429+
sourceMap: ModuleLoadResponse["sourceMap"],
430+
): babel.TransformOptions["inputSourceMap"] {
431+
if (!sourceMap) return undefined;
432+
return JSON.parse(decoder.decode(sourceMap));
433+
}

packages/plugin-vite/tests/build_test.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createLogger } from "vite";
2+
import type { BabelFileResult } from "@babel/core";
23
import { expect } from "@std/expect";
34
import { walk } from "@std/fs/walk";
45
import {
@@ -14,6 +15,7 @@ import {
1415
launchProd,
1516
usingEnv,
1617
} from "./test_utils.ts";
18+
import { toPosix } from "fresh/internal-dev";
1719
import * as path from "@std/path";
1820
import { FRESH_CSS_PLACEHOLDER } from "../src/plugins/server_snapshot.ts";
1921

@@ -848,7 +850,6 @@ integrationTest(
848850
});
849851

850852
const serverAssetsDir = path.join(tmp.tmp, "_fresh", "server", "assets");
851-
852853
for await (
853854
const entry of walk(serverAssetsDir, {
854855
exts: [".mjs"],
@@ -857,22 +858,26 @@ integrationTest(
857858
) {
858859
const js = await Deno.readTextFile(entry.path);
859860
const match = js.match(/\/\/# sourceMappingURL=(.+)$/m);
860-
expect(match).not.toBeNull();
861-
862861
const mapPath = path.join(path.dirname(entry.path), match![1]);
863862
const mapText = await Deno.readTextFile(mapPath);
864-
const map = JSON.parse(mapText);
865-
866-
expect(Array.isArray(map.sources)).toBe(true);
867-
expect(map.sources.length).toBeGreaterThan(0);
868-
expect(typeof map.mappings).toBe("string");
869-
expect(map.mappings.length).toBeGreaterThan(0);
863+
const map: NonNullable<BabelFileResult["map"]> = JSON.parse(mapText);
864+
865+
// check a specific sourcemap file which contains
866+
// the reference of original source file
867+
if (entry.name.includes("_fresh-route___tests_feed-")) {
868+
expect(
869+
map.sources.some((source) =>
870+
toPosix(source).endsWith("demo/routes/tests/feed.tsx")
871+
),
872+
).toBe(true);
873+
}
870874
}
871875
},
872876
);
873877

874878
// rollup specific test
875879
// https://rollupjs.org/troubleshooting/#warning-sourcemap-is-likely-to-be-incorrect
880+
// this test could be broke if it will migrate to rolldown
876881
integrationTest(
877882
"vite build - ssr sourcemap sould be generated without warings",
878883
async () => {

0 commit comments

Comments
 (0)