Skip to content

Commit 65657f4

Browse files
committed
version 1.0.2
1 parent 24100d6 commit 65657f4

File tree

4 files changed

+53
-49
lines changed

4 files changed

+53
-49
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "@idrinth/rollup-plugin-react-modular-css",
3-
"version": "1.0.0",
4-
"description": "",
3+
"version": "1.0.2",
4+
"description": "Provides a plugin to split css files out separately.",
55
"main": "src/index.js",
66
"repository": {
77
"url": "https://github.com/idrinth/rollup-plugin-react-modular-css"
88
},
9+
"type": "module",
910
"license": "MIT",
1011
"scripts": {
11-
"tsc": "tsc",
12+
"tsc": "tsc -p tsconfig.json",
1213
"prepublishOnly": "npm run tsc"
1314
},
1415
"dependencies": {

src/index.ts

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,5 @@
1-
import {
2-
readFileSync,
3-
writeFileSync,
4-
} from 'fs';
5-
import {
6-
dirname,
7-
} from 'path';
8-
import CleanCSS from 'clean-css';
9-
import {
10-
createHash,
11-
} from 'crypto';
1+
import pluginImpl from './plugin.js';
2+
import load from './load.js';
123

13-
const minifier = new CleanCSS();
14-
15-
export default function () {
16-
return {
17-
name: '@idrinth/rollup-plugin-react-modular-css',
18-
transform ( code: string, id: string ) {
19-
if (id.endsWith('src/main.tsx',)) {
20-
return null;
21-
}
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/src/load.js';\n(() => load('${hash}', '${name}'))()`,
38-
);
39-
}
40-
return code;
41-
}
42-
return null;
43-
}
44-
};
45-
}
4+
export const plugin = pluginImpl;
5+
export default load;

src/plugin.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
readFileSync,
3+
writeFileSync,
4+
} from 'fs';
5+
import {
6+
dirname,
7+
} from 'path';
8+
import CleanCSS from 'clean-css';
9+
import {
10+
createHash,
11+
} from 'crypto';
12+
13+
const minifier = new CleanCSS();
14+
15+
export default function () {
16+
return {
17+
name: '@idrinth/rollup-plugin-react-modular-css',
18+
transform ( code: string, id: string ) {
19+
if (id.endsWith('src/main.tsx',)) {
20+
return null;
21+
}
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/src/load.js';\n(() => load('${hash}', '${name}'))()`,
38+
);
39+
}
40+
return code;
41+
}
42+
return null;
43+
}
44+
};
45+
}

tsconfig.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
"module": "NodeNext",
88
"skipLibCheck": true,
99
"moduleResolution": "NodeNext",
10-
"allowImportingTsExtensions": true,
1110
"resolveJsonModule": true,
1211
"isolatedModules": true,
13-
"noEmit": true,
1412
"jsx": "react",
1513
"strict": true,
1614
"noUnusedLocals": true,

0 commit comments

Comments
 (0)