11import {
22 readFileSync ,
3- writeFileSync ,
43} from 'fs' ;
54import {
65 dirname ,
76} from 'path' ;
8- import CleanCSS from 'clean-css' ;
97import {
10- createHash ,
11- } from 'crypto' ;
12-
13- const minifier = new CleanCSS ( ) ;
8+ compileString ,
9+ } from 'sass' ;
10+ import writeCss from './write-css.js' ;
1411
1512export default function ( ) {
1613 return {
@@ -19,27 +16,26 @@ export default function () {
1916 if ( id . endsWith ( 'src/main.tsx' , ) ) {
2017 return null ;
2118 }
22- let matches = code . match ( / i m p o r t [ " ' ] \. \/ [ ^ " ] + ? \. c s s [ ' " ] ; / ug ) ;
23- if ( matches ) {
24- for ( const match of matches ) {
25- const path = ` ${ dirname ( id ) } / ${ match . replace ( / i m p o r t [ ' " ] \. \/ ( . * ) [ ' " ] ; / u , '$1' , ) } ` ;
26- const minified = minifier . minify ( readFileSync ( path , 'utf8' ) ) . styles ;
27- const hash = createHash ( 'sha256' )
28- . update ( minified , )
29- . digest ( 'hex' , ) ;
30- const name = id
31- . replace ( / \/ [ ^ / ] + . [ t j ] s x ? $ / u , '' , )
32- . split ( '/' , )
33- . pop ( ) ;
34- writeFileSync ( ` ${ process . cwd ( ) } /public/assets/ ${ name } - ${ hash } .min.css` , minified , 'utf8' , ) ;
35- code . replace (
36- matches [ 0 ] ,
37- `import load from '@idrinth/rollup-plugin-react-modular-css';\n(() => load(' ${ hash } ', ' ${ name } '))()` ,
38- ) ;
19+ let modified = true ;
20+ const css = code . matchAll ( / i m p o r t [ " ' ] \. \/ [ ^ " ] + ? \. c s s [ ' " ] ; / ug ) ;
21+ if ( css ) {
22+ for ( const match of css ) {
23+ const path = ` ${ dirname ( id ) } / ${ match [ 0 ] . replace ( / i m p o r t [ ' " ] \. \/ ( . * ) [ ' " ] ; / u , '$1' , ) } ` ;
24+ const data = readFileSync ( path , 'utf8' ) ;
25+ code = writeCss ( id , data , code , match , ) ;
26+ modified = true ;
27+ }
28+ }
29+ const scss = code . matchAll ( / i m p o r t [ " ' ] \. \/ [ ^ " ] + ? \. s c s s [ ' " ] ; / ug ) ;
30+ if ( scss ) {
31+ for ( const match of scss ) {
32+ const path = ` ${ dirname ( id ) } / ${ match [ 0 ] . replace ( / i m p o r t [ ' " ] \. \/ ( . * ) [ ' " ] ; / u , '$1' , ) } ` ;
33+ const data = compileString ( readFileSync ( path , 'utf8' ) , ) . css ;
34+ code = writeCss ( id , data , code , match , ) ;
35+ modified = true ;
3936 }
40- return code ;
4137 }
42- return null ;
38+ return modified ? code : null ;
4339 }
4440 } ;
4541}
0 commit comments