feat(init): add --vite to bootstrap with vite (#3270)

This commit is contained in:
Marvin Hagemeister
2025-08-28 15:43:36 +02:00
committed by GitHub
parent b0e75b7199
commit 62b62058fa
3 changed files with 118 additions and 6 deletions
+44 -5
View File
@@ -66,6 +66,7 @@ export async function initProject(
force?: boolean | null; force?: boolean | null;
tailwind?: boolean | null; tailwind?: boolean | null;
vscode?: boolean | null; vscode?: boolean | null;
vite?: boolean | null;
} = {}, } = {},
): Promise<void> { ): Promise<void> {
console.log(); console.log();
@@ -108,6 +109,8 @@ export async function initProject(
} }
} }
const useVite = flags.vite ?? false;
const useDocker = flags.docker; const useDocker = flags.docker;
let useTailwind = flags.tailwind || false; let useTailwind = flags.tailwind || false;
if (flags.tailwind == null) { if (flags.tailwind == null) {
@@ -320,7 +323,13 @@ ${GRADIENT_CSS}`;
${GRADIENT_CSS}`; ${GRADIENT_CSS}`;
const cssStyles = useTailwind ? TAILWIND_CSS : NO_TAILWIND_STYLES; const cssStyles = useTailwind ? TAILWIND_CSS : NO_TAILWIND_STYLES;
await writeFile("static/styles.css", cssStyles);
if (useVite) {
await writeFile("assets/styles.css", cssStyles);
await writeFile("client.ts", `import "./assets/styles.css";`);
} else {
await writeFile("static/styles.css", cssStyles);
}
// deno-fmt-ignore // deno-fmt-ignore
const STATIC_LOGO = const STATIC_LOGO =
`<svg width="40" height="40" fill="none" xmlns="http://www.w3.org/2000/svg"> `<svg width="40" height="40" fill="none" xmlns="http://www.w3.org/2000/svg">
@@ -458,8 +467,9 @@ export default define.page(function App({ Component }) {
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>${path.basename(projectDir)}</title> <title>${path.basename(projectDir)}</title>${
<link rel="stylesheet" href="/styles.css" /> useVite ? "" : `\n <link rel="stylesheet" href="/styles.css" />`
}
</head> </head>
<body> <body>
<Component /> <Component />
@@ -510,7 +520,9 @@ if (Deno.args.includes("build")) {
} else { } else {
await builder.listen(() => import("./main.ts")); await builder.listen(() => import("./main.ts"));
}`; }`;
await writeFile("dev.ts", DEV_TS); if (!useVite) {
await writeFile("dev.ts", DEV_TS);
}
const denoJson = { const denoJson = {
nodeModulesDir: "auto", nodeModulesDir: "auto",
@@ -555,7 +567,19 @@ if (Deno.args.includes("build")) {
}, },
}; };
if (useTailwind) { if (useVite) {
denoJson.tasks.dev = "vite";
denoJson.tasks.build = "vite build";
denoJson.imports["@fresh/plugin-vite"] = "jsr:@fresh/plugin-vite@^0.9.5";
denoJson.imports["vite"] = "npm:vite@^7.1.3";
if (useTailwind) {
denoJson.imports["tailwindcss"] =
`npm:tailwindcss@^${TAILWINDCSS_VERSION}`;
denoJson.imports["@tailwindcss/vite"] = `npm:@tailwindcss/vite@^4.1.12`;
}
} else if (useTailwind) {
denoJson.imports["tailwindcss"] = `npm:tailwindcss@^${TAILWINDCSS_VERSION}`; denoJson.imports["tailwindcss"] = `npm:tailwindcss@^${TAILWINDCSS_VERSION}`;
denoJson.imports["@fresh/plugin-tailwind"] = denoJson.imports["@fresh/plugin-tailwind"] =
`jsr:@fresh/plugin-tailwind@^${FRESH_TAILWIND_VERSION}`; `jsr:@fresh/plugin-tailwind@^${FRESH_TAILWIND_VERSION}`;
@@ -566,6 +590,21 @@ if (Deno.args.includes("build")) {
await writeFile("deno.json", denoJson); await writeFile("deno.json", denoJson);
if (useVite) {
let viteConfig = `import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";\n`;
if (useTailwind) {
viteConfig += `import tailwindcss from "@tailwindcss/vite";\n`;
}
viteConfig += `\nexport default defineConfig({
plugins: [fresh()${useTailwind ? ", tailwindcss()" : ""}],
});`;
await writeFile("vite.config.ts", viteConfig);
}
const README_MD = `# Fresh project const README_MD = `# Fresh project
Your new Fresh project is ready to go. You can follow the Fresh "Getting Your new Fresh project is ready to go. You can follow the Fresh "Getting
+72
View File
@@ -51,6 +51,20 @@ async function expectProjectFile(dir: string, pathname: string) {
} }
} }
async function expectNotProjectFile(dir: string, pathname: string) {
const filePath = path.join(dir, ...pathname.split("/").filter(Boolean));
try {
const stat = await Deno.stat(filePath);
if (stat.isFile) {
throw new Error(`A project file but expected it not to be: ${filePath}`);
}
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
}
async function readProjectFile(dir: string, pathname: string): Promise<string> { async function readProjectFile(dir: string, pathname: string): Promise<string> {
const filePath = path.join(dir, ...pathname.split("/").filter(Boolean)); const filePath = path.join(dir, ...pathname.split("/").filter(Boolean));
const content = await Deno.readTextFile(filePath); const content = await Deno.readTextFile(filePath);
@@ -247,3 +261,61 @@ Deno.test("init - errors on missing build cache in prod", async () => {
expect(stderr).toMatch(/Module not found/); expect(stderr).toMatch(/Module not found/);
}); });
// There is a peerDependency issue with links
Deno.test.ignore("init - vite dev server", async () => {
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], { vite: true });
await expectProjectFile(dir, "vite.config.ts");
await expectNotProjectFile(dir, "dev.ts");
await patchProject(dir);
await withChildProcessServer(
{ cwd: dir, args: ["task", "dev"] },
async (address) => {
await withBrowser(async (page) => {
await page.goto(address);
await page.locator("#decrement").click();
await waitForText(page, "button + p", "2");
});
},
);
});
// There is a peerDependency issue with links
Deno.test.ignore("init - vite build", async () => {
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], { vite: true });
await expectProjectFile(dir, "vite.config.ts");
await patchProject(dir);
// Build
await new Deno.Command(Deno.execPath(), {
args: ["task", "build"],
stdin: "null",
stdout: "piped",
stderr: "piped",
cwd: dir,
}).output();
await withChildProcessServer(
{ cwd: dir, env: { PORT: "0" }, args: ["task", "start"] },
async (address) => {
await withBrowser(async (page) => {
await page.goto(address);
await page.locator("button").click();
await waitForText(page, "button + p", "2");
});
},
);
});
+2 -1
View File
@@ -3,12 +3,13 @@ import { initProject } from "./init.ts";
import { InitError } from "./init.ts"; import { InitError } from "./init.ts";
const flags = parseArgs(Deno.args, { const flags = parseArgs(Deno.args, {
boolean: ["force", "tailwind", "vscode", "docker", "help"], boolean: ["force", "tailwind", "vscode", "docker", "help", "vite"],
default: { default: {
force: null, force: null,
tailwind: null, tailwind: null,
vscode: null, vscode: null,
docker: null, docker: null,
vite: null,
}, },
alias: { alias: {
help: "h", help: "h",