You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 01e4d99
Browse filesBrowse the repository at this point in the historyBrowse files
docs: build the front page example with native HTML and CSS
The splash example bundled one JavaScript file and asked the reader to
write the page by hand, pointing a <script> tag at dist/bundle.js. Webpack
handles all three now, so the example starts from src/index.html: the page
links a stylesheet and a script, entry points at the page, and the emitted
document is shown next to the config with its URLs rewritten to the hashed
assets. Verified against webpack 5.110.3 — an HTML entry turns on the html
and css support on its own, and htmlFilename keeps the page's name out of
the contenthash pattern.
The migration guide's companion-plugin table now covers the popular
html-webpack-plugin ecosystem rather than five entries, and a new section
maps its six hooks onto HtmlModulesPlugin's, with a plugin rewritten
through transformTags and injectTags as the worked example.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NuWDbcF2jvq7q1r8aEY5V9
Start without a configuration file, or provide a custom**webpack.config.js**:
1
+
Point webpack at the page in**webpack.config.js**:
2
2
3
3
```js
4
-
importpathfrom"node:path";
5
-
import { fileURLToPath } from"node:url";
6
-
7
-
const__filename=fileURLToPath(import.meta.url);
8
-
const__dirname=path.dirname(__filename);
9
-
10
4
exportdefault {
11
-
entry:"./src/index.js",
5
+
entry:"./src/index.html",
12
6
output: {
13
-
path:path.resolve(__dirname, "dist"),
14
-
filename:"bundle.js",
7
+
filename:"[contenthash].js",
8
+
cssFilename:"[contenthash].css",
9
+
htmlFilename:"[name].html",
15
10
},
16
11
};
17
12
```
18
13
19
-
Prefer a video walkthrough? **[Without config](https://youtu.be/3Nv9muOkb6k?t=21293)**
14
+
Webpack parses the page, bundles everything it references, and rewrites the URLs. No loader, no plugin.
15
+
16
+
Prefer a video walkthrough? **[Watch one here](https://youtu.be/3Nv9muOkb6k?t=21293)**
Copy file name to clipboardExpand all lines: src/content/guides/native-html.mdx
+71-17Lines changed: 71 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,14 +30,14 @@ With this option enabled, webpack understands `.html` files as first-class modul
30
30
31
31
"Built-in" means webpack does the work itself, with no loader or plugin in the chain — not that it covers everything `html-loader` and `html-webpack-plugin` do. The scope is exactly this:
| Parsing `.html`, as an [entry point](#1-html-as-an-entry-point) or [imported from JS](#3-html-imported-from-javascript)|**Template engines** — Pug, EJS, Handlebars and friends, unless a synchronous [`template`](#templating) hook is enough |
36
-
|[Extracting every URL a page references](#what-webpack-bundles-from-a-page) and rewriting it to the built filename |`html-loader`'s `sources.scriptingEnabled` and `postprocessor`|
37
-
| Bundling inline `<script>` and `<style>` bodies, and `style=""` attributes |`html-webpack-plugin`'s `chunksSortMode`, `xhtml`, `showErrors` and `cache`|
38
-
|[Generating a page](#2-a-generated-page-for-a-javascript-entry) per entrypoint with its chunks injected | The third-party plugins that tap `html-webpack-plugin`'s hooks, which have no counterpart on [`HtmlModulesPlugin`](#plugin-hooks)yet|
| Parsing `.html`, as an [entry point](#1-html-as-an-entry-point) or [imported from JS](#3-html-imported-from-javascript)|**Template engines** — Pug, EJS, Handlebars and friends, unless a synchronous [`template`](#templating) hook is enough |
36
+
|[Extracting every URL a page references](#what-webpack-bundles-from-a-page) and rewriting it to the built filename |`html-loader`'s `sources.scriptingEnabled` and `postprocessor`|
37
+
| Bundling inline `<script>` and `<style>` bodies, and `style=""` attributes |`html-webpack-plugin`'s `chunksSortMode`, `xhtml`, `showErrors` and `cache`|
38
+
|[Generating a page](#2-a-generated-page-for-a-javascript-entry) per entrypoint with its chunks injected | The third-party plugins that tap `html-webpack-plugin`'s hooks — they do not run as-is, but most [rewrite](#rewriting-a-plugin-that-tapped-its-hooks)into a few lines|
|[Minification](#minification) and Hot Module Replacement ||
41
41
42
42
So a project using `html-webpack-plugin` to scaffold a document around its bundles, or `html-loader` to import a partial, needs neither. A project that renders its page through a template engine, or depends on a plugin that hooks into `html-webpack-plugin`, keeps that plugin.
43
43
@@ -647,15 +647,22 @@ T> While `experiments.html` is `'auto'` (the default since 5.109.0), a `module.r
Companion plugins fold in too. Most of the popular ones are an option now; the rest are a few lines against the [`HtmlModulesPlugin` hooks](#rewriting-a-plugin-that-tapped-its-hooks):
| `html-webpack-injector` | [`output.html.inject`](/configuration/output/#outputhtmlinject), or put the tag where you want it in an HTML entry |
660
+
| `html-webpack-include-assets-plugin` | write the tag in the page; mark files copied verbatim with `<!-- webpackIgnore:true-->` |
661
+
| `html-webpack-exclude-assets-plugin`, `html-webpack-skip-assets-plugin` | nothing — a page carries what it references, and an entry opts out of a generated page with the descriptor `html:false` |
| `html-webpack-inject-attributes-plugin`, `webpack-nomodule-plugin`, `html-webpack-link-type-plugin` | the [`transformTags`](#rewriting-a-plugin-that-tapped-its-hooks) hook |
664
+
| `inject-body-webpack-plugin`, `html-webpack-inline-style-plugin`, `html-webpack-inline-svg-plugin` | write it in the page, or the [`transformHtml`](#rewriting-a-plugin-that-tapped-its-hooks) hook |
665
+
| `appcache-webpack-plugin` | n/a — AppCache was removed from browsers |
659
666
660
667
### Without a template
661
668
@@ -791,6 +798,53 @@ export default {
791
798
792
799
Each page pulls in exactly the chunks it references, so `chunks` / `excludeChunks` have nothing left to do. The same works with generated pages — `output.html:true` plus one JavaScript entry per page.
793
800
801
+
### Rewriting a plugin that tapped its hooks
802
+
803
+
A plugin written against `html-webpack-plugin` does not run without it, but the hook it used has a counterpart on [`HtmlModulesPlugin`](#plugin-hooks):
| `beforeAssetTagGeneration` | n/a — the tags come from the page and the chunk graph, so there is no asset list to edit up front |
808
+
| `alterAssetTags`, `alterAssetTagGroups` | [`transformTags`](/api/compilation-hooks/#transformtags) to change, remove or move existing tags, [`injectTags`](/api/compilation-hooks/#injecttags) to add new ones |
Both plugins hand you the hooks per compilation, so the shape of the plugin barely changes — swap `HtmlWebpackPlugin.getCompilationHooks(compilation)` for `compiler.webpack.html.HtmlModulesPlugin.getCompilationHooks(compilation)` and work with tag descriptors instead of the plugin's `assetTags`:
`transformTags` mutates in place (`attrs`, `remove:true`, `injectTo`), while `injectTags` and `transformHtml` are waterfalls — return the value you were given. Anything that only rewrites the final markup, such as inlining styles or injecting a snippet into `<body>`, is a one-line `transformHtml` tap.
847
+
794
848
## All options with examples
795
849
796
850
HTML is configured in four places: the [parser](#parser-options) and [generator](#generator-options) under `module`, the [output options](#output-options) that describe the emitted page, and the [minimizer](#minifier-options) options under `optimization`. The tables below list every option, with the reference entry linked from each name.
Copy file name to clipboardExpand all lines: src/content/index.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,8 +39,8 @@ import Install from "../components/Splash/install/install.mdx";
39
39
</div>
40
40
</div>
41
41
42
-
Then run `webpack` on the command-line to create `bundle.js`.
42
+
Then run `webpack` on the command-line: HTML, CSS and JavaScript are all handled by webpack itself, so `dist/` ends up with the page and the hashed assets it points at.
43
43
44
44
## Awesome, isn't it? Let's dive in!
45
45
46
-
**[Get Started](/guides/getting-started)** quickly in our **Guides** section, or dig into the **[Concepts](/concepts)** section for more high-level information on the core notions behind webpack.
46
+
**[Get Started](/guides/getting-started)** quickly in our **Guides** section, or dig into the **[Concepts](/concepts)** section for more high-level information on the core notions behind webpack. The [Native HTML](/guides/native-html/) and [Native CSS](/guides/native-css/) guides cover what the built-in support does, and how to migrate an existing setup onto it.
0 commit comments