Skip to content

Commit

Permalink
chore(inline-svg): correctly watch svg files and trigger full page re…
Browse files Browse the repository at this point in the history
…load
  • Loading branch information
vnphanquang committed Jul 9, 2024
1 parent 16b5eb6 commit 6f6a363
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-seas-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@svelte-put/inline-svg': patch
---

correct watcher logics to use directories instead of glob patterns
66 changes: 37 additions & 29 deletions packages/inline-svg/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function inlineSvg(sources, config) {
const { typedef, ...pConfig } = config ?? {};
const rConfig = resolveInlineSvgConfig(pConfig);
const rSources = resolveSources(sources);

return {
name: 'vite-plugin-svelte-preprocess-inline-svg',
api: {
Expand All @@ -26,40 +27,47 @@ export function inlineSvg(sources, config) {
enforce: 'pre',
configureServer(server) {
const root = server.config.root;
const rTypedef = typeof typedef === 'string'
? typedef
: typedef === true
? path.resolve(root, 'src/preprocess-inline-svg.d.ts')
: null

if (rTypedef) {
generateSourceTyping(rSources, rConfig, rTypedef);

const updateSourceTyping = debounce(() => {
generateSourceTyping(rSources, rConfig, rTypedef);
server.ws.send({ type: 'full-reload' });
});
const rTypedef =
typeof typedef === 'string'
? typedef
: typedef === true
? path.resolve(root, 'src/preprocess-inline-svg.d.ts')
: null;
if (rTypedef) generateSourceTyping(rSources, rConfig, rTypedef);

const directories = [...rSources.local.directories, ...rSources.dirs.flatMap((d) => d.directories)];

server.watcher.add(directories.map((dir) => `${dir}/**/*.svg`));
server.watcher.on('add', (file) => {
if (matchFileExtension(file, ['.svg'])) {
updateSourceTyping();
}
});
server.watcher.on('unlink', (file) => {
if (matchFileExtension(file, ['.svg'])) {
updateSourceTyping();
}
});
server.watcher.on('change', (file) => {
const reload = debounce(
/**
* @param {string} file
* @param {boolean} [skip]
*/
(file, skip = false) => {
if (matchFileExtension(file, ['.svg'])) {
if (rTypedef && !skip) {
generateSourceTyping(rSources, rConfig, rTypedef);
}
server.ws.send({ type: 'full-reload' });
server.moduleGraph.invalidateAll();
}
},
);

const directories = [
...rSources.local.directories,
...rSources.dirs.flatMap((d) => d.directories),
];
server.watcher.add(directories);

server.watcher
.on('add', (file) => {
reload(file);
})
.on('unlink', (file) => {
reload(file);
})
.on('change', (file) => {
reload(file, true);
});
}
}
},
};
}

Expand Down

0 comments on commit 6f6a363

Please sign in to comment.