├── .gitignore
├── .npmignore
├── LICENSE
├── README.md
├── assets
├── css-obfuscation.png
├── html-obfuscation.png
├── js-obfuscation.png
├── json-obfuscation.png
├── postcss-obfuscator.png
└── react-obfuscation.png
├── index.js
├── package-lock.json
├── package.json
├── style.css
├── test
├── demos
│ └── simple
│ │ ├── postcss-obfuscate.js
│ │ ├── postcss.config.js
│ │ ├── src
│ │ ├── css
│ │ │ ├── app.css
│ │ │ └── input.css
│ │ └── html
│ │ │ ├── 404.html
│ │ │ ├── app.html
│ │ │ └── index.htm
│ │ └── tailwind.config.js
├── test.js
└── unit
│ └── getClassNames.js
└── utils.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | test/**/build
13 | test/**/out
14 | test/**/css-obfuscator
15 | test/**/src/css/output.css
16 |
17 | # misc
18 | .DS_Store
19 | .env.local
20 | .env.development.local
21 | .env.test.local
22 | .env.production.local
23 |
24 | npm-debug.log*
25 | yarn-debug.log*
26 | yarn-error.log*
27 |
28 | # personalFiles & localFiles
29 | /MYFILES
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | /test
2 | /assets
3 | /MYFILES
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Najib Rachid
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | Intro➤Motivation➤Features➤Installation➤Usage➤Configuration➤FAQ➤Releases➤Contributing➤License➤Check Also
3 |
4 |
5 | # :space_invader: PostCSS Obfuscator
6 |
7 | - :date:**13-05-2023** :pushpin:**Beta Version 1.6.0**
8 | - :computer:NajibRachid :purple_circle:ANMOON :office: XHUB
9 |
10 | PostCSS plugin that helps you protect your CSS code by obfuscating class names and ids. with advanced customizable configuration.
11 |
12 | This plugin provides obfuscation capabilities to your CSS files by replacing class and id selectors with prefixed, simplified or randomly generated strings. This can help to protect your CSS code from reverse engineering and unauthorized copying, while also reducing the file size of your CSS files. plugin offers advanced customizable configuration.
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | ---
24 |
25 | ## Motivation
26 |
27 | - Protecting intellectual property, licensing & distribution: make it hard for others from stealing your design or using it without your permission.
28 | - Protect against Web scraping, data mining or any malicious activities like stealing data or content: make it hard extracting data from websites automatically using software tools, which use class names & ids.
29 | - Minfiy Your code even more: obsfucation/uglify can slightly reduce file size and improve its performance.
30 |
31 | ## Features
32 |
33 | - [x] No 3rd parties, dependencies. just vanilla Nodejs code.
34 | - [x] Quicker then you think.
35 | - [x] Postcss plugin, hence its intended to work with any build tool or task runner.
36 | - [x] Advanced Customizable configuration (Control is yours).
37 | - [x] Supports all files: .html, .js, .jsx, .vue, .php, ... you name it.
38 | - [x] Supports a wide range of CSS frameworks (Tailwidcss, Bootstrap, Bulma, ...).
39 |
40 | ## Installation
41 |
42 | ```sh
43 | # npm
44 | npm install postcss-obfuscator --save-dev
45 | ```
46 |
47 | ```sh
48 | # yarn
49 | yarn add postcss-obfuscator --dev
50 | ```
51 |
52 | ## Usage
53 |
54 | First, you need to add postcss-obfuscator to your PostCSS configuration. For example, if you're using postcss-cli, you can add it to your `postcss.config.js` file:
55 |
56 | ```js
57 | //postcss.config.js / postcss.config.cjs
58 | module.exports = {
59 | // other plugins
60 | plugins: [
61 | require("postcss-obfuscator")({
62 | /* options */
63 | }),
64 | ],
65 | };
66 | ```
67 |
68 | ## Configuartion
69 |
70 | The plugin has several options that you can configure to customize its behavior.
71 | **Here's the default donfiguration:**
72 |
73 | ```js
74 | const defaultOptions = {
75 | enable: true, // Enable plugin
76 | length: 5, // Random name length.
77 | classMethod: "random", // 'random', 'simple', 'none' obfuscation method for classes.
78 | classPrefix: "", // ClassName prefix.
79 | classSuffix: "", // ClassName suffix.
80 | classIgnore: [], // Class to ignore from obfuscation.
81 | ids: false, // Obfuscate #IdNames.
82 | idMethod: "random", // 'random', 'simple', 'none' obfuscation method for ids .
83 | idPrefix: "", // idName Prefix.
84 | idSuffix: "", // idName suffix.
85 | idIgnore: [], // Ids to ignore from obfuscation.
86 | indicatorStart: null, // Identify ids & classes by the preceding string.
87 | indicatorEnd: null, // Identify ids & classes by the following string.
88 | jsonsPath: "css-obfuscator", // Path and file name where to save obfuscation data.
89 | srcPath: "src", // Source of your files.
90 | desPath: "out", // Destination for obfuscated html/js/.. files.Be careful using the same directory as your src(you will lose your original files).
91 | extensions: [".html"], // Extesnion of files you want osbfucated ['.html', '.php', '.js', '.svelte'].
92 | htmlExcludes: [], // Files and paths to exclude from html obfuscation replacement.
93 | cssExcludes: [], // Files and paths to exclude from css obfuscation.
94 | fresh: false, // Create new obfuscation data list or use already existed one (to keep production cache or prevent data scrapping).
95 | multi: false, // Generate obsfucated data file for each css file.
96 | differMulti: false, // Generate different Random names for each file.
97 | formatJson: false, // Format obfuscation data JSON file.
98 | showConfig: false, // Show config on terminal when runinng.
99 | keepData: true, // Keep or delete Data after obfuscation is finished?
100 | preRun: () => Promise.resolve(), // do something before the plugin runs.
101 | callBack: function () {}, // Callback function to call after obfuscation is done.
102 | };
103 | ```
104 |
105 | - **`enable:`** Enable plugin, **default is true.**
106 | - **`length:`** Random name length for both ids and classes, **default is 5.**
107 | - **`classMethod:`** Obfuscation method for classes, options are: (`random`, `simple`, `none`). `simple` will remove pronounced vowels and digits, `none` will keep original name in case you want to just use prefixes or suffixes. **default is 'random'.**
108 | - **`classPrefix:`** Prefix for class names, **default is nothig.**
109 | - **`classSuffix:`** Suffix for class names, **default is nothig.**
110 | - **`classIgnore:`** Array of classes to ignore from obfuscation. **default is none.**
111 | - **`ids:`** Enable id Obfuscation, **default is false.**
112 | - **`idMethod:`** Obfuscation method for ids, options are also: (`random`, `simple`, `none`), __default is 'random'._
113 | - **`idPrefix:`** Prefix for id names, **default is nothig.**
114 | - **`idSuffix:`** Suffix for id names, **default is nothig.**
115 | - **`idIgnore:`** Array of ids to ignore from obfuscation. **default is none.**
116 | - **`indicator:`** Indicator used to replace names. **default is none.**
117 | - **`indicatorStart:`** Identify ids & classes by the preceding string. **default is none.**
118 | - **`indicatorEnd:`** Identify ids & classes by the following string. **default is none.**
119 | - **`jsonsPath:`** Path and file name where to save obfuscation data **default is: css-obfuscator.**
120 | - **`srcPath:`** Path for your source files, **default is: src.**
121 | - **`desPath:`** Destination path for obfuscated html/js/.. files. Be careful using the same directory as your src(you will lose your original files). **default is: out**.
122 | - **`extensions:`** Extesnions Array of files you want osbfucated ['.html', '.php', '.js', '.svelte'], **default is '.html'.**
123 | - **`htmlExcludes:`** Files and paths to exclude from html obfuscation replacement, **default is none.**
124 | - **`cssExcludes:`** Files and paths to exclude from css obfuscation, **default is none.**
125 | - **`fresh:`** Create new obfuscation data list or use already existed one (to keep production cache or prevent data scrapping). **default is false.**
126 | - **`multi:`** Generate obsfucated data file for each css file, **default is false.**
127 | - **`differMulti:`** Generate different Random names for each file, **default is false.**
128 | - **`formatJson:`** Format obfuscation data JSON file, **default is false.**
129 | - **`showConfig:`** Show config on terminal when runinng, **default is false.**
130 | - **`keepData:`** Keep or delete data after obfuscation is finished? **default is true.**
131 | - **`preRun:`** Do something before the plugin runs. **default is a promise that immediately resolves.**
132 | - **`callBack:`** Callback function to call after obfuscation is done. **default is an empty function.**
133 |
134 | ## npm scripts example
135 |
136 | Then npm scripts can be something like this:
137 |
138 | ```json
139 | "postcss": "postcss src/**/*.css --dir build",
140 | "postcss:watch": "postcss src/**/*.css --dir build --watch"
141 | "obfuscate": "node postcss-obfuscate", // for custome script.
142 | ```
143 |
144 | ## FAQ
145 |
146 | ### How it works basically?
147 |
148 | 1. Loop over all CSS files.
149 | 2. Uses built-in function(regex) to find classes and ids.
150 | 3. Saves Ids & classes in a JSON file key representing original Names. then generate random names as values.
151 | 4. Creates a new folder from the source folder.
152 | 5. Loops throw files and replace those keys with values from JSON files.
153 |
154 | ### Caveats?
155 |
156 | - Only CSS is supported so call the extension After your code was converted to CSS (Example: SCSS to CSS). It's generally better to call it the last.
157 | - One of the best practices is to avoid naming your ids & classes reserved words like HTML element names or attributes. (same for JS & CSS).
158 | - It uses a built-in function to find ids & CSS classes. so it may not work perfectly with advanced CSS selectors.
159 | - I advise `keepData` option as default, and using a different build directory: Using the same directory will replace your files and you may lose original classes and ids names. you will get a warning for that.
160 | - Postcss doesn't support nested directories by default. this is If you intend to work with the plugin's multi-option.
161 |
162 | ### Destination folder?
163 |
164 | It's better to keep your source files as they are for easy development. Consider specifying another folder for the build, if you choose your build directory to be the same as the source directory you will be replaced and you will lose your original files.
165 |
166 | ### Support for CSS framworks?
167 |
168 | It's designed to work with CSS, hence its supports any framework you can think of:
169 | - Tailwindcss.
170 | - Bootstrap.
171 | - Bulma.
172 | - ... .
173 | ### Use indicators?
174 |
175 | As mentioned this plugin uses Regex to replace all appearances of classes & ids on files with extensions you specify (be it html, cs, js, ...).
176 | Generally, if your class names are unique and avoid reserved keywords, then you got nothing to worry about, otherwise, we got you covered just use the `indicatorStart` & `indicatorEnd` options.
177 |
178 | For example `class`, `h1`, and `import` are examples of reserved keywords in (HTML, CSS, JS, PHP).
179 | so if you insist on breaking naming conventions and using them as id or class names:
180 | Then you can use either or both indicator options like this:
181 |
182 | ```js
183 | indicatorStart: "@",
184 | indicatorEnd: "#",
185 | ```
186 |
187 | ```html
188 |
191 |