Skip to content

Commit 86725f9

Browse files
authored
group css output by file (#3)
fixes idrinth-api-bench/issues#1050
1 parent 3624efa commit 86725f9

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@idrinth/rollup-plugin-react-modular-css",
3-
"version": "1.1.1",
2+
"name": "@idrinth-api-bench/rollup-plugin-react-modular-css",
3+
"version": "1.2.0",
44
"description": "Provides a plugin to split css files out separately.",
55
"main": "src/index.js",
66
"repository": {

src/plugin.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,32 @@ export default function () {
1313
return {
1414
name: '@idrinth/rollup-plugin-react-modular-css',
1515
transform ( code: string, id: string ) {
16-
if (id.endsWith('src/main.tsx',)) {
17-
return null;
18-
}
19-
let modified = true;
16+
const csss = [];
2017
const css = code.matchAll(/import ["']\.\/[^"]+?\.css['"];/ug);
2118
if (css) {
2219
for (const match of css) {
2320
const path = `${dirname(id)}/${match[0].replace(/import ['"]\.\/(.*)['"];/u, '$1',)}`;
2421
const data = readFileSync(path, 'utf8');
25-
code = writeCss(id, data, code, match,);
26-
modified = true;
22+
csss.push(data);
2723
}
2824
}
2925
const scss = code.matchAll(/import ["']\.\/[^"]+?\.scss['"];/ug);
3026
if (scss) {
3127
for (const match of scss) {
3228
const path = `${dirname(id)}/${match[0].replace(/import ['"]\.\/(.*)['"];/u, '$1',)}`;
3329
const data = compileString(readFileSync(path, 'utf8'),).css;
34-
code = writeCss(id, data, code, match,);
35-
modified = true;
30+
csss.push(data);
3631
}
3732
}
38-
return modified ? code : null;
33+
if (csss.length > 0) {
34+
return writeCss(
35+
id,
36+
csss.join('\n'),
37+
code
38+
.replace(/import ["']\.\/[^"]+?\.s?css['"];/ug, ''),
39+
);
40+
}
41+
return null;
3942
}
4043
};
4144
}

src/write-css.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ import CleanCSS from 'clean-css';
88

99
const minifier = new CleanCSS();
1010

11-
export default (id: string, css: string, code: string, match: string[]): string => {
11+
export default (id: string, css: string, code: string): string => {
1212
const minified = minifier.minify(css).styles;
1313
const hash = createHash('sha256')
1414
.update(css,)
1515
.digest('hex',);
1616
const name = id
17-
.replace(/\/index.[tj]sx?$/u, '',)
17+
.replace(/\.[tj]sx?$/u, '',)
18+
.replace(/\/index$/u, '',)
1819
.split('/',)
1920
.pop();
2021
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/src/load.js';\n(() => load('${hash}', '${name}'))()`,
24-
);
22+
return `import load from '@idrinth/rollup-plugin-react-modular-css/src/load.js';\n(() => load('${hash}', '${name}'))()\n${code}`;
2523
}

0 commit comments

Comments
 (0)