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

Add --noWatch option to serve command #523

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/itchy-peas-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ladle/react": patch
---

Add --noWatch option to serve command
7 changes: 6 additions & 1 deletion packages/ladle/lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ program
.option("--viteConfig [string]", "file with Vite configuration")
.option("--base [string]", "base URL path for build output")
.option("--mode [string]", "Vite mode")
.option("--noWatch", "Disable file system watching")
.action(serve);

program
Expand Down Expand Up @@ -55,7 +56,11 @@ program
.option("--base [string]", "base URL path for build output")
.option("--mode [string]", "Vite mode")
.action(async (params) => {
await preview({ ...params, previewHost: params.host, previewPort: params.port });
await preview({
...params,
previewHost: params.host,
previewPort: params.port,
});
});

program.parse(process.argv);
92 changes: 50 additions & 42 deletions packages/ladle/lib/cli/vite-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const bundler = async (config, configFolder) => {
fs: {
allow: [searchForWorkspaceRoot(process.cwd())],
},
// TODO: pass null instead once this diff is included in release
// https://github.com/vitejs/vite/pull/14208
// watch: config.noWatch ? null : undefined,
watch: {
ignored: config.noWatch ? "**" : undefined,
},
},
});
const vite = await createServer(viteConfig);
Expand Down Expand Up @@ -149,50 +155,52 @@ const bundler = async (config, configFolder) => {
http.createServer(app.callback()).listen(port, hostname, listenCallback);
}

// trigger full reload when new stories are added or removed
const watcher = chokidar.watch(config.stories, {
persistent: true,
ignoreInitial: true,
});
let checkSum = "";
const getChecksum = async () => {
try {
const entryData = await getEntryData(
await globby(
Array.isArray(config.stories) ? config.stories : [config.stories],
),
);
const jsonContent = getMetaJsonObject(entryData);
// loc changes should not grant a full reload
Object.keys(jsonContent.stories).forEach((storyId) => {
jsonContent.stories[storyId].locStart = 0;
jsonContent.stories[storyId].locEnd = 0;
});
return JSON.stringify(jsonContent);
} catch (e) {
return checkSum;
}
};
checkSum = await getChecksum();
const invalidate = async () => {
const newChecksum = await getChecksum();
if (checkSum === newChecksum) return;
checkSum = newChecksum;
const module = moduleGraph.getModuleById("\0virtual:generated-list");
if (module) {
moduleGraph.invalidateModule(module);
if (ws) {
ws.send({
type: "full-reload",
path: "*",
if (config.noWatch === false) {
// trigger full reload when new stories are added or removed
const watcher = chokidar.watch(config.stories, {
persistent: true,
ignoreInitial: true,
});
let checkSum = "";
const getChecksum = async () => {
try {
const entryData = await getEntryData(
await globby(
Array.isArray(config.stories) ? config.stories : [config.stories],
),
);
const jsonContent = getMetaJsonObject(entryData);
// loc changes should not grant a full reload
Object.keys(jsonContent.stories).forEach((storyId) => {
jsonContent.stories[storyId].locStart = 0;
jsonContent.stories[storyId].locEnd = 0;
});
return JSON.stringify(jsonContent);
} catch (e) {
return checkSum;
}
}
};
watcher
.on("add", invalidate)
.on("change", invalidate)
.on("unlink", invalidate);
};
checkSum = await getChecksum();
const invalidate = async () => {
const newChecksum = await getChecksum();
if (checkSum === newChecksum) return;
checkSum = newChecksum;
const module = moduleGraph.getModuleById("\0virtual:generated-list");
if (module) {
moduleGraph.invalidateModule(module);
if (ws) {
ws.send({
type: "full-reload",
path: "*",
});
}
}
};
watcher
.on("add", invalidate)
.on("change", invalidate)
.on("unlink", invalidate);
}
} catch (e) {
console.log(e);
}
Expand Down
1 change: 1 addition & 0 deletions packages/ladle/lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export type Config = {
outDir: string;
base?: string;
mode?: string;
noWatch?: boolean;
chasestarr marked this conversation as resolved.
Show resolved Hide resolved
hotkeys: {
fullscreen: string[];
search: string[];
Expand Down
Loading