Skip to content

Commit a7ad609

Browse files
committed
release v4.18.0, #147
1 parent 1967598 commit a7ad609

3 files changed

Lines changed: 176 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 4.18.0 release (2025-02-03)
4+
5+
- chore: bump the version for release
6+
- docs: add documentation for the router option
7+
38
## 4.18.0-beta.2 (2025-02-03)
49

510
- feat: if the `router` option is specified and not disabled,

README.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Additionally, CSS extracted from styles imported in JS can be injected into HTML
5757
- `<link href="../images/favicon.svg" type="image/svg" rel=icon />`
5858
- `<img src="@images/pic.png" srcset="@images/pic400.png 1x, @images/pic800.png 2x" />`\
5959
Source files will be resolved, processed and auto-replaced with correct URLs in the generated HTML.
60+
- **Resolves** [route URLs](#option-router) in `a.href`, useful for navigation in multi-pages.
6061
- **Inlines** [JS](#recipe-inline-js), [CSS](#recipe-inline-css) and [Images](#recipe-inline-image) into HTML. See [how to inline all resources](#recipe-inline-all-assets-to-html) into single HTML file.
6162
- Supports the [HMR for CSS](#option-css-hot) to update CSS in browser without a full reload.
6263
- Watches for changes in the [data file](#option-entry-data) linked to the template in the plugin option.
@@ -175,6 +176,9 @@ See full list of all [plugin options](#toc-plugin-options).
175176

176177
[Open an example in StackBlitz](https://stackblitz.com/edit/hello-world-webpack?file=webpack.config.js)
177178

179+
> [!TIP]
180+
> To resolve route URLs in `a.href` for navigation across multiple pages, you can enable the [router](#option-router) option.
181+
178182
---
179183
> 🦖 **Mozilla** already uses this plugin to build static HTML files for the [Mozilla AI GUIDE](https://github.com/mozilla/ai-guide) site.
180184
>
@@ -227,6 +231,7 @@ If you have discovered a bug or have a feature suggestion, feel free to create a
227231

228232
## 🔆 What's New in v4
229233

234+
- **NEW** added [router](#option-router) to resolve routes in `a.href`, useful for multi-pages (since `v4.18.0`).
230235
- **NEW** you can include Markdown `*.md` files in your HTML template (since `v4.6.0`).
231236
- **NEW** added supports the [HMR for CSS](#option-css-hot) (since `v4.5.0`).
232237
- **NEW** added supports the [multiple configurations](https://webpack.js.org/configuration/configuration-types/#exporting-multiple-configurations).
@@ -503,6 +508,8 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
503508
- [js](#option-js) (options for JS)
504509
- [css](#option-css) (options for CSS)
505510
- [data](#option-data) (🔗reference to [loaderOptions.data](#loader-option-data))
511+
- [sources](#option-sources) (🔗reference to [loaderOptions.sources](#loader-option-sources))
512+
- [router](#option-router) (enable resolving of route URLs in `a.href`)
506513
- [beforePreprocessor](#option-before-preprocessor) (callback, 🔗reference to [loaderOptions.beforePreprocessor](#loader-option-before-preprocessor))
507514
- [preprocessor](#option-preprocessor) (callback or string, 🔗reference to [loaderOptions.preprocessor](#loader-option-preprocessor))
508515
- [preprocessorOptions](#option-preprocessor) (🔗reference to [loaderOptions.preprocessorOptions](#loader-option-preprocessorOptions))
@@ -2207,6 +2214,165 @@ Please see the details below under the [data](#loader-option-data) loader option
22072214
22082215
#### [↑ back to contents](#contents)
22092216
2217+
<a id="option-sources" name="option-sources"></a>
2218+
2219+
### `sources`
2220+
2221+
Since the `v4.18.0`, the `sources` plugin option is the reference to [loaderOptions.sources](#loader-option-sources).
2222+
2223+
Now it is possible to define the `sources` option directly in the plugin options to simplify the config.
2224+
2225+
2226+
The NEW syntactic "sugar":
2227+
2228+
```js
2229+
new HtmlBundlerPlugin({
2230+
entry: {
2231+
index: './src/views/home.ejs',
2232+
},
2233+
// new reference to the loaderOptions.sources
2234+
sources: [...],
2235+
}),
2236+
```
2237+
2238+
The old syntax is still valid and will never be deprecated:
2239+
2240+
```js
2241+
new HtmlBundlerPlugin({
2242+
entry: {
2243+
index: './src/views/home.ejs',
2244+
},
2245+
loaderOptions: {
2246+
// original option is under loaderOptions
2247+
sources: [...],
2248+
},
2249+
}),
2250+
```
2251+
2252+
#### [↑ back to contents](#contents)
2253+
2254+
<a id="option-router" name="option-router"></a>
2255+
2256+
### `router`
2257+
2258+
```ts
2259+
type Router =
2260+
| boolean
2261+
| {
2262+
enabled?: boolean;
2263+
test?: RegExp | Array<RegExp>;
2264+
rewriteIndex?: false | string;
2265+
resolve?: (props: {
2266+
sourceRoute: string;
2267+
outputRoute: string;
2268+
sourceFile: string;
2269+
outputFile: string;
2270+
}) => void | string;
2271+
};
2272+
```
2273+
2274+
- `enabled` - Enable / disable the route resolving. Defaults `true`.
2275+
- `test` - The router resolves values matching to the RegExp only.\
2276+
Defaults, defined the same as [plugin.test](#option-test) option to match templates of used [preprocessor](#option-preprocessor).
2277+
- `rewriteIndex` - Rewrite the `index.html`.
2278+
- If [publicPath](#webpack-option-output-publicpath) is `auto`, replaces `index.html` with the specified string, should be on of: `'.'`, `''` (empty).
2279+
- If [publicPath](#webpack-option-output-publicpath) is not empty, replaces `index.html` with `''` (empty) regardless of specified value.
2280+
- If the route URL contains a query or a segment (e.g. `index.html#contact`), the `rewriteIndex` is ignored.
2281+
- `resolve()` - Allow modify the resolved route URL.\
2282+
The argument is the object:
2283+
- `sourceRoute` - The resolved attribute value (absolute path).
2284+
- `outputRoute` - The output URL regards [publicPath](#webpack-option-output-publicpath), e.g. by `auto` publicPath is relative to `outputFile`.
2285+
- `sourceFile` - The absolute path to the template file where is resolved the `sourceRoute`.
2286+
- `outputFile` - The output HTML filename of `sourceFile`, relative to the `dist/` directory.\
2287+
Return:
2288+
- void - Do nothing.
2289+
- string - The returned string replaces the value.
2290+
2291+
The `router` option enable resolving of `a.href` (by defaults) values as a route URL to inner pages.
2292+
This is useful for multi-page sites.
2293+
2294+
Using the [sources](#loader-option-sources) option you can specify any tag attribute which should be resolved.
2295+
2296+
> [!NOTE]
2297+
> The router resolves routes specified in the `entry` plugin option only. An external URL will be ignored.
2298+
2299+
2300+
```js
2301+
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
2302+
module.exports = {
2303+
resolve: {
2304+
alias: {
2305+
// recommended to use the alias for the route in templates
2306+
'@views': path.join(__dirname, 'src/views/pages'),
2307+
},
2308+
},
2309+
plugins: [
2310+
new HtmlBundlerPlugin({
2311+
entry: {
2312+
// multi-pages
2313+
index: './src/views/pages/home/index.html',
2314+
'news/sport/index': './src/views/pages/news/sport/index.html',
2315+
},
2316+
router: true, // enable resolving of route URLs in a.href
2317+
}),
2318+
],
2319+
};
2320+
```
2321+
2322+
In templates, specify the `source path` to the linked template.
2323+
The linked template must be specified in the `entry` plugin option too.
2324+
2325+
The source _./src/views/pages/index.html_
2326+
```html
2327+
<html>
2328+
<body>
2329+
<h1>Home</h1>
2330+
<a href="@views/home/index.html">Home</a> | <a href="@views/news/sport/index.html">Sport</a>
2331+
</body>
2332+
</html>
2333+
```
2334+
2335+
The source _./src/views/pages/news/sport/index.html_
2336+
```html
2337+
<html>
2338+
<body>
2339+
<h1>Sports News</h1>
2340+
<a href="@views/home/index.html">Home</a> | <a href="@views/news/sport/index.html">Sport</a>
2341+
</body>
2342+
</html>
2343+
```
2344+
2345+
> [!TIP]
2346+
> If you use a template, you can include navigation as a partial on all pages,
2347+
> since the route URLs will be automatically resolved from their source files.
2348+
2349+
The generated _dist/index.html_
2350+
2351+
```html
2352+
<html>
2353+
<body>
2354+
<h1>Home</h1>
2355+
<a href="index.html">Home</a> | <a href="news/sport/index.html">Sport</a>
2356+
</body>
2357+
</html>
2358+
```
2359+
2360+
The generated _dist/news/sport/index.html_
2361+
2362+
```html
2363+
<html>
2364+
<body>
2365+
<h1>Sports News</h1>
2366+
<a href="../../index.html">Home</a> | <a href="index.html">Sport</a>
2367+
</body>
2368+
</html>
2369+
```
2370+
2371+
> [!TIP]
2372+
> You can use the `router.rewriteIndex` option to rewrite the `index.html`.
2373+
2374+
#### [↑ back to contents](#contents)
2375+
22102376
<a id="option-before-preprocessor" name="option-before-preprocessor"></a>
22112377
22122378
### `beforePreprocessor`
@@ -3260,6 +3426,10 @@ The default loader handles HTML files and `EJS`-like templates.
32603426
32613427
### `sources`
32623428
3429+
> ℹ️ **Note**
3430+
>
3431+
> Since the `v4.18.0` is available new syntax, the [sources](#option-sources) option should be defined directly in the plugin option to simplify the config.
3432+
32633433
Type:
32643434
32653435
```ts

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "4.18.0-beta.2",
3+
"version": "4.18.0",
44
"description": "Generates complete single-page or multi-page website from source assets. Build-in support for Markdown, Eta, EJS, Handlebars, Nunjucks, Pug. Alternative to html-webpack-plugin.",
55
"keywords": [
66
"html",

0 commit comments

Comments
 (0)