Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Restart server when config file dependencies have changed #203

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/sidebar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Sidebar } from "zudoku";

export const sidebar: Sidebar = {
docs: [
{
type: "category",
label: "Getting started",
icon: "sparkles",
items: ["introduction", "app-quickstart", "html-quickstart"],
},
{
type: "category",
label: "Configuration",
icon: "cog",
link: "configuration/overview",
items: [
"configuration/api-reference",
"configuration/navigation",
"configuration/search",
"configuration/authentication",
"configuration/vite-config",
],
},
{
type: "category",
label: "Markdown",
icon: "book-open-text",
link: "markdown/overview",
items: ["markdown/mdx", "markdown/admonitions", "markdown/code-blocks"],
},
{
type: "category",
label: "Guide",
icon: "monitor-check",
items: ["environment-variables", "custom-pages", "using-multiple-apis"],
},
{
type: "category",
label: "Deployment",
icon: "cloud-upload",
link: "deployment",
items: [
"deploy/cloudflare-pages",
"deploy/github-pages",
"deploy/vercel",
"deploy/direct-upload",
],
},
{
type: "category",
label: "Extending",
icon: "blocks",
items: ["custom-plugins", "api-keys"],
},
],
};
2 changes: 1 addition & 1 deletion docs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"useUnknownInCatchVariables": false,
"jsx": "react-jsx"
},
"include": ["zudoku.config.tsx", "src"]
"include": ["zudoku.config.tsx", "src", "sidebar.ts"]
}
56 changes: 2 additions & 54 deletions docs/zudoku.config.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ZudokuConfig } from "zudoku";
import { sidebar } from "./sidebar";
import PreviewBanner from "./src/PreviewBanner";

const config: ZudokuConfig = {
Expand All @@ -23,60 +24,7 @@ const config: ZudokuConfig = {
{ from: "/docs/getting-started", to: "/docs/app-quickstart" },
],
topNavigation: [{ id: "docs", label: "Documentation" }],
sidebar: {
docs: [
{
type: "category",
label: "Getting started",
icon: "sparkles",
items: ["introduction", "app-quickstart", "html-quickstart"],
},
{
type: "category",
label: "Configuration",
icon: "cog",
link: "configuration/overview",
items: [
"configuration/api-reference",
"configuration/navigation",
"configuration/search",
"configuration/authentication",
"configuration/vite-config",
],
},
{
type: "category",
label: "Markdown",
icon: "book-open-text",
link: "markdown/overview",
items: ["markdown/mdx", "markdown/admonitions", "markdown/code-blocks"],
},
{
type: "category",
label: "Guide",
icon: "monitor-check",
items: ["environment-variables", "custom-pages", "using-multiple-apis"],
},
{
type: "category",
label: "Deployment",
icon: "cloud-upload",
link: "deployment",
items: [
"deploy/cloudflare-pages",
"deploy/github-pages",
"deploy/vercel",
"deploy/direct-upload",
],
},
{
type: "category",
label: "Extending",
icon: "blocks",
items: ["custom-plugins", "api-keys"],
},
],
},
sidebar,
};

export default config;
3 changes: 2 additions & 1 deletion packages/zudoku/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ZudokuConfig } from "./validators/validate.js";
import type { LoadedConfig } from "../vite/config.js";

export type URLString = `https://${string}` | `http://${string}`;

export { type ZudokuConfig };

