Replies: 1 comment
-
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
Webpack
Webpack uses css variable to store css modules data:
:export
and name mapping.When loading a css chunk, webpack can get the link tag, get the variable from that tag, and then register module to
RuntimeGlobals.moduleFactories
Rspack
Rspack css module has 2 sourceTypes, one is
css
, the other isjs
,css
source type is consumed by render_manifest hook, used to generate css contents. Thejs
source type is consumed by js modules that will import at runtime.For example, one css module will become:
There is a small issue though, when CSS is not needed to be exported, Rspack will still generate an unnecessary JS module that cannot be removed, you can see
other.css
above, because this part of JS and CSS belong to the same module. If treeshaking removes this module, the CSS will also not be generated.We don't need the empty js file, it will do nothing but make the bundle size a little larger.
Solution
This implementation is similar to webpack, but webpack uses css variable to store the meta data, which is not allowed in legacy browsers like IE 11.
We want the css module not appear directly in the
RuntimeGlobals.moduleFactories
, instead we store only the needed css's meta data into some kind of runtime module.Runtime module
When loading a new async chunk, the async chunk can access and modify runtime modules. We can do like this:
We have all the information we need for css module name mappings, so we can generate runtime code that inserts needed css modules at compile time. For those modules who don't need exports we can just skip it.
Benefits
This way we can get rid of empty js file for normal css type.
Beta Was this translation helpful? Give feedback.
All reactions