@typestyles/vite 0.4.3 → 0.4.5

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/README.md CHANGED
@@ -63,7 +63,7 @@ export default defineConfig({
63
63
  });
64
64
  ```
65
65
 
66
- **3. Link the CSS** in your HTML shell:
66
+ **3. Link the CSS** in your HTML shell (optional — the plugin auto-injects this during dev and build when extraction is enabled):
67
67
 
68
68
  ```html
69
69
  <link rel="stylesheet" href="/typestyles.css" />
package/dist/index.cjs CHANGED
@@ -23,15 +23,41 @@ __export(index_exports, {
23
23
  DEFAULT_EXTRACT_MODULE_CANDIDATES: () => import_build_runner2.DEFAULT_EXTRACT_MODULE_CANDIDATES,
24
24
  default: () => typestylesPlugin,
25
25
  discoverDefaultExtractModules: () => import_build_runner2.discoverDefaultExtractModules,
26
- extractNamespaces: () => import_build_runner.extractNamespaces
26
+ extractNamespaces: () => import_build_runner.extractNamespaces,
27
+ htmlLinksTypestylesCss: () => htmlLinksTypestylesCss,
28
+ moduleNeedsOverrideHmr: () => import_build_runner3.moduleNeedsOverrideHmr,
29
+ resolveTypestylesCssHref: () => resolveTypestylesCssHref
27
30
  });
28
31
  module.exports = __toCommonJS(index_exports);
29
32
  var import_node_path = require("path");
30
33
  var import_build_runner = require("@typestyles/build-runner");
31
34
  var import_build_runner2 = require("@typestyles/build-runner");
35
+ var import_build_runner3 = require("@typestyles/build-runner");
32
36
  function resolveExtractModulesForVite(root, extract) {
33
37
  return (0, import_build_runner.resolveExtractModules)(root, extract);
34
38
  }
