@typestyles/vite 0.0.0-unstable.82ebd9c88257 → 0.0.0-unstable.835df2a09a6d

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 CHANGED
@@ -31,7 +31,6 @@ var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
31
31
  var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
32
32
  var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
33
33
  var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
34
- var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
35
34
  function extractNamespaces(code) {
36
35
  const keys = [];
37
36
  const prefixes = [];
@@ -56,10 +55,15 @@ function extractNamespaces(code) {
56
55
  return { keys, prefixes };
57
56
  }
58
57
  function typestylesPlugin(options = {}) {
59
- const { warnDuplicates = true, mode = "runtime", extract } = options;
58
+ const { warnDuplicates = true, extract } = options;
59
+ const extractModules = extract?.modules ?? [];
60
+ const mode = options.mode ?? (extractModules.length > 0 ? "build" : "runtime");
60
61
  const moduleNamespaces = /* @__PURE__ */ new Map();
61
62
  let resolvedConfig = null;
62
63
  let isBuildCommand = false;
64
+ let isServe = false;
65
+ let devExtractCss = null;
66
+ let devExtractInFlight = null;
63
67
  return {
64
68
  name: "typestyles",
65
69
  enforce: "pre",
@@ -76,11 +80,69 @@ function typestylesPlugin(options = {}) {
76
80
  },
77
81
  configResolved(config) {
78
82
  resolvedConfig = config;
83
+ isServe = config.command === "serve";
84
+ },
85
+ configureServer(server) {
86
+ if (!extract || extractModules.length === 0) return;
87
+ if (mode === "runtime") return;
88
+ const fileName = extract.fileName ?? "typestyles.css";
89
+ const invalidateDevExtract = () => {
90
+ devExtractCss = null;
91
+ devExtractInFlight = null;
92
+ };
93
+ server.watcher.on("change", invalidateDevExtract);
94
+ server.watcher.on("add", invalidateDevExtract);
95
+ server.watcher.on("unlink", invalidateDevExtract);
96
+ server.middlewares.use(async (req, res, next) => {
97
+ if (req.method !== "GET" && req.method !== "HEAD") {
98
+ next();
99
+ return;
100
+ }
101
+ const config = resolvedConfig;
102
+ if (!config) {
103
+ next();
104
+ return;
105
+ }
106
+ const url = req.url?.split("?")[0] ?? "";
107
+ const base = config.base.replace(/\/$/, "") || "";
108
+ const expectedPath = base === "" || base === "/" ? `/${fileName.replace(/^\//, "")}` : `${base}/${fileName.replace(/^\//, "")}`;
109
+ if (url !== expectedPath) {
110
+ next();
111
+ return;
112
+ }
113
+ const root = config.root ?? process.cwd();
114
+ try {
115
+ if (devExtractCss !== null) {
116
+ res.statusCode = 200;
117
+ res.setHeader("Content-Type", "text/css; charset=utf-8");
118
+ res.setHeader("Cache-Control", "no-cache");
119
+ res.end(devExtractCss);
120
+ return;
121
+ }
122
+ if (!devExtractInFlight) {
123
+ devExtractInFlight = (0, import_build_runner.runTypestylesBuild)({
124
+ root,
125
+ modules: extractModules
126
+ }).then((css2) => {
127
+ devExtractCss = css2;
128
+ return css2;
129
+ }).finally(() => {
130
+ devExtractInFlight = null;
131
+ });
132
+ }
133
+ const css = await devExtractInFlight;
134
+ res.statusCode = 200;
135
+ res.setHeader("Content-Type", "text/css; charset=utf-8");
136
+ res.setHeader("Cache-Control", "no-cache");
137
+ res.end(css);
138
+ } catch (err) {
139
+ next(err);
140
+ }
141
+ });
79
142
  },
80
143
  transform(code, id) {
81
144
  if (!/\.[jt]sx?$/.test(id)) return null;
82
145
  if (id.includes("node_modules")) return null;
83
- if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
84
146
  const { keys, prefixes } = extractNamespaces(code);
85
147
  if (keys.length === 0 && prefixes.length === 0) return null;
86
148
  if (warnDuplicates) {
@@ -97,33 +159,36 @@ function typestylesPlugin(options = {}) {
97
159
  }
98
160
  }
99
161
  moduleNamespaces.set(id, { keys, prefixes });
162
+ if (!isServe) {
163
+ return null;
164
+ }
100
165
  const keysJSON = JSON.stringify(keys);
101
166
  const prefixesJSON = JSON.stringify(prefixes);
167
+ const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
168
+ `;
102
169
  const hmrCode = `
103
170
  if (import.meta.hot) {
104
- import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
105
- import.meta.hot.accept();
106
- import.meta.hot.dispose(() => {
107
- __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
108
- });
171
+ import.meta.hot.accept();
172
+ import.meta.hot.dispose(() => {
173
+ __typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
109
174
  });
110
175
  }`;
111
176
  return {
112
- code: code + hmrCode,
177
+ code: hmrImport + code + hmrCode,
113
178
  map: null
114
179
  };
115
180
  },
116
181
  async generateBundle() {
117
182
  if (!isBuildCommand) return;
118
183
  if (mode === "runtime") return;
119
- if (!extract || !extract.modules.length) return;
184
+ if (!extract || !extractModules.length) return;
120
185
  if (!resolvedConfig) return;
121
186
  const root = resolvedConfig.root ?? process.cwd();
122
187
  const fileName = extract.fileName ?? "typestyles.css";
123
188
  try {
124
189
  const css = await (0, import_build_runner.runTypestylesBuild)({
125
190
  root,
126
- modules: extract.modules
191
+ modules: extractModules
127
192
  });
128
193
  this.emitFile({
129
194
  type: "asset",
package/dist/index.d.cts CHANGED
@@ -16,9 +16,15 @@ interface TypestylesPluginOptions {
16
16
  warnDuplicates?: boolean;
17
17
  /**
18
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.
19
+ * - `"runtime"`: HMR only, no build-time extraction.
20
+ * - `"build"`: emit a static CSS asset on `vite build` and disable client injection there.
21
+ * - `"hybrid"`: extract on build and keep runtime-compatible behavior.
22
+ *
23
+ * When omitted and `extract.modules` is non-empty, defaults to `"build"`: `vite dev` keeps
24
+ * runtime injection + HMR (runtime is only disabled during `vite build`), serves the same
25
+ * extracted CSS at `extract.fileName` so `<link href="/typestyles.css">` is valid (avoids SPA
26
+ * fallback returning HTML for that URL), and production emits the CSS file. With no `extract`
27
+ * config, defaults to `"runtime"`.
22
28
  */
23
29
  mode?: 'runtime' | 'build' | 'hybrid';
24
30
  /**
@@ -37,9 +43,10 @@ declare function extractNamespaces(code: string): {
37
43
  /**
38
44
  * Vite plugin for typestyles HMR support.
39
45
  *
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.
46
+ * When a module that registers typestyles (e.g. `styles.component`, `tokens.create`) is edited,
47
+ * this plugin injects HMR accept/dispose handlers that invalidate that module's registrations
48
+ * before re-execution. Files may import from a local re-export such as `./typestyles` rather
49
+ * than `from 'typestyles'` directly — we key off API call patterns in the source, not the import path.
43
50
  */
44
51
  declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
45
52
 
package/dist/index.d.ts CHANGED
@@ -16,9 +16,15 @@ interface TypestylesPluginOptions {
16
16
  warnDuplicates?: boolean;
17
17
  /**
18
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.
19
+ * - `"runtime"`: HMR only, no build-time extraction.
20
+ * - `"build"`: emit a static CSS asset on `vite build` and disable client injection there.
21
+ * - `"hybrid"`: extract on build and keep runtime-compatible behavior.
22
+ *
23
+ * When omitted and `extract.modules` is non-empty, defaults to `"build"`: `vite dev` keeps
24
+ * runtime injection + HMR (runtime is only disabled during `vite build`), serves the same
25
+ * extracted CSS at `extract.fileName` so `<link href="/typestyles.css">` is valid (avoids SPA
26
+ * fallback returning HTML for that URL), and production emits the CSS file. With no `extract`
27
+ * config, defaults to `"runtime"`.
22
28
  */
23
29
  mode?: 'runtime' | 'build' | 'hybrid';
24
30
  /**
@@ -37,9 +43,10 @@ declare function extractNamespaces(code: string): {
37
43
  /**
38
44
  * Vite plugin for typestyles HMR support.
39
45
  *
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.
46
+ * When a module that registers typestyles (e.g. `styles.component`, `tokens.create`) is edited,
47
+ * this plugin injects HMR accept/dispose handlers that invalidate that module's registrations
48
+ * before re-execution. Files may import from a local re-export such as `./typestyles` rather
49
+ * than `from 'typestyles'` directly — we key off API call patterns in the source, not the import path.
43
50
  */
44
51
  declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
45
52
 
package/dist/index.js CHANGED
@@ -6,7 +6,6 @@ var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
6
6
  var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
7
7
  var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
8
8
  var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
9
- var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
10
9
  function extractNamespaces(code) {
11
10
  const keys = [];
12
11
  const prefixes = [];
@@ -31,10 +30,15 @@ function extractNamespaces(code) {
31
30
  return { keys, prefixes };
32
31
  }
33
32
  function typestylesPlugin(options = {}) {
34
- const { warnDuplicates = true, mode = "runtime", extract } = options;
33
+ const { warnDuplicates = true, extract } = options;
34
+ const extractModules = extract?.modules ?? [];
35
+ const mode = options.mode ?? (extractModules.length > 0 ? "build" : "runtime");
35
36
  const moduleNamespaces = /* @__PURE__ */ new Map();
36
37
  let resolvedConfig = null;
37
38
  let isBuildCommand = false;
39
+ let isServe = false;
40
+ let devExtractCss = null;
41
+ let devExtractInFlight = null;
38
42
  return {
39
43
  name: "typestyles",
40
44
  enforce: "pre",
@@ -51,11 +55,69 @@ function typestylesPlugin(options = {}) {
51
55
  },
52
56
  configResolved(config) {
53
57
  resolvedConfig = config;
58
+ isServe = config.command === "serve";
59
+ },
60
+ configureServer(server) {
61
+ if (!extract || extractModules.length === 0) return;
62
+ if (mode === "runtime") return;
63
+ const fileName = extract.fileName ?? "typestyles.css";
64
+ const invalidateDevExtract = () => {
65
+ devExtractCss = null;
66
+ devExtractInFlight = null;
67
+ };
68
+ server.watcher.on("change", invalidateDevExtract);
69
+ server.watcher.on("add", invalidateDevExtract);
70
+ server.watcher.on("unlink", invalidateDevExtract);
71
+ server.middlewares.use(async (req, res, next) => {
72
+ if (req.method !== "GET" && req.method !== "HEAD") {
73
+ next();
74
+ return;
75
+ }
76
+ const config = resolvedConfig;
77
+ if (!config) {
78
+ next();
79
+ return;
80
+ }
81
+ const url = req.url?.split("?")[0] ?? "";
82
+ const base = config.base.replace(/\/$/, "") || "";
83
+ const expectedPath = base === "" || base === "/" ? `/${fileName.replace(/^\//, "")}` : `${base}/${fileName.replace(/^\//, "")}`;
84
+ if (url !== expectedPath) {
85
+ next();
86
+ return;
87
+ }
88
+ const root = config.root ?? process.cwd();
89
+ try {
90
+ if (devExtractCss !== null) {
91
+ res.statusCode = 200;
92
+ res.setHeader("Content-Type", "text/css; charset=utf-8");
93
+ res.setHeader("Cache-Control", "no-cache");
94
+ res.end(devExtractCss);
95
+ return;
96
+ }
97
+ if (!devExtractInFlight) {
98
+ devExtractInFlight = runTypestylesBuild({
99
+ root,
100
+ modules: extractModules
101
+ }).then((css2) => {
102
+ devExtractCss = css2;
103
+ return css2;
104
+ }).finally(() => {
105
+ devExtractInFlight = null;
106
+ });
107
+ }
108
+ const css = await devExtractInFlight;
109
+ res.statusCode = 200;
110
+ res.setHeader("Content-Type", "text/css; charset=utf-8");
111
+ res.setHeader("Cache-Control", "no-cache");
112
+ res.end(css);
113
+ } catch (err) {
114
+ next(err);
115
+ }
116
+ });
54
117
  },
55
118
  transform(code, id) {
56
119
  if (!/\.[jt]sx?$/.test(id)) return null;
57
120
  if (id.includes("node_modules")) return null;
58
- if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
59
121
  const { keys, prefixes } = extractNamespaces(code);
60
122
  if (keys.length === 0 && prefixes.length === 0) return null;
61
123
  if (warnDuplicates) {
@@ -72,33 +134,36 @@ function typestylesPlugin(options = {}) {
72
134
  }
73
135
  }
74
136
  moduleNamespaces.set(id, { keys, prefixes });
137
+ if (!isServe) {
138
+ return null;
139
+ }
75
140
  const keysJSON = JSON.stringify(keys);
76
141
  const prefixesJSON = JSON.stringify(prefixes);
142
+ const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
143
+ `;
77
144
  const hmrCode = `
78
145
  if (import.meta.hot) {
79
- import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
80
- import.meta.hot.accept();
81
- import.meta.hot.dispose(() => {
82
- __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
83
- });
146
+ import.meta.hot.accept();
147
+ import.meta.hot.dispose(() => {
148
+ __typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
84
149
  });
85
150
  }`;
86
151
  return {
87
- code: code + hmrCode,
152
+ code: hmrImport + code + hmrCode,
88
153
  map: null
89
154
  };
90
155
  },
91
156
  async generateBundle() {
92
157
  if (!isBuildCommand) return;
93
158
  if (mode === "runtime") return;
94
- if (!extract || !extract.modules.length) return;
159
+ if (!extract || !extractModules.length) return;
95
160
  if (!resolvedConfig) return;
96
161
  const root = resolvedConfig.root ?? process.cwd();
97
162
  const fileName = extract.fileName ?? "typestyles.css";
98
163
  try {
99
164
  const css = await runTypestylesBuild({
100
165
  root,
101
- modules: extract.modules
166
+ modules: extractModules
102
167
  });
103
168
  this.emitFile({
104
169
  type: "asset",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typestyles/vite",
3
- "version": "0.0.0-unstable.82ebd9c88257",
3
+ "version": "0.0.0-unstable.835df2a09a6d",
4
4
  "description": "Vite plugin for typestyles HMR support",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -41,12 +41,12 @@
41
41
  "eslint": "^9.39.2",
42
42
  "tsup": "^8.0.0",
43
43
  "typescript": "^5.4.0",
44
- "typestyles": "0.0.0-unstable.82ebd9c88257",
44
+ "typestyles": "0.0.0-unstable.835df2a09a6d",
45
45
  "vite": "^6.0.0",
46
46
  "vitest": "^3.0.0"
47
47
  },
48
48
  "dependencies": {
49
- "@typestyles/build-runner": "0.0.0-unstable.82ebd9c88257"
49
+ "@typestyles/build-runner": "0.0.0-unstable.835df2a09a6d"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup src/index.ts --format esm,cjs --dts --clean --external typestyles,@typestyles/build-runner",