-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Hot Module Replacement (HMR) for CSS #64444
Draft
kevin940726
wants to merge
3
commits into
trunk
Choose a base branch
from
add/css-hmr
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# CSS HMR Loader | ||
|
||
A webpack loader to enable CSS Hot Module Replacement in Gutenberg. | ||
|
||
**This package is still experimental and breaking changes could be introduced in future minor versions (`v0.x`). Use it at your own risks.** | ||
|
||
<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const path = require( 'path' ); | ||
|
||
module.exports = () => {}; | ||
module.exports.pitch = function pitch( remainingRequest ) { | ||
const modulePath = path.join( __dirname, 'reload-css.js' ); | ||
const moduleLoader = JSON.stringify( `!${ modulePath }` ); | ||
const request = JSON.stringify( | ||
this.utils.contextify( | ||
this.context || this.rootContext, | ||
`!!${ remainingRequest }` | ||
) | ||
); | ||
|
||
if ( this.cacheable ) { | ||
this.cacheable(); | ||
} | ||
|
||
return ` | ||
if (module.hot) { | ||
const reloadCSS = require(${ moduleLoader })(); | ||
require(${ request }); | ||
|
||
module.hot.accept(${ request }, function () { | ||
reloadCSS(require(${ request }).default); | ||
}); | ||
module.hot.dispose(function () { | ||
reloadCSS(require(${ request }).default); | ||
}); | ||
} | ||
`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const log = require( 'webpack/hot/log' ); | ||
|
||
const visitedStylesheets = new WeakSet(); | ||
|
||
function debounce( fn, time ) { | ||
let timeout; | ||
return function ( ...args ) { | ||
clearTimeout( timeout ); | ||
timeout = setTimeout( () => { | ||
return fn.apply( this, args ); | ||
}, time ); | ||
}; | ||
} | ||
|
||
function updateCSS( stylesheet, url ) { | ||
const href = stylesheet.href; | ||
|
||
if ( ! href || ( url && ! href.includes( url ) ) ) { | ||
return; | ||
} | ||
|
||
if ( visitedStylesheets.has( stylesheet ) ) { | ||
return; | ||
} | ||
|
||
visitedStylesheets.add( stylesheet ); | ||
const element = stylesheet.cloneNode(); | ||
visitedStylesheets.add( element ); | ||
|
||
element.href = href.split( '?' )[ 0 ] + `?${ Date.now() }`; | ||
|
||
element.addEventListener( 'load', () => { | ||
if ( visitedStylesheets.has( element ) ) { | ||
visitedStylesheets.delete( element ); | ||
stylesheet.remove(); | ||
} | ||
} ); | ||
|
||
element.addEventListener( 'error', () => { | ||
if ( visitedStylesheets.has( element ) ) { | ||
visitedStylesheets.delete( element ); | ||
stylesheet.remove(); | ||
} | ||
} ); | ||
|
||
stylesheet.after( element ); | ||
} | ||
|
||
function getAllStylesheets( win ) { | ||
const links = [ | ||
...win.document.querySelectorAll( "link[rel='stylesheet']" ), | ||
]; | ||
|
||
// Recursively loop through all frames with the same origin. | ||
for ( let i = 0; i < win.frames.length; i++ ) { | ||
try { | ||
links.push( ...getAllStylesheets( win.frames[ i ] ) ); | ||
} catch ( err ) { | ||
// Ignore same origin policy errors. | ||
} | ||
} | ||
|
||
return links; | ||
} | ||
|
||
module.exports = function reloadCSS() { | ||
function update( url ) { | ||
const normalizedUrl = url.split( '?' )[ 0 ]; | ||
const stylesheets = getAllStylesheets( window ); | ||
let loaded = false; | ||
|
||
stylesheets.forEach( ( stylesheet ) => { | ||
updateCSS( stylesheet, normalizedUrl ); | ||
loaded = true; | ||
} ); | ||
|
||
if ( loaded ) { | ||
log( 'info', `[HMR] css reload ${ normalizedUrl }` ); | ||
} else { | ||
log( 'info', '[HMR] Reload all css' ); | ||
|
||
stylesheets.forEach( ( stylesheet ) => { | ||
updateCSS( stylesheet ); | ||
} ); | ||
} | ||
} | ||
|
||
return debounce( update, 50 ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@wordpress/css-hmr-loader", | ||
"version": "0.0.0", | ||
"private": true, | ||
"description": "A webpack loader to enable CSS Hot Module Replacement in Gutenberg.", | ||
"author": "The WordPress Contributors", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"CSS", | ||
"HMR", | ||
"webpack", | ||
"loader" | ||
], | ||
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/css-hmr-loader/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/WordPress/gutenberg.git", | ||
"directory": "packages/css-hmr-loader" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WordPress/gutenberg/issues" | ||
}, | ||
"engines": { | ||
"node": ">=18.12.0", | ||
"npm": ">=8.19.2" | ||
}, | ||
"main": "lib/index.js", | ||
"peerDependencies": { | ||
"webpack": "*" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the limitation of
wp-scripts start --hot
that force introducing a new script? I would be more in favor of always using the dev server forstart
by default even when not using hot module replacement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to use our own manual HMR setup so we don't want to pass
--hot
to webpack.wp-scripts start --hot
seems to always pass that to webpack which then override thehot: false
in the config.I agree using
start
will be ideal butserve
seems reasonable too, given that the official webpack command usesserve
to start a dev server too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure,
webpack serve
is a thing which is reflected inwp-scripts start
implementation. The same applies towebpack watch
but it doesn’t need to be exposed to the devs.