Skip to content

Commit

Permalink
feat: nitro plugin support in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Dec 1, 2024
1 parent a567f4f commit dea07bf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
17 changes: 15 additions & 2 deletions packages/vinxi/lib/build.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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: [
Expand Down
20 changes: 16 additions & 4 deletions packages/vinxi/lib/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit dea07bf

Please sign in to comment.