Skip to content

Commit efcb593

Browse files
1natsu172aklinker1
andauthored
perf: Reduce hypersensitive onChange of watcher (#978)
Co-authored-by: Aaron <[email protected]>
1 parent a82c7cb commit efcb593

File tree

8 files changed

+72
-6
lines changed

8 files changed

+72
-6
lines changed

packages/wxt/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@
8181
"prepack": "pnpm build"
8282
},
8383
"dependencies": {
84-
"@types/chrome": "^0.0.269",
8584
"@aklinker1/rollup-plugin-visualizer": "5.12.0",
85+
"@types/chrome": "^0.0.269",
8686
"@types/webextension-polyfill": "^0.10.7",
8787
"@webext-core/fake-browser": "^1.3.1",
8888
"@webext-core/isolated-element": "^1.1.2",
@@ -95,6 +95,7 @@
9595
"consola": "^3.2.3",
9696
"defu": "^6.1.4",
9797
"dequal": "^2.0.3",
98+
"dotenv": "^16.4.5",
9899
"esbuild": "^0.23.0",
99100
"execa": "^9.3.1",
100101
"fast-glob": "^3.3.2",
@@ -116,6 +117,7 @@
116117
"ohash": "^1.1.3",
117118
"open": "^10.1.0",
118119
"ora": "^8.1.0",
120+
"perfect-debounce": "^1.0.0",
119121
"picocolors": "^1.0.1",
120122
"prompts": "^2.4.2",
121123
"publish-browser-extension": "^2.1.3",

packages/wxt/src/core/create-server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { debounce } from 'perfect-debounce';
12
import {
23
BuildStepOutput,
34
EntrypointGroup,
@@ -144,7 +145,7 @@ function createFileReloader(server: WxtDevServer) {
144145
const fileChangedMutex = new Mutex();
145146
const changeQueue: Array<[string, string]> = [];
146147

147-
return async (event: string, path: string) => {
148+
const cb = async (event: string, path: string) => {
148149
changeQueue.push([event, path]);
149150

150151
await fileChangedMutex.runExclusive(async () => {
@@ -216,6 +217,11 @@ function createFileReloader(server: WxtDevServer) {
216217
}
217218
});
218219
};
220+
221+
return debounce(cb, wxt.config.dev.server!.watchDebounce, {
222+
leading: true,
223+
trailing: false,
224+
});
219225
}
220226

221227
/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { safeStringToNumber } from '../number';
3+
4+
describe('Number Utils', () => {
5+
describe('safeStringToNumber', () => {
6+
it.each([
7+
{ arg: '1000', expected: 1000 },
8+
{ arg: '', expected: 0 },
9+
{ arg: '12abc', expected: null },
10+
{ arg: undefined, expected: null },
11+
])(
12+
'should be safely converted from string to number: safeStringToNumber($arg) -> $expected',
13+
({ arg, expected }) => {
14+
expect(safeStringToNumber(arg)).toBe(expected);
15+
},
16+
);
17+
});
18+
});

packages/wxt/src/core/utils/building/resolve-config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { normalizePath } from '../paths';
2525
import glob from 'fast-glob';
2626
import { builtinModules } from '../../../builtin-modules';
2727
import { getEslintVersion } from '../eslint';
28+
import { safeStringToNumber } from '../number';
29+
import { loadEnv } from '../env';
2830

2931
/**
3032
* Given an inline config, discover the config file if necessary, merge the results, resolve any
@@ -72,6 +74,8 @@ export async function resolveConfig(
7274
const mode = mergedConfig.mode ?? COMMAND_MODES[command];
7375
const env: ConfigEnv = { browser, command, manifestVersion, mode };
7476

77+
loadEnv(mode); // Load any environment variables used below
78+
7579
const root = path.resolve(
7680
inlineConfig.root ?? userConfig.root ?? process.cwd(),
7781
);
@@ -124,6 +128,7 @@ export async function resolveConfig(
124128
devServerConfig = {
125129
port,
126130
hostname: mergedConfig.dev?.server?.hostname ?? 'localhost',
131+
watchDebounce: safeStringToNumber(process.env.WXT_WATCH_DEBOUNCE) ?? 800,
127132
};
128133
}
129134

packages/wxt/src/core/utils/env.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { config } from 'dotenv';
2+
3+
/**
4+
* Load environment files based on the current mode.
5+
*/
6+
export function loadEnv(mode: string) {
7+
return config({
8+
path: [`.env`, `.env.local`, `.env.${mode}`, `.env.${mode}.local`],
9+
});
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function safeStringToNumber(str: string | undefined): number | null {
2+
const num = Number(str);
3+
return isNaN(num) ? null : num;
4+
}

packages/wxt/src/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,25 @@ export interface ResolvedConfig {
12771277
server?: {
12781278
port: number;
12791279
hostname: string;
1280+
/**
1281+
* The milliseconds to debounce when a file is saved before reloading.
1282+
* The only way to set this option is to set the `WXT_WATCH_DEBOUNCE`
1283+
* environment variable, either globally (like in `.bashrc` file) or
1284+
* per-project (in `.env` file).
1285+
*
1286+
* For example:
1287+
* ```
1288+
* # ~/.zshrc
1289+
* export WXT_WATCH_DEBOUNCE=1000
1290+
* ```
1291+
* or
1292+
* ```
1293+
* # .env
1294+
* WXT_WATCH_DEBOUNCE=1000
1295+
* ```
1296+
* @default 800
1297+
*/
1298+
watchDebounce: number;
12801299
};
12811300
reloadCommand: string | false;
12821301
};

pnpm-lock.yaml

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)