Skip to content

Commit 533a26e

Browse files
committed
feat: add support for the ?inline query to load assets as data URL
1 parent 5390033 commit 533a26e

55 files changed

Lines changed: 634 additions & 224 deletions

Some content is hidden

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

CHANGELOG.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
# Change log
1+
# Changelog
2+
3+
## 4.12.0 (2025-01-12)
4+
5+
- feat(release): add support for the `?inline` query to load assets as data URL
6+
- test: add tests for `?inline` query
7+
- docs: update readme for new feature
8+
9+
## 4.12.0-beta.1 (2025-01-07)
10+
11+
- feat: add support for the `?inline` query to load assets as data URL in HTML, CSS and JS.
12+
By using the `?inline` query, the following configuration can now be omitted:
13+
```js
14+
{
15+
// since v4.12 is not need anymore
16+
resourceQuery: /inline/,
17+
type: 'asset/inline',
18+
},
19+
```
220

321
## 4.12.0-beta.0 (2025-01-06)
422

README.md

Lines changed: 107 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,8 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
556556
- [How to inline CSS in HTML](#recipe-inline-css)
557557
- [How to inline JS in HTML](#recipe-inline-js)
558558
- [How to inline SVG, PNG images in HTML](#recipe-inline-image)
559+
- [How to import SVG in JS w/o `svg-url-loader`](#recipe-import-svg) (as filename or as data URL)
559560
- [How to inline all resources into single HTML file](#recipe-inline-all-assets-to-html)
560-
- [How to import SVG in JS w/o `svg-url-loader`](#recipe-import-svg) (as filename, as data URL, as raw content)
561561
- [How to resolve source assets in an attribute containing JSON value](#recipe-resolve-attr-json)
562562
- [How to resolve source image in the `style` attribute](#recipe-resolve-attr-style-url)
563563
- [How to resolve source image in the `href` attribute](#recipe-resolve-attr-href-a-image) (`<a href="image.jpg">`)
@@ -1019,7 +1019,7 @@ class MyPlugin {
10191019

10201020
const { pluginName } = this;
10211021
const { webpack } = compiler; // instance of the Webpack
1022-
const fs = compiler.inputFileSystem.fileSystem; // instance of the Webpack FyleSystem
1022+
const fs = compiler.inputFileSystem.fileSystem; // instance of the Webpack FileSystem
10231023

10241024
// start your plugin from the webpack compilation hook
10251025
compiler.hooks.compilation.tap(pluginName, (compilation) => {
@@ -5579,7 +5579,7 @@ The generated HTML contains inline JS already compiled via Webpack:
55795579
55805580
You can inline the images in two ways:
55815581
5582-
- force inline image using `?inline` query
5582+
- force inline image using `?inline` query (works in HTML, CSS, [JS](#recipe-import-svg))
55835583
- auto inline by image size
55845584
55855585
Add to Webpack config the rule:
@@ -5589,25 +5589,18 @@ module: {
55895589
rules: [
55905590
{
55915591
test: /\.(png|jpe?g|svg|webp|ico)$/i,
5592-
oneOf: [
5593-
// inline image using `?inline` query
5594-
{
5595-
resourceQuery: /inline/,
5596-
type: 'asset/inline',
5597-
},
5598-
// auto inline by image size
5599-
{
5600-
type: 'asset',
5601-
parser: {
5602-
dataUrlCondition: {
5603-
maxSize: 1024,
5604-
},
5605-
},
5606-
generator: {
5607-
filename: 'assets/img/[name].[hash:8][ext]',
5592+
// auto inline by image size
5593+
{
5594+
type: 'asset',
5595+
parser: {
5596+
dataUrlCondition: {
5597+
maxSize: 1024,
56085598
},
56095599
},
5610-
],
5600+
generator: {
5601+
filename: 'assets/img/[name].[hash:8][ext]',
5602+
},
5603+
},
56115604
},
56125605
],
56135606
}
@@ -5619,63 +5612,6 @@ The plugin automatically inlines images smaller then `maxSize`.
56195612
56205613
#### [↑ back to contents](#contents)
56215614
5622-
<a id="recipe-inline-all-assets-to-html" name="recipe-inline-all-assets-to-html"></a>
5623-
5624-
## How to inline all resources into single HTML file
5625-
5626-
The bundler plugin can generate a single HTML file included all embedded dependencies
5627-
such as JS, CSS, fonts, images (PNG, SVG, etc..).
5628-
5629-
The fonts and images used in CSS will be inlined into CSS.
5630-
The generated CSS including inlined images will be inlined into HTML.
5631-
5632-
Just use the following config:
5633-
5634-
```js
5635-
const path = require('path');
5636-
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
5637-
5638-
module.exports = {
5639-
mode: 'production',
5640-
output: {
5641-
path: path.join(__dirname, 'dist/'),
5642-
},
5643-
plugins: [
5644-
new HtmlBundlerPlugin({
5645-
entry: {
5646-
index: './src/views/index.html',
5647-
},
5648-
css: {
5649-
inline: true, // inline CSS into HTML
5650-
},
5651-
js: {
5652-
inline: true, // inline JS into HTML
5653-
},
5654-
}),
5655-
],
5656-
module: {
5657-
rules: [
5658-
{
5659-
test: /\.(css|sass|scss)$/,
5660-
use: ['css-loader', 'sass-loader'],
5661-
},
5662-
// inline all assets: images, svg, fonts
5663-
{
5664-
test: /\.(png|jpe?g|webp|svg|woff2?)$/i,
5665-
type: 'asset/inline',
5666-
},
5667-
],
5668-
},
5669-
performance: false, // disable warning max size
5670-
};
5671-
```
5672-
5673-
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/edit/inline-all-assets-to-html?file=README.md)
5674-
5675-
---
5676-
5677-
#### [↑ back to contents](#contents)
5678-
56795615
<a id="recipe-import-svg" name="recipe-import-svg"></a>
56805616
56815617
## How to import SVG in JS
@@ -5685,15 +5621,17 @@ If you use it yet, remove this legacy loader from your configuration.
56855621
Since Webpack 5, you can use the native Webpack [Asset Modules](https://webpack.js.org/guides/asset-modules/),
56865622
which are supported by the Bundler Plugin.
56875623
5688-
> [!NOTE]
5689-
> This requires a couple of extra lines of configuration,
5690-
> but is faster since it doesn't use extra handlers like `svg-url-loader`.
5624+
#### Replacement of svg-url-loader options
56915625
5692-
### Use cases
5626+
- [encoding](https://github.com/bhovhannes/svg-url-loader#encoding) - Use the `?inline` or `?inline=base64` query.
5627+
- [prefix]() - Use the [output filename](https://webpack.js.org/guides/asset-modules/#custom-output-filename) option.
5628+
- [limit](https://github.com/bhovhannes/svg-url-loader#limit) - Use the [Rule.parser.dataUrlCondition](https://webpack.js.org/configuration/module/#ruleparserdataurlcondition) option.
5629+
- [iesafe](https://github.com/bhovhannes/svg-url-loader#iesafe) - The same as by `limit` using `Rule.parser.dataUrlCondition.maxSize = 4096`.
5630+
- [stripdeclarations](https://github.com/bhovhannes/svg-url-loader#stripdeclarations) - we wan't manipulate the SVG content, may be useful to read by [svgo](https://github.com/svg/svgo/issues/836).\
5631+
Anyway [In XML 1.1, the declaration is mandatory](https://stackoverflow.com/a/7007781/1696030), therefore this option is senseless.
56935632
5694-
#### Import SVG file in JS as output filename.
56955633
5696-
Using default configuration of module rules with `asset/resource` module type, the imported SVG file will contain a output filename.
5634+
To handle SVG files define the module configuration:
56975635
56985636
```js
56995637
module: {
@@ -5709,75 +5647,51 @@ module: {
57095647
}
57105648
```
57115649
5712-
The imported `file` contains the hashed output filename.
5650+
### Import SVG file as output filename.
5651+
5652+
Using the module configuration as `asset/resource`, the imported SVG file will contain a output filename.
5653+
57135654
57145655
```js
57155656
import file from './image.svg';
57165657

57175658
console.log(file); // img/image.416b7e1d.svg
57185659
```
57195660
5720-
#### Import SVG file in JS as data URL.
5661+
### Import SVG file as data URL.
57215662
5722-
To import SVG file as the data URL, use the `asset/inline` module type.
5723-
If you import an SVG file in different ways, you can define a URL query for each module type.
5663+
The Bundler Plugin supports the `?inline` URL query to force load SVG file as a data URL.
57245664
5725-
For example, use the `?dataurl` query to import a SVG file as data URL:
5665+
> [!NOTE]
5666+
> The `inline` query works independent of used module type.
57265667
5727-
```js
5728-
module: {
5729-
rules: [
5730-
{
5731-
test: /\.svg/i,
5732-
resourceQuery: /dataurl/, // <= you can define any query as you like
5733-
type: 'asset/inline',
5734-
},
5735-
],
5736-
}
5737-
```
5668+
The `inline` query parameter supports values of encoding:
5669+
- `utf8` - import SVG as UTF-8 data URL (defaults, can be omitted)
5670+
- `base64` - import SVG as base64-encoded data URL
57385671
5739-
By importing an SVG file using the `?dataurl` query, Webpack generates a base64-encoded data URL.
5672+
For example, import SVG as UTF-8 data URL:
57405673
57415674
```js
5742-
import file from './image.svg?dataurl'; // <= the `dataurl` query must be defined in Webpack
5743-
5744-
console.log(file); // data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...vc3ZnPgo=
5745-
```
5746-
5747-
#### Import SVG file in JS as source code.
5748-
5749-
To import the source code of the SVG file, use the `asset/source` module type.
5750-
If you import an SVG file in different ways, define a URL query for each module type.
5675+
import file from './image.svg?inline';
57515676

5752-
For example, use the `?raw` query to import the source code of the SVG file:
5753-
5754-
```js
5755-
module: {
5756-
rules: [
5757-
{
5758-
test: /\.svg/i,
5759-
resourceQuery: /raw/, // <= you can define any query as you like
5760-
type: 'asset/source',
5761-
},
5762-
],
5763-
}
5677+
console.log(file); // data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2...%3C%2Fsvg%3E
57645678
```
57655679
5766-
By importing an SVG file using the `?raw` query, you will get source code.
5680+
To import SVG as base64-encoded data URL, use the `?inline=base64` query.
57675681
57685682
```js
5769-
import file from './image.svg?raw'; // <= the `raw` query must be defined in Webpack
5683+
import file from './image.svg?inline=base64';
57705684

5771-
console.log(file); // <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 96 960 960">...</svg>
5685+
console.log(file); // data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...vc3ZnPgo=
57725686
```
57735687
5774-
### Import SVG in JS with different ways
5688+
### Manual import SVG in JS with different ways
57755689
57765690
For example, you need:
57775691
57785692
- by default, the output filename will be generated
5779-
- using `?dataurl` query, the base64-encoded data URL will be generated
5780-
- using `?raw` query, the source code of the SVG file will be loaded
5693+
- using the `?dataurl` custom query, the base64-encoded data URL will be generated
5694+
- using the `?raw` custom query, the source code of the SVG file will be loaded
57815695
57825696
57835697
Combine all your use cases in the Webpack configuration with `oneOf`:
@@ -5786,14 +5700,14 @@ Combine all your use cases in the Webpack configuration with `oneOf`:
57865700
module: {
57875701
rules: [
57885702
{
5789-
test: /\.svg/i, // note: don't add `$` to the end of the RegEx
5703+
test: /\.svg/i,
57905704
oneOf: [
5791-
// import SVG in JS as data URL using `?dataurl` query
5705+
// import SVG in JS as data URL using the `?dataurl` custom query
57925706
{
57935707
resourceQuery: /dataurl/,
57945708
type: 'asset/inline',
57955709
},
5796-
// import SVG in JS as raw content using `?raw` query
5710+
// import SVG in JS as raw content using `?raw` custom query
57975711
{
57985712
resourceQuery: /raw/,
57995713
type: 'asset/source',
@@ -5804,6 +5718,12 @@ module: {
58045718
type: 'asset/resource',
58055719
generator: {
58065720
filename: 'img/[name].[hash:8][ext]',
5721+
dataUrl: {
5722+
// encoding values:
5723+
// 'base64' - generates base64-encoded data URL (Webpack defaults)
5724+
// false - generates UTF-8 data URL, use it only for SVG
5725+
encoding: false,
5726+
},
58075727
},
58085728
},
58095729
],
@@ -5830,6 +5750,63 @@ See the [test case](https://github.com/webdiscus/html-bundler-webpack-plugin/tre
58305750
58315751
#### [↑ back to contents](#contents)
58325752
5753+
<a id="recipe-inline-all-assets-to-html" name="recipe-inline-all-assets-to-html"></a>
5754+
5755+
## How to inline all resources into single HTML file
5756+
5757+
The bundler plugin can generate a single HTML file included all embedded dependencies
5758+
such as JS, CSS, fonts, images (PNG, SVG, etc..).
5759+
5760+
The fonts and images used in CSS will be inlined into CSS.
5761+
The generated CSS including inlined images will be inlined into HTML.
5762+
5763+
Just use the following config:
5764+
5765+
```js
5766+
const path = require('path');
5767+
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
5768+
5769+
module.exports = {
5770+
mode: 'production',
5771+
output: {
5772+
path: path.join(__dirname, 'dist/'),
5773+
},
5774+
plugins: [
5775+
new HtmlBundlerPlugin({
5776+
entry: {
5777+
index: './src/views/index.html',
5778+
},
5779+
css: {
5780+
inline: true, // inline CSS into HTML
5781+
},
5782+
js: {
5783+
inline: true, // inline JS into HTML
5784+
},
5785+
}),
5786+
],
5787+
module: {
5788+
rules: [
5789+
{
5790+
test: /\.(css|sass|scss)$/,
5791+
use: ['css-loader', 'sass-loader'],
5792+
},
5793+
// inline all assets: images, svg, fonts
5794+
{
5795+
test: /\.(png|jpe?g|webp|svg|woff2?)$/i,
5796+
type: 'asset/inline',
5797+
},
5798+
],
5799+
},
5800+
performance: false, // disable warning max size
5801+
};
5802+
```
5803+
5804+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/edit/inline-all-assets-to-html?file=README.md)
5805+
5806+
---
5807+
5808+
#### [↑ back to contents](#contents)
5809+
58335810
<a id="recipe-resolve-attr-json" name="recipe-resolve-attr-json"></a>
58345811
58355812
## How to resolve source assets in an attribute containing JSON value

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.12.0-beta.0",
3+
"version": "4.12.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)