Skip to content

Commit ce45ae8

Browse files
committed
feat: if ?inline query is not specified, dataURL is a result of generator.dataUrl() function
1 parent 9df74e2 commit ce45ae8

58 files changed

Lines changed: 327 additions & 234 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
node-version: [18, 20, 22]
2020

2121
runs-on: ${{ matrix.os }}
22+
name: integration - Node ${{ matrix.node-version }} on ${{ matrix.os }}
2223
steps:
2324
- name: Checkout repository
2425
uses: actions/checkout@v4

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.19.0-beta.5
4+
5+
- feat: if ?inline query is not specified, dataURL is a result of generator.dataUrl() function.
6+
The encoding can be specified using a query value (`base64` or `escape`).
7+
38
## 4.19.0-beta.4
49

510
- feat: display a warning when used `embed` URL query for SVG files in JS or CSS

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Additionally, CSS extracted from styles imported in JS can be injected into HTML
6969

7070
See [full list of features](#features).
7171

72+
> [!IMPORTANT]
73+
> If this plugin makes your work easier, you might like to star ⭐ it.
74+
7275
## HTML template as entry point
7376

7477
<center>
@@ -179,13 +182,6 @@ See full list of all [plugin options](#toc-plugin-options).
179182
> [!TIP]
180183
> To resolve route URLs in `a.href` for navigation across multiple pages, you can enable the [router](#option-router) option.
181184
182-
---
183-
> 🦖 **Mozilla** already uses this plugin to build static HTML files for the [Mozilla AI GUIDE](https://github.com/mozilla/ai-guide) site.
184-
>
185-
> The plugin has been actively developed for more than 2 years, and since 2023 it is open source.\
186-
> Please support this project by giving it a star ⭐.
187-
---
188-
189185

190186
## ❤️ Sponsors & Patrons
191187

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.1",
3+
"version": "4.19.0-beta.5",
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/Plugin/AssetCompiler.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ class AssetCompiler {
435435
// TODO: collect errors
436436
});
437437

438+
// TODO: avoid random index order
439+
// compilation.hooks.beforeChunkIds.tap(pluginName, (chunks) => {
440+
// for (const chunk of chunks) {
441+
// if (chunk.name.startsWith('script')) {
442+
// // TODO: sort chinks by index and rename chunk.name, chunk.runtime according index
443+
// console.log(chunks);
444+
// }
445+
// }
446+
// });
447+
438448
// called after the succeedModule hook but right before the execution of a loader
439449
normalModuleHooks.loader.tap(pluginName, this.beforeLoader);
440450

@@ -548,6 +558,7 @@ class AssetCompiler {
548558
return;
549559
}
550560

561+
// 2. Invalidate a JavaScript file loaded in an entry template.
551562
// 2. Invalidate a JavaScript file loaded in an entry template.
552563

553564
if (actionType && isScript) {
@@ -742,11 +753,13 @@ class AssetCompiler {
742753
if (this.assetInline.isSvgFile(resource)) {
743754
svgOptions = this.pluginOption.getInlineSvgOptions(resource, createData);
744755

756+
//console.log('--- afterResolve: ', { issuer, resource, svgOptions });
757+
745758
if (svgOptions?.warning) {
746759
outputWarning(svgOptions.warning);
747760
}
748761

749-
isInlineSvg = svgOptions != null;
762+
isInlineSvg = svgOptions.inline;
750763
encoding = svgOptions?.encoding;
751764
}
752765

@@ -1005,7 +1018,7 @@ class AssetCompiler {
10051018
].includes(type);
10061019

10071020
const isSvgFile = this.assetInline.isSvgFile(resource);
1008-
const isInlineSvg = isSvgFile ? this.pluginOption.getInlineSvgOptions(resource, module) != null : false;
1021+
const isInlineSvg = isSvgFile && this.pluginOption.getInlineSvgOptions(resource, module).inline;
10091022

10101023
//console.log('*** isInlineSvg: ', { resource, isSvgFile, isInlineSvg });
10111024

@@ -1072,11 +1085,7 @@ class AssetCompiler {
10721085
*
10731086
* @param {Object} module The Webpack module.
10741087
*/
1075-
afterBuildModule(module) {
1076-
// if (module.resource.includes('&js')) {
1077-
// console.log('*** afterBuildModule: ', module);
1078-
// }
1079-
}
1088+
afterBuildModule(module) {}
10801089

10811090
/**
10821091
* @param {Array<Object>} result
@@ -1148,7 +1157,7 @@ class AssetCompiler {
11481157
? this.pluginOption.getInlineSvgOptions(resource, module)
11491158
: null;
11501159

1151-
if (svgOptions !== null) {
1160+
if (svgOptions?.inline) {
11521161
//console.log('*** renderManifest: ', { resource }, svgOptions);
11531162
this.assetInline.saveData(entry, chunk, module, codeGenerationResults, moduleType, svgOptions);
11541163
} else {

src/Plugin/AssetEntry.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,15 @@ class AssetEntry {
597597
* @return {string} Return the unique entry name.
598598
*/
599599
addToCompilation({ name, importFile, filenameTemplate, context, issuer }) {
600-
name = this.#getUniqueEntryName(name, importFile);
600+
//const issuerId = this.getByResource(issuer)?.id;
601+
602+
if (!name) {
603+
name = path.parse(importFile).name;
604+
}
605+
606+
let originalName = name;
607+
608+
name = this.#getUniqueEntryName(name);
601609

602610
// skip duplicate entries
603611
if (this.#hasEntry(name, importFile)) {
@@ -624,6 +632,7 @@ class AssetEntry {
624632
const assetEntryOptions = {
625633
id: undefined,
626634
name,
635+
originalName,
627636
filenameTemplate,
628637
filename: undefined,
629638
resource: importFile,
@@ -666,14 +675,9 @@ class AssetEntry {
666675

667676
/**
668677
* @param {string|null} name The entry name.
669-
* @param {string} file The source file.
670678
* @return {string } Return unique entry name.
671679
*/
672-
#getUniqueEntryName(name, file) {
673-
if (!name) {
674-
name = path.parse(file).name;
675-
}
676-
680+
#getUniqueEntryName(name) {
677681
let uniqueName = name;
678682
let hasEntry = this.compilation.entries.has(name);
679683

@@ -749,7 +753,10 @@ class AssetEntry {
749753
const { name, id } = assetEntryOptions;
750754

751755
this.entriesByName.set(name, assetEntryOptions);
752-
this.entriesById.set(id, assetEntryOptions);
756+
757+
if (id) {
758+
this.entriesById.set(id, assetEntryOptions);
759+
}
753760
}
754761

755762
/**

src/Plugin/Option.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,12 @@ class Option {
651651

652652
/** @type {'base64' | false | undefined } webpackModuleEncoding */
653653
const webpackModuleEncoding = module?.generatorOptions?.dataUrl?.encoding;
654+
const isDataUrlFunction = typeof module?.generatorOptions?.dataUrl === 'function';
654655

655656
let result = {
656-
encoding: this.svg.inline.encoding,
657+
isDataUrlFunction,
658+
encoding: null,
659+
inline: true,
657660
embed: this.svg.inline.embed,
658661
};
659662

@@ -683,7 +686,7 @@ class Option {
683686
break;
684687
// `?inline=false` - force disables inline
685688
case 'false':
686-
result = null;
689+
result.inline = false;
687690
break;
688691
// `?inline=embed` - force replaces <img> with <svg>
689692
case 'embed':
@@ -701,34 +704,23 @@ class Option {
701704
break;
702705
}
703706
} else if (this.options.svg.enabled !== true) {
704-
result = null;
707+
result.inline = false;
705708
}
706709

707-
if (result && result.encoding == null) {
710+
if (!isDataUrlFunction && result.encoding == null) {
708711
// TODO: test this case
709712
result.encoding = 'base64';
710713
}
711714

712715
if (isIssuerEntry && embed) {
713-
result = { embed: true };
716+
// embedded is always inlined, but in HTML only
717+
result.embed = true;
718+
result.inline = true;
714719
}
715720

716721
// save detected option into module
717722
if (meta && !('inlineSvg' in meta)) {
718723
meta.inlineSvg = result;
719-
720-
// if (!this.svgOptionCallCounter) {
721-
// this.svgOptionCallCounter = 1;
722-
// } else {
723-
// this.svgOptionCallCounter++;
724-
// }
725-
726-
// console.log(
727-
// '*** getInlineSvgOptions: ',
728-
// { issuer, resource, isIssuerEntry, embed, svgOptionCallCounter: this.svgOptionCallCounter, result }
729-
//
730-
// // module
731-
// );
732724
}
733725

734726
return result;

src/Plugin/Resolver.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ class Resolver {
274274
// if is used the filename like `./main.js`, then the resource is an absolute file
275275
// if is used the filename like `../js/main.js`, then the resource is null and the rawRequest is an absolute file
276276
const file = resource || rawRequest;
277+
277278
if (this.pluginOption.js.test.test(file) && this.assetEntry.isEntryResource(issuer.resource)) {
278279
// occur after rename/delete of a js file when the entry module was already rebuilt
279280
Snapshot.addMissingFile(issuer.resource, file);
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)