Skip to content

Commit 9fbc74b

Browse files
committed
initial
0 parents  commit 9fbc74b

File tree

6 files changed

+385
-0
lines changed

6 files changed

+385
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/.idea

package-lock.json

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

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@idrinth/rollup-plugin-react-modular-css",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"tsc": "tsc",
8+
"prepublishOnly": "npm run tsc"
9+
},
10+
"dependencies": {
11+
"clean-css": "^5.3.3",
12+
"rollup": "^4.13.1"
13+
},
14+
"devDependencies": {
15+
"@types/node": "^20.11.30",
16+
"@types/clean-css": "^4.2.11"
17+
},
18+
"private": false
19+
}

src/index.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';\n(() => load('${hash}', '${name}'))()`,
38+
);
39+
}
40+
return code;
41+
}
42+
return null;
43+
}
44+
};
45+
}

src/load.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
const header = document.getElementsByTagName('header')[0];
3+
export default (hash: string, name: string) => {
4+
const path = `/assets/${name}-${hash}.min.css`;
5+
if (document.querySelector(`link[href='${ path }']`)) {
6+
return;
7+
}
8+
const style= document.createElement('link');
9+
style.setAttribute('rel','stylesheet');
10+
style.setAttribute('href', path);
11+
header.appendChild(style);
12+
}

tsconfig.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"noImplicitAny": true,
4+
"target": "ES2020",
5+
"useDefineForClassFields": true,
6+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"module": "NodeNext",
8+
"skipLibCheck": true,
9+
"moduleResolution": "NodeNext",
10+
"allowImportingTsExtensions": true,
11+
"resolveJsonModule": true,
12+
"isolatedModules": true,
13+
"noEmit": true,
14+
"jsx": "react",
15+
"strict": true,
16+
"noUnusedLocals": true,
17+
"noUnusedParameters": true,
18+
"noFallthroughCasesInSwitch": true,
19+
"incremental": true
20+
},
21+
"include": [
22+
"src/**/*.ts",
23+
"node_modules/@types/*/*.d.ts"
24+
]
25+
}

0 commit comments

Comments
 (0)