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: bypass addWatchFile to avoid dependency issues in vite 2.5 #131

Merged
merged 1 commit into from
Aug 5, 2021
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
6 changes: 4 additions & 2 deletions packages/vite-plugin-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from './utils/options';
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';

import { setupWatchers } from './utils/watch';
import { ensureWatchedFile, setupWatchers } from './utils/watch';
import { resolveViaPackageJsonSvelte } from './utils/resolve';
import { addExtraPreprocessors } from './utils/preprocess';
import { PartialResolvedId } from 'rollup';
Expand Down Expand Up @@ -164,7 +164,9 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
logCompilerWarnings(compileData.compiled.warnings, options);
cache.update(compileData);
if (compileData.dependencies?.length && options.server) {
compileData.dependencies.forEach((d) => this.addWatchFile(d));
compileData.dependencies.forEach((d) => {
ensureWatchedFile(options.server!.watcher, d, options.root);
});
}
log.debug(`transform returns compiled js for ${filename}`);
return compileData.compiled.js;
Expand Down
28 changes: 21 additions & 7 deletions packages/vite-plugin-svelte/src/utils/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IdParser } from './id';
import { ResolvedOptions } from './options';
import { knownSvelteConfigNames } from './load-svelte-config';
import path from 'path';
import { FSWatcher } from 'vite';

export function setupWatchers(
options: ResolvedOptions,
Expand Down Expand Up @@ -71,22 +72,35 @@ export function setupWatchers(
};

// collection of watcher listeners by event
const listeners = {
add: [],
const listenerCollection = {
add: [] as Array<Function>,
change: [emitChangeEventOnDependants],
unlink: [removeUnlinkedFromCache, emitChangeEventOnDependants]
};
if (svelteConfigFile) {
listeners.change.push(restartOnConfigChange);
listeners.unlink.push(restartOnConfigChange);
listenerCollection.change.push(restartOnConfigChange);
listenerCollection.unlink.push(restartOnConfigChange);
} else {
// @ts-ignore
listeners.add.push(restartOnConfigAdd);
listenerCollection.add.push(restartOnConfigAdd);
}

Object.entries(listeners).forEach(([evt, listeners]) => {
Object.entries(listenerCollection).forEach(([evt, listeners]) => {
if (listeners.length > 0) {
watcher.on(evt, (filename) => listeners.forEach((listener) => listener(filename)));
}
});
}
// taken from vite utils
export function ensureWatchedFile(watcher: FSWatcher, file: string | null, root: string): void {
if (
file &&
// only need to watch if out of root
!file.startsWith(root + '/') &&
// some rollup plugins use null bytes for private resolved Ids
!file.includes('\0') &&
fs.existsSync(file)
) {
// resolve file to normalized system path
watcher.add(path.resolve(file));
}
}