Skip to content

Commit 32f46ee

Browse files
committed
feat(Pug): add experimental syntax to include compiled CSS into style tag
1 parent 6717ea1 commit 32f46ee

23 files changed

Lines changed: 219 additions & 125 deletions

File tree

CHANGELOG.md

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

3+
## 3.8.0 (2024-04-06)
4+
5+
- feat(Pug): add experimental (undocumented) syntax to include (using `?include` query) compiled CSS directly into style tag to allow keep tag attributes
6+
```pug
7+
style(scope='some')=require('./component.scss?include')
8+
```
9+
will be generate
10+
```html
11+
<style scope="some">
12+
... CSS ...
13+
</style>
14+
```
15+
- test: add test for lazy loading CSS using `fetch()` and `document.adoptedStyleSheets`
16+
- docs: update readme
17+
318
## 3.7.0 (2024-03-21)
419
520
- feat: add the possibility to add many post processes. Next postprocess receives the result from previous.

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
536536
- [How to config `splitChunks`](#recipe-split-chunks)
537537
- [How to keep package name for **split chunks** from **node_modules**](#recipe-split-chunks-keep-module-name)
538538
- [How to split CSS files](#recipe-split-css)
539+
- [How to split CSS files](#recipe-split-css)
539540
2. [Problems & Solutions](#solutions)
540541
- [Automatic resolving of file extensions](#solutions-resolve-extensions)
541542
- [How to use `@import url()` in CSS](#solutions-import-url-in-css)
@@ -6071,11 +6072,26 @@ If you commented out a tag and don't want to resolve files in the tag's [attribu
60716072
For example: `href` -> `x-href` or `src` -> `x-src`.
60726073
60736074
```html
6074-
<!-- <link x-href="./styles.css" rel="stylesheet /> -->
6075+
<!-- <link x-href="./styles.scss" rel="stylesheet /> -->
60756076
<!-- <script x-src="./main.js" defer="defer"></script> -->
60766077
<!-- <img x-src="./image.png"> -->
60776078
```
60786079
6080+
If used any [template engine](#template-engine) (defaults is [Eta](#loader-option-preprocessor-options-eta))
6081+
then can be used [templating comments](https://eta.js.org/docs/intro/template-syntax) `<%/* ... */%>`.
6082+
6083+
```html
6084+
<%/* <link rel="stylesheet href="./style.scss" /> Single line comment w/o resolving */%>
6085+
6086+
<%/*
6087+
Multiline comment w/o resolving of files in attributes
6088+
<img src="./image1.png" />
6089+
<img src="./image2.png" />
6090+
*/%>
6091+
```
6092+
6093+
The generated HTML will not contain templating comments.
6094+
60796095
60806096
#### [↑ back to contents](#contents)
60816097

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "3.7.0",
3+
"version": "3.8.0",
44
"description": "HTML bundler plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in HTML, supports template engines like Eta, EJS, Handlebars, Nunjucks.",
55
"keywords": [
66
"html",

src/Plugin/Collection.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,17 @@ class Collection {
901901
importedStyles.push(asset);
902902
} else if (inline) {
903903
content = this.#inlineStyle(content, resource, asset, LF) || content;
904+
} else {
905+
// special use case for Pug only e.g.: style(scope='some')=require('./component.css?include')
906+
const [, query] = resource.split('?');
907+
const isIncluded = query?.includes('include');
908+
if (isIncluded) {
909+
const startPos = content.indexOf(asset.assetFile);
910+
if (startPos > 0) {
911+
const source = CssExtractModule.getInlineSource(asset.assetFile);
912+
content = content.slice(0, startPos) + source + content.slice(startPos + asset.assetFile.length);
913+
}
914+
}
904915
}
905916

906917
// 1.1 compute CSS integrity
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!DOCTYPE html><html><head><style>h1{color:#228b22}</style>
2+
</head><body><h1>Home</h1><div class="my-component"><style scope="some">.my-component{color:violet}</style><div>my component</div></div></body></html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.my-component {
2+
color: violet;
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
html
2+
head
3+
//- inline CSS
4+
link(href=require('./style.css?inline') rel='stylesheet')
5+
body
6+
h1 Home
7+
8+
.my-component
9+
//- include compiled CSS directly into style tag to keep custom attributes
10+
style(scope='some')=require('./component.css?include')
11+
div my component
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: forestgreen;
3+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const path = require('path');
2+
const PugPlugin = require('@test/html-bundler-webpack-plugin');
3+
4+
module.exports = {
5+
mode: 'production',
6+
7+
output: {
8+
path: path.join(__dirname, 'dist/'),
9+
},
10+
11+
plugins: [
12+
new PugPlugin({
13+
entry: {
14+
index: './src/index.pug',
15+
},
16+
css: {
17+
filename: '[name].[contenthash:8].css',
18+
},
19+
preprocessor: 'pug',
20+
preprocessorOptions: {},
21+
}),
22+
],
23+
24+
module: {
25+
rules: [
26+
{
27+
test: /\.(css|sass|scss)$/,
28+
use: ['css-loader', 'sass-loader'],
29+
},
30+
],
31+
},
32+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Test</title>
5+
</head>
6+
<body>
7+
<h1>Hello World!</h1>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)