fix: improve Deno module resolution in Vite plugin (#3734)
## Summary Three targeted fixes to Fresh's Vite plugin for better module resolution, plus new test coverage. ### 1. Workspace import map resolution Pass the importer's file URL to `loader.resolve()` so bare specifiers from workspace member packages resolve through the correct import map — not just the root `deno.json`. Fixes #3654 ### 2. Remote island resolution Use `this.resolve(spec)` in the `fresh-island::` and `fresh-client-island::` resolveId handlers (`server_snapshot.ts` / `client_snapshot.ts`) so JSR remote islands get proper `\0deno::` specifiers. Without this, remote islands resolved to bare specifiers that Vite couldn't load. Fixes #3437 ### 3. Babel JSX operator precedence fix `!ssr && id.endsWith(".tsx") || id.endsWith(".jsx")` → `!ssr && (id.endsWith(".tsx") || id.endsWith(".jsx"))`. The missing parentheses caused `.jsx` files to incorrectly get Preact JSX transforms during SSR. ### Other changes - Bump `@deno/loader` from ^0.3.10 to ^0.4.0 (needed for workspace resolution fix) - Bump `@astral/astral` from ^0.5.5 to ^0.5.6 (fixes `@std/async@1.2.0` compat) - Add dev server tests for CJS module import and maxmind CJS (#3653) Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
3a51f82da6
commit
9875146cc4
@@ -74,7 +74,7 @@
|
||||
"@std/semver": "jsr:@std/semver@1",
|
||||
"@std/streams": "jsr:@std/streams@1",
|
||||
|
||||
"@astral/astral": "jsr:@astral/astral@^0.5.5",
|
||||
"@astral/astral": "jsr:@astral/astral@^0.5.6",
|
||||
"@marvinh-test/fresh-island": "jsr:@marvinh-test/fresh-island@^0.0.3",
|
||||
"linkedom": "npm:linkedom@^0.18.10",
|
||||
"@std/async": "jsr:@std/async@^1.0.13",
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
"imports": {
|
||||
"@babel/core": "npm:@babel/core@^7.28.0",
|
||||
"@babel/preset-react": "npm:@babel/preset-react@^7.27.1",
|
||||
"@deno/loader": "jsr:@deno/loader@^0.3.10",
|
||||
"@deno/loader": "jsr:@deno/loader@^0.4.0",
|
||||
"@marvinh-test/import-json": "jsr:@marvinh-test/import-json@^0.0.1",
|
||||
"@remix-run/node-fetch-server": "npm:@remix-run/node-fetch-server@^0.12.0",
|
||||
"@prefresh/vite": "npm:@prefresh/vite@^2.4.8",
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
type ResolvedFreshViteConfig,
|
||||
} from "./utils.ts";
|
||||
import { deno } from "./plugins/deno.ts";
|
||||
|
||||
import prefresh from "@prefresh/vite";
|
||||
import { serverEntryPlugin } from "./plugins/server_entry.ts";
|
||||
import { clientEntryPlugin } from "./plugins/client_entry.ts";
|
||||
|
||||
@@ -148,10 +148,15 @@ if (import.meta.hot) {
|
||||
filter: {
|
||||
id: /^fresh-client-island::/,
|
||||
},
|
||||
handler(id) {
|
||||
async handler(id) {
|
||||
const name = id.slice("fresh-client-island::".length);
|
||||
const full = entryToIsland.get(name);
|
||||
return full;
|
||||
if (full !== undefined) {
|
||||
// Re-resolve through the plugin pipeline so deno-specifiers
|
||||
// (jsr:, https:) are handled correctly.
|
||||
const resolved = await this.resolve(full);
|
||||
return resolved ?? full;
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -115,9 +115,20 @@ export function deno(): Plugin {
|
||||
? importer
|
||||
: undefined;
|
||||
|
||||
// For bare specifiers from non-deno importers, try resolving
|
||||
// with the importer's file URL so workspace import maps work
|
||||
let denoImporterUrl = denoImporter;
|
||||
if (
|
||||
denoImporter && !denoImporter.startsWith("file://") &&
|
||||
!denoImporter.startsWith("http") &&
|
||||
path.isAbsolute(denoImporter)
|
||||
) {
|
||||
denoImporterUrl = path.toFileUrl(denoImporter).href;
|
||||
}
|
||||
|
||||
let resolved = await loader.resolve(
|
||||
id,
|
||||
denoImporter,
|
||||
denoImporterUrl,
|
||||
ResolutionMode.Import,
|
||||
);
|
||||
|
||||
@@ -374,7 +385,7 @@ function babelTransform(
|
||||
|
||||
const presets: babel.PluginItem[] = [];
|
||||
if (
|
||||
!ssr && id.endsWith(".tsx") || id.endsWith(".jsx")
|
||||
!ssr && (id.endsWith(".tsx") || id.endsWith(".jsx"))
|
||||
) {
|
||||
presets.push([babelReact, {
|
||||
runtime: "automatic",
|
||||
|
||||
@@ -436,7 +436,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
filter: {
|
||||
id: /^fresh-island::.*/,
|
||||
},
|
||||
handler(id) {
|
||||
async handler(id) {
|
||||
let name = id.slice("fresh-island::".length);
|
||||
|
||||
if (JS_REG.test(name)) {
|
||||
@@ -444,7 +444,12 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||
}
|
||||
|
||||
const spec = islandSpecByName.get(name);
|
||||
if (spec !== undefined) return spec;
|
||||
if (spec !== undefined) {
|
||||
// Re-resolve through the plugin pipeline so deno-specifiers
|
||||
// (jsr:, https:) are handled correctly.
|
||||
const resolved = await this.resolve(spec);
|
||||
return resolved ?? spec;
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -382,15 +382,35 @@ integrationTest("vite dev - support jsx namespace", async () => {
|
||||
expect(text).toContain(`xml:space="preserve"`);
|
||||
});
|
||||
|
||||
// issue: https://github.com/denoland/fresh/issues/3653
|
||||
Deno.test({
|
||||
name: "vite dev - CJS module import",
|
||||
fn: async () => {
|
||||
const res = await fetch(`${demoServer.address()}/tests/commonjs`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("<h1>ok</h1>");
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite dev - maxmind CJS",
|
||||
fn: async () => {
|
||||
const res = await fetch(`${demoServer.address()}/tests/maxmind`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("<h1>maxmind</h1>");
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
// issue: https://github.com/denoland/fresh/issues/3666
|
||||
integrationTest(
|
||||
"vite dev - basePath does not intercept Vite URLs",
|
||||
async () => {
|
||||
const fixture = path.join(FIXTURE_DIR, "basepath");
|
||||
await launchDevServer(fixture, async (address) => {
|
||||
// `address` already includes the base path (e.g. http://localhost:PORT/ui)
|
||||
// Vite's /@vite/client should be accessible at {base}/@vite/client
|
||||
// Without the fix, Fresh's dev server intercepted this and returned 404.
|
||||
const viteClientRes = await fetch(`${address}/@vite/client`);
|
||||
await viteClientRes.body?.cancel();
|
||||
expect(viteClientRes.status).toEqual(200);
|
||||
|
||||
Reference in New Issue
Block a user