39
+ function resolveTypestylesCssHref(base, fileName) {
40
+ const normalizedFileName = fileName.replace(/^\//, "");
41
+ const normalizedBase = base.replace(/\/$/, "") || "";
42
+ if (normalizedBase === "" || normalizedBase === "/") {
43
+ return `/${normalizedFileName}`;
44
+ }
45
+ return `${normalizedBase}/${normalizedFileName}`;
46
+ }
47
+ function htmlLinksTypestylesCss(html, cssHref, fileName) {
48
+ const bareName = fileName.replace(/^\//, "");
49
+ const linkPattern = /<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi;
50
+ const hrefPattern = /\bhref=["']([^"']+)["']/i;
51
+ for (const match of html.matchAll(linkPattern)) {
52
+ const hrefMatch = match[0].match(hrefPattern);
53
+ const href = hrefMatch?.[1];
54
+ if (!href) continue;
55
+ if (href === cssHref || href.endsWith(`/${bareName}`) || href === bareName || href === `./${bareName}`) {
56
+ return true;
57
+ }
58
+ }
59
+ return false;
60
+ }
35
61
  function typestylesPlugin(options = {}) {
36
62
  const { warnDuplicates = true, extract } = options;
37
63
  let resolvedExtractModules = [];
@@ -125,30 +151,67 @@ function typestylesPlugin(options = {}) {
125
151
  if (!/\.[jt]sx?$/.test(id)) return null;
126
152
  if (id.includes("node_modules")) return null;
127
153
  const { keys, prefixes } = (0, import_build_runner.extractNamespaces)(code);
128
- if (keys.length === 0 && prefixes.length === 0) return null;
129
- if (warnDuplicates) {
154
+ const overrideHmr = (0, import_build_runner.moduleNeedsOverrideHmr)(code);
155
+ if (keys.length === 0 && prefixes.length === 0 && !overrideHmr) return null;
156
+ if (warnDuplicates && prefixes.length > 0) {
130
157
  (0, import_build_runner.reportDuplicateNamespaces)(moduleNamespaces, id, prefixes, this);
131
158
  }
132
- moduleNamespaces.set(id, { keys, prefixes });
159
+ if (keys.length > 0 || prefixes.length > 0) {
160
+ moduleNamespaces.set(id, { keys, prefixes });
161
+ }
133
162
  if (!isServe) {
134
163
  return null;
135
164
  }
136
165
  const keysJSON = JSON.stringify(keys);
137
166
  const prefixesJSON = JSON.stringify(prefixes);
138
- const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
167
+ const hmrImports = overrideHmr ? `import { invalidateKeys as __typestylesInvalidateKeys, createOverrideHmrSlot as __tsCreateOvHmr } from 'typestyles/hmr';
168
+ ` : `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
139
169
  `;
140
- const hmrCode = `
170
+ const overrideBootstrap = overrideHmr ? `const __tsOvHmr = __tsCreateOvHmr();
171
+ __tsOvHmr.activate();
172
+ ` : "";
173
+ const overrideTeardown = overrideHmr ? `
174
+ __tsOvHmr.deactivate();
175
+ ` : "";
176
+ const disposeBody = [
177
+ keys.length > 0 || prefixes.length > 0 ? `__typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});` : null,
178
+ overrideHmr ? `__tsOvHmr.dispose();` : null
179
+ ].filter(Boolean).join("\n ");
180
+ const hmrCode = `${overrideTeardown}
141
181
  if (import.meta.hot) {
142
182
  import.meta.hot.accept();
143
183
  import.meta.hot.dispose(() => {
144
- __typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
184
+ ${disposeBody}
145
185
  });
146
186
  }`;
147
187
  return {
148
- code: hmrImport + code + hmrCode,
188
+ code: hmrImports + overrideBootstrap + code + hmrCode,
149
189
  map: null
150
190
  };
151
191
  },
192
+ transformIndexHtml: {
193
+ order: "pre",
194
+ handler(html) {
195
+ if (resolvedExtractModules.length === 0) return;
196
+ if (resolvedMode === "runtime") return;
197
+ if (!resolvedConfig) return;
198
+ const fileName = extract?.fileName ?? "typestyles.css";
199
+ const href = resolveTypestylesCssHref(resolvedConfig.base, fileName);
200
+ if (htmlLinksTypestylesCss(html, href, fileName)) {
201
+ return;
202
+ }
203
+ return [
204
+ {
205
+ tag: "link",
206
+ attrs: {
207
+ rel: "stylesheet",
208
+ href
209
+ },
210
+ injectTo: "head"
211
+ }
212
+ ];
213
+ }
214
+ },
152
215
  async generateBundle() {
153
216
  if (!isBuildCommand) return;
154
217
  if (resolvedMode === "runtime") return;
@@ -178,5 +241,8 @@ if (import.meta.hot) {
178
241
  0 && (module.exports = {
179
242
  DEFAULT_EXTRACT_MODULE_CANDIDATES,
180
243
  discoverDefaultExtractModules,
181
- extractNamespaces
244
+ extractNamespaces,
245
+ htmlLinksTypestylesCss,
246
+ moduleNeedsOverrideHmr,
247
+ resolveTypestylesCssHref
182
248
  });
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Plugin } from 'vite';
2
2
  import { TypestylesIntegrationMode, TypestylesExtractOptions } from '@typestyles/build-runner';
3
- export { DEFAULT_EXTRACT_MODULE_CANDIDATES, TypestylesExtractOptions, discoverDefaultExtractModules, extractNamespaces } from '@typestyles/build-runner';
3
+ export { DEFAULT_EXTRACT_MODULE_CANDIDATES, TypestylesExtractOptions, discoverDefaultExtractModules, extractNamespaces, moduleNeedsOverrideHmr } from '@typestyles/build-runner';
4
4
 
5
5
  /**
6
6
  * Regex patterns to extract namespace strings from typestyles API calls.
@@ -42,6 +42,10 @@ interface TypestylesPluginOptions {
42
42
  */
43
43
  extract?: TypestylesExtractOptions;
44
44
  }
45
+ /** Public URL path for the extracted CSS asset (respects Vite `base`). */
46
+ declare function resolveTypestylesCssHref(base: string, fileName: string): string;
47
+ /** True when `html` already links the extracted stylesheet (avoids duplicate injection). */
48
+ declare function htmlLinksTypestylesCss(html: string, cssHref: string, fileName: string): boolean;
45
49
 
46
50
  /**
47
51
  * Vite plugin for typestyles HMR support.
@@ -53,4 +57,4 @@ interface TypestylesPluginOptions {
53
57
  */
54
58
  declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
55
59
 
56
- export { type TypestylesPluginOptions, typestylesPlugin as default };
60
+ export { type TypestylesPluginOptions, typestylesPlugin as default, htmlLinksTypestylesCss, resolveTypestylesCssHref };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Plugin } from 'vite';
2
2
  import { TypestylesIntegrationMode, TypestylesExtractOptions } from '@typestyles/build-runner';
3
- export { DEFAULT_EXTRACT_MODULE_CANDIDATES, TypestylesExtractOptions, discoverDefaultExtractModules, extractNamespaces } from '@typestyles/build-runner';
3
+ export { DEFAULT_EXTRACT_MODULE_CANDIDATES, TypestylesExtractOptions, discoverDefaultExtractModules, extractNamespaces, moduleNeedsOverrideHmr } from '@typestyles/build-runner';
4
4
 
5
5
  /**
6
6
  * Regex patterns to extract namespace strings from typestyles API calls.
@@ -42,6 +42,10 @@ interface TypestylesPluginOptions {
42
42
  */
43
43
  extract?: TypestylesExtractOptions;
44
44
  }
45
+ /** Public URL path for the extracted CSS asset (respects Vite `base`). */
46
+ declare function resolveTypestylesCssHref(base: string, fileName: string): string;
47
+ /** True when `html` already links the extracted stylesheet (avoids duplicate injection). */
48
+ declare function htmlLinksTypestylesCss(html: string, cssHref: string, fileName: string): boolean;
45
49
 
46
50
  /**
47
51
  * Vite plugin for typestyles HMR support.
@@ -53,4 +57,4 @@ interface TypestylesPluginOptions {
53
57
  */
54
58
  declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
55
59
 
56
- export { type TypestylesPluginOptions, typestylesPlugin as default };
60
+ export { type TypestylesPluginOptions, typestylesPlugin as default, htmlLinksTypestylesCss, resolveTypestylesCssHref };
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  import { resolve as resolvePath } from "path";
3
3
  import {
4
4
  extractNamespaces,
5
+ moduleNeedsOverrideHmr,
5
6
  reportDuplicateNamespaces,
6
7
  resolveExtractMode,
7
8
  resolveExtractModules,
@@ -11,9 +12,32 @@ import {
11
12
  DEFAULT_EXTRACT_MODULE_CANDIDATES,
12
13
  discoverDefaultExtractModules
13
14
  } from "@typestyles/build-runner";
15
+ import { moduleNeedsOverrideHmr as moduleNeedsOverrideHmr2 } from "@typestyles/build-runner";
14
16
  function resolveExtractModulesForVite(root, extract) {
15
17
  return resolveExtractModules(root, extract);
16
18
  }
19
+ function resolveTypestylesCssHref(base, fileName) {
20
+ const normalizedFileName = fileName.replace(/^\//, "");
21
+ const normalizedBase = base.replace(/\/$/, "") || "";
22
+ if (normalizedBase === "" || normalizedBase === "/") {
23
+ return `/${normalizedFileName}`;
24
+ }
25
+ return `${normalizedBase}/${normalizedFileName}`;
26
+ }
27
+ function htmlLinksTypestylesCss(html, cssHref, fileName) {
28
+ const bareName = fileName.replace(/^\//, "");
29
+ const linkPattern = /<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi;
30
+ const hrefPattern = /\bhref=["']([^"']+)["']/i;
31
+ for (const match of html.matchAll(linkPattern)) {
32
+ const hrefMatch = match[0].match(hrefPattern);
33
+ const href = hrefMatch?.[1];
34
+ if (!href) continue;
35
+ if (href === cssHref || href.endsWith(`/${bareName}`) || href === bareName || href === `./${bareName}`) {
36
+ return true;
37
+ }
38
+ }
39
+ return false;
40
+ }
17
41
  function typestylesPlugin(options = {}) {
18
42
  const { warnDuplicates = true, extract } = options;
19
43
  let resolvedExtractModules = [];
@@ -107,30 +131,67 @@ function typestylesPlugin(options = {}) {
107
131
  if (!/\.[jt]sx?$/.test(id)) return null;
108
132
  if (id.includes("node_modules")) return null;
109
133
  const { keys, prefixes } = extractNamespaces(code);
110
- if (keys.length === 0 && prefixes.length === 0) return null;
111
- if (warnDuplicates) {
134
+ const overrideHmr = moduleNeedsOverrideHmr(code);
135
+ if (keys.length === 0 && prefixes.length === 0 && !overrideHmr) return null;
136
+ if (warnDuplicates && prefixes.length > 0) {
112
137
  reportDuplicateNamespaces(moduleNamespaces, id, prefixes, this);
113
138
  }
114
- moduleNamespaces.set(id, { keys, prefixes });
139
+ if (keys.length > 0 || prefixes.length > 0) {
140
+ moduleNamespaces.set(id, { keys, prefixes });
141
+ }
115
142
  if (!isServe) {
116
143
  return null;
117
144
  }
118
145
  const keysJSON = JSON.stringify(keys);
119
146
  const prefixesJSON = JSON.stringify(prefixes);
120
- const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
147
+ const hmrImports = overrideHmr ? `import { invalidateKeys as __typestylesInvalidateKeys, createOverrideHmrSlot as __tsCreateOvHmr } from 'typestyles/hmr';
148
+ ` : `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
121
149
  `;
122
- const hmrCode = `
150
+ const overrideBootstrap = overrideHmr ? `const __tsOvHmr = __tsCreateOvHmr();
151
+ __tsOvHmr.activate();
152
+ ` : "";
153
+ const overrideTeardown = overrideHmr ? `
154
+ __tsOvHmr.deactivate();
155
+ ` : "";
156
+ const disposeBody = [
157
+ keys.length > 0 || prefixes.length > 0 ? `__typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});` : null,
158
+ overrideHmr ? `__tsOvHmr.dispose();` : null
159
+ ].filter(Boolean).join("\n ");
160
+ const hmrCode = `${overrideTeardown}
123
161
  if (import.meta.hot) {
124
162
  import.meta.hot.accept();
125
163
  import.meta.hot.dispose(() => {
126
- __typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
164
+ ${disposeBody}
127
165
  });
128
166
  }`;
129
167
  return {
130
- code: hmrImport + code + hmrCode,
168
+ code: hmrImports + overrideBootstrap + code + hmrCode,
131
169
  map: null
132
170
  };
133
171
  },
172
+ transformIndexHtml: {
173
+ order: "pre",
174
+ handler(html) {
175
+ if (resolvedExtractModules.length === 0) return;
176
+ if (resolvedMode === "runtime") return;
177
+ if (!resolvedConfig) return;
178
+ const fileName = extract?.fileName ?? "typestyles.css";
179
+ const href = resolveTypestylesCssHref(resolvedConfig.base, fileName);
180
+ if (htmlLinksTypestylesCss(html, href, fileName)) {
181
+ return;
182
+ }
183
+ return [
184
+ {
185
+ tag: "link",
186
+ attrs: {
187
+ rel: "stylesheet",
188
+ href
189
+ },
190
+ injectTo: "head"
191
+ }
192
+ ];
193
+ }
194
+ },
134
195
  async generateBundle() {
135
196
  if (!isBuildCommand) return;
136
197
  if (resolvedMode === "runtime") return;
@@ -160,5 +221,8 @@ export {
160
221
  DEFAULT_EXTRACT_MODULE_CANDIDATES,
161
222
  typestylesPlugin as default,
162
223
  discoverDefaultExtractModules,
163
- extractNamespaces
224
+ extractNamespaces,
225
+ htmlLinksTypestylesCss,
226
+ moduleNeedsOverrideHmr2 as moduleNeedsOverrideHmr,
227
+ resolveTypestylesCssHref
164
228
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typestyles/vite",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "description": "Vite plugin for typestyles HMR support",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -43,10 +43,10 @@
43
43
  "typescript": "^5.4.0",
44
44
  "vite": "^6.0.0",
45
45
  "vitest": "^3.0.0",
46
- "typestyles": "0.8.2"
46
+ "typestyles": "0.25.0"
47
47
  },
48
48
  "dependencies": {
49
- "@typestyles/build-runner": "0.5.2"
49
+ "@typestyles/build-runner": "0.5.3"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup src/index.ts --format esm,cjs --dts --clean --external typestyles,@typestyles/build-runner",