Skip to content
This repository was archived by the owner on Aug 27, 2026. It is now read-only.

fix: avoid reloading all csses when hot load - #1090

Merged
alexander-akait merged 3 commits into
webpack:masterfrom
yiminghe:optimize-locals
Apr 16, 2024
Merged

alexander-akait merged 3 commits into
webpack:masterfrom
yiminghe:optimize-locals

Conversation

@yiminghe

@yiminghe yiminghe commented Mar 20, 2024 •

Copy link
Copy Markdown
Contributor

This PR contains a:

  • bugfix
  • new feature
  • code refactor
  • test update
  • typo fix
  • metadata update

Motivation / Use-Case

  • only refresh page when locals change, otherwise accept and hot load
  • avoid reloading all csses

Breaking Changes

Additional Info

Fixes #692

@linux-foundation-easycla

linux-foundation-easycla Bot commented Mar 20, 2024 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: yiminghe / name: yiminghe (9bfc65e)
  • ✅ login: alexander-akait / name: Alexander Akait (ace213e, fc80832)

Comment thread src/loader.js
const hasLocals = !!(context.locals && Object.keys(context.locals).length);
const accept = hasLocals
? ""
: "module.hot.accept(undefined, cssReload);";

@alexander-akait alexander-akait Mar 20, 2024 •

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.

Why you change it?

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.

self accept is enough, it will update when dispose: https://webpack.js.org/api/hot-module-replacement/

@alexander-akait alexander-akait 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.

We need a test case, please add

@yiminghe yiminghe changed the title fix: avoid reload all css when hot load fix: avoid reloading all csses when hot load Mar 20, 2024
@yiminghe

Copy link
Copy Markdown
Contributor Author

We need a test case, please add

test cases updated

Comment thread src/loader.js Outdated
const localsJsonString = ${localsJsonString};
// ${Date.now()}
var cssReload = require(${stringifyRequest(
const cssReload = require(${stringifyRequest(

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.

Don't use const, it will be in the browser and break old browsers

Comment thread src/loader.js
? ""
: "module.hot.accept(undefined, cssReload);";

const localsJsonString = JSON.stringify(JSON.stringify(context.locals));

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.

Why double JSON.stringify? Just typo?

@yiminghe yiminghe Mar 28, 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.

note the interpolation:

`const localsJsonString = ${localsJsonString};`

string comparation is faster

@codecov

codecov Bot commented Mar 27, 2024 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 50.00000% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 92.00%. Comparing base (c7ff30d) to head (9bfc65e).
Report is 7 commits behind head on master.

❗ Current head 9bfc65e differs from pull request most recent head fc80832. Consider uploading reports for the commit fc80832 to get more accurate results

Files Patch % Lines
src/loader.js 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1090      +/-   ##
==========================================
+ Coverage   90.61%   92.00%   +1.38%     
==========================================
  Files           5        5              
  Lines         895      900       +5     
  Branches      255      258       +3     
==========================================
+ Hits          811      828      +17     
+ Misses         74       67       -7     
+ Partials       10        5       -5     

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

Comment thread test/cases/hmr/expected/main.js Outdated
module.hot.dispose(function(data) {
data.value = localsJsonString;
cssReload();
});

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.

We have a special test directory for HMR, will be great to add a test case there, I know your code works fine, but we should ensure about it

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.

what do you mean? I have added cases inside cases folder (hmr, hmr-locals)

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.

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.

@alexander-akait alexander-akait 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.

Let's do small improvements and I am fine with it

@alexander-akait

Copy link
Copy Markdown
Member

For fixing tests for OLD API just use OLD_API=1 npm run test:only

@alexander-akait

Copy link
Copy Markdown
Member

Thank you for your PR

@yiminghe

Copy link
Copy Markdown
Contributor Author

For fixing tests for OLD API just use OLD_API=1 npm run test:only

passed

@alexander-akait

alexander-akait commented Apr 8, 2024 •

Copy link
Copy Markdown
Member

@yiminghe We have:

var cssReload = require("/home/runner/work/mini-css-extract-plugin/mini-css-extract-plugin/src/hmr/hotModuleReplacement.js")(module.id, undefined);

As you can see it is an absolute path, let's use it from here https://github.com/webpack-contrib/css-loader/blob/master/src/utils.js#L15 (maybe we already have the same util here)

@yiminghe

yiminghe commented Apr 9, 2024

Copy link
Copy Markdown
Contributor Author

@yiminghe We have:

var cssReload = require("/home/runner/work/mini-css-extract-plugin/mini-css-extract-plugin/src/hmr/hotModuleReplacement.js")(module.id, undefined);

As you can see it is an absolute path, let's use it from here https://github.com/webpack-contrib/css-loader/blob/master/src/utils.js#L15 (maybe we already have the same util here)

updated

alexander-akait
alexander-akait previously approved these changes Apr 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants