Skip to content

perf: Reduce hypersensitive onChange of watcher - #978

Merged
aklinker1 merged 5 commits into
wxt-dev:mainfrom
1natsu172:debounce-watcher
Sep 22, 2024
Merged

aklinker1 merged 5 commits into
wxt-dev:mainfrom
1natsu172:debounce-watcher

Conversation

@1natsu172

Copy link
Copy Markdown
Contributor

refer #938

Debounce the watcher's onChange.

@netlify

netlify Bot commented Sep 12, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 5681c14
🔍 Latest deploy log https://app.netlify.com/sites/creative-fairy-df92c4/deploys/66f017c531fe800008ba41dd
😎 Deploy Preview https://deploy-preview-978--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Comment thread packages/wxt/package.json
Comment on lines -84 to +85
"@types/chrome": "^0.0.269",
"@aklinker1/rollup-plugin-visualizer": "5.12.0",
"@types/chrome": "^0.0.269",

@1natsu172 1natsu172 Sep 12, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto sorted by pnpm.

Comment thread packages/wxt/src/core/create-server.ts Outdated
Comment on lines +134 to +136
const reloadOnChange = debounce(createFileReloader(server), 3e2, {
leading: true,
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm expecting unrealistic and excessive changes by machine. Not sure if the wait-time should be shorter or longer.... 🤔

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

300ms still causes the double reloads issue, I have to bump it to at least 3s

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pnd280 I turned off trailing, so 300ms might have been OK. Please check. If it's still NG, please send us a captured video of your screen.

@1natsu172

1natsu172 commented Sep 14, 2024 •

Copy link
Copy Markdown
Contributor Author

NOTE: perfect-debounce behavior

Details

{ leading: true, trailing: true }

image

{ leading: false, trailing: false }

image

{ leading: true, trailing: false }

image

{ leading: false, trailing: true }

image

There is no maxWait, and leading has priority. unlike lodash, even { leading: true, trailing: true } does not fire a total of two times.

@1natsu172 1natsu172 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aklinker1 I implemented env variable loading and set the default value to a tentative 800ms (based on their logs). Please review when you have time!

Comment thread packages/wxt/src/core/wxt.ts Outdated
// See https://github.com/wxt-dev/wxt/issues/873#issuecomment-2254555523
process.env.NODE_ENV ??= command === 'serve' ? 'development' : 'production';

loadDotenv({ path: ['.env.wxt', '.env'], quiet: true });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right place to load? More top level? (cli.mjs?)

Comment thread packages/wxt/src/types.ts Outdated
/**
* WatchDebounce passed in from `.env.wxt` via the `WXT_WATCH_DEBOUNCE`. Which can suppress cases where dev server reloads are oversensitive.
*/
watchDebounce: number;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right place to set in? Or should separate to another type and config property?

@pkg-pr-new

pkg-pr-new Bot commented Sep 22, 2024 •

Copy link
Copy Markdown

Open in Stackblitz

@wxt-dev/auto-icons

pnpm add https://pkg.pr.new/@wxt-dev/auto-icons@978

@wxt-dev/module-react

pnpm add https://pkg.pr.new/@wxt-dev/module-react@978

@wxt-dev/i18n

pnpm add https://pkg.pr.new/@wxt-dev/i18n@978

@wxt-dev/module-solid

pnpm add https://pkg.pr.new/@wxt-dev/module-solid@978

@wxt-dev/module-svelte

pnpm add https://pkg.pr.new/@wxt-dev/module-svelte@978

@wxt-dev/module-vue

pnpm add https://pkg.pr.new/@wxt-dev/module-vue@978

wxt

pnpm add https://pkg.pr.new/wxt@978

commit: 5681c14

@codecov

codecov Bot commented Sep 22, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.13%. Comparing base (e760ee4) to head (5681c14).
Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #978      +/-   ##
==========================================
+ Coverage   81.73%   82.13%   +0.39%     
==========================================
  Files         125      127       +2     
  Lines        6604     6622      +18     
  Branches     1098     1104       +6     
==========================================
+ Hits         5398     5439      +41     
+ Misses       1192     1169      -23     
  Partials       14       14              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aklinker1 aklinker1 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1natsu172 sorry for the slow review on this! I made a couple of changes:

  1. Move config from ConfigEnv to ResolvedConfig because environment variables isn't what the "env" is referencing in "ConfigEnv". That object is more for storing information about the current build setup than storing all environment variables
  2. Updated .env loading behavior to match Vite's. See https://github.com/wxt-dev/wxt/blob/ef30d9aac1e7353a96ecf30c53df39b74a707516/packages/wxt/src/core/utils/env.ts
  3. Moved loading of environment variables into resolveConfig so it refreshes the environment variables on save
  4. Replaced @dotenvx/dotenvx with dotenv since we don't need the CLI, just the env loading behavior.

Otherwise it looks good! Thanks for fielding this one!

@1natsu172

Copy link
Copy Markdown
Contributor Author

@aklinker1 Oh you've already made a lot of changes and adjustments. Thanks!

@aklinker1

Copy link
Copy Markdown
Member

@1natsu172 want me to revert any of that? I figured since it took me a while to review, I'd make the changes myself to spend things up

@1natsu172

Copy link
Copy Markdown
Contributor Author

@aklinker1 No, it's okay! I was concerned about the type of env file, timing of loading, etc., but everything is clear now 👍

@aklinker1
aklinker1 merged commit efcb593 into wxt-dev:main Sep 22, 2024
@1natsu172
1natsu172 deleted the debounce-watcher branch November 15, 2024 18:51
stefanmaric added a commit to stefanmaric/wxt that referenced this pull request Feb 18, 2025
[PR wxt-dev#978](wxt-dev#978), released in
[v0.19.10](https://github.com/wxt-dev/wxt/releases/tag/wxt-v0.19.10),
broke variable expansion in dotenv files, a Vite built-in feature.

I encountered this while migrating from v0.18.2 to the latest.

Since already defined variables in process.env take presedence,
the call to dotenv-expand by vite won't have any effect. Calling
dotenv-expand ourselves to mimic what vite does restores the lost
functionality.

Vite has a slightly more complex env loading logic, so these changes
won't bring 100% parity, but should be good for most use-cases.

See: https://github.com/vitejs/vite/blob/642d528b7b403eb91c67ff809ffa0fb99a1ff56e/packages/vite/src/node/env.ts
stefanmaric added a commit to stefanmaric/wxt that referenced this pull request Feb 18, 2025
[PR wxt-dev#978](wxt-dev#978), released in
[v0.19.10](https://github.com/wxt-dev/wxt/releases/tag/wxt-v0.19.10),
broke variable expansion in dotenv files, a Vite built-in feature.

I encountered this while migrating from v0.18.2 to the latest.

Since already defined variables in process.env take presedence,
the call to dotenv-expand by vite won't have any effect. Calling
dotenv-expand ourselves to mimic what vite does restores the lost
functionality.

Vite has a slightly more complex env loading logic, so these changes
won't bring 100% parity, but should be good for most use-cases.

See: https://github.com/vitejs/vite/blob/642d528b7b403eb91c67ff809ffa0fb99a1ff56e/packages/vite/src/node/env.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants