diff --git a/deno.lock b/deno.lock index 8e97d87c..9df02b7f 100644 --- a/deno.lock +++ b/deno.lock @@ -16,7 +16,7 @@ "jsr:@std/assert@^1.0.14": "1.0.14", "jsr:@std/async@1": "1.0.14", "jsr:@std/async@^1.0.13": "1.0.14", - "jsr:@std/bytes@^1.0.2": "1.0.6", + "jsr:@std/bytes@^1.0.5": "1.0.6", "jsr:@std/bytes@^1.0.6": "1.0.6", "jsr:@std/cli@^1.0.19": "1.0.21", "jsr:@std/cli@^1.0.21": "1.0.21", @@ -41,7 +41,7 @@ "jsr:@std/http@^1.0.15": "1.0.20", "jsr:@std/internal@^1.0.10": "1.0.10", "jsr:@std/internal@^1.0.9": "1.0.10", - "jsr:@std/io@0.225": "0.225.0", + "jsr:@std/io@0.225": "0.225.2", "jsr:@std/io@0.225.0": "0.225.0", "jsr:@std/json@^1.0.2": "1.0.2", "jsr:@std/jsonc@1": "1.0.2", @@ -58,7 +58,7 @@ "jsr:@std/toml@^1.0.3": "1.0.8", "jsr:@std/uuid@^1.0.7": "1.0.9", "jsr:@std/yaml@^1.0.5": "1.0.9", - "jsr:@zip-js/zip-js@^2.7.52": "2.7.72", + "jsr:@zip-js/zip-js@^2.7.52": "2.7.73", "npm:@babel/core@^7.28.0": "7.28.3", "npm:@babel/preset-react@^7.27.1": "7.27.1_@babel+core@7.28.3", "npm:@mjackson/node-fetch-server@0.7": "0.7.0", @@ -98,10 +98,8 @@ "npm:tailwindcss@^4.1.10": "4.1.12", "npm:ts-morph@26": "26.0.0", "npm:vite-plugin-inspect@^11.3.2": "11.3.3_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0", - "npm:vite@*": "7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0", - "npm:vite@7.1.3": "7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0", + "npm:vite@*": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0", "npm:vite@7.1.4": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0", - "npm:vite@^7.1.3": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0", "npm:vite@^7.1.4": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0" }, "jsr": { @@ -247,9 +245,12 @@ "integrity": "e3be62ce42cab0e177c27698e5d9800122f67b766a0bea6ca4867886cbde8cf7" }, "@std/io@0.225.0": { - "integrity": "c1db7c5e5a231629b32d64b9a53139445b2ca640d828c26bf23e1c55f8c079b3", + "integrity": "c1db7c5e5a231629b32d64b9a53139445b2ca640d828c26bf23e1c55f8c079b3" + }, + "@std/io@0.225.2": { + "integrity": "3c740cd4ee4c082e6cfc86458f47e2ab7cb353dc6234d5e9b1f91a2de5f4d6c7", "dependencies": [ - "jsr:@std/bytes@^1.0.2" + "jsr:@std/bytes@^1.0.5" ] }, "@std/json@1.0.2": { @@ -318,8 +319,8 @@ "@zip-js/zip-js@2.7.62": { "integrity": "11cbe0746fa1e52e6e0a601c89ba97365f16e38a07f139b9d9914f988aec9081" }, - "@zip-js/zip-js@2.7.72": { - "integrity": "b72877f90aaefa1f1bd265d51f354bb58b6dd0d0e2799c865584acf49eae9115" + "@zip-js/zip-js@2.7.73": { + "integrity": "14c123f0e534377a6f47c5ba5293bb6c0f3e72e78c6a687108011605420a4867" } }, "npm": { @@ -2611,16 +2612,6 @@ "reusify@1.1.0": { "integrity": "sha512-g6QUff04oZpHs0eG5p83rFLhHeV00ug/Yf9nZM6fLeUrPguBTkTQOdpAWWspMh55TZfVQDPaN3NQJfbVRAxdIw==" }, - "rollup-plugin-visualizer@6.0.3": { - "integrity": "sha512-ZU41GwrkDcCpVoffviuM9Clwjy5fcUxlz0oMoTXTYsK+tcIFzbdacnrr2n8TXcHxbGKKXtOdjxM2HUS4HjkwIw==", - "dependencies": [ - "open@8.4.2", - "picomatch@4.0.3", - "source-map", - "yargs" - ], - "bin": true - }, "rollup-plugin-visualizer@6.0.3_rollup@4.50.0": { "integrity": "sha512-ZU41GwrkDcCpVoffviuM9Clwjy5fcUxlz0oMoTXTYsK+tcIFzbdacnrr2n8TXcHxbGKKXtOdjxM2HUS4HjkwIw==", "dependencies": [ @@ -2942,44 +2933,6 @@ "vite-dev-rpc@1.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0" ] }, - "vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3": { - "integrity": "sha512-OOUi5zjkDxYrKhTV3V7iKsoS37VUM7v40+HuwEmcrsf11Cdx9y3DIr2Px6liIcZFwt3XSRpQvFpL3WVy7ApkGw==", - "dependencies": [ - "@types/node@24.3.0", - "esbuild", - "fdir", - "picomatch@4.0.3", - "postcss", - "rollup", - "tinyglobby" - ], - "optionalDependencies": [ - "fsevents" - ], - "optionalPeers": [ - "@types/node@24.3.0" - ], - "bin": true - }, - "vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0": { - "integrity": "sha512-OOUi5zjkDxYrKhTV3V7iKsoS37VUM7v40+HuwEmcrsf11Cdx9y3DIr2Px6liIcZFwt3XSRpQvFpL3WVy7ApkGw==", - "dependencies": [ - "@types/node@24.2.0", - "esbuild", - "fdir", - "picomatch@4.0.3", - "postcss", - "rollup", - "tinyglobby" - ], - "optionalDependencies": [ - "fsevents" - ], - "optionalPeers": [ - "@types/node@24.2.0" - ], - "bin": true - }, "vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3": { "integrity": "sha512-X5QFK4SGynAeeIt+A7ZWnApdUyHYm+pzv/8/A57LqSGcI88U6R6ipOs3uCesdc6yl7nl+zNO0t8LmqAdXcQihw==", "dependencies": [ @@ -3232,7 +3185,7 @@ "dependencies": [ "npm:@tailwindcss/vite@^4.1.12", "npm:vite-plugin-inspect@^11.3.2", - "npm:vite@^7.1.3" + "npm:vite@^7.1.4" ] } } diff --git a/packages/plugin-vite/src/plugins/build_id.ts b/packages/plugin-vite/src/plugins/build_id.ts index 304c8f81..e65757c6 100644 --- a/packages/plugin-vite/src/plugins/build_id.ts +++ b/packages/plugin-vite/src/plugins/build_id.ts @@ -26,14 +26,17 @@ export function buildIdPlugin(): Plugin { return `\0fresh/build-id`; } }, - load(id) { - if (id !== `\0fresh/build-id`) return; - - return `export let BUILD_ID = ${JSON.stringify(buildId)}; + load: { + filter: { + id: /\0fresh\/build-id/, + }, + handler() { + return `export let BUILD_ID = ${JSON.stringify(buildId)}; export const DENO_DEPLOYMENT_ID = undefined; export function setBuildId(id) { BUILD_ID = id; }`; + }, }, }; } diff --git a/packages/plugin-vite/src/plugins/client_entry.ts b/packages/plugin-vite/src/plugins/client_entry.ts index 98a8ca44..c1501909 100644 --- a/packages/plugin-vite/src/plugins/client_entry.ts +++ b/packages/plugin-vite/src/plugins/client_entry.ts @@ -19,25 +19,32 @@ export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin { configResolved(config) { clientEntry = pathWithRoot(options.clientEntry, config.root); }, - resolveId(id) { - if (id === modName) { - return `\0${modName}`; - } else if (id === modNameUser) { - return clientEntry; - } + resolveId: { + filter: { + id: /(fresh:client-entry|fresh:client-entry-user)/, + }, + handler(id) { + if (id === modName) { + return `\0${modName}`; + } else if (id === modNameUser) { + return clientEntry; + } + }, }, - async load(id) { - if (id !== `\0${modName}`) return; + load: { + filter: { + id: /\0fresh:client-entry/, + }, + async handler() { + let exists = false; + try { + const stat = await Deno.stat(clientEntry); + exists = stat.isFile; + } catch { + exists = false; + } - let exists = false; - try { - const stat = await Deno.stat(clientEntry); - exists = stat.isFile; - } catch { - exists = false; - } - - return ` + return ` ${isDev ? 'import "preact/debug"' : ""} export * from "fresh/runtime-client"; ${exists ? `import "fresh:client-entry-user";` : ""} @@ -52,6 +59,7 @@ if (import.meta.hot) { }); } `; + }, }, }; } diff --git a/packages/plugin-vite/src/plugins/client_snapshot.ts b/packages/plugin-vite/src/plugins/client_snapshot.ts index 703d6815..9fcf40bb 100644 --- a/packages/plugin-vite/src/plugins/client_snapshot.ts +++ b/packages/plugin-vite/src/plugins/client_snapshot.ts @@ -48,37 +48,47 @@ export function clientSnapshot(options: ResolvedFreshViteConfig): Plugin { islands.add(filePath); } }, - resolveId(id) { - if (id === modName) { - return `\0${modName}`; - } - }, configureServer(devServer) { server = devServer; }, - load(id) { - if (id !== `\0${modName}`) return; - - const imports = Array.from(islands.keys()).map((file, i) => { - return `export const mod_${i} = await import(${JSON.stringify(file)});`; - }).join("\n"); - - if (isDev && server !== undefined) { - const mod = server.environments.ssr.moduleGraph.getModuleById( - "\0fresh:server-snapshot", - ); - if (mod) { - server.environments.ssr.moduleGraph.invalidateModule(mod); + resolveId: { + filter: { + id: /fresh:client-snapshot/, + }, + handler(id) { + if (id === modName) { + return `\0${modName}`; } - } + }, + }, + load: { + filter: { + id: /\0fresh:client-snapshot/, + }, + handler() { + const imports = Array.from(islands.keys()).map((file, i) => { + return `export const mod_${i} = await import(${ + JSON.stringify(file) + });`; + }).join("\n"); - return `${imports} + if (isDev && server !== undefined) { + const mod = server.environments.ssr.moduleGraph.getModuleById( + "\0fresh:server-snapshot", + ); + if (mod) { + server.environments.ssr.moduleGraph.invalidateModule(mod); + } + } + + return `${imports} if (import.meta.hot) { import.meta.hot.accept(() => { console.log("accepting client-snapshot") }); } `; + }, }, }; } diff --git a/packages/plugin-vite/src/plugins/deno.ts b/packages/plugin-vite/src/plugins/deno.ts index 1022238d..c3d9c16b 100644 --- a/packages/plugin-vite/src/plugins/deno.ts +++ b/packages/plugin-vite/src/plugins/deno.ts @@ -10,7 +10,7 @@ import * as path from "@std/path"; import * as babel from "@babel/core"; import babelReact from "@babel/preset-react"; import { httpAbsolute } from "./patches/http_absolute.ts"; -import { JS_REG } from "../utils.ts"; +import { JS_REG, JSX_REG } from "../utils.ts"; interface DenoState { type: RequestedModuleType; @@ -210,40 +210,50 @@ export function deno(): Plugin { code, }; }, - async transform(_, id, options) { - // This transform is a hack to be able to re-use Deno's precompile - // jsx transform. - if (!options?.ssr || !id.endsWith(".tsx") || id.endsWith(".jsx")) { - return; - } + transform: { + filter: { + id: JSX_REG, + }, + async handler(_, id, options) { + // This transform is a hack to be able to re-use Deno's precompile + // jsx transform. + if (!options?.ssr) { + return; + } - let actualId = id; - if (isDenoSpecifier(id)) { - const { specifier } = parseDenoSpecifier(id); - actualId = specifier; - } - if (path.isAbsolute(actualId)) { - actualId = path.toFileUrl(actualId).href; - } + let actualId = id; + if (isDenoSpecifier(id)) { + const { specifier } = parseDenoSpecifier(id); + actualId = specifier; + } + actualId = actualId.replace("?commonjs-es-import", ""); - const resolved = await ssrLoader.resolve( - actualId, - undefined, - ResolutionMode.Import, - ); - const result = await ssrLoader.load( - resolved, - RequestedModuleType.Default, - ); - if (result.kind === "external") { - return; - } + if (actualId.startsWith("\0")) { + actualId = actualId.slice(1); + } + if (path.isAbsolute(actualId)) { + actualId = path.toFileUrl(actualId).href; + } - const code = new TextDecoder().decode(result.code); + const resolved = await ssrLoader.resolve( + actualId, + undefined, + ResolutionMode.Import, + ); + const result = await ssrLoader.load( + resolved, + RequestedModuleType.Default, + ); + if (result.kind === "external") { + return; + } - return { - code, - }; + const code = new TextDecoder().decode(result.code); + + return { + code, + }; + }, }, }; } diff --git a/packages/plugin-vite/src/plugins/patches.ts b/packages/plugin-vite/src/plugins/patches.ts index 31ee2f57..76315a86 100644 --- a/packages/plugin-vite/src/plugins/patches.ts +++ b/packages/plugin-vite/src/plugins/patches.ts @@ -19,45 +19,48 @@ export function patches(): Plugin { applyToEnvironment() { return true; }, - transform(code, id, options) { - if (!JS_REG.test(id)) return; + transform: { + filter: { + id: JS_REG, + }, + handler(code, id, options) { + const presets = []; + if (!options?.ssr && JSX_REG.test(id)) { + presets.push([babelReact, { + runtime: "automatic", + importSource: "preact", + development: isDev, + }]); + } - const presets = []; - if (!options?.ssr && JSX_REG.test(id)) { - presets.push([babelReact, { - runtime: "automatic", - importSource: "preact", - development: isDev, - }]); - } + const plugins: babel.PluginItem[] = [ + cjsPlugin, + removePolyfills, + jsxComments, + inlineEnvVarsPlugin( + isDev ? "development" : "production", + ), + ]; - const plugins: babel.PluginItem[] = [ - cjsPlugin, - removePolyfills, - jsxComments, - inlineEnvVarsPlugin( - isDev ? "development" : "production", - ), - ]; + if (!options?.ssr) { + plugins.push(denoGlobal); + } - if (!options?.ssr) { - plugins.push(denoGlobal); - } + const res = babel.transformSync(code, { + filename: id, + babelrc: false, + compact: true, + plugins, + presets, + }); - const res = babel.transformSync(code, { - filename: id, - babelrc: false, - compact: true, - plugins, - presets, - }); - - if (res?.code) { - return { - code: res.code, - map: res.map, - }; - } + if (res?.code) { + return { + code: res.code, + map: res.map, + }; + } + }, }, }; } diff --git a/packages/plugin-vite/src/plugins/server_entry.ts b/packages/plugin-vite/src/plugins/server_entry.ts index 3df8c31f..8022c531 100644 --- a/packages/plugin-vite/src/plugins/server_entry.ts +++ b/packages/plugin-vite/src/plugins/server_entry.ts @@ -39,21 +39,28 @@ export function serverEntryPlugin( config.root, ); }, - resolveId(id) { - if (id === modName) { - return `\0${modName}`; - } + resolveId: { + filter: { + id: /fresh:server_entry/, + }, + handler(id) { + if (id === modName) { + return `\0${modName}`; + } + }, }, - load(id) { - if (id !== `\0${modName}`) return; + load: { + filter: { + id: /\0fresh:server_entry/, + }, + handler() { + let code = generateServerEntry({ + root: isDev ? path.relative(serverOutDir, root) : "..", + serverEntry: path.toFileUrl(serverEntry).href, + snapshotSpecifier: "fresh:server-snapshot", + }); - let code = generateServerEntry({ - root: isDev ? path.relative(serverOutDir, root) : "..", - serverEntry: path.toFileUrl(serverEntry).href, - snapshotSpecifier: "fresh:server-snapshot", - }); - - code += ` + code += ` export function registerStaticFile(prepared) { snapshot.staticFiles.set(prepared.name, { @@ -64,13 +71,14 @@ export function registerStaticFile(prepared) { } `; - if (isDev) { - code = `import "preact/debug"; + if (isDev) { + code = `import "preact/debug"; ${code} if (import.meta.hot) import.meta.hot.accept();`; - } + } - return code; + return code; + }, }, async writeBundle(_options, bundle) { const manifest = bundle[".vite/manifest.json"]; diff --git a/packages/plugin-vite/src/plugins/server_snapshot.ts b/packages/plugin-vite/src/plugins/server_snapshot.ts index 43ea25b0..07c1ea9f 100644 --- a/packages/plugin-vite/src/plugins/server_snapshot.ts +++ b/packages/plugin-vite/src/plugins/server_snapshot.ts @@ -111,202 +111,214 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] { viteServer.ws.send("fresh:reload"); }); }, - resolveId(id) { - if (id === modName) { - return `\0${modName}`; - } + resolveId: { + filter: { + id: /fresh:server-snapshot/, + }, + handler(id) { + if (id === modName) { + return `\0${modName}`; + } + }, }, - async load(id) { - if (id !== `\0${modName}`) return; + load: { + filter: { + id: /\0fresh:server-snapshot/, + }, - const result = await crawlFsItem({ - islandDir: options.islandsDir, - routeDir: options.routeDir, - ignore: options.ignore, - }); - - for (let i = 0; i < result.islands.length; i++) { - const spec = result.islands[i]; - const specName = specToName(spec); - const name = options.namer.getUniqueName(specName); - - islands.set(spec, { name, chunk: null }); - islandSpecByName.set(name, spec); - islandsByFile.add(spec); - } - - const staticFiles: PendingStaticFile[] = []; - let islandMods: IslandModChunk[] = []; - let clientEntry = "/@id/fresh:client-entry"; - let buildId = ""; - const entryAssets: string[] = []; - - if (isDev && server !== undefined) { - for (const id of islands.keys()) { - const mod = server.environments.client.moduleGraph.getModuleById( - id, - ); - if (mod !== undefined) { - const def = islands.get(id); - if (def !== undefined) def.chunk = mod.url; - } - } - - islandMods = Array.from(islands.entries()).map(([id, def]) => { - return { - name: def.name, - server: id, - browser: def.chunk ?? `/@id/fresh-island::${def.name}`, - css: [], - }; + async handler() { + const result = await crawlFsItem({ + islandDir: options.islandsDir, + routeDir: options.routeDir, + ignore: options.ignore, }); - } else { - buildId = await getBuildId(false); - const manifest = JSON.parse( - await Deno.readTextFile( - path.join(clientOutDir, ".vite", "manifest.json"), - ), - ) as Manifest; - const resolvedIslandSpecs = new Map(); - for (const spec of options.islandSpecifiers.keys()) { - const resolved = await this.resolve(spec); + for (let i = 0; i < result.islands.length; i++) { + const spec = result.islands[i]; + const specName = specToName(spec); + const name = options.namer.getUniqueName(specName); - if (resolved === null) continue; - - const id = resolved.id.startsWith("\0") - ? resolved.id.slice(1) - : resolved.id; - - resolvedIslandSpecs.set(id, spec); + islands.set(spec, { name, chunk: null }); + islandSpecByName.set(name, spec); + islandsByFile.add(spec); } - const clientEntryName = "client-entry"; + const staticFiles: PendingStaticFile[] = []; + let islandMods: IslandModChunk[] = []; + let clientEntry = "/@id/fresh:client-entry"; + let buildId = ""; + const entryAssets: string[] = []; - for (const chunk of Object.values(manifest)) { - if (chunk.name === clientEntryName) { - clientEntry = pathToSpec(clientOutDir, chunk.file); + if (isDev && server !== undefined) { + for (const id of islands.keys()) { + const mod = server.environments.client.moduleGraph.getModuleById( + id, + ); + if (mod !== undefined) { + const def = islands.get(id); + if (def !== undefined) def.chunk = mod.url; + } } - staticFiles.push({ - filePath: path.join(clientOutDir, chunk.file), - pathname: chunk.file, - hash: null, + islandMods = Array.from(islands.entries()).map(([id, def]) => { + return { + name: def.name, + server: id, + browser: def.chunk ?? `/@id/fresh-island::${def.name}`, + css: [], + }; }); + } else { + buildId = await getBuildId(false); + const manifest = JSON.parse( + await Deno.readTextFile( + path.join(clientOutDir, ".vite", "manifest.json"), + ), + ) as Manifest; + const resolvedIslandSpecs = new Map(); - if (chunk.css !== undefined) { - for (let i = 0; i < chunk.css.length; i++) { - const id = chunk.css[i]; + for (const spec of options.islandSpecifiers.keys()) { + const resolved = await this.resolve(spec); - const pathname = `/${id}`; + if (resolved === null) continue; - staticFiles.push({ - filePath: path.join(clientOutDir, id), - hash: null, - pathname, - }); + const id = resolved.id.startsWith("\0") + ? resolved.id.slice(1) + : resolved.id; - if (chunk.name === clientEntryName) { - entryAssets.push(pathname); - } - } + resolvedIslandSpecs.set(id, spec); } - const namer = new UniqueNamer(); - if (chunk.name === "client-snapshot") { - for (const id of chunk.dynamicImports ?? []) { - const mod = manifest[id]; + const clientEntryName = "client-entry"; - let serverPath = path.join(root, mod.src ?? id); - const idx = mod.src?.indexOf("deno::") ?? -1; - - if (idx > -1 && mod.src) { - const src = mod.src - .slice(idx) - .replace( - /(https?):\/([^/])/, - (_m, protocol, rest) => { - return `${protocol}://${rest}`; - }, - ); - serverPath = resolvedIslandSpecs.get(src)!; - } - - let spec = pathToSpec(clientOutDir, mod.file); - - if (spec.startsWith("./")) { - spec = spec.slice(1); - } - - const chunkCss = mod.css?.map((id) => `/${id}`) ?? []; - - const name = namer.getUniqueName(specToName(id)); - islandMods.push({ - name, - browser: spec, - server: serverPath, - css: chunkCss, - }); + for (const chunk of Object.values(manifest)) { + if (chunk.name === clientEntryName) { + clientEntry = pathToSpec(clientOutDir, chunk.file); } - } - } - - if (await fsAdapter.isDirectory(publicDir)) { - const entries = await fsAdapter.walk( - publicDir, - { - followSymlinks: false, - includeDirs: false, - includeFiles: true, - skip: options.ignore, - }, - ); - - for await (const entry of entries) { - const relative = path.relative(publicDir, entry.path); - const filePath = path.join(clientOutDir, relative); - - try { - await Deno.mkdir(path.dirname(filePath), { recursive: true }); - } catch (err) { - if (!(err instanceof Deno.errors.AlreadyExists)) { - throw err; - } - } - await Deno.copyFile(entry.path, filePath); staticFiles.push({ - filePath, + filePath: path.join(clientOutDir, chunk.file), + pathname: chunk.file, hash: null, - pathname: relative, }); + + if (chunk.css !== undefined) { + for (let i = 0; i < chunk.css.length; i++) { + const id = chunk.css[i]; + + const pathname = `/${id}`; + + staticFiles.push({ + filePath: path.join(clientOutDir, id), + hash: null, + pathname, + }); + + if (chunk.name === clientEntryName) { + entryAssets.push(pathname); + } + } + } + + const namer = new UniqueNamer(); + if (chunk.name === "client-snapshot") { + for (const id of chunk.dynamicImports ?? []) { + const mod = manifest[id]; + + let serverPath = path.join(root, mod.src ?? id); + const idx = mod.src?.indexOf("deno::") ?? -1; + + if (idx > -1 && mod.src) { + const src = mod.src + .slice(idx) + .replace( + /(https?):\/([^/])/, + (_m, protocol, rest) => { + return `${protocol}://${rest}`; + }, + ); + serverPath = resolvedIslandSpecs.get(src)!; + } + + let spec = pathToSpec(clientOutDir, mod.file); + + if (spec.startsWith("./")) { + spec = spec.slice(1); + } + + const chunkCss = mod.css?.map((id) => `/${id}`) ?? []; + + const name = namer.getUniqueName(specToName(id)); + islandMods.push({ + name, + browser: spec, + server: serverPath, + css: chunkCss, + }); + } + } + } + + if (await fsAdapter.isDirectory(publicDir)) { + const entries = await fsAdapter.walk( + publicDir, + { + followSymlinks: false, + includeDirs: false, + includeFiles: true, + skip: options.ignore, + }, + ); + + for await (const entry of entries) { + const relative = path.relative(publicDir, entry.path); + const filePath = path.join(clientOutDir, relative); + + try { + await Deno.mkdir(path.dirname(filePath), { recursive: true }); + } catch (err) { + if (!(err instanceof Deno.errors.AlreadyExists)) { + throw err; + } + } + await Deno.copyFile(entry.path, filePath); + + staticFiles.push({ + filePath, + hash: null, + pathname: relative, + }); + } } } - } - const code = await generateSnapshotServer({ - outDir: path.join(clientOutDir, ".."), - staticFiles, - buildId, - clientEntry, - entryAssets, - fsRoutesFiles: result.routes, - islands: islandMods, - writeSpecifier: (file) => { - const def = islands.get(file); - if (def) { - return `fresh-island::${def.name}`; - } - return path.toFileUrl(file).href; - }, - }); + const code = await generateSnapshotServer({ + outDir: path.join(clientOutDir, ".."), + staticFiles, + buildId, + clientEntry, + entryAssets, + fsRoutesFiles: result.routes, + islands: islandMods, + writeSpecifier: (file) => { + const def = islands.get(file); + if (def) { + return `fresh-island::${def.name}`; + } + return path.toFileUrl(file).href; + }, + }); - return code; + return code; + }, }, }, { name: "fresh:island-resolver", - resolveId(id) { - if (id.startsWith("fresh-island::")) { + resolveId: { + filter: { + id: /^fresh-island::.*/, + }, + handler(id) { let name = id.slice("fresh-island::".length); if (JS_REG.test(name)) { @@ -315,7 +327,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] { const spec = islandSpecByName.get(name); if (spec !== undefined) return spec; - } + }, }, }]; } diff --git a/packages/plugin-vite/src/plugins/verify_imports.ts b/packages/plugin-vite/src/plugins/verify_imports.ts index cb49b9c7..39c842f5 100644 --- a/packages/plugin-vite/src/plugins/verify_imports.ts +++ b/packages/plugin-vite/src/plugins/verify_imports.ts @@ -51,91 +51,98 @@ export function checkImports(pluginOptions: CheckImportOptions): Plugin { configResolved(config) { root = pathWithRoot(config.root); }, - async resolveId(id, importer, options) { - if ( - id.startsWith("\0") || id.startsWith("/@fs/") || - id.startsWith("fresh-island::") || id.startsWith("fresh:") || - id.includes("node_modules") || - importer && + resolveId: { + filter: { + id: [ + /^(?!\0|[\\/]@fs[\\/]|fresh-island::|fresh:)/, + /[\\/]node_modules[\\/]/, + ], + }, + async handler(id, importer, options) { + if ( + importer && (importer.startsWith("\0") || importer.includes("node_modules") || importer.includes("deno::")) - ) { - return; - } - const env = options.ssr ? "ssr" : "client"; + ) { + return; + } + const env = options.ssr ? "ssr" : "client"; - let result: ImportCheckDiagnostic | undefined; - if (id.startsWith(".")) { - const resolved = await this.resolve(id, importer, options); + let result: ImportCheckDiagnostic | undefined; + if (id.startsWith(".")) { + const resolved = await this.resolve(id, importer, options); - if (resolved !== null) { - const key = `${env}::${resolved.id}::${importer}`; - if (!seen.has(key)) { - result = check(pluginOptions, resolved.id, env); + if (resolved !== null) { + const key = `${env}::${resolved.id}::${importer}`; + if (!seen.has(key)) { + result = check(pluginOptions, resolved.id, env); + } + + seen.add(key); + } + } else { + const key = `${env}::${id}::${importer}`; + if (!seen.has(key)) { + result = check(pluginOptions, id, env); } - seen.add(key); } - } else { - const key = `${env}::${id}::${importer}`; - if (!seen.has(key)) { - result = check(pluginOptions, id, env); - } - seen.add(key); - } - if (result) { - const label = result.type === "warn" - ? cl.inverse(cl.yellow(` WARN `)) - : cl.inverse(cl.red(` ERROR `)); + if (result) { + const label = result.type === "warn" + ? cl.inverse(cl.yellow(` WARN `)) + : cl.inverse(cl.red(` ERROR `)); - // deno-lint-ignore no-console - console.log(); - // deno-lint-ignore no-console - console.log(); - // deno-lint-ignore no-console - console.log(`${label} ${result.message}`); - // deno-lint-ignore no-console - console.log(); + // deno-lint-ignore no-console + console.log(); + // deno-lint-ignore no-console + console.log(); + // deno-lint-ignore no-console + console.log(`${label} ${result.message}`); + // deno-lint-ignore no-console + console.log(); - if (importer !== undefined) { - const ancestors = findAncestors(this, importer, isDev); + if (importer !== undefined) { + const ancestors = findAncestors(this, importer, isDev); - if (ancestors && ancestors.length > 0) { - // deno-lint-ignore no-console - console.log(`The specifier ${cl.cyan(`"${id}"`)} was imported in:`); - ancestors.forEach((spec) => { - if (path.isAbsolute(spec)) { - spec = path.relative(root, spec); - } + if (ancestors && ancestors.length > 0) { // deno-lint-ignore no-console - console.log(` - ${cl.cyan(spec)}`); - }); + console.log( + `The specifier ${cl.cyan(`"${id}"`)} was imported in:`, + ); + ancestors.forEach((spec) => { + if (path.isAbsolute(spec)) { + spec = path.relative(root, spec); + } + // deno-lint-ignore no-console + console.log(` - ${cl.cyan(spec)}`); + }); + // deno-lint-ignore no-console + console.log(); + } + } + + if (result.hint) { + // deno-lint-ignore no-console + console.log(cl.bold(` hint: `) + result.hint); // deno-lint-ignore no-console console.log(); } - } - if (result.hint) { - // deno-lint-ignore no-console - console.log(cl.bold(` hint: `) + result.hint); - // deno-lint-ignore no-console - console.log(); + if (result.type === "error") { + this.error({ + message: result.message, + id: importer, + }); + } else { + this.warn({ + message: result.message, + id: importer, + }); + } } - - if (result.type === "error") { - this.error({ - message: result.message, - id: importer, - }); - } else { - this.warn({ - message: result.message, - id: importer, - }); - } - } + }, }, }; } diff --git a/www/deno.json b/www/deno.json index 6a219087..5353381b 100644 --- a/www/deno.json +++ b/www/deno.json @@ -6,7 +6,7 @@ }, "imports": { "@tailwindcss/vite": "npm:@tailwindcss/vite@^4.1.12", - "vite": "npm:vite@^7.1.3", + "vite": "npm:vite@^7.1.4", "vite-plugin-inspect": "npm:vite-plugin-inspect@^11.3.2" } }