@typestyles/vite 0.1.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/dist/index.cjs ADDED
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ default: () => typestylesPlugin,
24
+ extractNamespaces: () => extractNamespaces
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var STYLES_CREATE_RE = /styles\.create\(\s*['"]([^'"]+)['"]/g;
28
+ var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
29
+ var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
30
+ var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
31
+ var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
32
+ var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
33
+ var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
34
+ var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
35
+ function extractNamespaces(code) {
36
+ const keys = [];
37
+ const prefixes = [];
38
+ for (const match of code.matchAll(STYLES_CREATE_RE)) {
39
+ prefixes.push(`.${match[1]}-`);
40
+ }
41
+ for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
42
+ prefixes.push(`.${match[1]}-`);
43
+ }
44
+ for (const match of code.matchAll(TOKENS_CREATE_RE)) {
45
+ keys.push(`tokens:${match[1]}`);
46
+ }
47
+ for (const match of code.matchAll(CREATE_THEME_RE)) {
48
+ keys.push(`theme:${match[1]}`);
49
+ }
50
+ for (const match of code.matchAll(KEYFRAMES_CREATE_RE)) {
51
+ keys.push(`keyframes:${match[1]}`);
52
+ }
53
+ for (const match of code.matchAll(GLOBAL_STYLE_RE)) {
54
+ prefixes.push(match[1]);
55
+ }
56
+ for (const match of code.matchAll(GLOBAL_FONT_FACE_RE)) {
57
+ prefixes.push(`font-face:${match[1]}`);
58
+ }
59
+ return { keys, prefixes };
60
+ }
61
+ function typestylesPlugin(options = {}) {
62
+ const { warnDuplicates = true } = options;
63
+ const moduleNamespaces = /* @__PURE__ */ new Map();
64
+ return {
65
+ name: "typestyles",
66
+ enforce: "pre",
67
+ transform(code, id) {
68
+ if (!/\.[jt]sx?$/.test(id)) return null;
69
+ if (id.includes("node_modules")) return null;
70
+ if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
71
+ const { keys, prefixes } = extractNamespaces(code);
72
+ if (keys.length === 0 && prefixes.length === 0) return null;
73
+ if (warnDuplicates) {
74
+ for (const [otherId, other] of moduleNamespaces) {
75
+ if (otherId === id) continue;
76
+ for (const prefix of prefixes) {
77
+ if (other.prefixes.includes(prefix)) {
78
+ const ns = prefix.slice(1, -1);
79
+ this.warn(
80
+ `Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions.`
81
+ );
82
+ }
83
+ }
84
+ }
85
+ }
86
+ moduleNamespaces.set(id, { keys, prefixes });
87
+ const keysJSON = JSON.stringify(keys);
88
+ const prefixesJSON = JSON.stringify(prefixes);
89
+ const hmrCode = `
90
+ if (import.meta.hot) {
91
+ import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
92
+ import.meta.hot.accept();
93
+ import.meta.hot.dispose(() => {
94
+ __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
95
+ });
96
+ });
97
+ }`;
98
+ return {
99
+ code: code + hmrCode,
100
+ map: null
101
+ };
102
+ }
103
+ };
104
+ }
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ extractNamespaces
108
+ });
@@ -0,0 +1,24 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface TypestylesPluginOptions {
4
+ /** Warn about duplicate namespaces across modules. Defaults to true. */
5
+ warnDuplicates?: boolean;
6
+ }
7
+ /**
8
+ * Extract namespace information from source code.
9
+ * Returns { keys, prefixes } for invalidation.
10
+ */
11
+ declare function extractNamespaces(code: string): {
12
+ keys: string[];
13
+ prefixes: string[];
14
+ };
15
+ /**
16
+ * Vite plugin for typestyles HMR support.
17
+ *
18
+ * When a module that uses typestyles is edited, this plugin injects HMR
19
+ * accept/dispose handlers that invalidate the module's style registrations
20
+ * before re-execution — so updated CSS takes effect without a full reload.
21
+ */
22
+ declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
23
+
24
+ export { type TypestylesPluginOptions, typestylesPlugin as default, extractNamespaces };
@@ -0,0 +1,24 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface TypestylesPluginOptions {
4
+ /** Warn about duplicate namespaces across modules. Defaults to true. */
5
+ warnDuplicates?: boolean;
6
+ }
7
+ /**
8
+ * Extract namespace information from source code.
9
+ * Returns { keys, prefixes } for invalidation.
10
+ */
11
+ declare function extractNamespaces(code: string): {
12
+ keys: string[];
13
+ prefixes: string[];
14
+ };
15
+ /**
16
+ * Vite plugin for typestyles HMR support.
17
+ *
18
+ * When a module that uses typestyles is edited, this plugin injects HMR
19
+ * accept/dispose handlers that invalidate the module's style registrations
20
+ * before re-execution — so updated CSS takes effect without a full reload.
21
+ */
22
+ declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
23
+
24
+ export { type TypestylesPluginOptions, typestylesPlugin as default, extractNamespaces };
package/dist/index.js ADDED
@@ -0,0 +1,83 @@
1
+ // src/index.ts
2
+ var STYLES_CREATE_RE = /styles\.create\(\s*['"]([^'"]+)['"]/g;
3
+ var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
4
+ var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
5
+ var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
6
+ var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
7
+ var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
8
+ var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
9
+ var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
10
+ function extractNamespaces(code) {
11
+ const keys = [];
12
+ const prefixes = [];
13
+ for (const match of code.matchAll(STYLES_CREATE_RE)) {
14
+ prefixes.push(`.${match[1]}-`);
15
+ }
16
+ for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
17
+ prefixes.push(`.${match[1]}-`);
18
+ }
19
+ for (const match of code.matchAll(TOKENS_CREATE_RE)) {
20
+ keys.push(`tokens:${match[1]}`);
21
+ }
22
+ for (const match of code.matchAll(CREATE_THEME_RE)) {
23
+ keys.push(`theme:${match[1]}`);
24
+ }
25
+ for (const match of code.matchAll(KEYFRAMES_CREATE_RE)) {
26
+ keys.push(`keyframes:${match[1]}`);
27
+ }
28
+ for (const match of code.matchAll(GLOBAL_STYLE_RE)) {
29
+ prefixes.push(match[1]);
30
+ }
31
+ for (const match of code.matchAll(GLOBAL_FONT_FACE_RE)) {
32
+ prefixes.push(`font-face:${match[1]}`);
33
+ }
34
+ return { keys, prefixes };
35
+ }
36
+ function typestylesPlugin(options = {}) {
37
+ const { warnDuplicates = true } = options;
38
+ const moduleNamespaces = /* @__PURE__ */ new Map();
39
+ return {
40
+ name: "typestyles",
41
+ enforce: "pre",
42
+ transform(code, id) {
43
+ if (!/\.[jt]sx?$/.test(id)) return null;
44
+ if (id.includes("node_modules")) return null;
45
+ if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
46
+ const { keys, prefixes } = extractNamespaces(code);
47
+ if (keys.length === 0 && prefixes.length === 0) return null;
48
+ if (warnDuplicates) {
49
+ for (const [otherId, other] of moduleNamespaces) {
50
+ if (otherId === id) continue;
51
+ for (const prefix of prefixes) {
52
+ if (other.prefixes.includes(prefix)) {
53
+ const ns = prefix.slice(1, -1);
54
+ this.warn(
55
+ `Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions.`
56
+ );
57
+ }
58
+ }
59
+ }
60
+ }
61
+ moduleNamespaces.set(id, { keys, prefixes });
62
+ const keysJSON = JSON.stringify(keys);
63
+ const prefixesJSON = JSON.stringify(prefixes);
64
+ const hmrCode = `
65
+ if (import.meta.hot) {
66
+ import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
67
+ import.meta.hot.accept();
68
+ import.meta.hot.dispose(() => {
69
+ __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
70
+ });
71
+ });
72
+ }`;
73
+ return {
74
+ code: code + hmrCode,
75
+ map: null
76
+ };
77
+ }
78
+ };
79
+ }
80
+ export {
81
+ typestylesPlugin as default,
82
+ extractNamespaces
83
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@typestyles/vite",
3
+ "version": "0.1.0",
4
+ "description": "Vite plugin for typestyles HMR support",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "keywords": [
25
+ "vite",
26
+ "vite-plugin",
27
+ "typestyles",
28
+ "hmr",
29
+ "css-in-js"
30
+ ],
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/typestyles/typestyles",
35
+ "directory": "packages/vite"
36
+ },
37
+ "peerDependencies": {
38
+ "vite": ">=5.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "tsup": "^8.0.0",
42
+ "typescript": "^5.4.0",
43
+ "vite": "^6.0.0",
44
+ "vitest": "^3.0.0"
45
+ },
46
+ "scripts": {
47
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
48
+ "test": "vitest run",
49
+ "typecheck": "tsc --noEmit"
50
+ }
51
+ }