Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Type: `Boolean|Object`
Default: `true`

By default every loadable attributes (for example - `<img src="image.png">`) is imported (`const img = require('./image.png')` or `import img from "./image.png""`).
You may need to specify loaders for images in your configuration (recommended `file-loader` or `url-loader`).
You may need to specify loaders for images in your configuration (recommended [`asset modules`](https://webpack.js.org/guides/asset-modules/)).

Supported tags and attributes:

Expand Down Expand Up @@ -569,7 +569,7 @@ module.exports = {
},
{
test: /\.jpg$/,
loader: 'file-loader',
type: 'asset/resource',
},
],
},
Expand Down Expand Up @@ -597,8 +597,14 @@ module.exports = {
module.exports = {
module: {
rules: [
{ test: /\.jpg$/, loader: 'file-loader' },
{ test: /\.png$/, loader: 'url-loader' },
{
test: /\.jpg$/,
type: 'asset/resource',
},
{
test: /\.png$/,
type: 'asset/inline',
},
],
},
output: {
Expand Down Expand Up @@ -680,9 +686,16 @@ a {
module.exports = {
module: {
rules: [
{
test: /\.html$/,
type: 'asset/resource',
generator: {
filename: '[name][ext]',
},
},
{
test: /\.html$/i,
use: ['file-loader?name=[name].[ext]', 'extract-loader', 'html-loader'],
use: ['extract-loader', 'html-loader'],
},
{
test: /\.js$/i,
Expand All @@ -691,7 +704,7 @@ module.exports = {
},
{
test: /\.file.js$/i,
loader: 'file-loader',
type: 'asset/resource',
},
{
test: /\.css$/i,
Expand All @@ -700,7 +713,7 @@ module.exports = {
},
{
test: /\.file.css$/i,
loader: 'file-loader',
type: 'asset/resource',
},
],
},
Expand Down Expand Up @@ -802,26 +815,37 @@ module.exports = {

A very common scenario is exporting the HTML into their own _.html_ file, to
serve them directly instead of injecting with javascript. This can be achieved
with a combination of 3 loaders:
with a combination of 2 loaders:

- [file-loader](https://github.com/webpack/file-loader)
- [extract-loader](https://github.com/peerigon/extract-loader)
- html-loader

and [`asset modules`](https://webpack.js.org/guides/asset-modules/)

The html-loader will parse the URLs, require the images and everything you
expect. The extract loader will parse the javascript back into a proper html
file, ensuring images are required and point to proper path, and the file loader
file, ensuring images are required and point to proper path, and the [`asset modules`](https://webpack.js.org/guides/asset-modules/)
will write the _.html_ file for you. Example:

**webpack.config.js**

```js
module.exports = {
output: {
assetModuleFilename: '[name][ext]',
},
module: {
rules: [
{
test: /\.html$/,
type: 'asset/resource',
generator: {
filename: '[name][ext]',
},
},
{
test: /\.html$/i,
use: ['file-loader?name=[name].[ext]', 'extract-loader', 'html-loader'],
use: ['extract-loader', 'html-loader'],
},
],
},
Expand Down
93 changes: 12 additions & 81 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-import": "^2.22.1",
"file-loader": "^6.2.0",
"handlebars": "^4.7.6",
"husky": "^4.3.8",
"jest": "^26.6.3",
Expand All @@ -74,8 +73,7 @@
"prettier": "^2.1.2",
"standard-version": "^9.0.0",
"unescape-unicode": "^0.2.0",
"url-loader": "^4.1.1",
"webpack": "^5.19.0"
"webpack": "^5.21.2"
},
"keywords": [
"webpack",
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/esModule-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down Expand Up @@ -500,7 +500,7 @@ var ___HTML_LOADER_IMPORT_12___ = require(\\"./module.file.js\\");
var ___HTML_LOADER_IMPORT_13___ = require(\\"./fallback.file.js\\");
var ___HTML_LOADER_IMPORT_14___ = require(\\"aliasImageWithSpace\\");
var ___HTML_LOADER_IMPORT_15___ = require(\\"./webpack.svg\\");
var ___HTML_LOADER_IMPORT_16___ = require(\\"!!url-loader!./pixel.png\\");
var ___HTML_LOADER_IMPORT_16___ = require(\\"./pixel.png?url\\");
var ___HTML_LOADER_IMPORT_17___ = require(\\"./site.webmanifest\\");
var ___HTML_LOADER_IMPORT_18___ = require(\\"./browserconfig.xml\\");
var ___HTML_LOADER_IMPORT_19___ = require(\\"./sound.mp3\\");
Expand Down Expand Up @@ -979,7 +979,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/minimize-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down Expand Up @@ -500,7 +500,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down Expand Up @@ -979,7 +979,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down Expand Up @@ -1084,7 +1084,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down Expand Up @@ -1563,7 +1563,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down Expand Up @@ -1651,7 +1651,7 @@ import ___HTML_LOADER_IMPORT_12___ from \\"./module.file.js\\";
import ___HTML_LOADER_IMPORT_13___ from \\"./fallback.file.js\\";
import ___HTML_LOADER_IMPORT_14___ from \\"aliasImageWithSpace\\";
import ___HTML_LOADER_IMPORT_15___ from \\"./webpack.svg\\";
import ___HTML_LOADER_IMPORT_16___ from \\"!!url-loader!./pixel.png\\";
import ___HTML_LOADER_IMPORT_16___ from \\"./pixel.png?url\\";
import ___HTML_LOADER_IMPORT_17___ from \\"./site.webmanifest\\";
import ___HTML_LOADER_IMPORT_18___ from \\"./browserconfig.xml\\";
import ___HTML_LOADER_IMPORT_19___ from \\"./sound.mp3\\";
Expand Down
32 changes: 16 additions & 16 deletions test/__snapshots__/sources-option.test.js.snap

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions test/fixtures/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,12 @@ <h2>An Ordered HTML List</h2>

<button onclick=" document.getElementById('demo').innerHTML = Date()">The time is?</button>

<img src="!!url-loader!./pixel.png" alt="" />
<img src="./pixel.png?url" alt="" />

<img src="


!!url-loader!./pixel.png
./pixel.png?url


" alt="" />
Expand Down
Loading