Skip to content

Commit 33330bc

Browse files
committed
chore: add test and example for inline all assets into HTML
1 parent 8dc7753 commit 33330bc

25 files changed

Lines changed: 5083 additions & 0 deletions

File tree

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
525525
- [How to inline CSS in HTML](#recipe-inline-css)
526526
- [How to inline JS in HTML](#recipe-inline-js)
527527
- [How to inline SVG, PNG images in HTML](#recipe-inline-image)
528+
- [How to inline all resources into single HTML file](#recipe-inline-all-assets-to-html)
528529
- [How to resolve source assets in an attribute containing JSON value](#recipe-resolve-attr-json)
529530
- [How to load CSS file dynamically](#recipe-dynamic-load-css) (lazy loading CSS)
530531
- [How to import CSS class names in JS](#recipe-css-modules) (CSS modules)
@@ -5241,6 +5242,64 @@ module: {
52415242
52425243
The plugin automatically inlines images smaller then `maxSize`.
52435244
5245+
---
5246+
5247+
#### [↑ back to contents](#contents)
5248+
5249+
<a id="recipe-inline-all-assets-to-html" name="recipe-inline-all-assets-to-html"></a>
5250+
5251+
## How to inline all resources into single HTML file
5252+
5253+
The bundler plugin can generate a single HTML file included all embedded dependencies
5254+
such as JS, CSS, fonts, images (PNG, SVG, etc..).
5255+
5256+
The fonts and images used in CSS will be inlined into CSS.
5257+
The generated CSS including inlined images will be inlined into HTML.
5258+
5259+
Just use the following config:
5260+
5261+
```js
5262+
const path = require('path');
5263+
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
5264+
5265+
module.exports = {
5266+
mode: 'production',
5267+
output: {
5268+
path: path.join(__dirname, 'dist/'),
5269+
},
5270+
plugins: [
5271+
new HtmlBundlerPlugin({
5272+
entry: {
5273+
index: './src/views/index.html',
5274+
},
5275+
css: {
5276+
inline: true, // inline CSS into HTML
5277+
},
5278+
js: {
5279+
inline: true, // inline JS into HTML
5280+
},
5281+
}),
5282+
],
5283+
module: {
5284+
rules: [
5285+
{
5286+
test: /\.(css|sass|scss)$/,
5287+
use: ['css-loader', 'sass-loader'],
5288+
},
5289+
// inline all assets: images, svg, fonts
5290+
{
5291+
test: /\.(png|jpe?g|webp|svg|woff2?)$/i,
5292+
type: 'asset/inline',
5293+
},
5294+
],
5295+
},
5296+
performance: false, // disable warning max size
5297+
};
5298+
```
5299+
5300+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/edit/inline-all-assets-to-html?file=README.md)
5301+
5302+
52445303
---
52455304
52465305
#### [↑ back to contents](#contents)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Inline all assets into HTML
2+
3+
This is an example how to build website into single HTML file.
4+
All assets such as images, SVG, fonts will be inlined in CSS and in HTML.
5+
All generated JS and CSS will be inlined into HTML.
6+
7+
Use the [HTML Builder Plugin](https://github.com/webdiscus/html-bundler-webpack-plugin) for Webpack
8+
to compile and bundle source files in HTML.
9+
10+
## View in browser
11+
12+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/edit/inline-all-assets-to-html?file=README.md)
13+
14+
## How to use
15+
16+
```sh
17+
git clone https://github.com/webdiscus/html-bundler-webpack-plugin.git
18+
cd examples/inline-all-assets-to-html
19+
npm install
20+
npm run build
21+
```

0 commit comments

Comments
 (0)