Skip to content

Commit

Permalink
make outDir optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Jul 3, 2023
1 parent 472e693 commit 3d21b33
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 30 deletions.
5 changes: 0 additions & 5 deletions examples/solid/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export default createApp({
{
name: "public",
mode: "static",
build: {
outDir: "./.build/client",
},
dir: "./public",
base: "/",
},
Expand All @@ -18,7 +15,6 @@ export default createApp({
handler: "./app/client.tsx",
build: {
target: "browser",
outDir: "./.build/api",
plugins: () => [solid({ ssr: true })],
},
base: "/_build",
Expand All @@ -29,7 +25,6 @@ export default createApp({
handler: "./app/server.tsx",
build: {
target: "node",
outDir: "./.build/api",
plugins: () => [solid({ ssr: true })],
},
},
Expand Down
4 changes: 0 additions & 4 deletions examples/spa/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export default createApp({
{
name: "public",
mode: "static",
build: {
outDir: "./.build/client",
},
dir: "./public",
base: "/",
},
Expand All @@ -18,7 +15,6 @@ export default createApp({
handler: "./index.html",
build: {
target: "browser",
outDir: "./.build/api",
plugins: () => [reactRefresh()],
},
base: "/",
Expand Down
88 changes: 70 additions & 18 deletions packages/vinxi/lib/app.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,85 @@
import { join } from "pathe";
import { isAbsolute, join, relative } from "pathe";

import { resolveConfig } from "./router/node-handler.js";
import { FileSystemRouter } from "./file-system-router.js";
import invariant from "./invariant.js";

function resolveConfig(router, appConfig) {
let handler = relative(
appConfig.root,
router.handler
? isAbsolute(router.handler)
? router.handler
: join(appConfig.root, router.handler)
: undefined,
);

// invariant(handler, "No handler found for node-handler router");

let dir = router.dir
? isAbsolute(router.dir)
? router.dir
: join(appConfig.root, router.dir)
: undefined;

let routerStyle = router.style ?? "static";

// invariant(
// routerStyle !== "static" ? dir : true,
// `There should be dir provided if the router style is ${routerStyle}`,
// );

let fileRouter =
routerStyle !== "static" && router.dir
? new FileSystemRouter({ dir, style: router.style })
: undefined;

// invariant(
// fileRouter ? router.handler : true,
// "No handler found for SPA router. When `dir` is being used with `style` for file system routing, `handler` must be specified.",
// );

const buildConfig = router.build
? {
...router.build,
outDir: router.build.outDir
? join(appConfig.root, router.build.outDir)
: join(appConfig.root, ".nitro", "build", router.name),
}
: {
outDir: join(appConfig.root, ".nitro", "build", router.name),
};

return {
base: "/",
...router,
build: buildConfig,
root: appConfig.root,
dir,
style: routerStyle,
fileRouter,
handler,
};
}

export function createApp({ routers, bundlers = [] }) {
const config = {
bundlers,
routers,
root: process.cwd(),
};

config.bundlers = bundlers.map(resolveBuildConfig);

function resolveBuildConfig(bundler) {
let outDir = bundler.outDir ? join(config.root, bundler.outDir) : undefined;
return {
target: "static",
root: config.root,
...bundler,
outDir,
};
}
// function resolveBuildConfig(bundler) {
// let outDir = bundler.outDir ? join(config.root, bundler.outDir) : undefined;
// return {
// target: "static",
// root: config.root,
// ...bundler,
// outDir,
// };
// }

config.routers = routers.map((router, index) => {
return {
...resolveConfig(router, config),
build:
typeof router.build === "string"
? bundlers.find((bundler) => bundler.name === router.build)
: resolveBuildConfig(router.build),
index,
};
});
Expand Down
5 changes: 2 additions & 3 deletions packages/vinxi/vinxi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ export type BundlerConfig = {

type UserRouterConfig = {
name: string;
build: string | Omit<BundlerConfig, 'name'>;
prefix?: string;
bundler?: BundlerConfig;
build?:Omit<BundlerConfig, 'name'>;
base?: string;
index?: number;
devServer?: ViteDevServer;
root?: string;
Expand Down

0 comments on commit 3d21b33

Please sign in to comment.