|
4 | 4 | <br> |
5 | 5 | <a href="https://github.com/webdiscus/html-bundler-webpack-plugin">HTML Bundler Plugin for Webpack</a> |
6 | 6 | </h1> |
7 | | - <div>The plugin renders various templates with referenced source asset files into HTML</div> |
8 | 7 | </div> |
9 | 8 |
|
10 | | ---- |
11 | | - |
12 | 9 | [](https://www.npmjs.com/package/html-bundler-webpack-plugin 'download npm package') |
13 | 10 | [](https://nodejs.org) |
14 | 11 | [](https://webpack.js.org/) |
|
18 | 15 |
|
19 | 16 | ## HTML template as entry point |
20 | 17 |
|
21 | | -> This plugin allows using a template file as an [entry point](#option-entry). |
22 | | -
|
23 | | -The **HTML Bundler** generates static HTML or [template function](#template-in-js) from [any template](#template-engine) containing source files of scripts, styles, images, fonts and other resources, similar to how it works in [Vite](https://vitejs.dev/guide/#index-html-and-project-root). |
| 18 | +The **HTML Bundler** generates static HTML or [template function](#template-in-js) from [various templates](#template-engine) containing source files of scripts, styles, images, fonts and other resources, similar to how it works in [Vite](https://vitejs.dev/guide/#index-html-and-project-root). |
| 19 | +This plugin allows using a template file as an [entry point](#option-entry). |
24 | 20 |
|
25 | 21 | A template imported in JS will be compiled into [template function](#template-in-js). You can use the **template function** in JS to render the template with variables in runtime on the client-side in the browser. |
26 | 22 |
|
@@ -215,9 +211,11 @@ If you have discovered a bug or have a feature suggestion, feel free to create a |
215 | 211 |
|
216 | 212 | ## 🔆 What's New in v3 |
217 | 213 |
|
218 | | -- **NEW** added support for the [template function](#template-in-js) in JS runtime on the client-side. |
| 214 | +- **NEW** added supports the [template function](#template-in-js) in JS runtime on the client-side. |
219 | 215 | - **NEW** added [Pug](#using-template-pug) preprocessor. |
220 | 216 | - **NEW** added [Twig](#using-template-twig) preprocessor. |
| 217 | +- **NEW** added supports the dynamic import of styles. |
| 218 | +- **NEW** added supports the [CSS Modules](#recipe-css-modules) for styles imported in JS. |
221 | 219 | - **NEW** added CSS extraction from **styles** used in `*.vue` files. |
222 | 220 | - **NEW** added [Hooks & Callbacks](#plugin-hooks-and-callbacks). Now you can create own plugin to extend this plugin. |
223 | 221 | - **NEW** added the build-in [FaviconsBundlerPlugin](#favicons-bundler-plugin) to generate and inject favicon tags. |
@@ -528,6 +526,7 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate) |
528 | 526 | - [How to inline SVG, PNG images in HTML](#recipe-inline-image) |
529 | 527 | - [How to resolve source assets in an attribute containing JSON value](#recipe-resolve-attr-json) |
530 | 528 | - [How to load CSS file dynamically](#recipe-dynamic-load-css) (lazy loading CSS) |
| 529 | + - [How to import CSS class names in JS](#recipe-css-modules) (CSS modules) |
531 | 530 | - [How to load JS and CSS from `node_modules` in template](#recipe-load-js-css-from-node-modules) |
532 | 531 | - [How to import CSS or SCSS from `node_modules` in SCSS](#recipe-import-style-from-node-modules) |
533 | 532 | - [How to process a PHP template](#recipe-preprocessor-php) |
@@ -5252,6 +5251,59 @@ loadCSS(cssUrl); |
5252 | 5251 |
|
5253 | 5252 | See the [browser compatibility](https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets#browser_compatibility). |
5254 | 5253 |
|
| 5254 | +--- |
| 5255 | +
|
| 5256 | +#### [↑ back to contents](#contents) |
| 5257 | +
|
| 5258 | +<a id="recipe-css-modules" name="recipe-css-modules"></a> |
| 5259 | +
|
| 5260 | +## How to import CSS class names in JS |
| 5261 | +
|
| 5262 | +To import style `class names` in JS, add in the webpack config the [modules](https://github.com/webpack-contrib/css-loader#modules) option into `css-loader`: |
| 5263 | +```js |
| 5264 | +{ |
| 5265 | + test: /\.(css)$/, |
| 5266 | + use: [ |
| 5267 | + { |
| 5268 | + loader: 'css-loader', |
| 5269 | + options: { |
| 5270 | + modules: { |
| 5271 | + localIdentName: '[name]__[local]--[hash:base64:5]', |
| 5272 | + exportLocalsConvention: 'camelCase', |
| 5273 | + }, |
| 5274 | + }, |
| 5275 | + }, |
| 5276 | + ], |
| 5277 | +}, |
| 5278 | +``` |
| 5279 | +
|
| 5280 | +For example there is _./style.css_ file: |
| 5281 | +```css |
| 5282 | +.error-message { |
| 5283 | + color: red; |
| 5284 | +} |
| 5285 | +``` |
| 5286 | +
|
| 5287 | +In _./main.js_ file you can use class names with `camelCase`: |
| 5288 | +```js |
| 5289 | +import styles from './style.css'; |
| 5290 | + |
| 5291 | +element.innerHTML = `<div class="${styles.errorMessage}">`; |
| 5292 | +``` |
| 5293 | +
|
| 5294 | +The imported `styles` object contains generated class names like followings: |
| 5295 | +```js |
| 5296 | +{ |
| 5297 | + errorMessage: 'main__error-message--gvFM4', |
| 5298 | +} |
| 5299 | +``` |
| 5300 | +
|
| 5301 | +
|
| 5302 | +Read more information about [CSS Modules](https://github.com/css-modules/css-modules). |
| 5303 | +
|
| 5304 | +
|
| 5305 | +--- |
| 5306 | +
|
5255 | 5307 | #### [↑ back to contents](#contents) |
5256 | 5308 |
|
5257 | 5309 |
|
|
0 commit comments