@typestyles/vite 0.4.3 → 0.4.4

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,7 +23,9 @@ __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
+ resolveTypestylesCssHref: () => resolveTypestylesCssHref
27
29
  });
28
30
  module.exports = __toCommonJS(index_exports);
29
31
  var import_node_path = require("path");
@@ -32,6 +34,28 @@ var import_build_runner2 = require("@typestyles/build-runner");
32
34
  function resolveExtractModulesForVite(root, extract) {
33
35
  return (0, import_build_runner.resolveExtractModules)(root, extract);
34
36
  }
37
+ function resolveTypestylesCssHref(base, fileName) {
38
+ const normalizedFileName = fileName.replace(/^\//, "");
39
+ const normalizedBase = base.replace(/\/$/, "") || "";
40
+ if (normalizedBase === "" || normalizedBase === "/") {
41
+ return `/${normalizedFileName}`;
42
+ }
43
+ return `${normalizedBase}/${normalizedFileName}`;
44
+ }
45
+ function htmlLinksTypestylesCss(html, cssHref, fileName) {
46
+ const bareName = fileName.replace(/^\//, "");
47
+ const linkPattern = /<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi;
48
+ const hrefPattern = /\bhref=["']([^"']+)["']/i;
49
+ for (const match of html.matchAll(linkPattern)) {
50
+ const hrefMatch = match[0].match(hrefPattern);
51
+ const href = hrefMatch?.[1];
52
+ if (!href) continue;
53
+ if (href === cssHref || href.endsWith(`/${bareName}`) || href === bareName || href === `./${bareName}`) {
54
+ return true;
55
+ }
56
+ }
57
+ return false;
58
+ }
35
59
  function typestylesPlugin(options = {}) {
36
60
  const { warnDuplicates = true, extract } = options;
37
61
  let resolvedExtractModules = [];
@@ -149,6 +173,29 @@ if (import.meta.hot) {
149
173
  map: null
150
174
  };
151
175
  },
176
+ transformIndexHtml: {
177
+ order: "pre",
178
+ handler(html) {
179
+ if (resolvedExtractModules.length === 0) return;
180
+ if (resolvedMode === "runtime") return;
181
+ if (!resolvedConfig) return;
182
+ const fileName = extract?.fileName ?? "typestyles.css";
183
+ const href = resolveTypestylesCssHref(resolvedConfig.base, fileName);
184
+ if (htmlLinksTypestylesCss(html, href, fileName)) {
185
+ return;
186
+ }
187
+ return [
188
+ {
189
+ tag: "link",
190
+ attrs: {
191
+ rel: "stylesheet",
192
+ href
193
+ },
194
+ injectTo: "head"
195
+ }
196
+ ];
197
+ }
198
+ },
152
199
  async generateBundle() {
153
200
  if (!isBuildCommand) return;
154
201
  if (resolvedMode === "runtime") return;
@@ -178,5 +225,7 @@ if (import.meta.hot) {
178
225
  0 && (module.exports = {
179
226
  DEFAULT_EXTRACT_MODULE_CANDIDATES,
180
227
  discoverDefaultExtractModules,
181
- extractNamespaces
228
+ extractNamespaces,
229
+ htmlLinksTypestylesCss,
230
+ resolveTypestylesCssHref
182
231
  });
package/dist/index.d.cts CHANGED
@@ -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
@@ -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
@@ -14,6 +14,28 @@ import {
14
14
  function resolveExtractModulesForVite(root, extract) {
15
15
  return resolveExtractModules(root, extract);
16
16
  }
17
+ function resolveTypestylesCssHref(base, fileName) {
18
+ const normalizedFileName = fileName.replace(/^\//, "");
19
+ const normalizedBase = base.replace(/\/$/, "") || "";
20
+ if (normalizedBase === "" || normalizedBase === "/") {
21
+ return `/${normalizedFileName}`;
22
+ }
23
+ return `${normalizedBase}/${normalizedFileName}`;
24
+ }
25
+ function htmlLinksTypestylesCss(html, cssHref, fileName) {
26
+ const bareName = fileName.replace(/^\//, "");
27
+ const linkPattern = /<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi;
28
+ const hrefPattern = /\bhref=["']([^"']+)["']/i;
29
+ for (const match of html.matchAll(linkPattern)) {
30
+ const hrefMatch = match[0].match(hrefPattern);
31
+ const href = hrefMatch?.[1];
32
+ if (!href) continue;
33
+ if (href === cssHref || href.endsWith(`/${bareName}`) || href === bareName || href === `./${bareName}`) {
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ }
17
39
  function typestylesPlugin(options = {}) {
18
40
  const { warnDuplicates = true, extract } = options;
19
41
  let resolvedExtractModules = [];
@@ -131,6 +153,29 @@ if (import.meta.hot) {
131
153
  map: null
132
154
  };
133
155
  },
156
+ transformIndexHtml: {
157
+ order: "pre",
158
+ handler(html) {
159
+ if (resolvedExtractModules.length === 0) return;
160
+ if (resolvedMode === "runtime") return;
161
+ if (!resolvedConfig) return;
162
+ const fileName = extract?.fileName ?? "typestyles.css";
163
+ const href = resolveTypestylesCssHref(resolvedConfig.base, fileName);
164
+ if (htmlLinksTypestylesCss(html, href, fileName)) {
165
+ return;
166
+ }
167
+ return [
168
+ {
169
+ tag: "link",
170
+ attrs: {
171
+ rel: "stylesheet",
172
+ href
173
+ },
174
+ injectTo: "head"
175
+ }
176
+ ];
177
+ }
178
+ },
134
179
  async generateBundle() {
135
180
  if (!isBuildCommand) return;
136
181
  if (resolvedMode === "runtime") return;
@@ -160,5 +205,7 @@ export {
160
205
  DEFAULT_EXTRACT_MODULE_CANDIDATES,
161
206
  typestylesPlugin as default,
162
207
  discoverDefaultExtractModules,
163
- extractNamespaces
208
+ extractNamespaces,
209
+ htmlLinksTypestylesCss,
210
+ resolveTypestylesCssHref
164
211
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typestyles/vite",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Vite plugin for typestyles HMR support",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -43,7 +43,7 @@
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.9.0"
47
47
  },
48
48
  "dependencies": {
49
49
  "@typestyles/build-runner": "0.5.2"