@typestyles/vite 0.0.0-unstable.5b7138167233

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,148 @@
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 import_build_runner = require("@typestyles/build-runner");
28
+ var STYLES_CREATE_RE = /styles\.create\(\s*['"]([^'"]+)['"]/g;
29
+ var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
30
+ var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
31
+ var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
32
+ var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
33
+ var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
34
+ var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
35
+ var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
36
+ function extractNamespaces(code) {
37
+ const keys = [];
38
+ const prefixes = [];
39
+ for (const match of code.matchAll(STYLES_CREATE_RE)) {
40
+ prefixes.push(`.${match[1]}-`);
41
+ }
42
+ for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
43
+ prefixes.push(`.${match[1]}-`);
44
+ }
45
+ for (const match of code.matchAll(TOKENS_CREATE_RE)) {
46
+ keys.push(`tokens:${match[1]}`);
47
+ }
48
+ for (const match of code.matchAll(CREATE_THEME_RE)) {
49
+ keys.push(`theme:${match[1]}`);
50
+ }
51
+ for (const match of code.matchAll(KEYFRAMES_CREATE_RE)) {
52
+ keys.push(`keyframes:${match[1]}`);
53
+ }
54
+ for (const match of code.matchAll(GLOBAL_STYLE_RE)) {
55
+ prefixes.push(match[1]);
56
+ }
57
+ for (const match of code.matchAll(GLOBAL_FONT_FACE_RE)) {
58
+ prefixes.push(`font-face:${match[1]}`);
59
+ }
60
+ return { keys, prefixes };
61
+ }
62
+ function typestylesPlugin(options = {}) {
63
+ const { warnDuplicates = true, mode = "runtime", extract } = options;
64
+ const moduleNamespaces = /* @__PURE__ */ new Map();
65
+ let resolvedConfig = null;
66
+ let isBuildCommand = false;
67
+ return {
68
+ name: "typestyles",
69
+ enforce: "pre",
70
+ config(config, env) {
71
+ isBuildCommand = env.command === "build";
72
+ if (env.command === "build" && (mode === "build" || mode === "hybrid")) {
73
+ config.define = {
74
+ ...config.define ?? {},
75
+ // Inlined by the bundler so typestyles sheet skips creating <style> in production
76
+ __TYPESTYLES_RUNTIME_DISABLED__: JSON.stringify("true")
77
+ };
78
+ }
79
+ return config;
80
+ },
81
+ configResolved(config) {
82
+ resolvedConfig = config;
83
+ },
84
+ transform(code, id) {
85
+ if (!/\.[jt]sx?$/.test(id)) return null;
86
+ if (id.includes("node_modules")) return null;
87
+ if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
88
+ const { keys, prefixes } = extractNamespaces(code);
89
+ if (keys.length === 0 && prefixes.length === 0) return null;
90
+ if (warnDuplicates) {
91
+ for (const [otherId, other] of moduleNamespaces) {
92
+ if (otherId === id) continue;
93
+ for (const prefix of prefixes) {
94
+ if (other.prefixes.includes(prefix)) {
95
+ const ns = prefix.slice(1, -1);
96
+ this.warn(
97
+ `Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions.`
98
+ );
99
+ }
100
+ }
101
+ }
102
+ }
103
+ moduleNamespaces.set(id, { keys, prefixes });
104
+ const keysJSON = JSON.stringify(keys);
105
+ const prefixesJSON = JSON.stringify(prefixes);
106
+ const hmrCode = `
107
+ if (import.meta.hot) {
108
+ import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
109
+ import.meta.hot.accept();
110
+ import.meta.hot.dispose(() => {
111
+ __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
112
+ });
113
+ });
114
+ }`;
115
+ return {
116
+ code: code + hmrCode,
117
+ map: null
118
+ };
119
+ },
120
+ async generateBundle() {
121
+ if (!isBuildCommand) return;
122
+ if (mode === "runtime") return;
123
+ if (!extract || !extract.modules.length) return;
124
+ if (!resolvedConfig) return;
125
+ const root = resolvedConfig.root ?? process.cwd();
126
+ const fileName = extract.fileName ?? "typestyles.css";
127
+ try {
128
+ const css = await (0, import_build_runner.runTypestylesBuild)({
129
+ root,
130
+ modules: extract.modules
131
+ });
132
+ this.emitFile({
133
+ type: "asset",
134
+ fileName,
135
+ source: css
136
+ });
137
+ } catch (err) {
138
+ this.error(
139
+ `[typestyles] Failed to extract CSS: ${err instanceof Error ? err.message : String(err)}`
140
+ );
141
+ }
142
+ }
143
+ };
144
+ }
145
+ // Annotate the CommonJS export names for ESM import in node:
146
+ 0 && (module.exports = {
147
+ extractNamespaces
148
+ });
@@ -0,0 +1,46 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface TypestylesExtractOptions {
4
+ /**
5
+ * Modules that should be imported during build to register styles.
6
+ * Paths are resolved relative to Vite's project root.
7
+ */
8
+ modules: string[];
9
+ /**
10
+ * Output CSS filename (in the build assets). Defaults to "typestyles.css".
11
+ */
12
+ fileName?: string;
13
+ }
14
+ interface TypestylesPluginOptions {
15
+ /** Warn about duplicate namespaces across modules. Defaults to true. */
16
+ warnDuplicates?: boolean;
17
+ /**
18
+ * Mode for typestyles integration:
19
+ * - "runtime" (default): HMR only, no build-time extraction.
20
+ * - "build": emit a static CSS asset during build using typestyles/build.
21
+ * - "hybrid": both runtime injection and build-time CSS asset.
22
+ */
23
+ mode?: 'runtime' | 'build' | 'hybrid';
24
+ /**
25
+ * Options for build-time CSS extraction when mode is "build" or "hybrid".
26
+ */
27
+ extract?: TypestylesExtractOptions;
28
+ }
29
+ /**
30
+ * Extract namespace information from source code.
31
+ * Returns { keys, prefixes } for invalidation.
32
+ */
33
+ declare function extractNamespaces(code: string): {
34
+ keys: string[];
35
+ prefixes: string[];
36
+ };
37
+ /**
38
+ * Vite plugin for typestyles HMR support.
39
+ *
40
+ * When a module that uses typestyles is edited, this plugin injects HMR
41
+ * accept/dispose handlers that invalidate the module's style registrations
42
+ * before re-execution — so updated CSS takes effect without a full reload.
43
+ */
44
+ declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
45
+
46
+ export { type TypestylesExtractOptions, type TypestylesPluginOptions, typestylesPlugin as default, extractNamespaces };
@@ -0,0 +1,46 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface TypestylesExtractOptions {
4
+ /**
5
+ * Modules that should be imported during build to register styles.
6
+ * Paths are resolved relative to Vite's project root.
7
+ */
8
+ modules: string[];
9
+ /**
10
+ * Output CSS filename (in the build assets). Defaults to "typestyles.css".
11
+ */
12
+ fileName?: string;
13
+ }
14
+ interface TypestylesPluginOptions {
15
+ /** Warn about duplicate namespaces across modules. Defaults to true. */
16
+ warnDuplicates?: boolean;
17
+ /**
18
+ * Mode for typestyles integration:
19
+ * - "runtime" (default): HMR only, no build-time extraction.
20
+ * - "build": emit a static CSS asset during build using typestyles/build.
21
+ * - "hybrid": both runtime injection and build-time CSS asset.
22
+ */
23
+ mode?: 'runtime' | 'build' | 'hybrid';
24
+ /**
25
+ * Options for build-time CSS extraction when mode is "build" or "hybrid".
26
+ */
27
+ extract?: TypestylesExtractOptions;
28
+ }
29
+ /**
30
+ * Extract namespace information from source code.
31
+ * Returns { keys, prefixes } for invalidation.
32
+ */
33
+ declare function extractNamespaces(code: string): {
34
+ keys: string[];
35
+ prefixes: string[];
36
+ };
37
+ /**
38
+ * Vite plugin for typestyles HMR support.
39
+ *
40
+ * When a module that uses typestyles is edited, this plugin injects HMR
41
+ * accept/dispose handlers that invalidate the module's style registrations
42
+ * before re-execution — so updated CSS takes effect without a full reload.
43
+ */
44
+ declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
45
+
46
+ export { type TypestylesExtractOptions, type TypestylesPluginOptions, typestylesPlugin as default, extractNamespaces };
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
1
+ // src/index.ts
2
+ import { runTypestylesBuild } from "@typestyles/build-runner";
3
+ var STYLES_CREATE_RE = /styles\.create\(\s*['"]([^'"]+)['"]/g;
4
+ var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
5
+ var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
6
+ var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
7
+ var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
8
+ var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
9
+ var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
10
+ var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
11
+ function extractNamespaces(code) {
12
+ const keys = [];
13
+ const prefixes = [];
14
+ for (const match of code.matchAll(STYLES_CREATE_RE)) {
15
+ prefixes.push(`.${match[1]}-`);
16
+ }
17
+ for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
18
+ prefixes.push(`.${match[1]}-`);
19
+ }
20
+ for (const match of code.matchAll(TOKENS_CREATE_RE)) {
21
+ keys.push(`tokens:${match[1]}`);
22
+ }
23
+ for (const match of code.matchAll(CREATE_THEME_RE)) {
24
+ keys.push(`theme:${match[1]}`);
25
+ }
26
+ for (const match of code.matchAll(KEYFRAMES_CREATE_RE)) {
27
+ keys.push(`keyframes:${match[1]}`);
28
+ }
29
+ for (const match of code.matchAll(GLOBAL_STYLE_RE)) {
30
+ prefixes.push(match[1]);
31
+ }
32
+ for (const match of code.matchAll(GLOBAL_FONT_FACE_RE)) {
33
+ prefixes.push(`font-face:${match[1]}`);
34
+ }
35
+ return { keys, prefixes };
36
+ }
37
+ function typestylesPlugin(options = {}) {
38
+ const { warnDuplicates = true, mode = "runtime", extract } = options;
39
+ const moduleNamespaces = /* @__PURE__ */ new Map();
40
+ let resolvedConfig = null;
41
+ let isBuildCommand = false;
42
+ return {
43
+ name: "typestyles",
44
+ enforce: "pre",
45
+ config(config, env) {
46
+ isBuildCommand = env.command === "build";
47
+ if (env.command === "build" && (mode === "build" || mode === "hybrid")) {
48
+ config.define = {
49
+ ...config.define ?? {},
50
+ // Inlined by the bundler so typestyles sheet skips creating <style> in production
51
+ __TYPESTYLES_RUNTIME_DISABLED__: JSON.stringify("true")
52
+ };
53
+ }
54
+ return config;
55
+ },
56
+ configResolved(config) {
57
+ resolvedConfig = config;
58
+ },
59
+ transform(code, id) {
60
+ if (!/\.[jt]sx?$/.test(id)) return null;
61
+ if (id.includes("node_modules")) return null;
62
+ if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
63
+ const { keys, prefixes } = extractNamespaces(code);
64
+ if (keys.length === 0 && prefixes.length === 0) return null;
65
+ if (warnDuplicates) {
66
+ for (const [otherId, other] of moduleNamespaces) {
67
+ if (otherId === id) continue;
68
+ for (const prefix of prefixes) {
69
+ if (other.prefixes.includes(prefix)) {
70
+ const ns = prefix.slice(1, -1);
71
+ this.warn(
72
+ `Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions.`
73
+ );
74
+ }
75
+ }
76
+ }
77
+ }
78
+ moduleNamespaces.set(id, { keys, prefixes });
79
+ const keysJSON = JSON.stringify(keys);
80
+ const prefixesJSON = JSON.stringify(prefixes);
81
+ const hmrCode = `
82
+ if (import.meta.hot) {
83
+ import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
84
+ import.meta.hot.accept();
85
+ import.meta.hot.dispose(() => {
86
+ __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
87
+ });
88
+ });
89
+ }`;
90
+ return {
91
+ code: code + hmrCode,
92
+ map: null
93
+ };
94
+ },
95
+ async generateBundle() {
96
+ if (!isBuildCommand) return;
97
+ if (mode === "runtime") return;
98
+ if (!extract || !extract.modules.length) return;
99
+ if (!resolvedConfig) return;
100
+ const root = resolvedConfig.root ?? process.cwd();
101
+ const fileName = extract.fileName ?? "typestyles.css";
102
+ try {
103
+ const css = await runTypestylesBuild({
104
+ root,
105
+ modules: extract.modules
106
+ });
107
+ this.emitFile({
108
+ type: "asset",
109
+ fileName,
110
+ source: css
111
+ });
112
+ } catch (err) {
113
+ this.error(
114
+ `[typestyles] Failed to extract CSS: ${err instanceof Error ? err.message : String(err)}`
115
+ );
116
+ }
117
+ }
118
+ };
119
+ }
120
+ export {
121
+ typestylesPlugin as default,
122
+ extractNamespaces
123
+ };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@typestyles/vite",
3
+ "version": "0.0.0-unstable.5b7138167233",
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": "Apache-2.0",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/dbanksdesign/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
+ "typestyles": "0.0.0-unstable.5b7138167233",
44
+ "vite": "^6.0.0",
45
+ "vitest": "^3.0.0"
46
+ },
47
+ "dependencies": {
48
+ "@typestyles/build-runner": "0.0.0-unstable.5b7138167233"
49
+ },
50
+ "scripts": {
51
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean --external typestyles,@typestyles/build-runner",
52
+ "test": "vitest run",
53
+ "typecheck": "tsc --noEmit"
54
+ }
55
+ }