Skip to content

Commit 35d6b3f

Browse files
authored
vite: Disable the compiler's file watcher during builds (#1592)
1 parent 383bf1e commit 35d6b3f

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

.changeset/kind-chicken-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/vite-plugin': patch
3+
---
4+
5+
Disable the compiler's file watcher during builds

.changeset/wet-swans-rush.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@vanilla-extract/compiler': minor
3+
---
4+
5+
`createCompiler`: Add `enableFileWatcher` option
6+
7+
By default, the compiler sets up its own file watcher.
8+
This option allows you to disable it if necessary, such as during a production build.
9+
10+
**EXAMPLE USAGE**:
11+
12+
```ts
13+
const compiler = createCompiler({
14+
root: process.cwd(),
15+
enableFileWatcher: false
16+
});
17+
```

packages/compiler/src/compiler.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ const createViteServer = async ({
9393
root,
9494
identifiers,
9595
viteConfig,
96+
enableFileWatcher = true,
9697
}: Required<
9798
Pick<CreateCompilerOptions, 'root' | 'identifiers' | 'viteConfig'>
98-
>) => {
99+
> &
100+
Pick<CreateCompilerOptions, 'enableFileWatcher'>) => {
99101
const pkg = getPackageInfo(root);
100102
const vite = await import('vite');
101103

@@ -108,6 +110,7 @@ const createViteServer = async ({
108110
root,
109111
server: {
110112
hmr: false,
113+
watch: enableFileWatcher ? undefined : null,
111114
},
112115
logLevel: 'silent',
113116
optimizeDeps: {
@@ -194,9 +197,11 @@ const createViteServer = async ({
194197
},
195198
});
196199

197-
server.watcher.on('change', (filePath) => {
198-
runner.moduleCache.invalidateDepTree([filePath]);
199-
});
200+
if (enableFileWatcher) {
201+
server.watcher.on('change', (filePath) => {
202+
runner.moduleCache.invalidateDepTree([filePath]);
203+
});
204+
}
200205

201206
return {
202207
server,
@@ -244,6 +249,13 @@ interface ProcessedVanillaFile {
244249

245250
export interface CreateCompilerOptions {
246251
root: string;
252+
/**
253+
* By default, the compiler sets up its own file watcher. This option allows you to disable it if
254+
* necessary, such as during a production build.
255+
*
256+
* @default true
257+
*/
258+
enableFileWatcher?: boolean;
247259
cssImportSpecifier?: (filePath: string) => string;
248260
identifiers?: IdentifierOption;
249261
viteConfig?: ViteUserConfig;
@@ -257,6 +269,7 @@ export const createCompiler = ({
257269
identifiers = 'debug',
258270
cssImportSpecifier = (filePath) => filePath + '.vanilla.css',
259271
viteConfig,
272+
enableFileWatcher,
260273
viteResolve,
261274
vitePlugins,
262275
}: CreateCompilerOptions): Compiler => {
@@ -272,6 +285,7 @@ export const createCompiler = ({
272285
resolve: viteResolve,
273286
plugins: vitePlugins,
274287
},
288+
enableFileWatcher,
275289
});
276290

277291
const processVanillaFileCache = new Map<
@@ -388,8 +402,8 @@ export const createCompiler = ({
388402
throw new Error(`Can't find ModuleNode for ${filePath}`);
389403
}
390404

391-
const cssImports = [];
392-
const orderedComposedClassLists = [];
405+
const cssImports: string[] = [];
406+
const orderedComposedClassLists: Composition[] = [];
393407

394408
const scanModule = createModuleScanner();
395409
const { cssDeps, watchFiles } = scanModule(moduleNode);

packages/vite-plugin/src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ const withUserPluginFilter =
6060
export function vanillaExtractPlugin({
6161
identifiers,
6262
unstable_pluginFilter: pluginFilter = defaultPluginFilter,
63-
unstable_mode: mode = 'emitCss',
63+
unstable_mode = 'emitCss',
6464
}: Options = {}): Plugin {
6565
let config: ResolvedConfig;
6666
let configEnv: ConfigEnv;
6767
let server: ViteDevServer;
6868
let packageName: string;
6969
let compiler: Compiler | undefined;
70+
let isBuild: boolean;
7071
const vitePromise = import('vite');
7172

7273
const getIdentOption = () =>
@@ -127,11 +128,12 @@ export function vanillaExtractPlugin({
127128
},
128129
async configResolved(_resolvedConfig) {
129130
config = _resolvedConfig;
131+
isBuild = config.command === 'build' && !config.build.watch;
130132
packageName = getPackageInfo(config.root).name;
131133
},
132134
async buildStart() {
133135
// Ensure we re-use the compiler instance between builds, e.g. in watch mode
134-
if (mode !== 'transform' && !compiler) {
136+
if (unstable_mode !== 'transform' && !compiler) {
135137
const { loadConfigFromFile } = await vitePromise;
136138

137139
let configForViteCompiler: UserConfig | undefined;
@@ -167,6 +169,7 @@ export function vanillaExtractPlugin({
167169
identifiers: getIdentOption(),
168170
cssImportSpecifier: fileIdToVirtualId,
169171
viteConfig,
172+
enableFileWatcher: !isBuild,
170173
});
171174
}
172175
},
@@ -189,7 +192,7 @@ export function vanillaExtractPlugin({
189192

190193
const identOption = getIdentOption();
191194

192-
if (mode === 'transform') {
195+
if (unstable_mode === 'transform') {
193196
return transform({
194197
source: code,
195198
filePath: normalizePath(validId),
@@ -212,7 +215,7 @@ export function vanillaExtractPlugin({
212215
};
213216

214217
// We don't need to watch files in build mode
215-
if (config.command === 'build' && !config.build.watch) {
218+
if (isBuild) {
216219
return result;
217220
}
218221

test-helpers/src/startFixture/esbuild.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface EsbuildFixtureOptions {
1212
mode?: 'development' | 'production';
1313
port: number;
1414
}
15+
1516
export const startEsbuildFixture = async (
1617
fixtureName: string,
1718
{ type, mode = 'development', port = 3000 }: EsbuildFixtureOptions,
@@ -48,8 +49,17 @@ export const startEsbuildFixture = async (
4849
outdir,
4950
});
5051

52+
await ctx.watch();
53+
5154
const server = await ctx.serve({ servedir: outdir, port });
5255

56+
const esbuildLiveReloadScript = `
57+
<script type="module">
58+
new EventSource('/esbuild').addEventListener('change', () =>
59+
location.reload(),
60+
);
61+
</script>`;
62+
5363
await fs.writeFile(
5464
path.join(outdir, 'index.html'),
5565
`<!DOCTYPE html>
@@ -58,6 +68,7 @@ export const startEsbuildFixture = async (
5868
<meta charset="utf-8">
5969
<title>esbuild - ${fixtureName}</title>
6070
<link rel="stylesheet" type="text/css" href="index.css" />
71+
${mode !== 'production' ? esbuildLiveReloadScript : ''}
6172
</head>
6273
<body>
6374
<script src="index.js"></script>

0 commit comments

Comments
 (0)