@typestyles/vite 0.0.0-unstable.835df2a09a6d → 0.0.0-unstable.921c88bcff7e

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
@@ -20,23 +20,37 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ DEFAULT_EXTRACT_MODULE_CANDIDATES: () => import_build_runner2.DEFAULT_EXTRACT_MODULE_CANDIDATES,
23
24
  default: () => typestylesPlugin,
25
+ discoverDefaultExtractModules: () => import_build_runner2.discoverDefaultExtractModules,
24
26
  extractNamespaces: () => extractNamespaces
25
27
  });
26
28
  module.exports = __toCommonJS(index_exports);
29
+ var import_node_path = require("path");
27
30
  var import_build_runner = require("@typestyles/build-runner");
31
+ var import_build_runner2 = require("@typestyles/build-runner");
28
32
  var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
33
+ var STYLES_CLASS_RE = /styles\.class\(\s*['"]([^'"]+)['"]/g;
29
34
  var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
30
35
  var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
31
36
  var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
32
37
  var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
33
38
  var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
39
+ function resolveExtractModules(root, extract) {
40
+ if (extract?.modules !== void 0) {
41
+ return extract.modules;
42
+ }
43
+ return (0, import_build_runner.discoverDefaultExtractModules)(root);
44
+ }
34
45
  function extractNamespaces(code) {
35
46
  const keys = [];
36
47
  const prefixes = [];
37
48
  for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
38
49
  prefixes.push(`.${match[1]}-`);
39
50
  }
51
+ for (const match of code.matchAll(STYLES_CLASS_RE)) {
52
+ prefixes.push(`.${match[1]}-`);
53
+ }
40
54
  for (const match of code.matchAll(TOKENS_CREATE_RE)) {
41
55
  keys.push(`tokens:${match[1]}`);
42
56
  }
@@ -56,8 +70,8 @@ function extractNamespaces(code) {
56
70
  }
57
71
  function typestylesPlugin(options = {}) {
58
72
  const { warnDuplicates = true, extract } = options;
59
- const extractModules = extract?.modules ?? [];
60
- const mode = options.mode ?? (extractModules.length > 0 ? "build" : "runtime");
73
+ let resolvedExtractModules = [];
74
+ let resolvedMode = "runtime";
61
75
  const moduleNamespaces = /* @__PURE__ */ new Map();
62
76
  let resolvedConfig = null;
63
77
  let isBuildCommand = false;
@@ -69,7 +83,10 @@ function typestylesPlugin(options = {}) {
69
83
  enforce: "pre",
70
84
  config(config, env) {
71
85
  isBuildCommand = env.command === "build";
72
- if (env.command === "build" && (mode === "build" || mode === "hybrid")) {
86
+ const root = (0, import_node_path.resolve)(config.root ?? process.cwd());
87
+ resolvedExtractModules = resolveExtractModules(root, extract);
88
+ resolvedMode = options.mode ?? (resolvedExtractModules.length > 0 ? "build" : "runtime");
89
+ if (env.command === "build" && (resolvedMode === "build" || resolvedMode === "hybrid")) {
73
90
  config.define = {
74
91
  ...config.define ?? {},
75
92
  // Inlined by the bundler so typestyles sheet skips creating <style> in production
@@ -83,9 +100,9 @@ function typestylesPlugin(options = {}) {
83
100
  isServe = config.command === "serve";
84
101
  },
85
102
  configureServer(server) {
86
- if (!extract || extractModules.length === 0) return;
87
- if (mode === "runtime") return;
88
- const fileName = extract.fileName ?? "typestyles.css";
103
+ if (resolvedExtractModules.length === 0) return;
104
+ if (resolvedMode === "runtime") return;
105
+ const fileName = extract?.fileName ?? "typestyles.css";
89
106
  const invalidateDevExtract = () => {
90
107
  devExtractCss = null;
91
108
  devExtractInFlight = null;
@@ -122,7 +139,7 @@ function typestylesPlugin(options = {}) {
122
139
  if (!devExtractInFlight) {
123
140
  devExtractInFlight = (0, import_build_runner.runTypestylesBuild)({
124
141
  root,
125
- modules: extractModules
142
+ modules: resolvedExtractModules
126
143
  }).then((css2) => {
127
144
  devExtractCss = css2;
128
145
  return css2;
@@ -151,8 +168,8 @@ function typestylesPlugin(options = {}) {
151
168
  for (const prefix of prefixes) {
152
169
  if (other.prefixes.includes(prefix)) {
153
170
  const ns = prefix.slice(1, -1);
154
- this.warn(
155
- `Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions.`
171
+ this.error(
172
+ `[typestyles] Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions. Use a distinct name or isolate with createStyles({ scopeId: fileScopeId(import.meta) }).`
156
173
  );
157
174
  }
158
175
  }
@@ -180,15 +197,15 @@ if (import.meta.hot) {
180
197
  },
181
198
  async generateBundle() {
182
199
  if (!isBuildCommand) return;
183
- if (mode === "runtime") return;
184
- if (!extract || !extractModules.length) return;
200
+ if (resolvedMode === "runtime") return;
201
+ if (!resolvedExtractModules.length) return;
185
202
  if (!resolvedConfig) return;
186
203
  const root = resolvedConfig.root ?? process.cwd();
187
- const fileName = extract.fileName ?? "typestyles.css";
204
+ const fileName = extract?.fileName ?? "typestyles.css";
188
205
  try {
189
206
  const css = await (0, import_build_runner.runTypestylesBuild)({
190
207
  root,
191
- modules: extractModules
208
+ modules: resolvedExtractModules
192
209
  });
193
210
  this.emitFile({
194
211
  type: "asset",
@@ -205,5 +222,7 @@ if (import.meta.hot) {
205
222
  }
206
223
  // Annotate the CommonJS export names for ESM import in node:
207
224
  0 && (module.exports = {
225
+ DEFAULT_EXTRACT_MODULE_CANDIDATES,
226
+ discoverDefaultExtractModules,
208
227
  extractNamespaces
209
228
  });
package/dist/index.d.cts CHANGED
@@ -1,18 +1,26 @@
1
1
  import { Plugin } from 'vite';
2
+ export { DEFAULT_EXTRACT_MODULE_CANDIDATES, discoverDefaultExtractModules } from '@typestyles/build-runner';
2
3
 
3
4
  interface TypestylesExtractOptions {
4
5
  /**
5
6
  * Modules that should be imported during build to register styles.
6
7
  * Paths are resolved relative to Vite's project root.
8
+ *
9
+ * When omitted, the plugin looks for the first existing file among
10
+ * {@link DEFAULT_EXTRACT_MODULE_CANDIDATES} under the project root (same resolution as when
11
+ * `extract` itself is omitted).
7
12
  */
8
- modules: string[];
13
+ modules?: string[];
9
14
  /**
10
15
  * Output CSS filename (in the build assets). Defaults to "typestyles.css".
11
16
  */
12
17
  fileName?: string;
13
18
  }
14
19
  interface TypestylesPluginOptions {
15
- /** Warn about duplicate namespaces across modules. Defaults to true. */
20
+ /**
21
+ * When true (default), fail the build if the same logical `styles.component` / `styles.class`
22
+ * namespace appears in more than one module. Set to `false` to skip this check.
23
+ */
16
24
  warnDuplicates?: boolean;
17
25
  /**
18
26
  * Mode for typestyles integration:
@@ -20,15 +28,19 @@ interface TypestylesPluginOptions {
20
28
  * - `"build"`: emit a static CSS asset on `vite build` and disable client injection there.
21
29
  * - `"hybrid"`: extract on build and keep runtime-compatible behavior.
22
30
  *
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"`.
31
+ * When omitted and at least one extraction module is resolved (explicit `extract.modules`, or a
32
+ * discovered convention entry file), defaults to `"build"`: `vite dev` keeps runtime
33
+ * injection + HMR (runtime is only disabled during `vite build`), serves the same extracted CSS
34
+ * at `extract.fileName` so `<link href="/typestyles.css">` is valid (avoids SPA fallback
35
+ * returning HTML for that URL), and production emits the CSS file. If no modules are resolved,
36
+ * defaults to `"runtime"`.
28
37
  */
29
38
  mode?: 'runtime' | 'build' | 'hybrid';
30
39
  /**
31
40
  * Options for build-time CSS extraction when mode is "build" or "hybrid".
41
+ *
42
+ * Omitted entirely: discover a convention entry under the project root (see
43
+ * {@link discoverDefaultExtractModules}); if none exist, stays in runtime-only mode.
32
44
  */
33
45
  extract?: TypestylesExtractOptions;
34
46
  }
@@ -43,7 +55,7 @@ declare function extractNamespaces(code: string): {
43
55
  /**
44
56
  * Vite plugin for typestyles HMR support.
45
57
  *
46
- * When a module that registers typestyles (e.g. `styles.component`, `tokens.create`) is edited,
58
+ * When a module that registers typestyles (e.g. `styles.component`, `styles.class`, `tokens.create`) is edited,
47
59
  * this plugin injects HMR accept/dispose handlers that invalidate that module's registrations
48
60
  * before re-execution. Files may import from a local re-export such as `./typestyles` rather
49
61
  * than `from 'typestyles'` directly — we key off API call patterns in the source, not the import path.
package/dist/index.d.ts CHANGED
@@ -1,18 +1,26 @@
1
1
  import { Plugin } from 'vite';
2
+ export { DEFAULT_EXTRACT_MODULE_CANDIDATES, discoverDefaultExtractModules } from '@typestyles/build-runner';
2
3
 
3
4
  interface TypestylesExtractOptions {
4
5
  /**
5
6
  * Modules that should be imported during build to register styles.
6
7
  * Paths are resolved relative to Vite's project root.
8
+ *
9
+ * When omitted, the plugin looks for the first existing file among
10
+ * {@link DEFAULT_EXTRACT_MODULE_CANDIDATES} under the project root (same resolution as when
11
+ * `extract` itself is omitted).
7
12
  */
8
- modules: string[];
13
+ modules?: string[];
9
14
  /**
10
15
  * Output CSS filename (in the build assets). Defaults to "typestyles.css".
11
16
  */
12
17
  fileName?: string;
13
18
  }
14
19
  interface TypestylesPluginOptions {
15
- /** Warn about duplicate namespaces across modules. Defaults to true. */
20
+ /**
21
+ * When true (default), fail the build if the same logical `styles.component` / `styles.class`
22
+ * namespace appears in more than one module. Set to `false` to skip this check.
23
+ */
16
24
  warnDuplicates?: boolean;
17
25
  /**
18
26
  * Mode for typestyles integration:
@@ -20,15 +28,19 @@ interface TypestylesPluginOptions {
20
28
  * - `"build"`: emit a static CSS asset on `vite build` and disable client injection there.
21
29
  * - `"hybrid"`: extract on build and keep runtime-compatible behavior.
22
30
  *
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"`.
31
+ * When omitted and at least one extraction module is resolved (explicit `extract.modules`, or a
32
+ * discovered convention entry file), defaults to `"build"`: `vite dev` keeps runtime
33
+ * injection + HMR (runtime is only disabled during `vite build`), serves the same extracted CSS
34
+ * at `extract.fileName` so `<link href="/typestyles.css">` is valid (avoids SPA fallback
35
+ * returning HTML for that URL), and production emits the CSS file. If no modules are resolved,
36
+ * defaults to `"runtime"`.
28
37
  */
29
38
  mode?: 'runtime' | 'build' | 'hybrid';
30
39
  /**
31
40
  * Options for build-time CSS extraction when mode is "build" or "hybrid".
41
+ *
42
+ * Omitted entirely: discover a convention entry under the project root (see
43
+ * {@link discoverDefaultExtractModules}); if none exist, stays in runtime-only mode.
32
44
  */
33
45
  extract?: TypestylesExtractOptions;
34
46
  }
@@ -43,7 +55,7 @@ declare function extractNamespaces(code: string): {
43
55
  /**
44
56
  * Vite plugin for typestyles HMR support.
45
57
  *
46
- * When a module that registers typestyles (e.g. `styles.component`, `tokens.create`) is edited,
58
+ * When a module that registers typestyles (e.g. `styles.component`, `styles.class`, `tokens.create`) is edited,
47
59
  * this plugin injects HMR accept/dispose handlers that invalidate that module's registrations
48
60
  * before re-execution. Files may import from a local re-export such as `./typestyles` rather
49
61
  * than `from 'typestyles'` directly — we key off API call patterns in the source, not the import path.
package/dist/index.js CHANGED
@@ -1,17 +1,32 @@
1
1
  // src/index.ts
2
- import { runTypestylesBuild } from "@typestyles/build-runner";
2
+ import { resolve as resolvePath } from "path";
3
+ import { discoverDefaultExtractModules, runTypestylesBuild } from "@typestyles/build-runner";
4
+ import {
5
+ DEFAULT_EXTRACT_MODULE_CANDIDATES,
6
+ discoverDefaultExtractModules as discoverDefaultExtractModules2
7
+ } from "@typestyles/build-runner";
3
8
  var STYLES_COMPONENT_RE = /styles\.component\(\s*['"]([^'"]+)['"]/g;
9
+ var STYLES_CLASS_RE = /styles\.class\(\s*['"]([^'"]+)['"]/g;
4
10
  var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
5
11
  var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
6
12
  var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
7
13
  var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
8
14
  var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
15
+ function resolveExtractModules(root, extract) {
16
+ if (extract?.modules !== void 0) {
17
+ return extract.modules;
18
+ }
19
+ return discoverDefaultExtractModules(root);
20
+ }
9
21
  function extractNamespaces(code) {
10
22
  const keys = [];
11
23
  const prefixes = [];
12
24
  for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
13
25
  prefixes.push(`.${match[1]}-`);
14
26
  }
27
+ for (const match of code.matchAll(STYLES_CLASS_RE)) {
28
+ prefixes.push(`.${match[1]}-`);
29
+ }
15
30
  for (const match of code.matchAll(TOKENS_CREATE_RE)) {
16
31
  keys.push(`tokens:${match[1]}`);
17
32
  }
@@ -31,8 +46,8 @@ function extractNamespaces(code) {
31
46
  }
32
47
  function typestylesPlugin(options = {}) {
33
48
  const { warnDuplicates = true, extract } = options;
34
- const extractModules = extract?.modules ?? [];
35
- const mode = options.mode ?? (extractModules.length > 0 ? "build" : "runtime");
49
+ let resolvedExtractModules = [];
50
+ let resolvedMode = "runtime";
36
51
  const moduleNamespaces = /* @__PURE__ */ new Map();
37
52
  let resolvedConfig = null;
38
53
  let isBuildCommand = false;
@@ -44,7 +59,10 @@ function typestylesPlugin(options = {}) {
44
59
  enforce: "pre",
45
60
  config(config, env) {
46
61
  isBuildCommand = env.command === "build";
47
- if (env.command === "build" && (mode === "build" || mode === "hybrid")) {
62
+ const root = resolvePath(config.root ?? process.cwd());
63
+ resolvedExtractModules = resolveExtractModules(root, extract);
64
+ resolvedMode = options.mode ?? (resolvedExtractModules.length > 0 ? "build" : "runtime");
65
+ if (env.command === "build" && (resolvedMode === "build" || resolvedMode === "hybrid")) {
48
66
  config.define = {
49
67
  ...config.define ?? {},
50
68
  // Inlined by the bundler so typestyles sheet skips creating <style> in production
@@ -58,9 +76,9 @@ function typestylesPlugin(options = {}) {
58
76
  isServe = config.command === "serve";
59
77
  },
60
78
  configureServer(server) {
61
- if (!extract || extractModules.length === 0) return;
62
- if (mode === "runtime") return;
63
- const fileName = extract.fileName ?? "typestyles.css";
79
+ if (resolvedExtractModules.length === 0) return;
80
+ if (resolvedMode === "runtime") return;
81
+ const fileName = extract?.fileName ?? "typestyles.css";
64
82
  const invalidateDevExtract = () => {
65
83
  devExtractCss = null;
66
84
  devExtractInFlight = null;
@@ -97,7 +115,7 @@ function typestylesPlugin(options = {}) {
97
115
  if (!devExtractInFlight) {
98
116
  devExtractInFlight = runTypestylesBuild({
99
117
  root,
100
- modules: extractModules
118
+ modules: resolvedExtractModules
101
119
  }).then((css2) => {
102
120
  devExtractCss = css2;
103
121
  return css2;
@@ -126,8 +144,8 @@ function typestylesPlugin(options = {}) {
126
144
  for (const prefix of prefixes) {
127
145
  if (other.prefixes.includes(prefix)) {
128
146
  const ns = prefix.slice(1, -1);
129
- this.warn(
130
- `Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions.`
147
+ this.error(
148
+ `[typestyles] Style namespace "${ns}" is also used in ${otherId}. Duplicate namespaces cause class name collisions. Use a distinct name or isolate with createStyles({ scopeId: fileScopeId(import.meta) }).`
131
149
  );
132
150
  }
133
151
  }
@@ -155,15 +173,15 @@ if (import.meta.hot) {
155
173
  },
156
174
  async generateBundle() {
157
175
  if (!isBuildCommand) return;
158
- if (mode === "runtime") return;
159
- if (!extract || !extractModules.length) return;
176
+ if (resolvedMode === "runtime") return;
177
+ if (!resolvedExtractModules.length) return;
160
178
  if (!resolvedConfig) return;
161
179
  const root = resolvedConfig.root ?? process.cwd();
162
- const fileName = extract.fileName ?? "typestyles.css";
180
+ const fileName = extract?.fileName ?? "typestyles.css";
163
181
  try {
164
182
  const css = await runTypestylesBuild({
165
183
  root,
166
- modules: extractModules
184
+ modules: resolvedExtractModules
167
185
  });
168
186
  this.emitFile({
169
187
  type: "asset",
@@ -179,6 +197,8 @@ if (import.meta.hot) {
179
197
  };
180
198
  }
181
199
  export {
200
+ DEFAULT_EXTRACT_MODULE_CANDIDATES,
182
201
  typestylesPlugin as default,
202
+ discoverDefaultExtractModules2 as discoverDefaultExtractModules,
183
203
  extractNamespaces
184
204
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typestyles/vite",
3
- "version": "0.0.0-unstable.835df2a09a6d",
3
+ "version": "0.0.0-unstable.921c88bcff7e",
4
4
  "description": "Vite plugin for typestyles HMR support",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -31,7 +31,7 @@
31
31
  "license": "Apache-2.0",
32
32
  "repository": {
33
33
  "type": "git",
34
- "url": "https://github.com/dbanksdesign/typestyles",
34
+ "url": "https://github.com/type-styles/typestyles",
35
35
  "directory": "packages/vite"
36
36
  },
37
37
  "peerDependencies": {
@@ -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.835df2a09a6d",
44
+ "typestyles": "0.0.0-unstable.921c88bcff7e",
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.835df2a09a6d"
49
+ "@typestyles/build-runner": "0.0.0-unstable.921c88bcff7e"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup src/index.ts --format esm,cjs --dts --clean --external typestyles,@typestyles/build-runner",