@typestyles/vite 0.0.0-unstable.7772c75b22e2 → 0.0.0-unstable.7a3ebfd340c8
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/LICENSE +204 -0
- package/README.md +176 -0
- package/dist/index.cjs +78 -58
- package/dist/index.d.cts +22 -26
- package/dist/index.d.ts +22 -26
- package/dist/index.js +83 -59
- package/package.json +3 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
Copyright 2026 The TypeStyles Authors
|
|
7
|
+
|
|
8
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
9
|
+
|
|
10
|
+
1. Definitions.
|
|
11
|
+
|
|
12
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
13
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
14
|
+
|
|
15
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
16
|
+
the copyright owner that is granting the License.
|
|
17
|
+
|
|
18
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
19
|
+
other entities that control, are controlled by, or are under common
|
|
20
|
+
control with that entity. For the purposes of this definition,
|
|
21
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
22
|
+
direction or management of such entity, whether by contract or
|
|
23
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
24
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
25
|
+
|
|
26
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
27
|
+
exercising permissions granted by this License.
|
|
28
|
+
|
|
29
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
30
|
+
including but not limited to software source code, documentation
|
|
31
|
+
source, and configuration files.
|
|
32
|
+
|
|
33
|
+
"Object" form shall mean any form resulting from mechanical
|
|
34
|
+
transformation or translation of a Source form, including but
|
|
35
|
+
not limited to compiled object code, generated documentation,
|
|
36
|
+
and conversions to other media types.
|
|
37
|
+
|
|
38
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
39
|
+
Object form, made available under the License, as indicated by a
|
|
40
|
+
copyright notice that is included in or attached to the work
|
|
41
|
+
(an example is provided in the Appendix below).
|
|
42
|
+
|
|
43
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
44
|
+
form, that is based on (or derived from) the Work and for which the
|
|
45
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
46
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
47
|
+
of this License, Derivative Works shall not include works that remain
|
|
48
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
49
|
+
the Work and Derivative Works thereof.
|
|
50
|
+
|
|
51
|
+
"Contribution" shall mean any work of authorship, including
|
|
52
|
+
the original version of the Work and any modifications or additions
|
|
53
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
54
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
55
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
56
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
57
|
+
means any form of electronic, verbal, or written communication sent
|
|
58
|
+
to the Licensor or its representatives, including but not limited to
|
|
59
|
+
communication on electronic mailing lists, source code control systems,
|
|
60
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
61
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
62
|
+
excluding communication that is conspicuously marked or otherwise
|
|
63
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
64
|
+
|
|
65
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
66
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
67
|
+
subsequently incorporated within the Work.
|
|
68
|
+
|
|
69
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
70
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
71
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
72
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
73
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
74
|
+
Work and such Derivative Works in Source or Object form.
|
|
75
|
+
|
|
76
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
77
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
78
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
79
|
+
(except as stated in this section) patent license to make, have made,
|
|
80
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
81
|
+
where such license applies only to those patent claims licensable
|
|
82
|
+
by such Contributor that are necessarily infringed by their
|
|
83
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
84
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
85
|
+
institute patent litigation against any entity (including a
|
|
86
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
87
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
88
|
+
or contributory patent infringement, then any patent licenses
|
|
89
|
+
granted to You under this License for that Work shall terminate
|
|
90
|
+
as of the date such litigation is filed.
|
|
91
|
+
|
|
92
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
93
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
94
|
+
modifications, and in Source or Object form, provided that You
|
|
95
|
+
meet the following conditions:
|
|
96
|
+
|
|
97
|
+
(a) You must give any other recipients of the Work or
|
|
98
|
+
Derivative Works a copy of this License; and
|
|
99
|
+
|
|
100
|
+
(b) You must cause any modified files to carry prominent notices
|
|
101
|
+
stating that You changed the files; and
|
|
102
|
+
|
|
103
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
104
|
+
that You distribute, all copyright, patent, trademark, and
|
|
105
|
+
attribution notices from the Source form of the Work,
|
|
106
|
+
excluding those notices that do not pertain to any part of
|
|
107
|
+
the Derivative Works; and
|
|
108
|
+
|
|
109
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
110
|
+
distribution, then any Derivative Works that You distribute must
|
|
111
|
+
include a readable copy of the attribution notices contained
|
|
112
|
+
within such NOTICE file, excluding those notices that do not
|
|
113
|
+
pertain to any part of the Derivative Works, in at least one
|
|
114
|
+
of the following places: within a NOTICE text file distributed
|
|
115
|
+
as part of the Derivative Works; within the Source form or
|
|
116
|
+
documentation, if provided along with the Derivative Works; or,
|
|
117
|
+
within a display generated by the Derivative Works, if and
|
|
118
|
+
wherever such third-party notices normally appear. The contents
|
|
119
|
+
of the NOTICE file are for informational purposes only and
|
|
120
|
+
do not modify the License. You may add Your own attribution
|
|
121
|
+
notices within Derivative Works that You distribute, alongside
|
|
122
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
123
|
+
that such additional attribution notices cannot be construed
|
|
124
|
+
as modifying the License.
|
|
125
|
+
|
|
126
|
+
You may add Your own copyright statement to Your modifications and
|
|
127
|
+
may provide additional or different license terms and conditions
|
|
128
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
129
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
130
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
131
|
+
the conditions stated in this License.
|
|
132
|
+
|
|
133
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
134
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
135
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
136
|
+
this License, without any additional terms or conditions.
|
|
137
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
138
|
+
the terms of any separate license agreement you may have executed
|
|
139
|
+
with Licensor regarding such Contributions.
|
|
140
|
+
|
|
141
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
142
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
143
|
+
except as required for reasonable and customary use in describing the
|
|
144
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
145
|
+
|
|
146
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
147
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
148
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
149
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
150
|
+
implied, including, without limitation, any warranties or conditions
|
|
151
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
152
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
153
|
+
appropriateness of using or redistributing the Work and assume any
|
|
154
|
+
risks associated with Your exercise of permissions under this License.
|
|
155
|
+
|
|
156
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
157
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
158
|
+
unless required by applicable law (such as deliberate and grossly
|
|
159
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
160
|
+
liable to You for damages, including any direct, indirect, special,
|
|
161
|
+
incidental, or consequential damages of any character arising as a
|
|
162
|
+
result of this License or out of the use or inability to use the
|
|
163
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
164
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
165
|
+
other commercial damages or losses), even if such Contributor
|
|
166
|
+
has been advised of the possibility of such damages.
|
|
167
|
+
|
|
168
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
169
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
170
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
171
|
+
or other liability obligations and/or rights consistent with this
|
|
172
|
+
License. However, in accepting such obligations, You may act only
|
|
173
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
174
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
175
|
+
defend, and hold each Contributor harmless for any liability
|
|
176
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
177
|
+
of your accepting any such warranty or additional liability.
|
|
178
|
+
|
|
179
|
+
END OF TERMS AND CONDITIONS
|
|
180
|
+
|
|
181
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
182
|
+
|
|
183
|
+
To apply the Apache License to your work, attach the following
|
|
184
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
185
|
+
replaced with your own identifying information. (Don't include
|
|
186
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
187
|
+
comment syntax for the file format. We also recommend that a
|
|
188
|
+
file or class name and description of purpose be included on the
|
|
189
|
+
same "printed page" as the copyright notice for easier
|
|
190
|
+
identification within third-party archives.
|
|
191
|
+
|
|
192
|
+
Copyright [yyyy] [name of copyright owner]
|
|
193
|
+
|
|
194
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
195
|
+
you may not use this file except in compliance with the License.
|
|
196
|
+
You may obtain a copy of the License at
|
|
197
|
+
|
|
198
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
199
|
+
|
|
200
|
+
Unless required by applicable law or agreed to in writing, software
|
|
201
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
202
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
203
|
+
See the License for the specific language governing permissions and
|
|
204
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# @typestyles/vite
|
|
2
|
+
|
|
3
|
+
Vite plugin for [typestyles](https://github.com/type-styles/typestyles): style HMR in development, optional zero-runtime CSS extraction in production.
|
|
4
|
+
|
|
5
|
+
TypeStyles works without a bundler plugin — styles inject at runtime on first use. This plugin is optional but recommended for Vite apps: it keeps component state while styles hot-reload, catches duplicate namespace collisions at build time, and can emit a static `typestyles.css` file so production ships without client-side `<style>` injection.
|
|
6
|
+
|
|
7
|
+
## When to use this
|
|
8
|
+
|
|
9
|
+
| Goal | Setup |
|
|
10
|
+
| --------------------------------------------------------------- | ----------------------------------------------------------------------------------------------- |
|
|
11
|
+
| Faster dev feedback (HMR instead of full reload) | `typestyles()` with no extraction entry |
|
|
12
|
+
| Runtime in dev, static CSS in production (recommended for SPAs) | Add a [convention entry](#convention-entry) or `extract.modules` — `mode` defaults to `'build'` |
|
|
13
|
+
| Injection-only everywhere | `typestyles({ mode: 'runtime' })` or omit any extraction entry |
|
|
14
|
+
|
|
15
|
+
Full guide: [Zero-runtime extraction](https://typestyles.dev/docs/zero-runtime) · [Vite plugin docs](https://typestyles.dev/docs/vite-plugin)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install typestyles
|
|
21
|
+
npm install -D @typestyles/vite
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Peer dependency: Vite >= 5.
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
27
|
+
|
|
28
|
+
### HMR only
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
// vite.config.ts
|
|
32
|
+
import { defineConfig } from 'vite';
|
|
33
|
+
import react from '@vitejs/plugin-react';
|
|
34
|
+
import typestyles from '@typestyles/vite';
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
plugins: [react(), typestyles()],
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Edit any file that calls `styles.component`, `styles.class`, `tokens.create`, etc. Styles update in place without a full page reload.
|
|
42
|
+
|
|
43
|
+
### Runtime in dev, zero-runtime in production (recommended)
|
|
44
|
+
|
|
45
|
+
**1. Create a convention entry** that imports every style registration side effect:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
// src/typestyles-entry.ts
|
|
49
|
+
import './tokens';
|
|
50
|
+
import './components/button';
|
|
51
|
+
import './components/card';
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**2. Add the plugin** (no `mode` needed — discovery defaults to `'build'`):
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { defineConfig } from 'vite';
|
|
58
|
+
import react from '@vitejs/plugin-react';
|
|
59
|
+
import typestyles from '@typestyles/vite';
|
|
60
|
+
|
|
61
|
+
export default defineConfig({
|
|
62
|
+
plugins: [react(), typestyles()],
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**3. Link the CSS** in your HTML shell (optional — the plugin auto-injects this during dev and build when extraction is enabled):
|
|
67
|
+
|
|
68
|
+
```html
|
|
69
|
+
<link rel="stylesheet" href="/typestyles.css" />
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
During `vite dev`, styles still inject at runtime and HMR works. During `vite build`, the plugin emits `typestyles.css`, defines `__TYPESTYLES_RUNTIME_DISABLED__`, and the browser loads one cacheable stylesheet.
|
|
73
|
+
|
|
74
|
+
Example app: [`examples/vite-app`](../../examples/vite-app)
|
|
75
|
+
|
|
76
|
+
## Convention entry
|
|
77
|
+
|
|
78
|
+
When `extract.modules` is omitted, the plugin discovers the first existing file (tries `.ts`, `.tsx`, `.js`, `.mjs` per base):
|
|
79
|
+
|
|
80
|
+
```text
|
|
81
|
+
src/typestyles-entry.ts
|
|
82
|
+
src/typestyles.ts
|
|
83
|
+
src/styles/index.ts
|
|
84
|
+
src/styles.ts
|
|
85
|
+
styles/typestyles-entry.ts
|
|
86
|
+
styles/typestyles.ts
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Re-export helpers from `@typestyles/build-runner`:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { DEFAULT_EXTRACT_MODULE_CANDIDATES, discoverDefaultExtractModules } from '@typestyles/vite';
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Configuration
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
typestyles({
|
|
99
|
+
/** Default: true — fail the build on duplicate styles.component / styles.class namespaces */
|
|
100
|
+
warnDuplicates?: boolean;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* - 'runtime': HMR only, no extraction
|
|
104
|
+
* - 'build': extract on vite build; runtime stays on during vite dev (default when modules resolve)
|
|
105
|
+
* - 'hybrid': same as build for Vite today
|
|
106
|
+
*/
|
|
107
|
+
mode?: 'runtime' | 'build' | 'hybrid';
|
|
108
|
+
|
|
109
|
+
extract?: {
|
|
110
|
+
/** Explicit entry files relative to project root */
|
|
111
|
+
modules?: string[];
|
|
112
|
+
/** Output asset name. Default: 'typestyles.css' */
|
|
113
|
+
fileName?: string;
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Explicit extraction modules
|
|
119
|
+
|
|
120
|
+
Use when you have multiple entry files or non-standard paths:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
typestyles({
|
|
124
|
+
extract: {
|
|
125
|
+
modules: ['src/styles/tokens.ts', 'src/styles/components.ts'],
|
|
126
|
+
fileName: 'assets/typestyles.css',
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Force runtime-only
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
typestyles({ mode: 'runtime' });
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Even with a convention entry present, this keeps `<style>` injection in production builds.
|
|
138
|
+
|
|
139
|
+
## How it works
|
|
140
|
+
|
|
141
|
+
**HMR:** On each `.ts`/`.tsx` module that registers typestyles APIs, the plugin injects `import.meta.hot` accept/dispose handlers. On dispose, it calls `invalidateKeys` from `typestyles/hmr` so stale rules are removed before the module re-executes. HMR code is only injected during `vite dev`, not in production bundles.
|
|
142
|
+
|
|
143
|
+
**Extraction:** On `vite build`, matching entry modules are bundled and executed in Node via `@typestyles/build-runner`. The collected CSS is emitted as a static asset. In dev, the same extraction runs on demand when the browser requests `/typestyles.css` so your `<link>` href works without SPA fallback returning HTML.
|
|
144
|
+
|
|
145
|
+
**Duplicate detection:** The plugin scans every source file for namespace strings (`styles.component('button', …)` → `.button-`). Collisions across files fail the build with a clear error.
|
|
146
|
+
|
|
147
|
+
## Verify production output
|
|
148
|
+
|
|
149
|
+
Extraction is execute-and-collect: styles unreachable from your entry file are silently dropped. After `vite build`, run:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { verifyTypestylesBuild } from '@typestyles/build-runner';
|
|
153
|
+
|
|
154
|
+
verifyTypestylesBuild({
|
|
155
|
+
root: process.cwd(),
|
|
156
|
+
cssFile: 'dist/typestyles.css',
|
|
157
|
+
requiredCssSubstrings: ['.button-base'],
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
See [Zero-runtime extraction — verification](https://typestyles.dev/docs/zero-runtime#verify-the-build).
|
|
162
|
+
|
|
163
|
+
## Related packages
|
|
164
|
+
|
|
165
|
+
| Package | Use for |
|
|
166
|
+
| --------------------------------------------- | ------------------------------------- |
|
|
167
|
+
| [`@typestyles/astro`](../astro) | Astro integration (wraps this plugin) |
|
|
168
|
+
| [`@typestyles/rollup`](../rollup) | Rollup / Rolldown |
|
|
169
|
+
| [`@typestyles/esbuild`](../esbuild) | esbuild |
|
|
170
|
+
| [`@typestyles/webpack`](../webpack) | webpack |
|
|
171
|
+
| [`@typestyles/next`](../next) | Next.js App/Pages Router |
|
|
172
|
+
| [`@typestyles/build-runner`](../build-runner) | Shared extraction + CI verification |
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
Apache-2.0
|
package/dist/index.cjs
CHANGED
|
@@ -23,50 +23,40 @@ __export(index_exports, {
|
|
|
23
23
|
DEFAULT_EXTRACT_MODULE_CANDIDATES: () => import_build_runner2.DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
24
24
|
default: () => typestylesPlugin,
|
|
25
25
|
discoverDefaultExtractModules: () => import_build_runner2.discoverDefaultExtractModules,
|
|
26
|
-
extractNamespaces: () => extractNamespaces
|
|
26
|
+
extractNamespaces: () => import_build_runner.extractNamespaces,
|
|
27
|
+
htmlLinksTypestylesCss: () => htmlLinksTypestylesCss,
|
|
28
|
+
moduleNeedsOverrideHmr: () => import_build_runner3.moduleNeedsOverrideHmr,
|
|
29
|
+
resolveTypestylesCssHref: () => resolveTypestylesCssHref
|
|
27
30
|
});
|
|
28
31
|
module.exports = __toCommonJS(index_exports);
|
|
29
32
|
var import_node_path = require("path");
|
|
30
33
|
var import_build_runner = require("@typestyles/build-runner");
|
|
31
34
|
var import_build_runner2 = require("@typestyles/build-runner");
|
|
32
|
-
var
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
|
|
36
|
-
var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
|
|
37
|
-
var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
|
|
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);
|
|
35
|
+
var import_build_runner3 = require("@typestyles/build-runner");
|
|
36
|
+
function resolveExtractModulesForVite(root, extract) {
|
|
37
|
+
return (0, import_build_runner.resolveExtractModules)(root, extract);
|
|
44
38
|
}
|
|
45
|
-
function
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
for (const match of code.matchAll(STYLES_CLASS_RE)) {
|
|
52
|
-
prefixes.push(`.${match[1]}-`);
|
|
53
|
-
}
|
|
54
|
-
for (const match of code.matchAll(TOKENS_CREATE_RE)) {
|
|
55
|
-
keys.push(`tokens:${match[1]}`);
|
|
56
|
-
}
|
|
57
|
-
for (const match of code.matchAll(CREATE_THEME_RE)) {
|
|
58
|
-
keys.push(`theme:${match[1]}`);
|
|
39
|
+
function resolveTypestylesCssHref(base, fileName) {
|
|
40
|
+
const normalizedFileName = fileName.replace(/^\//, "");
|
|
41
|
+
const normalizedBase = base.replace(/\/$/, "") || "";
|
|
42
|
+
if (normalizedBase === "" || normalizedBase === "/") {
|
|
43
|
+
return `/${normalizedFileName}`;
|
|
59
44
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
for (const match of
|
|
67
|
-
|
|
45
|
+
return `${normalizedBase}/${normalizedFileName}`;
|
|
46
|
+
}
|
|
47
|
+
function htmlLinksTypestylesCss(html, cssHref, fileName) {
|
|
48
|
+
const bareName = fileName.replace(/^\//, "");
|
|
49
|
+
const linkPattern = /<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi;
|
|
50
|
+
const hrefPattern = /\bhref=["']([^"']+)["']/i;
|
|
51
|
+
for (const match of html.matchAll(linkPattern)) {
|
|
52
|
+
const hrefMatch = match[0].match(hrefPattern);
|
|
53
|
+
const href = hrefMatch?.[1];
|
|
54
|
+
if (!href) continue;
|
|
55
|
+
if (href === cssHref || href.endsWith(`/${bareName}`) || href === bareName || href === `./${bareName}`) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
68
58
|
}
|
|
69
|
-
return
|
|
59
|
+
return false;
|
|
70
60
|
}
|
|
71
61
|
function typestylesPlugin(options = {}) {
|
|
72
62
|
const { warnDuplicates = true, extract } = options;
|
|
@@ -84,8 +74,8 @@ function typestylesPlugin(options = {}) {
|
|
|
84
74
|
config(config, env) {
|
|
85
75
|
isBuildCommand = env.command === "build";
|
|
86
76
|
const root = (0, import_node_path.resolve)(config.root ?? process.cwd());
|
|
87
|
-
resolvedExtractModules =
|
|
88
|
-
resolvedMode = options.mode
|
|
77
|
+
resolvedExtractModules = resolveExtractModulesForVite(root, extract);
|
|
78
|
+
resolvedMode = (0, import_build_runner.resolveExtractMode)(options.mode, resolvedExtractModules);
|
|
89
79
|
if (env.command === "build" && (resolvedMode === "build" || resolvedMode === "hybrid")) {
|
|
90
80
|
config.define = {
|
|
91
81
|
...config.define ?? {},
|
|
@@ -160,41 +150,68 @@ function typestylesPlugin(options = {}) {
|
|
|
160
150
|
transform(code, id) {
|
|
161
151
|
if (!/\.[jt]sx?$/.test(id)) return null;
|
|
162
152
|
if (id.includes("node_modules")) return null;
|
|
163
|
-
const { keys, prefixes } = extractNamespaces(code);
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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) }).`
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
153
|
+
const { keys, prefixes } = (0, import_build_runner.extractNamespaces)(code);
|
|
154
|
+
const overrideHmr = (0, import_build_runner.moduleNeedsOverrideHmr)(code);
|
|
155
|
+
if (keys.length === 0 && prefixes.length === 0 && !overrideHmr) return null;
|
|
156
|
+
if (warnDuplicates && prefixes.length > 0) {
|
|
157
|
+
(0, import_build_runner.reportDuplicateNamespaces)(moduleNamespaces, id, prefixes, this);
|
|
158
|
+
}
|
|
159
|
+
if (keys.length > 0 || prefixes.length > 0) {
|
|
160
|
+
moduleNamespaces.set(id, { keys, prefixes });
|
|
177
161
|
}
|
|
178
|
-
moduleNamespaces.set(id, { keys, prefixes });
|
|
179
162
|
if (!isServe) {
|
|
180
163
|
return null;
|
|
181
164
|
}
|
|
182
165
|
const keysJSON = JSON.stringify(keys);
|
|
183
166
|
const prefixesJSON = JSON.stringify(prefixes);
|
|
184
|
-
const
|
|
167
|
+
const hmrImports = overrideHmr ? `import { invalidateKeys as __typestylesInvalidateKeys, createOverrideHmrSlot as __tsCreateOvHmr } from 'typestyles/hmr';
|
|
168
|
+
` : `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
|
|
185
169
|
`;
|
|
186
|
-
const
|
|
170
|
+
const overrideBootstrap = overrideHmr ? `const __tsOvHmr = __tsCreateOvHmr();
|
|
171
|
+
__tsOvHmr.activate();
|
|
172
|
+
` : "";
|
|
173
|
+
const overrideTeardown = overrideHmr ? `
|
|
174
|
+
__tsOvHmr.deactivate();
|
|
175
|
+
` : "";
|
|
176
|
+
const disposeBody = [
|
|
177
|
+
keys.length > 0 || prefixes.length > 0 ? `__typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});` : null,
|
|
178
|
+
overrideHmr ? `__tsOvHmr.dispose();` : null
|
|
179
|
+
].filter(Boolean).join("\n ");
|
|
180
|
+
const hmrCode = `${overrideTeardown}
|
|
187
181
|
if (import.meta.hot) {
|
|
188
182
|
import.meta.hot.accept();
|
|
189
183
|
import.meta.hot.dispose(() => {
|
|
190
|
-
|
|
184
|
+
${disposeBody}
|
|
191
185
|
});
|
|
192
186
|
}`;
|
|
193
187
|
return {
|
|
194
|
-
code:
|
|
188
|
+
code: hmrImports + overrideBootstrap + code + hmrCode,
|
|
195
189
|
map: null
|
|
196
190
|
};
|
|
197
191
|
},
|
|
192
|
+
transformIndexHtml: {
|
|
193
|
+
order: "pre",
|
|
194
|
+
handler(html) {
|
|
195
|
+
if (resolvedExtractModules.length === 0) return;
|
|
196
|
+
if (resolvedMode === "runtime") return;
|
|
197
|
+
if (!resolvedConfig) return;
|
|
198
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
199
|
+
const href = resolveTypestylesCssHref(resolvedConfig.base, fileName);
|
|
200
|
+
if (htmlLinksTypestylesCss(html, href, fileName)) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
return [
|
|
204
|
+
{
|
|
205
|
+
tag: "link",
|
|
206
|
+
attrs: {
|
|
207
|
+
rel: "stylesheet",
|
|
208
|
+
href
|
|
209
|
+
},
|
|
210
|
+
injectTo: "head"
|
|
211
|
+
}
|
|
212
|
+
];
|
|
213
|
+
}
|
|
214
|
+
},
|
|
198
215
|
async generateBundle() {
|
|
199
216
|
if (!isBuildCommand) return;
|
|
200
217
|
if (resolvedMode === "runtime") return;
|
|
@@ -224,5 +241,8 @@ if (import.meta.hot) {
|
|
|
224
241
|
0 && (module.exports = {
|
|
225
242
|
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
226
243
|
discoverDefaultExtractModules,
|
|
227
|
-
extractNamespaces
|
|
244
|
+
extractNamespaces,
|
|
245
|
+
htmlLinksTypestylesCss,
|
|
246
|
+
moduleNeedsOverrideHmr,
|
|
247
|
+
resolveTypestylesCssHref
|
|
228
248
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
-
|
|
2
|
+
import { TypestylesIntegrationMode, TypestylesExtractOptions } from '@typestyles/build-runner';
|
|
3
|
+
export { DEFAULT_EXTRACT_MODULE_CANDIDATES, TypestylesExtractOptions, discoverDefaultExtractModules, extractNamespaces, moduleNeedsOverrideHmr } from '@typestyles/build-runner';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
fileName?: string;
|
|
18
|
-
}
|
|
5
|
+
/**
|
|
6
|
+
* Regex patterns to extract namespace strings from typestyles API calls.
|
|
7
|
+
*
|
|
8
|
+
* Matches:
|
|
9
|
+
* styles.component('button', ...) → prefix ".button-"
|
|
10
|
+
* styles.class('card', ...) → prefix ".card-"
|
|
11
|
+
* tokens.create('color', ...) → key "tokens:color"
|
|
12
|
+
* tokens.createTheme({ name: 'dark', … }) → key "theme:dark"
|
|
13
|
+
* createTheme('dark', …) → key "theme:dark" (low-level positional)
|
|
14
|
+
* keyframes.create('fadeIn', ...) → key "keyframes:fadeIn"
|
|
15
|
+
* global.style('body', ...) → prefix "body"
|
|
16
|
+
* global.fontFace('Inter', ...) → prefix "font-face:Inter"
|
|
17
|
+
*/
|
|
19
18
|
interface TypestylesPluginOptions {
|
|
20
19
|
/**
|
|
21
20
|
* When true (default), fail the build if the same logical `styles.component` / `styles.class`
|
|
@@ -35,7 +34,7 @@ interface TypestylesPluginOptions {
|
|
|
35
34
|
* returning HTML for that URL), and production emits the CSS file. If no modules are resolved,
|
|
36
35
|
* defaults to `"runtime"`.
|
|
37
36
|
*/
|
|
38
|
-
mode?:
|
|
37
|
+
mode?: TypestylesIntegrationMode;
|
|
39
38
|
/**
|
|
40
39
|
* Options for build-time CSS extraction when mode is "build" or "hybrid".
|
|
41
40
|
*
|
|
@@ -44,14 +43,11 @@ interface TypestylesPluginOptions {
|
|
|
44
43
|
*/
|
|
45
44
|
extract?: TypestylesExtractOptions;
|
|
46
45
|
}
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
keys: string[];
|
|
53
|
-
prefixes: string[];
|
|
54
|
-
};
|
|
46
|
+
/** Public URL path for the extracted CSS asset (respects Vite `base`). */
|
|
47
|
+
declare function resolveTypestylesCssHref(base: string, fileName: string): string;
|
|
48
|
+
/** True when `html` already links the extracted stylesheet (avoids duplicate injection). */
|
|
49
|
+
declare function htmlLinksTypestylesCss(html: string, cssHref: string, fileName: string): boolean;
|
|
50
|
+
|
|
55
51
|
/**
|
|
56
52
|
* Vite plugin for typestyles HMR support.
|
|
57
53
|
*
|
|
@@ -62,4 +58,4 @@ declare function extractNamespaces(code: string): {
|
|
|
62
58
|
*/
|
|
63
59
|
declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
|
|
64
60
|
|
|
65
|
-
export { type
|
|
61
|
+
export { type TypestylesPluginOptions, typestylesPlugin as default, htmlLinksTypestylesCss, resolveTypestylesCssHref };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
-
|
|
2
|
+
import { TypestylesIntegrationMode, TypestylesExtractOptions } from '@typestyles/build-runner';
|
|
3
|
+
export { DEFAULT_EXTRACT_MODULE_CANDIDATES, TypestylesExtractOptions, discoverDefaultExtractModules, extractNamespaces, moduleNeedsOverrideHmr } from '@typestyles/build-runner';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
fileName?: string;
|
|
18
|
-
}
|
|
5
|
+
/**
|
|
6
|
+
* Regex patterns to extract namespace strings from typestyles API calls.
|
|
7
|
+
*
|
|
8
|
+
* Matches:
|
|
9
|
+
* styles.component('button', ...) → prefix ".button-"
|
|
10
|
+
* styles.class('card', ...) → prefix ".card-"
|
|
11
|
+
* tokens.create('color', ...) → key "tokens:color"
|
|
12
|
+
* tokens.createTheme({ name: 'dark', … }) → key "theme:dark"
|
|
13
|
+
* createTheme('dark', …) → key "theme:dark" (low-level positional)
|
|
14
|
+
* keyframes.create('fadeIn', ...) → key "keyframes:fadeIn"
|
|
15
|
+
* global.style('body', ...) → prefix "body"
|
|
16
|
+
* global.fontFace('Inter', ...) → prefix "font-face:Inter"
|
|
17
|
+
*/
|
|
19
18
|
interface TypestylesPluginOptions {
|
|
20
19
|
/**
|
|
21
20
|
* When true (default), fail the build if the same logical `styles.component` / `styles.class`
|
|
@@ -35,7 +34,7 @@ interface TypestylesPluginOptions {
|
|
|
35
34
|
* returning HTML for that URL), and production emits the CSS file. If no modules are resolved,
|
|
36
35
|
* defaults to `"runtime"`.
|
|
37
36
|
*/
|
|
38
|
-
mode?:
|
|
37
|
+
mode?: TypestylesIntegrationMode;
|
|
39
38
|
/**
|
|
40
39
|
* Options for build-time CSS extraction when mode is "build" or "hybrid".
|
|
41
40
|
*
|
|
@@ -44,14 +43,11 @@ interface TypestylesPluginOptions {
|
|
|
44
43
|
*/
|
|
45
44
|
extract?: TypestylesExtractOptions;
|
|
46
45
|
}
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
keys: string[];
|
|
53
|
-
prefixes: string[];
|
|
54
|
-
};
|
|
46
|
+
/** Public URL path for the extracted CSS asset (respects Vite `base`). */
|
|
47
|
+
declare function resolveTypestylesCssHref(base: string, fileName: string): string;
|
|
48
|
+
/** True when `html` already links the extracted stylesheet (avoids duplicate injection). */
|
|
49
|
+
declare function htmlLinksTypestylesCss(html: string, cssHref: string, fileName: string): boolean;
|
|
50
|
+
|
|
55
51
|
/**
|
|
56
52
|
* Vite plugin for typestyles HMR support.
|
|
57
53
|
*
|
|
@@ -62,4 +58,4 @@ declare function extractNamespaces(code: string): {
|
|
|
62
58
|
*/
|
|
63
59
|
declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
|
|
64
60
|
|
|
65
|
-
export { type
|
|
61
|
+
export { type TypestylesPluginOptions, typestylesPlugin as default, htmlLinksTypestylesCss, resolveTypestylesCssHref };
|
package/dist/index.js
CHANGED
|
@@ -1,48 +1,42 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { resolve as resolvePath } from "path";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
extractNamespaces,
|
|
5
|
+
moduleNeedsOverrideHmr,
|
|
6
|
+
reportDuplicateNamespaces,
|
|
7
|
+
resolveExtractMode,
|
|
8
|
+
resolveExtractModules,
|
|
9
|
+
runTypestylesBuild
|
|
10
|
+
} from "@typestyles/build-runner";
|
|
4
11
|
import {
|
|
5
12
|
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
6
|
-
discoverDefaultExtractModules
|
|
13
|
+
discoverDefaultExtractModules
|
|
7
14
|
} from "@typestyles/build-runner";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var CREATE_THEME_RE = /(?:tokens\.)?createTheme\(\s*['"]([^'"]+)['"]/g;
|
|
12
|
-
var KEYFRAMES_CREATE_RE = /keyframes\.create\(\s*['"]([^'"]+)['"]/g;
|
|
13
|
-
var GLOBAL_STYLE_RE = /global\.style\(\s*['"]([^'"]+)['"]/g;
|
|
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);
|
|
15
|
+
import { moduleNeedsOverrideHmr as moduleNeedsOverrideHmr2 } from "@typestyles/build-runner";
|
|
16
|
+
function resolveExtractModulesForVite(root, extract) {
|
|
17
|
+
return resolveExtractModules(root, extract);
|
|
20
18
|
}
|
|
21
|
-
function
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
for (const match of code.matchAll(STYLES_CLASS_RE)) {
|
|
28
|
-
prefixes.push(`.${match[1]}-`);
|
|
29
|
-
}
|
|
30
|
-
for (const match of code.matchAll(TOKENS_CREATE_RE)) {
|
|
31
|
-
keys.push(`tokens:${match[1]}`);
|
|
19
|
+
function resolveTypestylesCssHref(base, fileName) {
|
|
20
|
+
const normalizedFileName = fileName.replace(/^\//, "");
|
|
21
|
+
const normalizedBase = base.replace(/\/$/, "") || "";
|
|
22
|
+
if (normalizedBase === "" || normalizedBase === "/") {
|
|
23
|
+
return `/${normalizedFileName}`;
|
|
32
24
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
for (const match of
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
25
|
+
return `${normalizedBase}/${normalizedFileName}`;
|
|
26
|
+
}
|
|
27
|
+
function htmlLinksTypestylesCss(html, cssHref, fileName) {
|
|
28
|
+
const bareName = fileName.replace(/^\//, "");
|
|
29
|
+
const linkPattern = /<link\b[^>]*\brel=["']stylesheet["'][^>]*>/gi;
|
|
30
|
+
const hrefPattern = /\bhref=["']([^"']+)["']/i;
|
|
31
|
+
for (const match of html.matchAll(linkPattern)) {
|
|
32
|
+
const hrefMatch = match[0].match(hrefPattern);
|
|
33
|
+
const href = hrefMatch?.[1];
|
|
34
|
+
if (!href) continue;
|
|
35
|
+
if (href === cssHref || href.endsWith(`/${bareName}`) || href === bareName || href === `./${bareName}`) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
44
38
|
}
|
|
45
|
-
return
|
|
39
|
+
return false;
|
|
46
40
|
}
|
|
47
41
|
function typestylesPlugin(options = {}) {
|
|
48
42
|
const { warnDuplicates = true, extract } = options;
|
|
@@ -60,8 +54,8 @@ function typestylesPlugin(options = {}) {
|
|
|
60
54
|
config(config, env) {
|
|
61
55
|
isBuildCommand = env.command === "build";
|
|
62
56
|
const root = resolvePath(config.root ?? process.cwd());
|
|
63
|
-
resolvedExtractModules =
|
|
64
|
-
resolvedMode = options.mode
|
|
57
|
+
resolvedExtractModules = resolveExtractModulesForVite(root, extract);
|
|
58
|
+
resolvedMode = resolveExtractMode(options.mode, resolvedExtractModules);
|
|
65
59
|
if (env.command === "build" && (resolvedMode === "build" || resolvedMode === "hybrid")) {
|
|
66
60
|
config.define = {
|
|
67
61
|
...config.define ?? {},
|
|
@@ -137,40 +131,67 @@ function typestylesPlugin(options = {}) {
|
|
|
137
131
|
if (!/\.[jt]sx?$/.test(id)) return null;
|
|
138
132
|
if (id.includes("node_modules")) return null;
|
|
139
133
|
const { keys, prefixes } = extractNamespaces(code);
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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) }).`
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
134
|
+
const overrideHmr = moduleNeedsOverrideHmr(code);
|
|
135
|
+
if (keys.length === 0 && prefixes.length === 0 && !overrideHmr) return null;
|
|
136
|
+
if (warnDuplicates && prefixes.length > 0) {
|
|
137
|
+
reportDuplicateNamespaces(moduleNamespaces, id, prefixes, this);
|
|
138
|
+
}
|
|
139
|
+
if (keys.length > 0 || prefixes.length > 0) {
|
|
140
|
+
moduleNamespaces.set(id, { keys, prefixes });
|
|
153
141
|
}
|
|
154
|
-
moduleNamespaces.set(id, { keys, prefixes });
|
|
155
142
|
if (!isServe) {
|
|
156
143
|
return null;
|
|
157
144
|
}
|
|
158
145
|
const keysJSON = JSON.stringify(keys);
|
|
159
146
|
const prefixesJSON = JSON.stringify(prefixes);
|
|
160
|
-
const
|
|
147
|
+
const hmrImports = overrideHmr ? `import { invalidateKeys as __typestylesInvalidateKeys, createOverrideHmrSlot as __tsCreateOvHmr } from 'typestyles/hmr';
|
|
148
|
+
` : `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
|
|
161
149
|
`;
|
|
162
|
-
const
|
|
150
|
+
const overrideBootstrap = overrideHmr ? `const __tsOvHmr = __tsCreateOvHmr();
|
|
151
|
+
__tsOvHmr.activate();
|
|
152
|
+
` : "";
|
|
153
|
+
const overrideTeardown = overrideHmr ? `
|
|
154
|
+
__tsOvHmr.deactivate();
|
|
155
|
+
` : "";
|
|
156
|
+
const disposeBody = [
|
|
157
|
+
keys.length > 0 || prefixes.length > 0 ? `__typestylesInvalidateKeys(${keysJSON}, ${prefixesJSON});` : null,
|
|
158
|
+
overrideHmr ? `__tsOvHmr.dispose();` : null
|
|
159
|
+
].filter(Boolean).join("\n ");
|
|
160
|
+
const hmrCode = `${overrideTeardown}
|
|
163
161
|
if (import.meta.hot) {
|
|
164
162
|
import.meta.hot.accept();
|
|
165
163
|
import.meta.hot.dispose(() => {
|
|
166
|
-
|
|
164
|
+
${disposeBody}
|
|
167
165
|
});
|
|
168
166
|
}`;
|
|
169
167
|
return {
|
|
170
|
-
code:
|
|
168
|
+
code: hmrImports + overrideBootstrap + code + hmrCode,
|
|
171
169
|
map: null
|
|
172
170
|
};
|
|
173
171
|
},
|
|
172
|
+
transformIndexHtml: {
|
|
173
|
+
order: "pre",
|
|
174
|
+
handler(html) {
|
|
175
|
+
if (resolvedExtractModules.length === 0) return;
|
|
176
|
+
if (resolvedMode === "runtime") return;
|
|
177
|
+
if (!resolvedConfig) return;
|
|
178
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
179
|
+
const href = resolveTypestylesCssHref(resolvedConfig.base, fileName);
|
|
180
|
+
if (htmlLinksTypestylesCss(html, href, fileName)) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
return [
|
|
184
|
+
{
|
|
185
|
+
tag: "link",
|
|
186
|
+
attrs: {
|
|
187
|
+
rel: "stylesheet",
|
|
188
|
+
href
|
|
189
|
+
},
|
|
190
|
+
injectTo: "head"
|
|
191
|
+
}
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
},
|
|
174
195
|
async generateBundle() {
|
|
175
196
|
if (!isBuildCommand) return;
|
|
176
197
|
if (resolvedMode === "runtime") return;
|
|
@@ -199,6 +220,9 @@ if (import.meta.hot) {
|
|
|
199
220
|
export {
|
|
200
221
|
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
201
222
|
typestylesPlugin as default,
|
|
202
|
-
|
|
203
|
-
extractNamespaces
|
|
223
|
+
discoverDefaultExtractModules,
|
|
224
|
+
extractNamespaces,
|
|
225
|
+
htmlLinksTypestylesCss,
|
|
226
|
+
moduleNeedsOverrideHmr2 as moduleNeedsOverrideHmr,
|
|
227
|
+
resolveTypestylesCssHref
|
|
204
228
|
};
|
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.7a3ebfd340c8",
|
|
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.7a3ebfd340c8",
|
|
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.7a3ebfd340c8"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external typestyles,@typestyles/build-runner",
|