@typestyles/vite 0.0.0-unstable.bad151c39628 → 0.0.0-unstable.c467888a22b7

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,20 +25,15 @@ __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;
32
31
  var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
33
32
  var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
34
33
  var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
35
- var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
36
34
  function extractNamespaces(code) {
37
35
  const keys = [];
38
36
  const prefixes = [];
39
- for (const match of code.matchAll(STYLES_CREATE_RE)) {
40
- prefixes.push(`.${match[1]}-`);
41
- }
42
37
  for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
43
38
  prefixes.push(`.${match[1]}-`);
44
39
  }
@@ -60,10 +55,15 @@ function extractNamespaces(code) {
60
55
  return { keys, prefixes };
61
56
  }
62
57
  function typestylesPlugin(options = {}) {
63
- 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");
64
61
  const moduleNamespaces = /* @__PURE__ */ new Map();
65
62
  let resolvedConfig = null;
66
63
  let isBuildCommand = false;
64
+ let isServe = false;
65
+ let devExtractCss = null;
66
+ let devExtractInFlight = null;
67
67
  return {
68
68
  name: "typestyles",
69
69
  enforce: "pre",
@@ -80,11 +80,69 @@ function typestylesPlugin(options = {}) {
80
80
  },
81
81
  configResolved(config) {
82
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
+ });
83
142
  },
84
143
  transform(code, id) {
85
144
  if (!/\.[jt]sx?$/.test(id)) return null;
86
145
  if (id.includes("node_modules")) return null;
87
- if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
88
146
  const { keys, prefixes } = extractNamespaces(code);
89
147
  if (keys.length === 0 && prefixes.length === 0) return null;
90
148
  if (warnDuplicates) {
@@ -101,33 +159,36 @@ function typestylesPlugin(options = {}) {
101
159
  }
102
160
  }
103
161
  moduleNamespaces.set(id, { keys, prefixes });
162
+ if (!isServe) {
163
+ return null;
164
+ }
104
165
  const keysJSON = JSON.stringify(keys);
105
166
  const prefixesJSON = JSON.stringify(prefixes);
167
+ const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
168
+ `;
106
169
  const hmrCode = `
107
170
  if (import.meta.hot) {
108
- import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
109
- import.meta.hot.accept();
110
- import.meta.hot.dispose(() => {
111
- __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
112
- });
171
+ import.meta.hot.accept();
172
+ import.meta.hot.dispose(() => {
173
+ __typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
113
174
  });
114
175
  }`;
115
176
  return {
116
- code: code + hmrCode,
177
+ code: hmrImport + code + hmrCode,
117
178
  map: null
118
179
  };
119
180
  },
120
181
  async generateBundle() {
121
182
  if (!isBuildCommand) return;
122
183
  if (mode === "runtime") return;
123
- if (!extract || !extract.modules.length) return;
184
+ if (!extract || !extractModules.length) return;
124
185
  if (!resolvedConfig) return;
125
186
  const root = resolvedConfig.root ?? process.cwd();
126
187
  const fileName = extract.fileName ?? "typestyles.css";
127
188
  try {
128
189
  const css = await (0, import_build_runner.runTypestylesBuild)({
129
190
  root,
130
- modules: extract.modules
191
+ modules: extractModules
131
192
  });
132
193
  this.emitFile({
133
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
@@ -1,19 +1,14 @@
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;
7
6
  var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
8
7
  var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
9
8
  var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
10
- var TYPESTYLES_IMPORT_RE = /(?:from\s+|import\s+|require\s*\(\s*)['"]typestyles['"]/;
11
9
  function extractNamespaces(code) {
12
10
  const keys = [];
13
11
  const prefixes = [];
14
- for (const match of code.matchAll(STYLES_CREATE_RE)) {
15
- prefixes.push(`.${match[1]}-`);
16
- }
17
12
  for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
18
13
  prefixes.push(`.${match[1]}-`);
19
14
  }
@@ -35,10 +30,15 @@ function extractNamespaces(code) {
35
30
  return { keys, prefixes };
36
31
  }
37
32
  function typestylesPlugin(options = {}) {
38
- 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");
39
36
  const moduleNamespaces = /* @__PURE__ */ new Map();
40
37
  let resolvedConfig = null;
41
38
  let isBuildCommand = false;
39
+ let isServe = false;
40
+ let devExtractCss = null;
41
+ let devExtractInFlight = null;
42
42
  return {
43
43
  name: "typestyles",
44
44
  enforce: "pre",
@@ -55,11 +55,69 @@ function typestylesPlugin(options = {}) {
55
55
  },
56
56
  configResolved(config) {
57
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
+ });
58
117
  },
59
118
  transform(code, id) {
60
119
  if (!/\.[jt]sx?$/.test(id)) return null;
61
120
  if (id.includes("node_modules")) return null;
62
- if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
63
121
  const { keys, prefixes } = extractNamespaces(code);
64
122
  if (keys.length === 0 && prefixes.length === 0) return null;
65
123
  if (warnDuplicates) {
@@ -76,33 +134,36 @@ function typestylesPlugin(options = {}) {
76
134
  }
77
135
  }
78
136
  moduleNamespaces.set(id, { keys, prefixes });
137
+ if (!isServe) {
138
+ return null;
139
+ }
79
140
  const keysJSON = JSON.stringify(keys);
80
141
  const prefixesJSON = JSON.stringify(prefixes);
142
+ const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
143
+ `;
81
144
  const hmrCode = `
82
145
  if (import.meta.hot) {
83
- import('typestyles/hmr').then(({ invalidateKeys: __typestyles_invalidateKeys }) => {
84
- import.meta.hot.accept();
85
- import.meta.hot.dispose(() => {
86
- __typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
87
- });
146
+ import.meta.hot.accept();
147
+ import.meta.hot.dispose(() => {
148
+ __typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
88
149
  });
89
150
  }`;
90
151
  return {
91
- code: code + hmrCode,
152
+ code: hmrImport + code + hmrCode,
92
153
  map: null
93
154
  };
94
155
  },
95
156
  async generateBundle() {
96
157
  if (!isBuildCommand) return;
97
158
  if (mode === "runtime") return;
98
- if (!extract || !extract.modules.length) return;
159
+ if (!extract || !extractModules.length) return;
99
160
  if (!resolvedConfig) return;
100
161
  const root = resolvedConfig.root ?? process.cwd();
101
162
  const fileName = extract.fileName ?? "typestyles.css";
102
163
  try {
103
164
  const css = await runTypestylesBuild({
104
165
  root,
105
- modules: extract.modules
166
+ modules: extractModules
106
167
  });
107
168
  this.emitFile({
108
169
  type: "asset",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typestyles/vite",
3
- "version": "0.0.0-unstable.bad151c39628",
3
+ "version": "0.0.0-unstable.c467888a22b7",
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.bad151c39628",
44
+ "typestyles": "0.0.0-unstable.c467888a22b7",
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.bad151c39628"
49
+ "@typestyles/build-runner": "0.0.0-unstable.c467888a22b7"
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
  }