@typestyles/vite 0.0.0-unstable.82ebd9c88257 → 0.0.0-unstable.8bf64b0f3f7d
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 +94 -14
- package/dist/index.d.cts +23 -7
- package/dist/index.d.ts +23 -7
- package/dist/index.js +96 -15
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -20,18 +20,27 @@ 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;
|
|
29
33
|
var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
|
|
30
34
|
var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
|
|
31
35
|
var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
|
|
32
36
|
var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
|
|
33
37
|
var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
|
|
34
|
-
|
|
38
|
+
function resolveExtractModules(root, extract) {
|
|
39
|
+
if (extract?.modules !== void 0) {
|
|
40
|
+
return extract.modules;
|
|
41
|
+
}
|
|
42
|
+
return (0, import_build_runner.discoverDefaultExtractModules)(root);
|
|
43
|
+
}
|
|
35
44
|
function extractNamespaces(code) {
|
|
36
45
|
const keys = [];
|
|
37
46
|
const prefixes = [];
|
|
@@ -56,16 +65,24 @@ function extractNamespaces(code) {
|
|
|
56
65
|
return { keys, prefixes };
|
|
57
66
|
}
|
|
58
67
|
function typestylesPlugin(options = {}) {
|
|
59
|
-
const { warnDuplicates = true,
|
|
68
|
+
const { warnDuplicates = true, extract } = options;
|
|
69
|
+
let resolvedExtractModules = [];
|
|
70
|
+
let resolvedMode = "runtime";
|
|
60
71
|
const moduleNamespaces = /* @__PURE__ */ new Map();
|
|
61
72
|
let resolvedConfig = null;
|
|
62
73
|
let isBuildCommand = false;
|
|
74
|
+
let isServe = false;
|
|
75
|
+
let devExtractCss = null;
|
|
76
|
+
let devExtractInFlight = null;
|
|
63
77
|
return {
|
|
64
78
|
name: "typestyles",
|
|
65
79
|
enforce: "pre",
|
|
66
80
|
config(config, env) {
|
|
67
81
|
isBuildCommand = env.command === "build";
|
|
68
|
-
|
|
82
|
+
const root = (0, import_node_path.resolve)(config.root ?? process.cwd());
|
|
83
|
+
resolvedExtractModules = resolveExtractModules(root, extract);
|
|
84
|
+
resolvedMode = options.mode ?? (resolvedExtractModules.length > 0 ? "build" : "runtime");
|
|
85
|
+
if (env.command === "build" && (resolvedMode === "build" || resolvedMode === "hybrid")) {
|
|
69
86
|
config.define = {
|
|
70
87
|
...config.define ?? {},
|
|
71
88
|
// Inlined by the bundler so typestyles sheet skips creating <style> in production
|
|
@@ -76,11 +93,69 @@ function typestylesPlugin(options = {}) {
|
|
|
76
93
|
},
|
|
77
94
|
configResolved(config) {
|
|
78
95
|
resolvedConfig = config;
|
|
96
|
+
isServe = config.command === "serve";
|
|
97
|
+
},
|
|
98
|
+
configureServer(server) {
|
|
99
|
+
if (resolvedExtractModules.length === 0) return;
|
|
100
|
+
if (resolvedMode === "runtime") return;
|
|
101
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
102
|
+
const invalidateDevExtract = () => {
|
|
103
|
+
devExtractCss = null;
|
|
104
|
+
devExtractInFlight = null;
|
|
105
|
+
};
|
|
106
|
+
server.watcher.on("change", invalidateDevExtract);
|
|
107
|
+
server.watcher.on("add", invalidateDevExtract);
|
|
108
|
+
server.watcher.on("unlink", invalidateDevExtract);
|
|
109
|
+
server.middlewares.use(async (req, res, next) => {
|
|
110
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
111
|
+
next();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const config = resolvedConfig;
|
|
115
|
+
if (!config) {
|
|
116
|
+
next();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const url = req.url?.split("?")[0] ?? "";
|
|
120
|
+
const base = config.base.replace(/\/$/, "") || "";
|
|
121
|
+
const expectedPath = base === "" || base === "/" ? `/${fileName.replace(/^\//, "")}` : `${base}/${fileName.replace(/^\//, "")}`;
|
|
122
|
+
if (url !== expectedPath) {
|
|
123
|
+
next();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const root = config.root ?? process.cwd();
|
|
127
|
+
try {
|
|
128
|
+
if (devExtractCss !== null) {
|
|
129
|
+
res.statusCode = 200;
|
|
130
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
131
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
132
|
+
res.end(devExtractCss);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (!devExtractInFlight) {
|
|
136
|
+
devExtractInFlight = (0, import_build_runner.runTypestylesBuild)({
|
|
137
|
+
root,
|
|
138
|
+
modules: resolvedExtractModules
|
|
139
|
+
}).then((css2) => {
|
|
140
|
+
devExtractCss = css2;
|
|
141
|
+
return css2;
|
|
142
|
+
}).finally(() => {
|
|
143
|
+
devExtractInFlight = null;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const css = await devExtractInFlight;
|
|
147
|
+
res.statusCode = 200;
|
|
148
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
149
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
150
|
+
res.end(css);
|
|
151
|
+
} catch (err) {
|
|
152
|
+
next(err);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
79
155
|
},
|
|
80
156
|
transform(code, id) {
|
|
81
157
|
if (!/\.[jt]sx?$/.test(id)) return null;
|
|
82
158
|
if (id.includes("node_modules")) return null;
|
|
83
|
-
if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
|
|
84
159
|
const { keys, prefixes } = extractNamespaces(code);
|
|
85
160
|
if (keys.length === 0 && prefixes.length === 0) return null;
|
|
86
161
|
if (warnDuplicates) {
|
|
@@ -97,33 +172,36 @@ function typestylesPlugin(options = {}) {
|
|
|
97
172
|
}
|
|
98
173
|
}
|
|
99
174
|
moduleNamespaces.set(id, { keys, prefixes });
|
|
175
|
+
if (!isServe) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
100
178
|
const keysJSON = JSON.stringify(keys);
|
|
101
179
|
const prefixesJSON = JSON.stringify(prefixes);
|
|
180
|
+
const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
|
|
181
|
+
`;
|
|
102
182
|
const hmrCode = `
|
|
103
183
|
if (import.meta.hot) {
|
|
104
|
-
import
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
__typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
|
|
108
|
-
});
|
|
184
|
+
import.meta.hot.accept();
|
|
185
|
+
import.meta.hot.dispose(() => {
|
|
186
|
+
__typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
|
|
109
187
|
});
|
|
110
188
|
}`;
|
|
111
189
|
return {
|
|
112
|
-
code: code + hmrCode,
|
|
190
|
+
code: hmrImport + code + hmrCode,
|
|
113
191
|
map: null
|
|
114
192
|
};
|
|
115
193
|
},
|
|
116
194
|
async generateBundle() {
|
|
117
195
|
if (!isBuildCommand) return;
|
|
118
|
-
if (
|
|
119
|
-
if (!
|
|
196
|
+
if (resolvedMode === "runtime") return;
|
|
197
|
+
if (!resolvedExtractModules.length) return;
|
|
120
198
|
if (!resolvedConfig) return;
|
|
121
199
|
const root = resolvedConfig.root ?? process.cwd();
|
|
122
|
-
const fileName = extract
|
|
200
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
123
201
|
try {
|
|
124
202
|
const css = await (0, import_build_runner.runTypestylesBuild)({
|
|
125
203
|
root,
|
|
126
|
-
modules:
|
|
204
|
+
modules: resolvedExtractModules
|
|
127
205
|
});
|
|
128
206
|
this.emitFile({
|
|
129
207
|
type: "asset",
|
|
@@ -140,5 +218,7 @@ if (import.meta.hot) {
|
|
|
140
218
|
}
|
|
141
219
|
// Annotate the CommonJS export names for ESM import in node:
|
|
142
220
|
0 && (module.exports = {
|
|
221
|
+
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
222
|
+
discoverDefaultExtractModules,
|
|
143
223
|
extractNamespaces
|
|
144
224
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
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
|
|
13
|
+
modules?: string[];
|
|
9
14
|
/**
|
|
10
15
|
* Output CSS filename (in the build assets). Defaults to "typestyles.css".
|
|
11
16
|
*/
|
|
@@ -16,13 +21,23 @@ interface TypestylesPluginOptions {
|
|
|
16
21
|
warnDuplicates?: boolean;
|
|
17
22
|
/**
|
|
18
23
|
* Mode for typestyles integration:
|
|
19
|
-
* - "runtime"
|
|
20
|
-
* - "build"
|
|
21
|
-
* - "hybrid"
|
|
24
|
+
* - `"runtime"`: HMR only, no build-time extraction.
|
|
25
|
+
* - `"build"`: emit a static CSS asset on `vite build` and disable client injection there.
|
|
26
|
+
* - `"hybrid"`: extract on build and keep runtime-compatible behavior.
|
|
27
|
+
*
|
|
28
|
+
* When omitted and at least one extraction module is resolved (explicit `extract.modules`, or a
|
|
29
|
+
* discovered convention entry file), defaults to `"build"`: `vite dev` keeps runtime
|
|
30
|
+
* injection + HMR (runtime is only disabled during `vite build`), serves the same extracted CSS
|
|
31
|
+
* at `extract.fileName` so `<link href="/typestyles.css">` is valid (avoids SPA fallback
|
|
32
|
+
* returning HTML for that URL), and production emits the CSS file. If no modules are resolved,
|
|
33
|
+
* defaults to `"runtime"`.
|
|
22
34
|
*/
|
|
23
35
|
mode?: 'runtime' | 'build' | 'hybrid';
|
|
24
36
|
/**
|
|
25
37
|
* Options for build-time CSS extraction when mode is "build" or "hybrid".
|
|
38
|
+
*
|
|
39
|
+
* Omitted entirely: discover a convention entry under the project root (see
|
|
40
|
+
* {@link discoverDefaultExtractModules}); if none exist, stays in runtime-only mode.
|
|
26
41
|
*/
|
|
27
42
|
extract?: TypestylesExtractOptions;
|
|
28
43
|
}
|
|
@@ -37,9 +52,10 @@ declare function extractNamespaces(code: string): {
|
|
|
37
52
|
/**
|
|
38
53
|
* Vite plugin for typestyles HMR support.
|
|
39
54
|
*
|
|
40
|
-
* When a module that
|
|
41
|
-
* accept/dispose handlers that invalidate
|
|
42
|
-
* before re-execution
|
|
55
|
+
* When a module that registers typestyles (e.g. `styles.component`, `tokens.create`) is edited,
|
|
56
|
+
* this plugin injects HMR accept/dispose handlers that invalidate that module's registrations
|
|
57
|
+
* before re-execution. Files may import from a local re-export such as `./typestyles` rather
|
|
58
|
+
* than `from 'typestyles'` directly — we key off API call patterns in the source, not the import path.
|
|
43
59
|
*/
|
|
44
60
|
declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
|
|
45
61
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
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
|
|
13
|
+
modules?: string[];
|
|
9
14
|
/**
|
|
10
15
|
* Output CSS filename (in the build assets). Defaults to "typestyles.css".
|
|
11
16
|
*/
|
|
@@ -16,13 +21,23 @@ interface TypestylesPluginOptions {
|
|
|
16
21
|
warnDuplicates?: boolean;
|
|
17
22
|
/**
|
|
18
23
|
* Mode for typestyles integration:
|
|
19
|
-
* - "runtime"
|
|
20
|
-
* - "build"
|
|
21
|
-
* - "hybrid"
|
|
24
|
+
* - `"runtime"`: HMR only, no build-time extraction.
|
|
25
|
+
* - `"build"`: emit a static CSS asset on `vite build` and disable client injection there.
|
|
26
|
+
* - `"hybrid"`: extract on build and keep runtime-compatible behavior.
|
|
27
|
+
*
|
|
28
|
+
* When omitted and at least one extraction module is resolved (explicit `extract.modules`, or a
|
|
29
|
+
* discovered convention entry file), defaults to `"build"`: `vite dev` keeps runtime
|
|
30
|
+
* injection + HMR (runtime is only disabled during `vite build`), serves the same extracted CSS
|
|
31
|
+
* at `extract.fileName` so `<link href="/typestyles.css">` is valid (avoids SPA fallback
|
|
32
|
+
* returning HTML for that URL), and production emits the CSS file. If no modules are resolved,
|
|
33
|
+
* defaults to `"runtime"`.
|
|
22
34
|
*/
|
|
23
35
|
mode?: 'runtime' | 'build' | 'hybrid';
|
|
24
36
|
/**
|
|
25
37
|
* Options for build-time CSS extraction when mode is "build" or "hybrid".
|
|
38
|
+
*
|
|
39
|
+
* Omitted entirely: discover a convention entry under the project root (see
|
|
40
|
+
* {@link discoverDefaultExtractModules}); if none exist, stays in runtime-only mode.
|
|
26
41
|
*/
|
|
27
42
|
extract?: TypestylesExtractOptions;
|
|
28
43
|
}
|
|
@@ -37,9 +52,10 @@ declare function extractNamespaces(code: string): {
|
|
|
37
52
|
/**
|
|
38
53
|
* Vite plugin for typestyles HMR support.
|
|
39
54
|
*
|
|
40
|
-
* When a module that
|
|
41
|
-
* accept/dispose handlers that invalidate
|
|
42
|
-
* before re-execution
|
|
55
|
+
* When a module that registers typestyles (e.g. `styles.component`, `tokens.create`) is edited,
|
|
56
|
+
* this plugin injects HMR accept/dispose handlers that invalidate that module's registrations
|
|
57
|
+
* before re-execution. Files may import from a local re-export such as `./typestyles` rather
|
|
58
|
+
* than `from 'typestyles'` directly — we key off API call patterns in the source, not the import path.
|
|
43
59
|
*/
|
|
44
60
|
declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
|
|
45
61
|
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import {
|
|
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;
|
|
4
9
|
var TOKENS_CREATE_RE = /tokens\.create\(\s*['"]([^'"]+)['"]/g;
|
|
5
10
|
var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
|
|
6
11
|
var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
|
|
7
12
|
var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
|
|
8
13
|
var GLOBAL_FONT_FACE_RE = /global\.fontFace\(\s*['"]([^'"]+)['"]/g;
|
|
9
|
-
|
|
14
|
+
function resolveExtractModules(root, extract) {
|
|
15
|
+
if (extract?.modules !== void 0) {
|
|
16
|
+
return extract.modules;
|
|
17
|
+
}
|
|
18
|
+
return discoverDefaultExtractModules(root);
|
|
19
|
+
}
|
|
10
20
|
function extractNamespaces(code) {
|
|
11
21
|
const keys = [];
|
|
12
22
|
const prefixes = [];
|
|
@@ -31,16 +41,24 @@ function extractNamespaces(code) {
|
|
|
31
41
|
return { keys, prefixes };
|
|
32
42
|
}
|
|
33
43
|
function typestylesPlugin(options = {}) {
|
|
34
|
-
const { warnDuplicates = true,
|
|
44
|
+
const { warnDuplicates = true, extract } = options;
|
|
45
|
+
let resolvedExtractModules = [];
|
|
46
|
+
let resolvedMode = "runtime";
|
|
35
47
|
const moduleNamespaces = /* @__PURE__ */ new Map();
|
|
36
48
|
let resolvedConfig = null;
|
|
37
49
|
let isBuildCommand = false;
|
|
50
|
+
let isServe = false;
|
|
51
|
+
let devExtractCss = null;
|
|
52
|
+
let devExtractInFlight = null;
|
|
38
53
|
return {
|
|
39
54
|
name: "typestyles",
|
|
40
55
|
enforce: "pre",
|
|
41
56
|
config(config, env) {
|
|
42
57
|
isBuildCommand = env.command === "build";
|
|
43
|
-
|
|
58
|
+
const root = resolvePath(config.root ?? process.cwd());
|
|
59
|
+
resolvedExtractModules = resolveExtractModules(root, extract);
|
|
60
|
+
resolvedMode = options.mode ?? (resolvedExtractModules.length > 0 ? "build" : "runtime");
|
|
61
|
+
if (env.command === "build" && (resolvedMode === "build" || resolvedMode === "hybrid")) {
|
|
44
62
|
config.define = {
|
|
45
63
|
...config.define ?? {},
|
|
46
64
|
// Inlined by the bundler so typestyles sheet skips creating <style> in production
|
|
@@ -51,11 +69,69 @@ function typestylesPlugin(options = {}) {
|
|
|
51
69
|
},
|
|
52
70
|
configResolved(config) {
|
|
53
71
|
resolvedConfig = config;
|
|
72
|
+
isServe = config.command === "serve";
|
|
73
|
+
},
|
|
74
|
+
configureServer(server) {
|
|
75
|
+
if (resolvedExtractModules.length === 0) return;
|
|
76
|
+
if (resolvedMode === "runtime") return;
|
|
77
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
78
|
+
const invalidateDevExtract = () => {
|
|
79
|
+
devExtractCss = null;
|
|
80
|
+
devExtractInFlight = null;
|
|
81
|
+
};
|
|
82
|
+
server.watcher.on("change", invalidateDevExtract);
|
|
83
|
+
server.watcher.on("add", invalidateDevExtract);
|
|
84
|
+
server.watcher.on("unlink", invalidateDevExtract);
|
|
85
|
+
server.middlewares.use(async (req, res, next) => {
|
|
86
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
87
|
+
next();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const config = resolvedConfig;
|
|
91
|
+
if (!config) {
|
|
92
|
+
next();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const url = req.url?.split("?")[0] ?? "";
|
|
96
|
+
const base = config.base.replace(/\/$/, "") || "";
|
|
97
|
+
const expectedPath = base === "" || base === "/" ? `/${fileName.replace(/^\//, "")}` : `${base}/${fileName.replace(/^\//, "")}`;
|
|
98
|
+
if (url !== expectedPath) {
|
|
99
|
+
next();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const root = config.root ?? process.cwd();
|
|
103
|
+
try {
|
|
104
|
+
if (devExtractCss !== null) {
|
|
105
|
+
res.statusCode = 200;
|
|
106
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
107
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
108
|
+
res.end(devExtractCss);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (!devExtractInFlight) {
|
|
112
|
+
devExtractInFlight = runTypestylesBuild({
|
|
113
|
+
root,
|
|
114
|
+
modules: resolvedExtractModules
|
|
115
|
+
}).then((css2) => {
|
|
116
|
+
devExtractCss = css2;
|
|
117
|
+
return css2;
|
|
118
|
+
}).finally(() => {
|
|
119
|
+
devExtractInFlight = null;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
const css = await devExtractInFlight;
|
|
123
|
+
res.statusCode = 200;
|
|
124
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
125
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
126
|
+
res.end(css);
|
|
127
|
+
} catch (err) {
|
|
128
|
+
next(err);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
54
131
|
},
|
|
55
132
|
transform(code, id) {
|
|
56
133
|
if (!/\.[jt]sx?$/.test(id)) return null;
|
|
57
134
|
if (id.includes("node_modules")) return null;
|
|
58
|
-
if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
|
|
59
135
|
const { keys, prefixes } = extractNamespaces(code);
|
|
60
136
|
if (keys.length === 0 && prefixes.length === 0) return null;
|
|
61
137
|
if (warnDuplicates) {
|
|
@@ -72,33 +148,36 @@ function typestylesPlugin(options = {}) {
|
|
|
72
148
|
}
|
|
73
149
|
}
|
|
74
150
|
moduleNamespaces.set(id, { keys, prefixes });
|
|
151
|
+
if (!isServe) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
75
154
|
const keysJSON = JSON.stringify(keys);
|
|
76
155
|
const prefixesJSON = JSON.stringify(prefixes);
|
|
156
|
+
const hmrImport = `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
|
|
157
|
+
`;
|
|
77
158
|
const hmrCode = `
|
|
78
159
|
if (import.meta.hot) {
|
|
79
|
-
import
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
__typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
|
|
83
|
-
});
|
|
160
|
+
import.meta.hot.accept();
|
|
161
|
+
import.meta.hot.dispose(() => {
|
|
162
|
+
__typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});
|
|
84
163
|
});
|
|
85
164
|
}`;
|
|
86
165
|
return {
|
|
87
|
-
code: code + hmrCode,
|
|
166
|
+
code: hmrImport + code + hmrCode,
|
|
88
167
|
map: null
|
|
89
168
|
};
|
|
90
169
|
},
|
|
91
170
|
async generateBundle() {
|
|
92
171
|
if (!isBuildCommand) return;
|
|
93
|
-
if (
|
|
94
|
-
if (!
|
|
172
|
+
if (resolvedMode === "runtime") return;
|
|
173
|
+
if (!resolvedExtractModules.length) return;
|
|
95
174
|
if (!resolvedConfig) return;
|
|
96
175
|
const root = resolvedConfig.root ?? process.cwd();
|
|
97
|
-
const fileName = extract
|
|
176
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
98
177
|
try {
|
|
99
178
|
const css = await runTypestylesBuild({
|
|
100
179
|
root,
|
|
101
|
-
modules:
|
|
180
|
+
modules: resolvedExtractModules
|
|
102
181
|
});
|
|
103
182
|
this.emitFile({
|
|
104
183
|
type: "asset",
|
|
@@ -114,6 +193,8 @@ if (import.meta.hot) {
|
|
|
114
193
|
};
|
|
115
194
|
}
|
|
116
195
|
export {
|
|
196
|
+
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
117
197
|
typestylesPlugin as default,
|
|
198
|
+
discoverDefaultExtractModules2 as discoverDefaultExtractModules,
|
|
118
199
|
extractNamespaces
|
|
119
200
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typestyles/vite",
|
|
3
|
-
"version": "0.0.0-unstable.
|
|
3
|
+
"version": "0.0.0-unstable.8bf64b0f3f7d",
|
|
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.
|
|
44
|
+
"typestyles": "0.0.0-unstable.8bf64b0f3f7d",
|
|
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.
|
|
49
|
+
"@typestyles/build-runner": "0.0.0-unstable.8bf64b0f3f7d"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external typestyles,@typestyles/build-runner",
|