@typestyles/vite 0.0.0-unstable.5bbf3d344ebb → 0.0.0-unstable.6d8114ad35fb

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 = [];
@@ -62,6 +61,9 @@ function typestylesPlugin(options = {}) {
62
61
  const moduleNamespaces = /* @__PURE__ */ new Map();
63
62
  let resolvedConfig = null;
64
63
  let isBuildCommand = false;
64
+ let isServe = false;
65
+ let devExtractCss = null;
66
+ let devExtractInFlight = null;
65
67
  return {
66
68
  name: "typestyles",
67
69
  enforce: "pre",
@@ -78,11 +80,69 @@ function typestylesPlugin(options = {}) {
78
80
  },
79
81
  configResolved(config) {
80
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
+ });
81
142
  },
82
143
  transform(code, id) {
83
144
  if (!/\.[jt]sx?$/.test(id)) return null;
84
145
  if (id.includes("node_modules")) return null;
85
- if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
86
146
  const { keys, prefixes } = extractNamespaces(code);
87
147
  if (keys.length === 0 && prefixes.length === 0) return null;
88
148
  if (warnDuplicates) {
@@ -99,19 +159,22 @@ function typestylesPlugin(options = {}) {
99
159
  }
100
160
  }
101
161
  moduleNamespaces.set(id, { keys, prefixes });
162
+ if (!isServe) {
163
+ return null;
164
+ }
102
165
  const keysJSON = JSON.stringify(keys);
103
166
  const prefixesJSON = JSON.stringify(prefixes);
167
+ const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
168
+ `;
104
169
  const hmrCode = `
105
170
  if (import.meta.hot) {
106
- import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
107
- import.meta.hot.accept();
108
- import.meta.hot.dispose(() => {
109
- __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
110
- });
171
+ import.meta.hot.accept();
172
+ import.meta.hot.dispose(() => {
173
+ __typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
111
174
  });
112
175
  }`;
113
176
  return {
114
- code: code + hmrCode,
177
+ code: hmrImport + code + hmrCode,
115
178
  map: null
116
179
  };
117
180
  },
package/dist/index.d.cts CHANGED
@@ -21,8 +21,10 @@ interface TypestylesPluginOptions {
21
21
  * - `"hybrid"`: extract on build and keep runtime-compatible behavior.
22
22
  *
23
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`), and production emits
25
- * the CSS file. With no `extract` config, defaults to `"runtime"`.
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"`.
26
28
  */
27
29
  mode?: 'runtime' | 'build' | 'hybrid';
28
30
  /**
@@ -41,9 +43,10 @@ declare function extractNamespaces(code: string): {
41
43
  /**
42
44
  * Vite plugin for typestyles HMR support.
43
45
  *
44
- * When a module that uses typestyles is edited, this plugin injects HMR
45
- * accept/dispose handlers that invalidate the module's style registrations
46
- * 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.
47
50
  */
48
51
  declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
49
52
 
package/dist/index.d.ts CHANGED
@@ -21,8 +21,10 @@ interface TypestylesPluginOptions {
21
21
  * - `"hybrid"`: extract on build and keep runtime-compatible behavior.
22
22
  *
23
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`), and production emits
25
- * the CSS file. With no `extract` config, defaults to `"runtime"`.
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"`.
26
28
  */
27
29
  mode?: 'runtime' | 'build' | 'hybrid';
28
30
  /**
@@ -41,9 +43,10 @@ declare function extractNamespaces(code: string): {
41
43
  /**
42
44
  * Vite plugin for typestyles HMR support.
43
45
  *
44
- * When a module that uses typestyles is edited, this plugin injects HMR
45
- * accept/dispose handlers that invalidate the module's style registrations
46
- * 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.
47
50
  */
48
51
  declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
49
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 = [];
@@ -37,6 +36,9 @@ function typestylesPlugin(options = {}) {
37
36
  const moduleNamespaces = /* @__PURE__ */ new Map();
38
37
  let resolvedConfig = null;
39
38
  let isBuildCommand = false;
39
+ let isServe = false;
40
+ let devExtractCss = null;
41
+ let devExtractInFlight = null;
40
42
  return {
41
43
  name: "typestyles",
42
44
  enforce: "pre",
@@ -53,11 +55,69 @@ function typestylesPlugin(options = {}) {
53
55
  },
54
56
  configResolved(config) {
55
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
+ });
56
117
  },
57
118
  transform(code, id) {
58
119
  if (!/\.[jt]sx?$/.test(id)) return null;
59
120
  if (id.includes("node_modules")) return null;
60
- if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
61
121
  const { keys, prefixes } = extractNamespaces(code);
62
122
  if (keys.length === 0 && prefixes.length === 0) return null;
63
123
  if (warnDuplicates) {
@@ -74,19 +134,22 @@ function typestylesPlugin(options = {}) {
74
134
  }
75
135
  }
76
136
  moduleNamespaces.set(id, { keys, prefixes });
137
+ if (!isServe) {
138
+ return null;
139
+ }
77
140
  const keysJSON = JSON.stringify(keys);
78
141
  const prefixesJSON = JSON.stringify(prefixes);
142
+ const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
143
+ `;
79
144
  const hmrCode = `
80
145
  if (import.meta.hot) {
81
- import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
82
- import.meta.hot.accept();
83
- import.meta.hot.dispose(() => {
84
- __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
85
- });
146
+ import.meta.hot.accept();
147
+ import.meta.hot.dispose(() => {
148
+ __typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
86
149
  });
87
150
  }`;
88
151
  return {
89
- code: code + hmrCode,
152
+ code: hmrImport + code + hmrCode,
90
153
  map: null
91
154
  };
92
155
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typestyles/vite",
3
- "version": "0.0.0-unstable.5bbf3d344ebb",
3
+ "version": "0.0.0-unstable.6d8114ad35fb",
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.5bbf3d344ebb",
44
+ "typestyles": "0.0.0-unstable.6d8114ad35fb",
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.5bbf3d344ebb"
49
+ "@typestyles/build-runner": "0.0.0-unstable.6d8114ad35fb"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup src/index.ts --format esm,cjs --dts --clean --external typestyles,@typestyles/build-runner",