@sylphx/silk-vite-plugin 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +141 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 SylphX Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sylphx/silk-vite-plugin
|
|
3
|
+
* Build-time CSS extraction for zero runtime overhead
|
|
4
|
+
*/
|
|
5
|
+
import type { Plugin } from 'vite';
|
|
6
|
+
export interface SilkPluginOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Output CSS file path (relative to outDir)
|
|
9
|
+
* @default 'silk.css'
|
|
10
|
+
*/
|
|
11
|
+
outputFile?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Include CSS in HTML automatically
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
inject?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Minify CSS output
|
|
19
|
+
* @default true in production
|
|
20
|
+
*/
|
|
21
|
+
minify?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Watch mode for development
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
watch?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare function silk(options?: SilkPluginOptions): Plugin;
|
|
29
|
+
export default silk;
|
|
30
|
+
/**
|
|
31
|
+
* Client-side script for hot CSS updates
|
|
32
|
+
* This should be imported in the app entry point
|
|
33
|
+
*/
|
|
34
|
+
export declare const silkClient = "\nif (import.meta.hot) {\n import.meta.hot.on('silk:update', ({ css }) => {\n let style = document.querySelector('style[data-silk]')\n if (!style) {\n style = document.createElement('style')\n style.setAttribute('data-silk', '')\n document.head.appendChild(style)\n }\n style.textContent = css\n })\n}\n";
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAiB,MAAM,MAAM,CAAA;AAKjD,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAEhB;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAEhB;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED,wBAAgB,IAAI,CAAC,OAAO,GAAE,iBAAsB,GAAG,MAAM,CAyI5D;AAED,eAAe,IAAI,CAAA;AAEnB;;;GAGG;AACH,eAAO,MAAM,UAAU,iVAYtB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sylphx/silk-vite-plugin
|
|
3
|
+
* Build-time CSS extraction for zero runtime overhead
|
|
4
|
+
*/
|
|
5
|
+
import { cssRules } from '@sylphx/silk';
|
|
6
|
+
export function silk(options = {}) {
|
|
7
|
+
const { outputFile = 'silk.css', inject = true, minify, watch = true } = options;
|
|
8
|
+
let server;
|
|
9
|
+
let isBuild = false;
|
|
10
|
+
const cssCache = new Set();
|
|
11
|
+
/**
|
|
12
|
+
* Collect CSS rules from runtime
|
|
13
|
+
*/
|
|
14
|
+
function collectCSS() {
|
|
15
|
+
const rules = [];
|
|
16
|
+
for (const [className, rule] of cssRules) {
|
|
17
|
+
if (!cssCache.has(className)) {
|
|
18
|
+
rules.push(rule);
|
|
19
|
+
cssCache.add(className);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return rules.join('\n');
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Minify CSS (basic implementation)
|
|
26
|
+
*/
|
|
27
|
+
function minifyCSS(css) {
|
|
28
|
+
return css
|
|
29
|
+
.replace(/\s+/g, ' ')
|
|
30
|
+
.replace(/\s*([{}:;,])\s*/g, '$1')
|
|
31
|
+
.replace(/;}/g, '}')
|
|
32
|
+
.trim();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Generate CSS output
|
|
36
|
+
*/
|
|
37
|
+
function generateCSS() {
|
|
38
|
+
let css = collectCSS();
|
|
39
|
+
if (minify ?? isBuild) {
|
|
40
|
+
css = minifyCSS(css);
|
|
41
|
+
}
|
|
42
|
+
return css;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
name: 'silk',
|
|
46
|
+
configResolved(config) {
|
|
47
|
+
isBuild = config.command === 'build';
|
|
48
|
+
},
|
|
49
|
+
configureServer(_server) {
|
|
50
|
+
server = _server;
|
|
51
|
+
// Hot reload CSS in dev mode
|
|
52
|
+
if (watch) {
|
|
53
|
+
const watcher = setInterval(() => {
|
|
54
|
+
const newCSS = collectCSS();
|
|
55
|
+
if (newCSS) {
|
|
56
|
+
server?.ws.send({
|
|
57
|
+
type: 'custom',
|
|
58
|
+
event: 'silk:update',
|
|
59
|
+
data: { css: newCSS },
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}, 100);
|
|
63
|
+
server.httpServer?.on('close', () => {
|
|
64
|
+
clearInterval(watcher);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
transformIndexHtml: {
|
|
69
|
+
order: 'post',
|
|
70
|
+
handler(html) {
|
|
71
|
+
if (!inject)
|
|
72
|
+
return html;
|
|
73
|
+
const css = generateCSS();
|
|
74
|
+
if (!css)
|
|
75
|
+
return html;
|
|
76
|
+
// Inject CSS into head
|
|
77
|
+
const styleTag = `<style data-silk>${css}</style>`;
|
|
78
|
+
if (html.includes('</head>')) {
|
|
79
|
+
return html.replace('</head>', `${styleTag}\n</head>`);
|
|
80
|
+
}
|
|
81
|
+
return `${styleTag}\n${html}`;
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
generateBundle(_, bundle) {
|
|
85
|
+
if (!isBuild)
|
|
86
|
+
return;
|
|
87
|
+
const css = generateCSS();
|
|
88
|
+
if (!css)
|
|
89
|
+
return;
|
|
90
|
+
// Emit CSS file
|
|
91
|
+
this.emitFile({
|
|
92
|
+
type: 'asset',
|
|
93
|
+
fileName: outputFile,
|
|
94
|
+
source: css,
|
|
95
|
+
});
|
|
96
|
+
// Update HTML to reference external CSS file
|
|
97
|
+
for (const fileName in bundle) {
|
|
98
|
+
const chunk = bundle[fileName];
|
|
99
|
+
if (chunk && chunk.type === 'asset' && fileName.endsWith('.html')) {
|
|
100
|
+
const asset = chunk; // OutputAsset type
|
|
101
|
+
const html = asset.source;
|
|
102
|
+
const linkTag = `<link rel="stylesheet" href="/${outputFile}">`;
|
|
103
|
+
// Replace inline style with link tag
|
|
104
|
+
asset.source = html
|
|
105
|
+
.replace(/<style data-silk>[\s\S]*?<\/style>/, linkTag)
|
|
106
|
+
.replace('</head>', `${linkTag}\n</head>`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
// Handle hot updates in dev mode
|
|
111
|
+
handleHotUpdate({ file, server }) {
|
|
112
|
+
if (file.endsWith('.ts') || file.endsWith('.tsx')) {
|
|
113
|
+
// Trigger CSS update
|
|
114
|
+
const css = generateCSS();
|
|
115
|
+
server.ws.send({
|
|
116
|
+
type: 'custom',
|
|
117
|
+
event: 'silk:update',
|
|
118
|
+
data: { css },
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
export default silk;
|
|
125
|
+
/**
|
|
126
|
+
* Client-side script for hot CSS updates
|
|
127
|
+
* This should be imported in the app entry point
|
|
128
|
+
*/
|
|
129
|
+
export const silkClient = `
|
|
130
|
+
if (import.meta.hot) {
|
|
131
|
+
import.meta.hot.on('silk:update', ({ css }) => {
|
|
132
|
+
let style = document.querySelector('style[data-silk]')
|
|
133
|
+
if (!style) {
|
|
134
|
+
style = document.createElement('style')
|
|
135
|
+
style.setAttribute('data-silk', '')
|
|
136
|
+
document.head.appendChild(style)
|
|
137
|
+
}
|
|
138
|
+
style.textContent = css
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
`;
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sylphx/silk-vite-plugin",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vite plugin for Silk - Build-time CSS extraction with production optimizations",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"silk",
|
|
7
|
+
"vite",
|
|
8
|
+
"vite-plugin",
|
|
9
|
+
"css-in-js",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"author": "SylphX Ltd",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"homepage": "https://sylphx.com",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/sylphxltd/silk.git",
|
|
18
|
+
"directory": "packages/vite-plugin"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/sylphxltd/silk/issues"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc",
|
|
39
|
+
"dev": "tsc --watch",
|
|
40
|
+
"prepublishOnly": "bun run build"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@sylphx/silk": "workspace:*"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"typescript": "^5.3.0",
|
|
47
|
+
"vite": "^5.0.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"vite": "^4.0.0 || ^5.0.0"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
}
|
|
55
|
+
}
|