Skip to content

Commit d403863

Browse files
committed
feat: add supports the CSS Modules for styles imported in JS
1 parent a57a02e commit d403863

31 files changed

Lines changed: 2147 additions & 1618 deletions

CHANGELOG.md

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

3+
## 3.10.0 (2024-04-18)
4+
5+
- feat: add support the [CSS Modules](https://github.com/css-modules/css-modules) for styles imported in JS using the [css-loader modules](https://github.com/webpack-contrib/css-loader#modules) option.\
6+
The CSS _module rule_ in the webpack config:
7+
```js
8+
{
9+
test: /\.(css)$/,
10+
use: [
11+
{
12+
loader: 'css-loader',
13+
options: {
14+
modules: {
15+
localIdentName: '[name]__[local]__[hash:base64:5]',
16+
exportLocalsConvention: 'camelCase',
17+
},
18+
},
19+
},
20+
],
21+
},
22+
```
23+
CSS:
24+
```css
25+
.red {
26+
color: red;
27+
}
28+
.green {
29+
color: green;
30+
}
31+
```
32+
Using in JS:
33+
```js
34+
// the styles contains CSS class names: { red: 'main__red__us4Tv', green: 'main__green__bcpRp' }
35+
import styles from './main.css';
36+
```
37+
338
## 3.9.1 (2024-04-10)
439

540
- fix: issue when used js dynamic import with magic comments /* webpackPrefetch: true */ and css.inline=true, #88

README.md

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
<br>
55
<a href="https://github.com/webdiscus/html-bundler-webpack-plugin">HTML Bundler Plugin for Webpack</a>
66
</h1>
7-
<div>The plugin renders various templates with referenced source asset files into HTML</div>
87
</div>
98

10-
---
11-
129
[![npm](https://img.shields.io/npm/v/html-bundler-webpack-plugin?logo=npm&color=brightgreen 'npm package')](https://www.npmjs.com/package/html-bundler-webpack-plugin 'download npm package')
1310
[![node](https://img.shields.io/node/v/html-bundler-webpack-plugin)](https://nodejs.org)
1411
[![node](https://img.shields.io/github/package-json/dependency-version/webdiscus/html-bundler-webpack-plugin/peer/webpack)](https://webpack.js.org/)
@@ -18,9 +15,8 @@
1815

1916
## HTML template as entry point
2017

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).
2420

2521
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.
2622

@@ -215,9 +211,11 @@ If you have discovered a bug or have a feature suggestion, feel free to create a
215211

216212
## 🔆 What's New in v3
217213

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.
219215
- **NEW** added [Pug](#using-template-pug) preprocessor.
220216
- **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.
221219
- **NEW** added CSS extraction from **styles** used in `*.vue` files.
222220
- **NEW** added [Hooks & Callbacks](#plugin-hooks-and-callbacks). Now you can create own plugin to extend this plugin.
223221
- **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)
528526
- [How to inline SVG, PNG images in HTML](#recipe-inline-image)
529527
- [How to resolve source assets in an attribute containing JSON value](#recipe-resolve-attr-json)
530528
- [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)
531530
- [How to load JS and CSS from `node_modules` in template](#recipe-load-js-css-from-node-modules)
532531
- [How to import CSS or SCSS from `node_modules` in SCSS](#recipe-import-style-from-node-modules)
533532
- [How to process a PHP template](#recipe-preprocessor-php)
@@ -5252,6 +5251,59 @@ loadCSS(cssUrl);
52525251
52535252
See the [browser compatibility](https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets#browser_compatibility).
52545253
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+
52555307
#### [↑ back to contents](#contents)
52565308
52575309

0 commit comments

Comments
 (0)