@cascivo/vite-plugin 0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) cascivo contributors
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
13
+ all 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/README.md ADDED
@@ -0,0 +1,82 @@
1
+ <!-- generated by scripts/readme/generate.ts — edit readme.body.md, not this file -->
2
+
3
+ <div align="center">
4
+ <a href="https://cascivo.com"><img src="https://cascivo.com/favicon.svg" width="72" height="72" alt="cascivo logo"></a>
5
+ <h1>@cascivo/vite-plugin</h1>
6
+ <p><strong>Vite plugin — wrap JS-imported third-party stylesheets into a low-priority CSS @layer</strong></p>
7
+
8
+ [![npm](https://img.shields.io/npm/v/%40cascivo%2Fvite-plugin?style=flat-square&color=0079bf)](https://www.npmjs.com/package/@cascivo/vite-plugin)
9
+ [![downloads](https://img.shields.io/npm/dm/%40cascivo%2Fvite-plugin?style=flat-square&color=0079bf)](https://www.npmjs.com/package/@cascivo/vite-plugin)
10
+ [![license](https://img.shields.io/npm/l/%40cascivo%2Fvite-plugin?style=flat-square&color=0079bf)](https://github.com/cascivo/cascivo/blob/main/LICENSE)
11
+ ![types](https://img.shields.io/badge/types-included-0079bf?style=flat-square&logo=typescript&logoColor=white)
12
+
13
+ [npm](https://www.npmjs.com/package/@cascivo/vite-plugin) · [cascivo.com](https://cascivo.com) · [Docs](https://docs.cascivo.com) · [Storybook](https://storybook.cascivo.com) · [GitHub](https://github.com/cascivo/cascivo)
14
+
15
+ </div>
16
+
17
+ ---
18
+
19
+ Wrap JS-imported third-party stylesheets into a low-priority cascade `@layer` so
20
+ cascivo's tokens and themes always win.
21
+
22
+ ## Why
23
+
24
+ cascivo ships every rule inside a [cascade layer](https://github.com/cascivo/cascivo/blob/main/docs/CSS-LAYERS-PITFALL.md).
25
+ Layer order beats specificity, so unlayered author CSS — like a charting library's
26
+ global stylesheet — stomps your theme. The native fix is
27
+ `@import url('lib.css') layer(vendor)` from a CSS file (see
28
+ [docs/THIRD-PARTY-CSS.md](https://github.com/cascivo/cascivo/blob/main/docs/THIRD-PARTY-CSS.md)).
29
+ But that only works from CSS — a stylesheet imported from **JavaScript**
30
+ (`import 'lib/styles.css'`, how most JS libraries ship) has nowhere to attach
31
+ `layer()`. This plugin closes that gap.
32
+
33
+ ## Usage
34
+
35
+ ```ts
36
+ // vite.config.ts
37
+ import { cascivoLayers } from '@cascivo/vite-plugin'
38
+
39
+ export default defineConfig({
40
+ plugins: [
41
+ cascivoLayers({
42
+ imports: {
43
+ 'recharts/styles.css': 'vendor',
44
+ 'some-widget/dist/styles.css': 'vendor',
45
+ },
46
+ }),
47
+ ],
48
+ })
49
+ ```
50
+
51
+ Each key is matched against the tail of the resolved module id; each value is the
52
+ cascade layer to wrap it in. Your app CSS must declare the layer **before** the
53
+ cascivo layers:
54
+
55
+ ```css
56
+ @layer vendor, cascivo.reset, cascivo.base, cascivo.tokens, cascivo.component,
57
+ cascivo.theme, cascivo.blocks, cascivo.override;
58
+ ```
59
+
60
+ ## Scope
61
+
62
+ - **Vite only.** A webpack/Next.js loader is not included.
63
+ - Cannot fix runtime-injected `<style>` tags or inline `style=""` attributes — those
64
+ bypass the layer system entirely. Use the library's own theming API or a Shadow DOM
65
+ boundary there.
66
+
67
+ ## API
68
+
69
+ - `cascivoLayers({ imports })` — the Vite plugin.
70
+ - `wrapCssInLayer(source, layer?)` — the underlying pure transform, exported for testing.
71
+
72
+ ## Install
73
+
74
+ ```sh
75
+ pnpm add @cascivo/vite-plugin
76
+ ```
77
+
78
+ ---
79
+
80
+ [cascivo.com](https://cascivo.com) · [Docs](https://docs.cascivo.com) · [Storybook](https://storybook.cascivo.com) · [GitHub](https://github.com/cascivo/cascivo) · AI agents: use [`@cascivo/mcp`](https://github.com/cascivo/cascivo/tree/main/packages/mcp) and [`registry.json`](https://github.com/cascivo/cascivo/blob/main/registry.json) · MIT
81
+
82
+ <div align="center"><a href="https://cascivo.com"><img src="https://cascivo.com/favicon.svg" width="28" height="28" alt="cascivo"></a></div>
@@ -0,0 +1,51 @@
1
+ //#region src/index.d.ts
2
+ /**
3
+ * @cascivo/vite-plugin — layer JS-imported vendor CSS.
4
+ *
5
+ * The native recipe (`@import url('lib.css') layer(vendor)` from a CSS file) is
6
+ * the primary, tooling-free way to tame third-party stylesheets — see
7
+ * docs/THIRD-PARTY-CSS.md. What native CSS cannot do is layer a stylesheet
8
+ * imported from JavaScript (`import 'lib/styles.css'`), which is how most JS
9
+ * libraries ship. This plugin closes exactly that gap: it wraps configured
10
+ * node_modules `.css` imports into a low-priority `@layer` at build time so the
11
+ * cascivo layers always win.
12
+ */
13
+ /**
14
+ * The subset of Vite's `Plugin` interface this package implements. Declared
15
+ * structurally so the package doesn't require `vite`'s types to be resolvable;
16
+ * the returned object is assignable to Vite's `Plugin`.
17
+ */
18
+ interface VitePlugin {
19
+ name: string;
20
+ enforce?: 'pre' | 'post';
21
+ transform?: (code: string, id: string) => {
22
+ code: string;
23
+ map: null;
24
+ } | null | undefined;
25
+ }
26
+ interface CascivoLayersOptions {
27
+ /**
28
+ * Map a stylesheet's module id (matched against the tail of the resolved id,
29
+ * e.g. `"recharts/styles.css"`) to the cascade layer it should live in
30
+ * (typically `"vendor"`). The layer must be declared before the cascivo
31
+ * layers in your app's order statement — the scaffold from `cascivo create`
32
+ * already includes a `vendor` slot.
33
+ */
34
+ imports: Record<string, string>;
35
+ }
36
+ /**
37
+ * Wrap a stylesheet's rules in `@layer <layer> { … }`. Because an `@import` may
38
+ * not appear inside a layer block, top-level `@import`s are hoisted out and
39
+ * rewritten to carry `layer(<layer>)` themselves; a leading `@charset` is kept
40
+ * first. Pure string-in/string-out.
41
+ */
42
+ declare function wrapCssInLayer(source: string, layer?: string): string;
43
+ /**
44
+ * Vite plugin that wraps configured node_modules stylesheets in a cascade
45
+ * `@layer`. Runs `enforce: 'pre'` so it transforms the raw CSS before Vite's
46
+ * own CSS pipeline processes it.
47
+ */
48
+ declare function cascivoLayers(options: CascivoLayersOptions): VitePlugin;
49
+ //#endregion
50
+ export { CascivoLayersOptions, VitePlugin, cascivoLayers, cascivoLayers as default, wrapCssInLayer };
51
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;AAiBA;;;;;;;;;;;;AAG+D;AAG/D;;UANiB,UAAA;EACf,IAAA;EACA,OAAA;EACA,SAAA,IAAa,IAAA,UAAc,EAAA;IAAiB,IAAA;IAAc,GAAA;EAAA;AAAA;AAAA,UAG3C,oBAAA;EAuDY;;;;;;;EA/C3B,OAAA,EAAS,MAAM;AAAA;;;;;;;iBAoBD,cAAA,CAAe,MAAA,UAAgB,KAAgB;;;;;;iBA2B/C,aAAA,CAAc,OAAA,EAAS,oBAAA,GAAuB,UAAU"}
package/dist/index.mjs ADDED
@@ -0,0 +1,62 @@
1
+ //#region src/index.ts
2
+ const IMPORT_RE = /@import\s+[^;]+;/g;
3
+ const CHARSET_RE = /^\s*@charset\s+[^;]+;/i;
4
+ /** Add `layer(<layer>)` to an `@import` statement that isn't already layered. */
5
+ function layerizeImport(stmt, layer) {
6
+ if (/\blayer\b/.test(stmt)) return stmt;
7
+ const head = stmt.match(/@import\s+(?:url\([^)]*\)|"[^"]*"|'[^']*')/i);
8
+ if (!head) return stmt;
9
+ return stmt.replace(head[0], `${head[0]} layer(${layer})`);
10
+ }
11
+ /**
12
+ * Wrap a stylesheet's rules in `@layer <layer> { … }`. Because an `@import` may
13
+ * not appear inside a layer block, top-level `@import`s are hoisted out and
14
+ * rewritten to carry `layer(<layer>)` themselves; a leading `@charset` is kept
15
+ * first. Pure string-in/string-out.
16
+ */
17
+ function wrapCssInLayer(source, layer = "vendor") {
18
+ let charset = "";
19
+ let body = source;
20
+ const cm = body.match(CHARSET_RE);
21
+ if (cm) {
22
+ charset = `${cm[0].trim()}\n`;
23
+ body = body.slice(cm[0].length);
24
+ }
25
+ const imports = [];
26
+ body = body.replace(IMPORT_RE, (stmt) => {
27
+ imports.push(layerizeImport(stmt.trim(), layer));
28
+ return "";
29
+ });
30
+ const importsBlock = imports.length > 0 ? `${imports.join("\n")}\n` : "";
31
+ return `${charset}${importsBlock}@layer ${layer} {\n${body.trim()}\n}\n`;
32
+ }
33
+ /** Normalize a module id: drop the query/hash and Windows backslashes. */
34
+ function normalizeId(id) {
35
+ return (id.split("?")[0] ?? id).replace(/\\/g, "/");
36
+ }
37
+ /**
38
+ * Vite plugin that wraps configured node_modules stylesheets in a cascade
39
+ * `@layer`. Runs `enforce: 'pre'` so it transforms the raw CSS before Vite's
40
+ * own CSS pipeline processes it.
41
+ */
42
+ function cascivoLayers(options) {
43
+ const entries = Object.entries(options?.imports ?? {});
44
+ return {
45
+ name: "cascivo:vendor-layers",
46
+ enforce: "pre",
47
+ transform(code, id) {
48
+ const clean = normalizeId(id);
49
+ if (!clean.endsWith(".css") || !clean.includes("node_modules")) return null;
50
+ const match = entries.find(([key]) => clean.endsWith(key));
51
+ if (!match) return null;
52
+ return {
53
+ code: wrapCssInLayer(code, match[1]),
54
+ map: null
55
+ };
56
+ }
57
+ };
58
+ }
59
+ //#endregion
60
+ export { cascivoLayers, cascivoLayers as default, wrapCssInLayer };
61
+
62
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @cascivo/vite-plugin — layer JS-imported vendor CSS.\n *\n * The native recipe (`@import url('lib.css') layer(vendor)` from a CSS file) is\n * the primary, tooling-free way to tame third-party stylesheets — see\n * docs/THIRD-PARTY-CSS.md. What native CSS cannot do is layer a stylesheet\n * imported from JavaScript (`import 'lib/styles.css'`), which is how most JS\n * libraries ship. This plugin closes exactly that gap: it wraps configured\n * node_modules `.css` imports into a low-priority `@layer` at build time so the\n * cascivo layers always win.\n */\n\n/**\n * The subset of Vite's `Plugin` interface this package implements. Declared\n * structurally so the package doesn't require `vite`'s types to be resolvable;\n * the returned object is assignable to Vite's `Plugin`.\n */\nexport interface VitePlugin {\n name: string\n enforce?: 'pre' | 'post'\n transform?: (code: string, id: string) => { code: string; map: null } | null | undefined\n}\n\nexport interface CascivoLayersOptions {\n /**\n * Map a stylesheet's module id (matched against the tail of the resolved id,\n * e.g. `\"recharts/styles.css\"`) to the cascade layer it should live in\n * (typically `\"vendor\"`). The layer must be declared before the cascivo\n * layers in your app's order statement — the scaffold from `cascivo create`\n * already includes a `vendor` slot.\n */\n imports: Record<string, string>\n}\n\nconst IMPORT_RE = /@import\\s+[^;]+;/g\nconst CHARSET_RE = /^\\s*@charset\\s+[^;]+;/i\n\n/** Add `layer(<layer>)` to an `@import` statement that isn't already layered. */\nfunction layerizeImport(stmt: string, layer: string): string {\n if (/\\blayer\\b/.test(stmt)) return stmt\n const head = stmt.match(/@import\\s+(?:url\\([^)]*\\)|\"[^\"]*\"|'[^']*')/i)\n if (!head) return stmt\n return stmt.replace(head[0], `${head[0]} layer(${layer})`)\n}\n\n/**\n * Wrap a stylesheet's rules in `@layer <layer> { … }`. Because an `@import` may\n * not appear inside a layer block, top-level `@import`s are hoisted out and\n * rewritten to carry `layer(<layer>)` themselves; a leading `@charset` is kept\n * first. Pure string-in/string-out.\n */\nexport function wrapCssInLayer(source: string, layer = 'vendor'): string {\n let charset = ''\n let body = source\n const cm = body.match(CHARSET_RE)\n if (cm) {\n charset = `${cm[0].trim()}\\n`\n body = body.slice(cm[0].length)\n }\n const imports: string[] = []\n body = body.replace(IMPORT_RE, (stmt) => {\n imports.push(layerizeImport(stmt.trim(), layer))\n return ''\n })\n const importsBlock = imports.length > 0 ? `${imports.join('\\n')}\\n` : ''\n return `${charset}${importsBlock}@layer ${layer} {\\n${body.trim()}\\n}\\n`\n}\n\n/** Normalize a module id: drop the query/hash and Windows backslashes. */\nfunction normalizeId(id: string): string {\n return (id.split('?')[0] ?? id).replace(/\\\\/g, '/')\n}\n\n/**\n * Vite plugin that wraps configured node_modules stylesheets in a cascade\n * `@layer`. Runs `enforce: 'pre'` so it transforms the raw CSS before Vite's\n * own CSS pipeline processes it.\n */\nexport function cascivoLayers(options: CascivoLayersOptions): VitePlugin {\n const entries = Object.entries(options?.imports ?? {})\n return {\n name: 'cascivo:vendor-layers',\n enforce: 'pre',\n transform(code, id) {\n const clean = normalizeId(id)\n if (!clean.endsWith('.css') || !clean.includes('node_modules')) return null\n const match = entries.find(([key]) => clean.endsWith(key))\n if (!match) return null\n return { code: wrapCssInLayer(code, match[1]), map: null }\n },\n }\n}\n\nexport default cascivoLayers\n"],"mappings":";AAkCA,MAAM,YAAY;AAClB,MAAM,aAAa;;AAGnB,SAAS,eAAe,MAAc,OAAuB;CAC3D,IAAI,YAAY,KAAK,IAAI,GAAG,OAAO;CACnC,MAAM,OAAO,KAAK,MAAM,6CAA6C;CACrE,IAAI,CAAC,MAAM,OAAO;CAClB,OAAO,KAAK,QAAQ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
+ "description": "Vite plugin — wrap JS-imported third-party stylesheets into a low-priority CSS @layer",
6
+ "keywords": [
7
+ "cascivo",
8
+ "css",
9
+ "css-layers",
10
+ "vendor",
11
+ "vite",
12
+ "vite-plugin"
13
+ ],
14
+ "homepage": "https://github.com/cascivo/cascivo/tree/main/packages/vite-plugin#readme",
15
+ "bugs": "https://github.com/cascivo/cascivo/issues",
16
+ "license": "MIT",
17
+ "author": "urbanisierung",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/cascivo/cascivo.git",
21
+ "directory": "packages/vite-plugin"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "types": "./dist/index.d.mts",
28
+ "exports": {
29
+ ".": {
30
+ "import": "./dist/index.mjs",
31
+ "types": "./dist/index.d.mts",
32
+ "default": "./dist/index.mjs"
33
+ }
34
+ },
35
+ "publishConfig": {
36
+ "access": "public",
37
+ "provenance": true
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "^25",
41
+ "typescript": "^5",
42
+ "vite-plus": "^0.2.1"
43
+ },
44
+ "peerDependencies": {
45
+ "vite": ">=5"
46
+ },
47
+ "peerDependenciesMeta": {
48
+ "vite": {
49
+ "optional": true
50
+ }
51
+ },
52
+ "scripts": {
53
+ "build": "vp pack",
54
+ "check": "tsc --noEmit",
55
+ "test": "vp test"
56
+ }
57
+ }
package/readme.body.md ADDED
@@ -0,0 +1,52 @@
1
+ Wrap JS-imported third-party stylesheets into a low-priority cascade `@layer` so
2
+ cascivo's tokens and themes always win.
3
+
4
+ ## Why
5
+
6
+ cascivo ships every rule inside a [cascade layer](https://github.com/cascivo/cascivo/blob/main/docs/CSS-LAYERS-PITFALL.md).
7
+ Layer order beats specificity, so unlayered author CSS — like a charting library's
8
+ global stylesheet — stomps your theme. The native fix is
9
+ `@import url('lib.css') layer(vendor)` from a CSS file (see
10
+ [docs/THIRD-PARTY-CSS.md](https://github.com/cascivo/cascivo/blob/main/docs/THIRD-PARTY-CSS.md)).
11
+ But that only works from CSS — a stylesheet imported from **JavaScript**
12
+ (`import 'lib/styles.css'`, how most JS libraries ship) has nowhere to attach
13
+ `layer()`. This plugin closes that gap.
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ // vite.config.ts
19
+ import { cascivoLayers } from '@cascivo/vite-plugin'
20
+
21
+ export default defineConfig({
22
+ plugins: [
23
+ cascivoLayers({
24
+ imports: {
25
+ 'recharts/styles.css': 'vendor',
26
+ 'some-widget/dist/styles.css': 'vendor',
27
+ },
28
+ }),
29
+ ],
30
+ })
31
+ ```
32
+
33
+ Each key is matched against the tail of the resolved module id; each value is the
34
+ cascade layer to wrap it in. Your app CSS must declare the layer **before** the
35
+ cascivo layers:
36
+
37
+ ```css
38
+ @layer vendor, cascivo.reset, cascivo.base, cascivo.tokens, cascivo.component,
39
+ cascivo.theme, cascivo.blocks, cascivo.override;
40
+ ```
41
+
42
+ ## Scope
43
+
44
+ - **Vite only.** A webpack/Next.js loader is not included.
45
+ - Cannot fix runtime-injected `<style>` tags or inline `style=""` attributes — those
46
+ bypass the layer system entirely. Use the library's own theming API or a Shadow DOM
47
+ boundary there.
48
+
49
+ ## API
50
+
51
+ - `cascivoLayers({ imports })` — the Vite plugin.
52
+ - `wrapCssInLayer(source, layer?)` — the underlying pure transform, exported for testing.