diff --git a/.changeset/cuddly-emus-join.md b/.changeset/cuddly-emus-join.md new file mode 100644 index 00000000..077dcc87 --- /dev/null +++ b/.changeset/cuddly-emus-join.md @@ -0,0 +1,5 @@ +--- +"vinxi": patch +--- + +feat: nitro plugin support in dev diff --git a/.changeset/tender-coats-repeat.md b/.changeset/tender-coats-repeat.md index 31c15a03..86905fa3 100644 --- a/.changeset/tender-coats-repeat.md +++ b/.changeset/tender-coats-repeat.md @@ -1,5 +1,5 @@ --- -"@fake-scope/fake-pkg": patch +"vinxi": minor --- chore: bump nitro to v2.10.4, h3 to v13.0 diff --git a/packages/vinxi/lib/build.js b/packages/vinxi/lib/build.js index 948abb80..be54b41c 100644 --- a/packages/vinxi/lib/build.js +++ b/packages/vinxi/lib/build.js @@ -1,6 +1,7 @@ import { mkdir, rm, rmdir } from "fs/promises"; import { createRequire } from "module"; import { build, copyPublicAssets, createNitro, prerender } from "nitropack"; +import { isRelative } from "ufo"; import { readdirSync, statSync, writeFileSync } from "node:fs"; import { pathToFileURL } from "node:url"; @@ -12,7 +13,13 @@ import invariant from "./invariant.js"; import { c, consola, log, withLogger } from "./logger.js"; import { viteManifestPath } from "./manifest-path.js"; import { createSPAManifest } from "./manifest/spa-manifest.js"; -import { handlerModule, join, relative, virtualId } from "./path.js"; +import { + handlerModule, + isAbsolute, + join, + relative, + virtualId, +} from "./path.js"; import { config } from "./plugins/config.js"; import { manifest } from "./plugins/manifest.js"; import { routes } from "./plugins/routes.js"; @@ -124,7 +131,13 @@ export async function createBuild(app, buildConfig) { fileURLToPath(new URL("./app-fetch.js", import.meta.url)), fileURLToPath(new URL("./app-manifest.js", import.meta.url)), "$vinxi/chunks", - ...(app.config.server.plugins ?? []), + ...(app.config.server.plugins ?? []).map((plugin) => + isRelative(plugin) + ? plugin + : isAbsolute(plugin) + ? plugin + : require.resolve(plugin, { paths: [app.config.root] }), + ), ], buildDir: ".vinxi", handlers: [ diff --git a/packages/vinxi/lib/dev-server.js b/packages/vinxi/lib/dev-server.js index bcee4b0a..dc448253 100644 --- a/packages/vinxi/lib/dev-server.js +++ b/packages/vinxi/lib/dev-server.js @@ -205,16 +205,28 @@ export async function createDevServer( // } // Running plugins manually + const devViteServer = await import("vite").then(({ createServer }) => + createServer({}), + ); + const plugins = [ - new URL("./app-fetch.js", import.meta.url).href, - new URL("./app-manifest.js", import.meta.url).href, + fileURLToPath(new URL("./app-fetch.js", import.meta.url)), + fileURLToPath(new URL("./app-manifest.js", import.meta.url)), + ...(app.config.server.plugins ?? []), ]; for (const plugin of plugins) { - const { default: pluginFn } = await import(plugin); - await pluginFn(devApp); + const { default: pluginFn } = await devViteServer.ssrLoadModule(plugin); + try { + await pluginFn(devApp); + } catch (error) { + console.error(`Error running plugin ${plugin}`); + console.error(error); + } } + await devViteServer.close(); + return { ...devApp, listen: async () => {