Skip to content

Commit

Permalink
better support vite.config.js by default
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Aug 1, 2023
1 parent 982878a commit ba888f1
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 46 deletions.
21 changes: 0 additions & 21 deletions examples/solid/spa/basic/app.config.js

This file was deleted.

1 change: 1 addition & 0 deletions examples/solid/spa/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"solid-js": "^1.7.8",
"tailwindcss": "^3.3.2",
"vinxi": "workspace:^",
"vite": "^4.4.8",
"vite-plugin-solid": "^2.7.0"
}
}
6 changes: 6 additions & 0 deletions examples/solid/spa/basic/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import solid from "vite-plugin-solid";

export default defineConfig({
plugins: [solid()],
});
22 changes: 4 additions & 18 deletions packages/vinxi/bin/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
import { loadConfig } from "c12";
import mri from "mri";
import { join, resolve } from "pathe";
import { pathToFileURL } from "url";
import { resolve } from "pathe";

import { loadApp } from "../lib/load-app.js";

async function main() {
const args = mri(process.argv.slice(2));
Expand All @@ -11,21 +11,7 @@ async function main() {

const configFile = args.config;
globalThis.MANIFEST = {};

/** @type {{ config: import("../lib/app.js").App }}*/
const { config: app } = await loadConfig(
configFile
? {
configFile,
}
: {
name: "app",
},
);

if (!app.config) {
throw new Error("No config found");
}
const app = await loadApp(configFile);

if (command === "dev") {
const { createDevServer } = await import("../lib/dev-server.js");
Expand Down
5 changes: 2 additions & 3 deletions packages/vinxi/lib/app-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { parentPort } from "node:worker_threads";

import { createDevServer } from "./dev-server.js";
import invariant from "./invariant.js";
import { loadApp } from "./load-app.js";

/**
*
Expand Down Expand Up @@ -130,9 +131,7 @@ class AppWorker {
const {
req: { url, method, headers },
} = rest;
const { config: app } = await loadConfig({
name: "app",
});
const app = await loadApp();

try {
if (!this.server) {
Expand Down
55 changes: 55 additions & 0 deletions packages/vinxi/lib/load-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { loadConfig } from "c12";

import { createApp } from "./app.js";

/**
*
* @param {string | undefined} configFile
* @returns {Promise<import("./app.js").App>}
*/
export async function loadApp(configFile = undefined) {
/** @type {{ config: import("./app.js").App }}*/
let { config: app } = await loadConfig(
configFile
? {
configFile,
}
: {
name: "app",
},
);

if (!app.config) {
const { config } = await loadConfig({
name: "vite",
});

if (config.config) {
console.warn("Found vite.config.js with app config");
// @ts-expect-error trying to send c12's config as app
return config;
} else {
console.warn("No app config found. Assuming SPA app.");
return createApp({
routers: [
{
name: "public",
mode: "static",
dir: "./public",
},
{
name: "client",
mode: "spa",
handler: "./index.html",
build: {
target: "browser",
plugins: () => config.plugins ?? [],
},
},
],
});
}
}

return app;
}
73 changes: 69 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba888f1

Please sign in to comment.