Skip to content

Commit

Permalink
feat(watcher): use @parcel/watcher to watch entire app directory
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 2, 2023
1 parent c8388f1 commit c695053
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"test": "pnpm lint && vitest run --coverage"
},
"dependencies": {
"@parcel/watcher": "^2.2.0",
"citty": "^0.1.2",
"clipboardy": "^3.0.0",
"consola": "^3.2.3",
Expand Down
132 changes: 124 additions & 8 deletions pnpm-lock.yaml

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

5 changes: 3 additions & 2 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,17 @@ export async function createImporter(input: string, _cwd?: string) {
input = `./${input}`;
}

Check warning on line 86 in src/_utils.ts

View check run for this annotation

Codecov / codecov/patch

src/_utils.ts#L83-L86

Added lines #L83 - L86 were not covered by tests
const entry = _jitiRequire.resolve(resolve(cwd, input));
const entry = _jitiRequire.resolve(input);

const _import = () => {
const r = _jitiRequire(input);
return Promise.resolve(r.default || r);
};

return {
cwd,
relative: (path: string) => relative(cwd, path),

Check warning on line 96 in src/_utils.ts

View check run for this annotation

Codecov / codecov/patch

src/_utils.ts#L95-L96

Added lines #L95 - L96 were not covered by tests
entry,
relativeEntry: relative(cwd, entry),
import: _import,
};
}
22 changes: 17 additions & 5 deletions src/watch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RequestListener } from "node:http";
import { watch } from "node:fs";
import { consola } from "consola";
import { dirname } from "pathe";
import type { Listener, ListenOptions, WatchOptions } from "./types";
import { listen } from "./listen";
import { createImporter } from "./_utils";
Expand All @@ -21,20 +21,32 @@ export async function listenAndWatch(

resolveHandle();

const watcher = await watch(importer.entry, () => {
logger.info(`\`${importer.relativeEntry}\` changed, Reloading...`);
// https://github.com/parcel-bundler/watcher
const { subscribe } = await import("@parcel/watcher").then(
(r) => r.default || r,
);

const entryDir = dirname(importer.entry);
const watcher = await subscribe(entryDir, (_error, events) => {
logger.log(
`🔃 ${events
.map((e) => `\`./${importer.relative(e.path)}\` ${e.type}d`)
.join(", ")}. Reloading server...`,
);

Check warning on line 35 in src/watch.ts

View check run for this annotation

Codecov / codecov/patch

src/watch.ts#L24-L35

Added lines #L24 - L35 were not covered by tests
resolveHandle();
});

const listenter = await listen((...args) => {
return handle(...args);
}, options);

logger.info(`Watching \`${importer.relativeEntry}\` for changes.`);
logger.log(`👀 Watching \`./${importer.relative(entryDir)}\` for changes.`);

Check warning on line 43 in src/watch.ts

View check run for this annotation

Codecov / codecov/patch

src/watch.ts#L43

Added line #L43 was not covered by tests

const _close = listenter.close;
listenter.close = async () => {
watcher.close();
await watcher.unsubscribe().catch((error) => {
logger.error(error);
});

Check warning on line 49 in src/watch.ts

View check run for this annotation

Codecov / codecov/patch

src/watch.ts#L47-L49

Added lines #L47 - L49 were not covered by tests
await _close();
};

Expand Down

0 comments on commit c695053

Please sign in to comment.