Skip to content
Open
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
53 changes: 53 additions & 0 deletions packages/wxt/e2e/tests/dev.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { InlineConfig } from 'vite';
import { describe, expect, it } from 'vitest';
import { TestProject, occupyPort } from '../utils';

Expand Down Expand Up @@ -84,4 +85,56 @@ describe('Dev Mode', () => {
await freePort();
}
});
it('should pass watchOptions to the Vite dev server watcher', async () => {
let watcherOptions: NonNullable<InlineConfig['server']>['watch'];

const project = new TestProject();
project.addFile(
'entrypoints/background.ts',
'export default defineBackground(() => {})',
);

const server = await project.startServer({
runner: { disabled: true },
vite: () => ({
server: {
watch: {
awaitWriteFinish: true,
ignored: ['vite-ignore/**'],
},
},
}),
watchOptions: {
usePolling: true,
interval: 1000,
ignored: ['custom-ignore/**'],
},
hooks: {
'vite:devServer:extendConfig': (config) => {
watcherOptions = config.server?.watch;
},
},
});

try {
expect(watcherOptions).toMatchObject({
awaitWriteFinish: true,
usePolling: true,
interval: 1000,
});

expect(watcherOptions).toEqual(
expect.objectContaining({
ignored: expect.arrayContaining([
expect.stringContaining('.output'),
expect.stringContaining('.wxt'),
'vite-ignore/**',
'custom-ignore/**',
]),
}),
);
} finally {
await server.stop();
}
});
});
6 changes: 3 additions & 3 deletions packages/wxt/src/core/builders/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
VirtualModuleId,
} from '../../utils/virtual-modules';
import * as wxtPlugins from './plugins';
import { resolveWatchOptions } from '../../utils/watch-options';

export async function createViteBuilder(
wxtConfig: ResolvedConfig,
Expand Down Expand Up @@ -66,9 +67,8 @@ export async function createViteBuilder(
}

config.server ??= {};
config.server.watch = {
ignored: [`${wxtConfig.outBaseDir}/**`, `${wxtConfig.wxtDir}/**`],
};

config.server.watch = resolveWatchOptions(wxtConfig, config.server.watch);

// TODO: Remove once https://github.com/wxt-dev/wxt/pull/1411 is merged
config.legacy ??= {};
Expand Down
6 changes: 5 additions & 1 deletion packages/wxt/src/core/create-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createFileReloader,
reloadContentScripts,
} from './utils/create-file-reloader';
import { resolveWatchOptions } from './utils/watch-options';

/**
* Creates a dev server and pre-builds all the files that need to exist before
Expand Down Expand Up @@ -152,7 +153,10 @@ async function createServerInternal(): Promise<WxtDevServer> {
logBabelSyntaxError(err);
wxt.logger.info('Waiting for syntax error to be fixed...');
await new Promise<void>((resolve) => {
const watcher = chokidar.watch(err.id, { ignoreInitial: true });
const watcher = chokidar.watch(err.id, {
...resolveWatchOptions(wxt.config),
ignoreInitial: true,
});
watcher.on('all', () => {
watcher.close();
wxt.logger.info('Syntax error resolved, rebuilding...');
Expand Down
1 change: 1 addition & 0 deletions packages/wxt/src/core/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export async function resolveConfig(
builtinModules,
userModules,
plugins: [],
watchOptions: mergedConfig.watchOptions,
...moduleOptions,
};
}
Expand Down
25 changes: 25 additions & 0 deletions packages/wxt/src/core/utils/watch-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { AnymatchPattern } from 'vite';
import type { ResolvedConfig, WxtWatchOptions } from '../../types';

export function resolveWatchOptions(
config: ResolvedConfig,
viteWatchOptions?: WxtWatchOptions | null,
): WxtWatchOptions {
return {
...viteWatchOptions,
...config.watchOptions,
ignored: [
`${config.outBaseDir}/**`,
`${config.wxtDir}/**`,
...normalizeIgnored(viteWatchOptions?.ignored),
...normalizeIgnored(config.watchOptions?.ignored),
],
};
}

function normalizeIgnored(
ignored: WxtWatchOptions['ignored'] | undefined,
): AnymatchPattern[] {
if (ignored == null) return [];
return Array.isArray(ignored) ? ignored : [ignored];
}
9 changes: 9 additions & 0 deletions packages/wxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ export interface InlineConfig {
* "wxt-module-analytics").
*/
modules?: string[];

/**
* Configure file watching behavior during development. These options are
* passed to Vite's dev server watcher.
*/
watchOptions?: WxtWatchOptions;
}

// TODO: Extract to @wxt/vite-builder and use module augmentation to include the vite field
Expand Down Expand Up @@ -1571,6 +1577,7 @@ export interface ResolvedConfig {
hooks: NestedHooks<WxtHooks>;
builtinModules: WxtModule<any>[];
userModules: WxtModuleWithMetadata<any>[];
watchOptions?: WxtWatchOptions;
/**
* An array of string to import plugins from. These paths should be resolvable
* by vite, and they should `export default defineWxtPlugin(...)`.
Expand Down Expand Up @@ -1801,3 +1808,5 @@ export interface WxtDirFileEntry {
/** Set to `true` to add a reference to this file in `.wxt/wxt.d.ts`. */
tsReference?: boolean;
}

export type WxtWatchOptions = NonNullable<vite.ServerOptions['watch']>;