@typestyles/vite 0.0.0-unstable.22588769eb5a → 0.0.0-unstable.29db9c6fb45f

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
@@ -25,7 +25,6 @@ __export(index_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(index_exports);
27
27
  var import_build_runner = require("@typestyles/build-runner");
28
- var STYLES_CREATE_RE = /styles\.create\(\s*['"]([^'"]+)['"]/g;
29
28
  var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
30
29
  var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
31
30
  var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
@@ -36,9 +35,6 @@ var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles[
36
35
  function extractNamespaces(code) {
37
36
  const keys = [];
38
37
  const prefixes = [];
39
- for (const match of code.matchAll(STYLES_CREATE_RE)) {
40
- prefixes.push(`.${match[1]}-`);
41
- }
42
38
  for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
43
39
  prefixes.push(`.${match[1]}-`);
44
40
  }
@@ -60,10 +56,14 @@ function extractNamespaces(code) {
60
56
  return { keys, prefixes };
61
57
  }
62
58
  function typestylesPlugin(options = {}) {
63
- const { warnDuplicates = true, mode = "runtime", extract } = options;
59
+ const { warnDuplicates = true, extract } = options;
60
+ const extractModules = extract?.modules ?? [];
61
+ const mode = options.mode ?? (extractModules.length > 0 ? "build" : "runtime");
64
62
  const moduleNamespaces = /* @__PURE__ */ new Map();
65
63
  let resolvedConfig = null;
66
64
  let isBuildCommand = false;
65
+ let devExtractCss = null;
66
+ let devExtractInFlight = null;
67
67
  return {
68
68
  name: "typestyles",
69
69
  enforce: "pre",
@@ -81,6 +81,64 @@ function typestylesPlugin(options = {}) {
81
81
  configResolved(config) {
82
82
  resolvedConfig = config;
83
83
  },
84
+ configureServer(server) {
85
+ if (!extract || extractModules.length === 0) return;
86
+ if (mode === "runtime") return;
87
+ const fileName = extract.fileName ?? "typestyles.css";
88
+ const invalidateDevExtract = () => {
89
+ devExtractCss = null;
90
+ devExtractInFlight = null;
91
+ };
92
+ server.watcher.on("change", invalidateDevExtract);
93
+ server.watcher.on("add", invalidateDevExtract);
94
+ server.watcher.on("unlink", invalidateDevExtract);
95
+ server.middlewares.use(async (req, res, next) => {
96
+ if (req.method !== "GET" && req.method !== "HEAD") {
97
+ next();
98
+ return;
99
+ }
100
+ const config = resolvedConfig;
101
+ if (!config) {
102
+ next();
103
+ return;
104
+ }
105
+ const url = req.url?.split("?")[0] ?? "";
106
+ const base = config.base.replace(/\/$/, "") || "";
107
+ const expectedPath = base === "" || base === "/" ? `/${fileName.replace(/^\//, "")}` : `${base}/${fileName.replace(/^\//, "")}`;
108
+ if (url !== expectedPath) {
109
+ next();
110
+ return;
111
+ }
112
+ const root = config.root ?? process.cwd();
113
+ try {
114
+ if (devExtractCss !== null) {
115
+ res.statusCode = 200;
116
+ res.setHeader("Content-Type", "text/css; charset=utf-8");
117
+ res.setHeader("Cache-Control", "no-cache");
118
+ res.end(devExtractCss);
119
+ return;
120
+ }
121
+ if (!devExtractInFlight) {
122
+ devExtractInFlight = (0, import_build_runner.runTypestylesBuild)({
123
+ root,
124
+ modules: extractModules
125
+ }).then((css2) => {
126
+ devExtractCss = css2;
127
+ return css2;
128
+ }).finally(() => {
129
+ devExtractInFlight = null;
130
+ });
131
+ }
132
+ const css = await devExtractInFlight;
133
+ res.statusCode = 200;
134
+ res.setHeader("Content-Type", "text/css; charset=utf-8");
135
+ res.setHeader("Cache-Control", "no-cache");
136
+ res.end(css);
137
+ } catch (err) {
138
+ next(err);
139
+ }
140
+ });
141
+ },
84
142
  transform(code, id) {
85
143
  if (!/\.[jt]sx?$/.test(id)) return null;
86
144
  if (id.includes("node_modules")) return null;
@@ -120,14 +178,14 @@ if (import.meta.hot) {
120
178
  async generateBundle() {
121
179
  if (!isBuildCommand) return;
122
180
  if (mode === "runtime") return;
123
- if (!extract || !extract.modules.length) return;
181
+ if (!extract || !extractModules.length) return;
124
182
  if (!resolvedConfig) return;
125
183
  const root = resolvedConfig.root ?? process.cwd();
126
184
  const fileName = extract.fileName ?? "typestyles.css";
127
185
  try {
128
186
  const css = await (0, import_build_runner.runTypestylesBuild)({
129
187
  root,
130
- modules: extract.modules
188
+ modules: extractModules
131
189
  });
132
190
  this.emitFile({
133
191
  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
  /**
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
  /**
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  // src/index.ts
2
2
  import { runTypestylesBuild } from "@typestyles/build-runner";
3
- var STYLES_CREATE_RE = /styles\.create\(\s*['"]([^'"]+)['"]/g;
4
3
  var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
5
4
  var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
6
5
  var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
@@ -11,9 +10,6 @@ var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles[
11
10
  function extractNamespaces(code) {
12
11
  const keys = [];
13
12
  const prefixes = [];
14
- for (const match of code.matchAll(STYLES_CREATE_RE)) {
15
- prefixes.push(`.${match[1]}-`);
16
- }
17
13
  for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
18
14
  prefixes.push(`.${match[1]}-`);
19
15
  }
@@ -35,10 +31,14 @@ function extractNamespaces(code) {
35
31
  return { keys, prefixes };
36
32
  }
37
33
  function typestylesPlugin(options = {}) {
38
- const { warnDuplicates = true, mode = "runtime", extract } = options;
34
+ const { warnDuplicates = true, extract } = options;
35
+ const extractModules = extract?.modules ?? [];
36
+ const mode = options.mode ?? (extractModules.length > 0 ? "build" : "runtime");
39
37
  const moduleNamespaces = /* @__PURE__ */ new Map();
40
38
  let resolvedConfig = null;
41
39
  let isBuildCommand = false;
40
+ let devExtractCss = null;
41
+ let devExtractInFlight = null;
42
42
  return {
43
43
  name: "typestyles",
44
44
  enforce: "pre",
@@ -56,6 +56,64 @@ function typestylesPlugin(options = {}) {
56
56
  configResolved(config) {
57
57
  resolvedConfig = config;
58
58
  },
59
+ configureServer(server) {
60
+ if (!extract || extractModules.length === 0) return;
61
+ if (mode === "runtime") return;
62
+ const fileName = extract.fileName ?? "typestyles.css";
63
+ const invalidateDevExtract = () => {
64
+ devExtractCss = null;
65
+ devExtractInFlight = null;
66
+ };
67
+ server.watcher.on("change", invalidateDevExtract);
68
+ server.watcher.on("add", invalidateDevExtract);
69
+ server.watcher.on("unlink", invalidateDevExtract);
70
+ server.middlewares.use(async (req, res, next) => {
71
+ if (req.method !== "GET" && req.method !== "HEAD") {
72
+ next();
73
+ return;
74
+ }
75
+ const config = resolvedConfig;
76
+ if (!config) {
77
+ next();
78
+ return;
79
+ }
80
+ const url = req.url?.split("?")[0] ?? "";
81
+ const base = config.base.replace(/\/$/, "") || "";
82
+ const expectedPath = base === "" || base === "/" ? `/${fileName.replace(/^\//, "")}` : `${base}/${fileName.replace(/^\//, "")}`;
83
+ if (url !== expectedPath) {
84
+ next();
85
+ return;
86
+ }
87
+ const root = config.root ?? process.cwd();
88
+ try {
89
+ if (devExtractCss !== null) {
90
+ res.statusCode = 200;
91
+ res.setHeader("Content-Type", "text/css; charset=utf-8");
92
+ res.setHeader("Cache-Control", "no-cache");
93
+ res.end(devExtractCss);
94
+ return;
95
+ }
96
+ if (!devExtractInFlight) {
97
+ devExtractInFlight = runTypestylesBuild({
98
+ root,
99
+ modules: extractModules
100
+ }).then((css2) => {
101
+ devExtractCss = css2;
102
+ return css2;
103
+ }).finally(() => {
104
+ devExtractInFlight = null;
105
+ });
106
+ }
107
+ const css = await devExtractInFlight;
108
+ res.statusCode = 200;
109
+ res.setHeader("Content-Type", "text/css; charset=utf-8");
110
+ res.setHeader("Cache-Control", "no-cache");
111
+ res.end(css);
112
+ } catch (err) {
113
+ next(err);
114
+ }
115
+ });
116
+ },
59
117
  transform(code, id) {
60
118
  if (!/\.[jt]sx?$/.test(id)) return null;
61
119
  if (id.includes("node_modules")) return null;
@@ -95,14 +153,14 @@ if (import.meta.hot) {
95
153
  async generateBundle() {
96
154
  if (!isBuildCommand) return;
97
155
  if (mode === "runtime") return;
98
- if (!extract || !extract.modules.length) return;
156
+ if (!extract || !extractModules.length) return;
99
157
  if (!resolvedConfig) return;
100
158
  const root = resolvedConfig.root ?? process.cwd();
101
159
  const fileName = extract.fileName ?? "typestyles.css";
102
160
  try {
103
161
  const css = await runTypestylesBuild({
104
162
  root,
105
- modules: extract.modules
163
+ modules: extractModules
106
164
  });
107
165
  this.emitFile({
108
166
  type: "asset",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typestyles/vite",
3
- "version": "0.0.0-unstable.22588769eb5a",
3
+ "version": "0.0.0-unstable.29db9c6fb45f",
4
4
  "description": "Vite plugin for typestyles HMR support",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -38,18 +38,20 @@
38
38
  "vite": ">=5.0.0"
39
39
  },
40
40
  "devDependencies": {
41
+ "eslint": "^9.39.2",
41
42
  "tsup": "^8.0.0",
42
43
  "typescript": "^5.4.0",
43
- "typestyles": "0.0.0-unstable.22588769eb5a",
44
+ "typestyles": "0.0.0-unstable.29db9c6fb45f",
44
45
  "vite": "^6.0.0",
45
46
  "vitest": "^3.0.0"
46
47
  },
47
48
  "dependencies": {
48
- "@typestyles/build-runner": "0.0.0-unstable.22588769eb5a"
49
+ "@typestyles/build-runner": "0.0.0-unstable.29db9c6fb45f"
49
50
  },
50
51
  "scripts": {
51
52
  "build": "tsup src/index.ts --format esm,cjs --dts --clean --external typestyles,@typestyles/build-runner",
52
53
  "test": "vitest run",
54
+ "lint": "eslint .",
53
55
  "typecheck": "tsc --noEmit"
54
56
  }
55
57
  }