Skip to content

Commit 5d3834a

Browse files
committed
version 1.1.0
1 parent 5b3fa91 commit 5d3834a

File tree

4 files changed

+241
-30
lines changed

4 files changed

+241
-30
lines changed

package-lock.json

Lines changed: 192 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@idrinth/rollup-plugin-react-modular-css",
3-
"version": "1.0.5",
3+
"version": "1.1.0",
44
"description": "Provides a plugin to split css files out separately.",
55
"main": "src/index.js",
66
"repository": {
@@ -14,7 +14,8 @@
1414
},
1515
"dependencies": {
1616
"clean-css": "^5.3.3",
17-
"rollup": "^4.13.1"
17+
"rollup": "^4.13.1",
18+
"sass": "^1.74.1"
1819
},
1920
"devDependencies": {
2021
"@types/node": "^20.11.30",

src/plugin.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import {
22
readFileSync,
3-
writeFileSync,
43
} from 'fs';
54
import {
65
dirname,
76
} from 'path';
8-
import CleanCSS from 'clean-css';
97
import {
10-
createHash,
11-
} from 'crypto';
12-
13-
const minifier = new CleanCSS();
8+
compileString,
9+
} from 'sass';
10+
import writeCss from './write-css.js';
1411

1512
export 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(/import ["']\.\/[^"]+?\.css['"];/ug);
23-
if (matches) {
24-
for (const match of matches) {
25-
const path = `${dirname(id)}/${match.replace(/import ['"]\.\/(.*)['"];/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(/\/[^/]+.[tj]sx?$/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(/import ["']\.\/[^"]+?\.css['"];/ug);
21+
if (css) {
22+
for (const match of css) {
23+
const path = `${dirname(id)}/${match[0].replace(/import ['"]\.\/(.*)['"];/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(/import ["']\.\/[^"]+?\.scss['"];/ug);
30+
if (scss) {
31+
for (const match of scss) {
32+
const path = `${dirname(id)}/${match[0].replace(/import ['"]\.\/(.*)['"];/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
}

src/write-css.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {
2+
writeFileSync,
3+
} from "fs";
4+
import {
5+
createHash,
6+
} from "crypto";
7+
import CleanCSS from "clean-css";
8+
9+
const minifier = new CleanCSS();
10+
11+
export default (id: string, css: string, code: string, match: string[]): string => {
12+
const minified = minifier.minify(css).styles;
13+
const hash = createHash('sha256')
14+
.update(css,)
15+
.digest('hex',);
16+
const name = id
17+
.replace(/\/[^/]+.[tj]sx?$/u, '',)
18+
.split('/',)
19+
.pop();
20+
writeFileSync(`${process.cwd()}/public/assets/${name}-${hash}.min.css`, minified, 'utf8',);
21+
return code.replace(
22+
match[0],
23+
`import load from '@idrinth/rollup-plugin-react-modular-css';\n(() => load('${hash}', '${name}'))()`,
24+
);
25+
}

0 commit comments

Comments
 (0)