Replies: 1 comment 1 reply
-
I don't get this, I know moduleA maybe depends on moduleB's state but it doesn't need to depends on moduleB's codegen result |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Make rspack handling assets in a native way.
Motivation
In favor of different loaders that transpile assets to a JavaScript module is not decent, since the module is not able to share the code generation result with other generators as we cannot separate the result and the new architecture is built to treat assets natively.
For the status quo, the internal support for
css
is a pain in the ass. In CSS files, developers may useurl
keyword to import a resource, which currently is not supported by rspack, which means we have to support it in the first place.Detailed design
Supported module types
SourceType::ResourceAsset
orSourceType::InlineAsset
Module type is a native type that is internally supported by rspack. In other words, if a native type is registered then you can use this type as a target for module finalization. For example, if you set a module as
ModuleType::JSON
, then internally, rspack will use the internal JSON parser and JSON generators, etc to handle the corresponding type.Parsing
Each parser will only be registered once. Each
ModuleType
should register its own parser, for example, when the asset type was set toModuleType::Asset
, we need to determine whether to useInline
orResource
on each real parsing job by checking the source size.Dependency collectingIn the example above,
index.js
depends onindex.css
, which meansindex.css
is a dependency ofindex.js
. Renderings based on the module render itself are not enough for dealing with the import statement. If we parse the dependency with range(startIndex
andendIndex
, for example, the range ofimages/rspack.png
/index.css
) collected, we can modify the importing statement on the code generation stage.Code generation
Returning different
SourceType
sSourceType
is a type for module that emitted by the plugin and it's a subset forModuleType
. ForJs
/Jsx
/Ts
/Tsx
, the plugin returnsSourceType::JavaScript
on basic use basis. ForResourceAsset
, the plugin returnsSourceType::Asset
andSourceType::JavaScript
and Rspack emits the asset to real file system, JavaScript will be handled by plugin-javascript and do the code generation as that we already did.Since this is what Rspack based on to treat asset emission in the code generation stage, and only
ModuleType::ResourceAsset
(after finalization for each module) will be converted toSourceType::Asset
, for other assets will only converted to JavaScript code.Delayed generation jobs and shared code generation stateFor the state of the art for Rspack, the code generation order in each chunk is unordered, which means when a CSS makes a reference to other files on the file system, with the module graph, the file may not be generated prior to the CSS module and this is very important as CSS module need to replace this when it comes to its stage of code generation.
We define modules in the same chunk as different code generation jobs. As for the example above, we have to distinguish jobs that have depends on the other job from those that don't have and run dependency jobs first:
Also, a data structure to share code generation results between jobs.
Dependency renderingFor each dependency need to impl the Dependency trait:
By achieving this, we can replace URLs in the CSS. The details of this will be stated in RFC003: CSS Support.
Assets emitting
Rspack emits assets that were generated in code generation results.
Changes of rspack-sources
rspack-sources converts the source to a string, in order to keep assets compatible with the current architecture, we need to support native buffering for assets.
Support
ReplaceSource
forrspack-sources
in order to support dependency renderingAlternatives
Treat assets like javascript modules, and Rspack emits them directly to the file system in the corresponding stage of code generation, return javascript module to module graph. This is a not preferable way to handle assets since only by handling assets in a native way we can handle CSS in a native way or we may need to convert CSS to a JavaScript module to invoke any other processes(like what webpack4 did) for local assets.
Prior Art
In webpack, the source type we mentioned above is called module type in
modules.rule.type
Module type is a raw type that was set by users, which could also be included natively like webpack natively handles
*.json
astype: "json"
. When webpack creates a module, the module hasn't been parsed before, so webpack defines its module type in resolve stage, which is known as Source Type in rspack.How webpack processes assets
When it comes to the parsing stage(
buildModule
in webpack), for each module type, like"asset"
, webpack will test if this asset fulfills the asset size limit and determine whether the module should be set toinline
orresource
. Then in the code generation stage, if an asset was priorly set toresource
, webpack will generate two code generation resultsjavascript
andasset
. Forjavascript
, the asset generator generates a module that returns the corresponding URL concatenated to public URL. Forasset
, the asset generator emits the buffer to the real filesystem.Below, we use module type to state the prior art of webpack.
Module type supported by webpack
This module type is firstly added in webpack 4 (full changelog):
In webpack 5 with the experimental feature
css
enabled, webpack can thus handlecss
files natively. You may lookup webpack configmodules.rule.type
to get more of them.In the example down below, we handle files suffixed with
j
with nativejson
handler.Unresolved questions
We haven't handle the dependency that imported with import assertions and css imports with modifiers(media query / support / layer).
Beta Was this translation helpful? Give feedback.
All reactions