Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit aade0f1

Browse files
committed
feat: add tailwind class names obfuscation by webpack
1 parent 2410004 commit aade0f1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

next.config.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
1+
const incstr = require('incstr');
2+
3+
const classNames = {};
4+
5+
const generateClassName = incstr.idGenerator({
6+
alphabet: 'abcdefghijklmnopqrstuvwxyz'
7+
});
8+
19
/** @type {import('next').NextConfig} */
210
const nextConfig = {
311
reactStrictMode: true,
412
swcMinify: true,
513
images: {
614
unoptimized: true,
715
},
16+
webpack: (config, {dev, isServer}) => {
17+
18+
if (!dev) {
19+
const MangleCssClassPlugin = require('mangle-css-class-webpack-plugin');
20+
config.plugins.push(new MangleCssClassPlugin({
21+
classNameRegExp: '((hover|focus|active|disabled|visited|first|last|odd|even|group-hover|focus-within|xs|sm|md|lg|xl)(\\\\\\\\\\\\\\\\|\\\\)?:)*(-?tw-)[a-zA-Z0-9_-]*(\/[0-9])?',
22+
ignorePrefixRegExp: '((hover|focus|active|disabled|visited|first|last|odd|even|group-hover|focus-within|xs|sm|md||lg|xl)(\\\\\\\\\\\\\\\\|\\\\)?:)*',
23+
// reserveClassName: ['fa', 'fas', 'far',],
24+
log: false,
25+
classGenerator: (original, opts, context) => {
26+
if (classNames[original]) {
27+
return classNames[original];
28+
}
29+
30+
let nextId;
31+
32+
do {
33+
// Class name cannot start with a number.
34+
nextId = generateClassName();
35+
} while (/^[0-9_-]/.test(nextId));
36+
37+
return (classNames[original] = nextId);
38+
}
39+
}));
40+
}
41+
42+
return config;
43+
},
844
}
945

1046
module.exports = nextConfig

tailwind.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const defaultTheme = require('tailwindcss/defaultTheme')
22

33
/** @type {import('tailwindcss').Config} */
44
module.exports = {
5+
prefix: 'tw-',
6+
darkMode: 'class',
57
content: [
68
"./pages/**/*.{js,ts,jsx,tsx}",
79
"./components/**/*.{js,ts,jsx,tsx}",

0 commit comments

Comments
 (0)