Skip to content

Commit 46e6b1f

Browse files
committed
feat: introduces mangleCssVariables option
Closes #51
1 parent 241e984 commit 46e6b1f

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = {
3636
plugins: [
3737
new MangleCssClassPlugin({
3838
classNameRegExp: '[cl]-[a-z][a-zA-Z0-9_]*',
39+
mangleCssVariables: true,
3940
log: true,
4041
}),
4142
],
@@ -90,6 +91,9 @@ classGenerator: (original, opts, context) => {
9091
}
9192
```
9293

94+
#### mangleCssVariables
95+
When truthy, the plugin will also mangle CSS variables (custom properties) whose name is matching the same ``classNameRegExp``. Disabled by default.
96+
9397
### Example
9498
#### Source code
9599
```html

lib/optimizer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ const validate = (opts, classGenerator) => {
1919
const optimize = (compiler, [file, originalSource], compilation, opts, classGenerator) => {
2020
let classnameRegex;
2121
if (file.match(/.+\.css.*$/)) {
22-
classnameRegex = new RegExp(`\\\.(${opts.classNameRegExp})`, 'g');
22+
classnameRegex = opts.mangleCssVariables
23+
? new RegExp(`(?:\\\.|--)(${opts.classNameRegExp})`, 'g')
24+
: new RegExp(`\\\.(${opts.classNameRegExp})`, 'g');
2325
} else if (file.match(/.+\.js.*$/) || file.match(/.+\.html.*$/)) {
24-
classnameRegex = new RegExp(`["'\`.\\\s](${opts.classNameRegExp})`, 'g');
26+
classnameRegex = opts.mangleCssVariables
27+
? new RegExp(`(?:["'\`.\\\s]|--)(${opts.classNameRegExp})`, 'g')
28+
: new RegExp(`["'\`.\\\s](${opts.classNameRegExp})`, 'g');
2529
}
2630
if (!classnameRegex) {
2731
return;

spec/BasicSpec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,27 @@ describe('MangleCssClassPlugin', () => {
221221
})]
222222
}, ["<p class=\\\"c1\\\">hoge-a<div class=\\\"b c1\\\">CASE 4</div></p>"], done);
223223
});
224+
225+
it('replace a css variable', (done) => {
226+
testPlugin({
227+
entry: path.join(__dirname, 'fixtures/case5.js'),
228+
output: {
229+
path: OUTPUT_DIR,
230+
filename: 'case5.js'
231+
},
232+
module: {
233+
rules: [
234+
{
235+
test: /\.css$/,
236+
use: ['style-loader', 'css-loader']
237+
},
238+
]
239+
},
240+
plugins: [new MangleCssClassPlugin({
241+
classNameRegExp: defaultCssClassRegExp,
242+
mangleCssVariables: true,
243+
log: true,
244+
})]
245+
}, [":root {\\\\n --a: 100%;\\\\n}\\\\n\\\\n.b {\\\\n width: var(--a);"], done);
246+
});
224247
});

spec/fixtures/case5.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:root {
2+
--l-a: 100%;
3+
}
4+
5+
.l-b {
6+
width: var(--l-a);
7+
}

spec/fixtures/case5.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './case5.css';

0 commit comments

Comments
 (0)