Skip to content

Commit 7b6d3e9

Browse files
committed
docs: add in readme the recipe "How to import SVG in JavaScript"
1 parent 4dc9fe2 commit 7b6d3e9

21 files changed

Lines changed: 302 additions & 19 deletions

File tree

CHANGELOG.md

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

3+
## 4.11.2 (2025-01-05)
4+
5+
- chore: update dependencies
6+
- chore: update license to current date
7+
- docs: add in readme the recipe "How to import SVG in JavaScript"
8+
39
## 4.11.1 (2024-12-27)
410

511
- fix: in TypeScript the `renderStage` option should be optional

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ISC License
22

3-
Copyright (c) 2024, webdiscus
3+
Copyright (c) 2025, webdiscus
44

55
Permission to use, copy, modify, and/or distribute this software for any
66
purpose with or without fee is hereby granted, provided that the above

README.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
557557
- [How to inline JS in HTML](#recipe-inline-js)
558558
- [How to inline SVG, PNG images in HTML](#recipe-inline-image)
559559
- [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)
560561
- [How to resolve source assets in an attribute containing JSON value](#recipe-resolve-attr-json)
561562
- [How to resolve source image in the `style` attribute](#recipe-resolve-attr-style-url)
562563
- [How to resolve source image in the `href` attribute](#recipe-resolve-attr-href-a-image) (`<a href="image.jpg">`)
@@ -5671,6 +5672,159 @@ module.exports = {
56715672
56725673
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/edit/inline-all-assets-to-html?file=README.md)
56735674
5675+
---
5676+
5677+
#### [↑ back to contents](#contents)
5678+
5679+
<a id="recipe-import-svg" name="recipe-import-svg"></a>
5680+
5681+
## How to import SVG in JS
5682+
5683+
You don't need the [svg-url-loader](https://github.com/bhovhannes/svg-url-loader) anymore.
5684+
If you use it yet, remove this legacy loader from your configuration.
5685+
Since Webpack 5, you can use the native Webpack [Asset Modules](https://webpack.js.org/guides/asset-modules/),
5686+
which are supported by the Bundler Plugin.
5687+
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`.
5691+
5692+
### Use cases
5693+
5694+
#### Import SVG file in JS as output filename.
5695+
5696+
Using default configuration of module rules with `asset/resource` module type, the imported SVG file will contain a output filename.
5697+
5698+
```js
5699+
module: {
5700+
rules: [
5701+
{
5702+
test: /\.svg/i,
5703+
type: 'asset/resource',
5704+
generator: {
5705+
filename: 'img/[name].[hash:8][ext]',
5706+
},
5707+
},
5708+
],
5709+
}
5710+
```
5711+
5712+
The imported `file` contains the hashed output filename.
5713+
5714+
```js
5715+
import file from './image.svg';
5716+
5717+
console.log(file); // img/image.416b7e1d.svg
5718+
```
5719+
5720+
#### Import SVG file in JS as data URL.
5721+
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.
5724+
5725+
For example, use the `?dataurl` query to import a SVG file as data URL:
5726+
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+
```
5738+
5739+
By importing an SVG file using the `?dataurl` query, Webpack generates a base64-encoded data URL.
5740+
5741+
```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.
5751+
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+
}
5764+
```
5765+
5766+
By importing an SVG file using the `?raw` query, you will get source code.
5767+
5768+
```js
5769+
import file from './image.svg?raw'; // <= the `raw` query must be defined in Webpack
5770+
5771+
console.log(file); // <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 96 960 960">...</svg>
5772+
```
5773+
5774+
### Import SVG in JS with different ways
5775+
5776+
For example, you need:
5777+
5778+
- 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
5781+
5782+
5783+
Combine all your use cases in the Webpack configuration with `oneOf`:
5784+
5785+
```js
5786+
module: {
5787+
rules: [
5788+
{
5789+
test: /\.svg/i, // note: don't add `$` to the end of the RegEx
5790+
oneOf: [
5791+
// import SVG in JS as data URL using `?dataurl` query
5792+
{
5793+
resourceQuery: /dataurl/,
5794+
type: 'asset/inline',
5795+
},
5796+
// import SVG in JS as raw content using `?raw` query
5797+
{
5798+
resourceQuery: /raw/,
5799+
type: 'asset/source',
5800+
},
5801+
// fallback to default behavior:
5802+
// import SVG in JS as output filename
5803+
{
5804+
type: 'asset/resource',
5805+
generator: {
5806+
filename: 'img/[name].[hash:8][ext]',
5807+
},
5808+
},
5809+
],
5810+
},
5811+
],
5812+
},
5813+
```
5814+
5815+
Now you can import SVG with different ways in one JS file.
5816+
5817+
```js
5818+
import svgFilename from './image.svg'; // import as output filename
5819+
import svgDataUrl from './image.svg?dataurl'; // import as data URL
5820+
import svgSource from './image.svg?raw'; // import as source code
5821+
5822+
console.log(svgFilename); // img/image.416b7e1d.svg
5823+
console.log(svgDataUrl); // data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...vc3ZnPgo=
5824+
console.log(svgSource); // <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 96 960 960">...</svg>
5825+
```
5826+
5827+
See the [test case](https://github.com/webdiscus/html-bundler-webpack-plugin/tree/master/test/cases/js-import-image-svg) as working example.
56745828
56755829
---
56765830

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "4.11.1",
3+
"version": "4.11.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",
@@ -146,7 +146,7 @@
146146
},
147147
"dependencies": {
148148
"@types/html-minifier-terser": "^7.0.2",
149-
"ansis": "3.4.0-beta.1",
149+
"ansis": "3.6.0",
150150
"enhanced-resolve": ">=5.7.0",
151151
"eta": "^3.5.0",
152152
"html-minifier-terser": "^7.2.0"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html><head><script src="js/file1.034defc3.js"></script><script src="js/file2.e5c7f644.js"></script><script src="js/file3.f1b372d2.js"></script><script src="js/file4.daa27ac4.js"></script><script src="js/file5.c863871f.js"></script><script src="js/cli.3c1e5e89.js"></script><script src="js/index.6110d541.js"></script></head><body><h1>Hello World!</h1></body></html>
1+
<!DOCTYPE html><html><head><script src="js/file1.034defc3.js"></script><script src="js/file2.e5c7f644.js"></script><script src="js/file3.f1b372d2.js"></script><script src="js/file4.daa27ac4.js"></script><script src="js/file5.c863871f.js"></script><script src="js/cli.250af678.js"></script><script src="js/index.ca3dc62f.js"></script></head><body><h1>Hello World!</h1></body></html>

test/cases/_pug/require-script-tags/expected/js/cli.250af678.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cases/_pug/require-script-tags/expected/js/cli.3c1e5e89.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/cases/_pug/require-script-tags/expected/js/index.6110d541.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)