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

fix(watcher): use @parcel/watcher-wasm for non node.js environment compatibility #85

Merged
merged 4 commits into from
Aug 2, 2023
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"test": "pnpm lint && vitest run --coverage"
},
"dependencies": {
"@parcel/watcher": "^2.2.0",
"@parcel/watcher-wasm": "2.3.0-alpha.1",
"citty": "^0.1.2",
"clipboardy": "^3.0.0",
"consola": "^3.2.3",
Expand Down Expand Up @@ -66,4 +66,4 @@
"vitest": "^0.34.1"
},
"packageManager": "[email protected]"
}
}
128 changes: 10 additions & 118 deletions pnpm-lock.yaml

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

74 changes: 43 additions & 31 deletions src/server/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extname } from "node:path";
import { consola } from "consola";
import type { AsyncSubscription } from "@parcel/watcher";
import type { AsyncSubscription } from "@parcel/watcher-wasm";
import type { ConsolaInstance } from "consola";
import type { Listener, ListenOptions } from "../types";
import { listen } from "../listen";
Expand Down Expand Up @@ -44,39 +44,51 @@ export async function listenAndWatch(
};

// Start watcher
// https://github.com/parcel-bundler/watcher
const { subscribe } = await import("@parcel/watcher").then(
(r) => r.default || r,
);
// https://github.com/parcel-bundler/watcher#wasm
try {
const { default: init, subscribe } = await import("@parcel/watcher-wasm");

const jsExts = new Set([".js", ".mjs", ".cjs", ".ts", ".mts", ".cts"]);
watcher = await subscribe(
devServer.cwd,
(_error, events) => {
const filteredEvents = events.filter((e) => jsExts.has(extname(e.path)));
if (filteredEvents.length === 0) {
return;
}
const eventsString = filteredEvents
.map((e) => `${devServer.resolver.formateRelative(e.path)} ${e.type}d`)
.join(", ");
logger.start(` Reloading server (${eventsString})`);
devServer.reload();
},
{
ignore: options.ignore || [
"**/.git/**",
"**/node_modules/**",
"**/dist/**",
],
},
);
const jsExts = new Set([".js", ".mjs", ".cjs", ".ts", ".mts", ".cts"]);

logger.log(
`πŸ‘€ Watching ${devServer.resolver.formateRelative(
await (init as any)();

watcher = await subscribe(
devServer.cwd,
)} for changes`,
);
(_error, events) => {
const filteredEvents = events.filter((e) =>
jsExts.has(extname(e.path)),
);
if (filteredEvents.length === 0) {
return;
}
const eventsString = filteredEvents
.map(
(e) => `${devServer.resolver.formateRelative(e.path)} ${e.type}d`,
)
.join(", ");
logger.start(` Reloading server (${eventsString})`);
devServer.reload();
},
{
ignore: options.ignore || [
"**/.git/**",
"**/node_modules/**",
"**/dist/**",
],
},
);

logger.log(
`πŸ‘€ Watching ${devServer.resolver.formateRelative(
devServer.cwd,
)} for changes`,
);
} catch (error) {
logger.error(error);
logger.warn(
"πŸ‘€ Cannot start the watcher! Please report this issue to `https://github.com/unjs/listhen`",
);
}

return listenter;
}