Skip to content

Commit 1967598

Browse files
committed
feat: if the router option is specified and not disabled then ensure that a.href is present in the sources
1 parent 6332827 commit 1967598

38 files changed

Lines changed: 388 additions & 46 deletions

File tree

CHANGELOG.md

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

3+
## 4.18.0-beta.2 (2025-02-03)
4+
5+
- feat: if the `router` option is specified and not disabled,
6+
then ensure that `a.href` is present in the `sources` option
7+
- fix: if router is disabled and sources contains attributes matching a route file, then doesn't resolve it
8+
39
## 4.18.0-beta.1 (2025-02-03)
410

511
- feat: revert improvement of the `sources[].filter()` function introduced in beta.0, it returns a boolean or void only

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.1",
3+
"version": "4.18.0-beta.2",
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",

src/Loader/Modes/PreprocessorModeAbstract.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class PreprocessorModeAbstract {
8484
* @param {string} file The required file.
8585
* @param {string} issuer The issuer of required file.
8686
* @param {string|number|null} entryId The entry id where is loaded the resource.
87-
* @return {{requireExpression: string, resolvedFile: string}}
87+
* @return {{requireExpression: string, resolvedFile: string} | false}
8888
*/
8989
requireFile(file, issuer, entryId) {
9090
let resolvedFile = '';
@@ -100,8 +100,14 @@ class PreprocessorModeAbstract {
100100
let isRouteFile = this.loaderOption.isRoute(resolvedFile);
101101
let isRequestUrl = isUrl(resolvedFile);
102102

103-
if (isRouteFile && !isRequestUrl) {
104-
this.collection.saveInnerRoute(resolvedFile, issuer);
103+
if (isRouteFile) {
104+
if (!this.loaderOption.isRouterEnabled()) {
105+
return false;
106+
}
107+
108+
if (!isRequestUrl) {
109+
this.collection.saveInnerRoute(resolvedFile, issuer);
110+
}
105111
}
106112

107113
if (isEntryFile || isRouteFile || isRequestUrl) {

src/Loader/Option.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ class Option {
346346
return this.#pluginOption.isRoute(resource);
347347
}
348348

349+
/**
350+
* Whether the router is force disabled.
351+
* @return {boolean}
352+
*/
353+
isRouterEnabled() {
354+
return this.#pluginOption.isRouterEnabled();
355+
}
356+
349357
/**
350358
* Whether the file matches a template entry file.
351359
*

src/Plugin/Collection.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -991,43 +991,42 @@ class Collection {
991991
promise = promise.then((value) => this.pluginOption.postprocess(value, templateInfo, compilation) || value);
992992
}
993993

994-
// 2.1 postprocess of resolved page URL in a.href
995-
const routesPages = this.innerRoute.get(resourcePath);
994+
// 2.1 postprocess of resolved page URLs matched to router.test from attributes specified in sources, e.g. a.href
995+
const routerPages = this.innerRoute.get(resourcePath);
996996

997-
if (isRouterEnabled && routesPages) {
997+
if (isRouterEnabled && routerPages) {
998998
promise = promise.then((content) => {
999999
const sourceFile = resourcePath;
10001000
const outputFile = entryFilename;
10011001

10021002
// sourceRoute is the resolved value of a tag attribute defined in sources option
1003-
for (let sourceRoute of routesPages) {
1003+
for (let sourceRoute of routerPages) {
10041004
const [sourceRoutePath, sourceRouteQuery] = splitUrl(sourceRoute);
10051005
const outputValue = entrySourceFiles.get(sourceRoutePath);
10061006
let outputRoute = this.pluginOption.getOutputFilename(outputValue, outputFile);
1007+
let outputRouteWithQuery = outputRoute + sourceRouteQuery;
10071008
let wasRouteModified = false;
10081009

10091010
if (resolveRoute) {
10101011
let result = resolveRoute({
10111012
sourceRoute,
1012-
outputRoute: outputRoute + sourceRouteQuery,
1013+
outputRoute: outputRouteWithQuery,
10131014
sourceFile,
10141015
outputFile,
10151016
});
10161017

1017-
if (typeof result === 'string' && result !== outputRoute) {
1018+
if (typeof result === 'string' && result !== outputRouteWithQuery) {
10181019
outputRoute = result;
10191020
wasRouteModified = true;
10201021
}
10211022
}
10221023

10231024
if (!wasRouteModified) {
1024-
if (!sourceRouteQuery) {
1025-
if (outputValue.includes('index.html') && routerOptions.rewriteIndex !== false) {
1026-
let index = outputRoute === 'index.html' ? routerOptions.rewriteIndex : '';
1027-
outputRoute = outputRoute.replace('index.html', index);
1028-
}
1029-
} else {
1030-
outputRoute += sourceRouteQuery;
1025+
if (sourceRouteQuery) {
1026+
outputRoute = outputRouteWithQuery;
1027+
} else if (outputValue.includes('index.html') && routerOptions.rewriteIndex !== false) {
1028+
let index = outputRoute === 'index.html' ? routerOptions.rewriteIndex : '';
1029+
outputRoute = outputRoute.replace('index.html', index);
10311030
}
10321031
}
10331032

src/Plugin/Option.js

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,6 @@ class Option {
114114
this.options.hotUpdate = this.options.hotUpdate === true;
115115
}
116116

117-
/**
118-
* @return {boolean}
119-
*/
120-
isRouterEnabled() {
121-
return this.router.enabled === true;
122-
}
123-
124-
/**
125-
* Get router option for resolving a page URL.
126-
*
127-
* @return {Object|null}
128-
*/
129-
getRouter() {
130-
return this.router;
131-
}
132-
133117
/**
134118
* Get custom page url resolver.
135119
*
@@ -352,6 +336,10 @@ class Option {
352336
this.router.rewriteIndex = !this.isAutoPublicPath() && this.getPublicPath() ? '' : rewriteIndex;
353337
}
354338
}
339+
340+
if (this.isRouterEnabled()) {
341+
this.addDefaultsRouterOptionsToSources();
342+
}
355343
}
356344

357345
if (this.router.test instanceof RegExp) {
@@ -361,6 +349,34 @@ class Option {
361349
}
362350
}
363351

352+
/**
353+
* If router option is defined and is not false, then add default options to sources,
354+
* to resolve route in attributes.
355+
*/
356+
addDefaultsRouterOptionsToSources() {
357+
if (!Array.isArray(this.options.sources)) {
358+
this.options.sources = [];
359+
}
360+
361+
const { sources } = this.options;
362+
let tagA = sources.find(({ tag }) => tag === 'a');
363+
let hasHref = false;
364+
365+
if (tagA) {
366+
if (Array.isArray(tagA.attributes)) {
367+
hasHref = tagA.attributes.some((attr) => attr === 'href');
368+
} else {
369+
tagA.attributes = [];
370+
}
371+
372+
if (!hasHref) {
373+
tagA.attributes.push('href');
374+
}
375+
} else {
376+
sources.push({ tag: 'a', attributes: ['href'] });
377+
}
378+
}
379+
364380
initWatchMode() {
365381
const { publicPath } = this.webpackOptions.output;
366382
if (publicPath == null || publicPath === 'auto') {
@@ -499,6 +515,16 @@ class Option {
499515
return testRegExpArray(resource, this.router.test);
500516
}
501517

518+
/**
519+
* Whether the router is force disabled.
520+
* Defaults, when sources options matches a template, then it will be resolved.
521+
*
522+
* @return {boolean}
523+
*/
524+
isRouterEnabled() {
525+
return this.router.enabled !== false;
526+
}
527+
502528
/**
503529
* @param {string} resource The resource file, including a query.
504530
* @return {boolean}
@@ -676,6 +702,15 @@ class Option {
676702
return this.options.css;
677703
}
678704

705+
/**
706+
* Get router option for resolving a page URL.
707+
*
708+
* @return {Object|null}
709+
*/
710+
getRouter() {
711+
return this.router;
712+
}
713+
679714
/**
680715
* @return {WatchFiles}
681716
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h1{color:green}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h1{color:red}
1.74 KB
Loading
Binary file not shown.

0 commit comments

Comments
 (0)