1+ /*
2+
3+ MIT License
4+
5+ Copyright (c) 2025 JustDeveloper <https://justdeveloper.is-a.dev/>
6+
7+ Permission is hereby granted, free of charge, to any person obtaining a copy
8+ of this software and associated documentation files (the "Software"), to deal
9+ in the Software without restriction, including without limitation the rights
10+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+ copies of the Software, and to permit persons to whom the Software is
12+ furnished to do so, subject to the following conditions:
13+
14+ The above copyright notice and this permission notice shall be included in all
15+ copies or substantial portions of the Software.
16+
17+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+ SOFTWARE.
24+
25+ */
26+
27+ const fs = require ( 'fs' ) ;
28+ const path = require ( 'path' ) ;
29+
30+ const deployDir = process . argv [ 2 ] || __dirname ;
31+
32+ function compressFile ( filePath ) {
33+ let content = fs . readFileSync ( filePath , 'utf8' ) ;
34+
35+ if ( filePath . endsWith ( '.js' ) ) {
36+ content = content . replace ( / \/ \/ .* \n / g, '\n' ) ;
37+ }
38+
39+ content = content . replace ( / ( \s * [ " ' ` ] ) ( [ ^ " ' \n ` ] * ) ( [ " ' ` ] \s * ) / g, ( match , p1 , p2 , p3 ) => {
40+ return p1 + p2 . replace ( / \s + / g, ' ' ) + p3 ;
41+ } ) ;
42+
43+ content = content . replace ( / \n \s * / g, ' ' ) . replace ( / \s + / g, ' ' ) ;
44+
45+ if ( filePath . endsWith ( '.css' ) || filePath . endsWith ( '.js' ) ) {
46+ content = content . replace ( / ; \s * } / g, '}' ) . replace ( / ; \s * $ / , '' ) ;
47+ }
48+
49+ if ( filePath . endsWith ( '.js' ) || filePath . endsWith ( '.json' ) || filePath . endsWith ( '.webmanifest' ) ) {
50+ content = content . replace ( / , \s * } / g, '}' ) . replace ( / , \s * ] } / g, ']' ) ;
51+ }
52+
53+ fs . writeFileSync ( filePath , content , 'utf8' ) ;
54+ }
55+
56+ function findAndCompressFiles ( dir ) {
57+ fs . readdirSync ( dir ) . forEach ( file => {
58+ const filePath = path . join ( dir , file ) ;
59+ const stat = fs . statSync ( filePath ) ;
60+ if ( stat . isDirectory ( ) ) {
61+ findAndCompressFiles ( filePath ) ;
62+ } else if (
63+ file . endsWith ( '.html' ) ||
64+ file . endsWith ( '.svg' ) ||
65+ file . endsWith ( '.css' ) ||
66+ file . endsWith ( '.js' ) ||
67+ file . endsWith ( '.json' ) ||
68+ file . endsWith ( '.webmanifest' )
69+ ) {
70+ compressFile ( filePath ) ;
71+ }
72+ } ) ;
73+ }
74+
75+ findAndCompressFiles ( deployDir ) ;
0 commit comments