RFC-012: HTML Plugin #500
Closed
byted-meow
started this conversation in
RFC
Replies: 1 comment 2 replies
-
{
"plugins": [
[
"html",
{
"template": "index.html"
}
]
]
} The Configuration is not friendly to typescript, maybe we just put it into configuraion {
"html": [{"template": "index.html"},{"template": "other.html"}]
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Html plugin for rspack, which collect emitted assets from entries and write them to a HTML file.
Motivation
It's hard to track emitted assets manually as it may contain hash. This plugin can do this automatically so that no need to change the
<script src="">
each time after pack.Guide-level explanation
It is a new plugin and should not affect the other part of rspack.
Reference-level explanation
Introduce and config this plugin
The plugin behaves like other rust inline plugins as it's currently not easy to load external rust plugin.
Introduce the plugin in
rspack.config.json
like this:It reads both its own config (for filename, template...) and rspack config (for publicPath...) to the structure:
Retrieve the Entrypoints from compilation
After initialization, it taps into process_assets hooks to analyze assets.
Parse Template and Emit the Html Assets
This plugin use
swc-html
to parse the HTML template files(if we are about to supportejs
-like template compile, it happens before this step).For better and easy plugin system, we create a simple custom html tag structure instead of the ast provided swc-html.
The assets got from the previous step are grouped by their type (or extensions, if type is not provided).
mjs
,js
files are appended to end of<body>
by default, andcss
appended to end of<head>
.After all tags are set, they are inserted to their place in swc-html's document. And use code_gen to convert it back to HTML text content.
These steps are all done in
process_assets
hook.Drawbacks
It just collects all the entry assets and appends them to html template. It does not function as html is a first-class citizen.
No dependencies in the Html template are collected. If we should support something like favicon, an extra work is needed.
Rationale and alternatives
We could also implement parcel-like html plugin which treat html as first-class citizen.
Prior art
html-webpack-plugin use webpack child compilation to compile template. And it does not parse the html file, but use only regex to match meta, head and body tag.
Unresolved questions
process_assets
hook and a better compilation & assets implementation are todos and will be discussed in another RFC.Future possibilities
If this plugin wins the first-citizen one, some new features may be added to it.
EJS-like Template Compilation
As there's no ejs implementation in rust, we could use handlebars by default to compile html files.
Subresource Integrity Support
We could also inline the subresource plugin and create sha384 for entries using
sha2
. This is much more performant (and a bit less flexible).Plugin System
A plugin system is the best way to add extensibility to it. For example, js plugins could be used to compile the template, and node
crypto
could be used to create sha384...And others...
support html minify
an extra config
minify
is provided, if true, use swc-html-minifier to minify output htmlsupport hmr, hash and cache...
Beta Was this translation helpful? Give feedback.
All reactions