export interface ZudokuPluginOptions extends ZudokuConfig {
export interface ZudokuPluginOptions extends LoadedConfig {
rootDir: string;
moduleDir: string;

Expand Down
2 changes: 2 additions & 0 deletions packages/zudoku/src/config/validators/InputSidebarSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ export const InputSidebarItemCategorySchema: z.ZodType<InputSidebarItemCategory>
export const InputSidebarSchema = InputSidebarItemSchema.array();

export type InputSidebar = z.infer<typeof InputSidebarSchema>;

export type ConfigSidebar = Record<string, InputSidebar>;
2 changes: 1 addition & 1 deletion packages/zudoku/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Config
export type { ZudokuConfig } from "./config/config.js";
export type { InputSidebar as Sidebar } from "./config/validators/InputSidebarSchema.js";
export type { ConfigSidebar as Sidebar } from "./config/validators/InputSidebarSchema.js";
export type { MDXImport } from "./lib/plugins/markdown/index.js";
12 changes: 9 additions & 3 deletions packages/zudoku/src/vite/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function getPluginOptions({
}): ZudokuPluginOptions {
const moduleDir = getModuleDir();
return {
...config,
...config!,
rootDir: dir,
moduleDir,
mode,
Expand All @@ -151,10 +151,16 @@ export function getPluginOptions({
export async function getViteConfig(
dir: string,
configEnv: ZudokuConfigEnv,
onConfigChange?: (config: LoadedConfig) => void,
): Promise<InlineConfig> {
const config = await loadZudokuConfig(dir);

const onConfigChange = () => loadZudokuConfig(dir, true);
const handleConfigChange = async () => {
const config = await loadZudokuConfig(dir, true);
onConfigChange?.(config);

return config;
};

validateConfig(config);

Expand Down Expand Up @@ -236,7 +242,7 @@ export async function getViteConfig(
vitePluginSsrCss({
entries: [`${pluginOptions.moduleDir}/src/app/entry.client.tsx`],
}),
vitePlugin(pluginOptions, onConfigChange),
vitePlugin(pluginOptions, handleConfigChange),
],
css: {
postcss: {
Expand Down
14 changes: 9 additions & 5 deletions packages/zudoku/src/vite/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ export class DevServer {
async start() {
const app = express();

const viteConfig = await getViteConfig(this.options.dir, {
mode: "development",
command: "serve",
isSsrBuild: this.options.ssr,
});
const viteConfig = await getViteConfig(
this.options.dir,
{
mode: "development",
command: "serve",
isSsrBuild: this.options.ssr,
},
(zudokuConfig) => (this.currentConfig = zudokuConfig),
);

const vite = await createViteServer(viteConfig);

Expand Down
16 changes: 11 additions & 5 deletions packages/zudoku/src/vite/plugin-config-reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@ import path from "node:path";
import { type Plugin } from "vite";
import { printDiagnosticsToConsole } from "../cli/common/output.js";
import { type ZudokuPluginOptions } from "../config/config.js";
import { getConfigFilePath, type LoadedConfig } from "./config.js";
import { type LoadedConfig } from "./config.js";

export const createConfigReloadPlugin = (
initialConfig: ZudokuPluginOptions,
onConfigChange?: () => Promise<LoadedConfig>,
): [Plugin, () => ZudokuPluginOptions] => {
let currentConfig = initialConfig;
let importDependencies = initialConfig.__meta.dependencies;

const plugin = {
name: "zudoku-config-reload",
configureServer: ({ watcher, restart }) => {
if (!onConfigChange) return;

watcher.on("change", async (file) => {
const configFilePath = await getConfigFilePath(currentConfig.rootDir);

if (file !== configFilePath) return;
if (!file.startsWith(currentConfig.rootDir)) return;
if (!importDependencies.includes(file)) return;

const newConfig = await onConfigChange();
currentConfig = { ...initialConfig, ...newConfig };

importDependencies = newConfig.__meta.dependencies;

// Assume `.tsx` files are handled by HMR (skip if the config file itself changed)
if (file !== newConfig.__meta.path && file.endsWith(".tsx")) return;

await restart();
printDiagnosticsToConsole(
`${new Date().toLocaleTimeString()} Config ${path.basename(file)} changed. Restarted server.`,
`[${new Date().toLocaleTimeString()}]: Config ${path.basename(currentConfig.__meta.path)} changed. Restarted server.`,
);
});
},
Expand Down
13 changes: 8 additions & 5 deletions packages/zudoku/vite.standalone.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ const entries: Record<string, string> = {
demo: "./src/app/demo.tsx",
};

const pluginOptions = getPluginOptions({
mode: "standalone",
dir: path.resolve(__dirname),
});
const config = {
...getPluginOptions({
mode: "standalone",
dir: path.resolve(__dirname),
}),
__meta: {},
};

export default defineConfig({
worker: {
Expand Down Expand Up @@ -55,7 +58,7 @@ export default defineConfig({
},
},
},
plugins: [vitePlugin(pluginOptions)],
plugins: [vitePlugin(config)],
css: {
postcss: {
plugins: [
Expand Down