@typestyles/vite 0.0.0-unstable.aecc3ce3af10 → 0.0.0-unstable.b8d219d17c27
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 +162 -62
- package/dist/index.d.cts +41 -27
- package/dist/index.d.ts +41 -27
- package/dist/index.js +166 -61
- package/package.json +6 -4
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
|
@@ -20,56 +20,63 @@ 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,
|
|
24
|
-
|
|
25
|
+
discoverDefaultExtractModules: () => import_build_runner2.discoverDefaultExtractModules,
|
|
26
|
+
extractNamespaces: () => import_build_runner.extractNamespaces,
|
|
27
|
+
htmlLinksTypestylesCss: () => htmlLinksTypestylesCss,
|
|
28
|
+
moduleNeedsOverrideHmr: () => import_build_runner3.moduleNeedsOverrideHmr,
|
|
29
|
+
resolveTypestylesCssHref: () => resolveTypestylesCssHref
|
|
25
30
|
});
|
|
26
31
|
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
var import_node_path = require("path");
|
|
27
33
|
var import_build_runner = require("@typestyles/build-runner");
|
|
28
|
-
var
|
|
29
|
-
var
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const prefixes = [];
|
|
39
|
-
for (const match of code.matchAll(STYLES_CREATE_RE)) {
|
|
40
|
-
prefixes.push(`.${match[1]}-`);
|
|
41
|
-
}
|
|
42
|
-
for (const match of code.matchAll(STYLES_COMPONENT_RE)) {
|
|
43
|
-
prefixes.push(`.${match[1]}-`);
|
|
44
|
-
}
|
|
45
|
-
for (const match of code.matchAll(TOKENS_CREATE_RE)) {
|
|
46
|
-
keys.push(`tokens:${match[1]}`);
|
|
47
|
-
}
|
|
48
|
-
for (const match of code.matchAll(CREATE_THEME_RE)) {
|
|
49
|
-
keys.push(`theme:${match[1]}`);
|
|
50
|
-
}
|
|
51
|
-
for (const match of code.matchAll(KEYFRAMES_CREATE_RE)) {
|
|
52
|
-
keys.push(`keyframes:${match[1]}`);
|
|
53
|
-
}
|
|
54
|
-
for (const match of code.matchAll(GLOBAL_STYLE_RE)) {
|
|
55
|
-
prefixes.push(match[1]);
|
|
34
|
+
var import_build_runner2 = require("@typestyles/build-runner");
|
|
35
|
+
var import_build_runner3 = require("@typestyles/build-runner");
|
|
36
|
+
function resolveExtractModulesForVite(root, extract) {
|
|
37
|
+
return (0, import_build_runner.resolveExtractModules)(root, extract);
|
|
38
|
+
}
|
|
39
|
+
function resolveTypestylesCssHref(base, fileName) {
|
|
40
|
+
const normalizedFileName = fileName.replace(/^\//, "");
|
|
41
|
+
const normalizedBase = base.replace(/\/$/, "") || "";
|
|
42
|
+
if (normalizedBase === "" || normalizedBase === "/") {
|
|
43
|
+
return `/${normalizedFileName}`;
|
|
56
44
|
}
|
|
57
|
-
|
|
58
|
-
|
|
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
|
+
}
|
|
59
58
|
}
|
|
60
|
-
return
|
|
59
|
+
return false;
|
|
61
60
|
}
|
|
62
61
|
function typestylesPlugin(options = {}) {
|
|
63
|
-
const { warnDuplicates = true,
|
|
62
|
+
const { warnDuplicates = true, extract } = options;
|
|
63
|
+
let resolvedExtractModules = [];
|
|
64
|
+
let resolvedMode = "runtime";
|
|
64
65
|
const moduleNamespaces = /* @__PURE__ */ new Map();
|
|
65
66
|
let resolvedConfig = null;
|
|
66
67
|
let isBuildCommand = false;
|
|
68
|
+
let isServe = false;
|
|
69
|
+
let devExtractCss = null;
|
|
70
|
+
let devExtractInFlight = null;
|
|
67
71
|
return {
|
|
68
72
|
name: "typestyles",
|
|
69
73
|
enforce: "pre",
|
|
70
74
|
config(config, env) {
|
|
71
75
|
isBuildCommand = env.command === "build";
|
|
72
|
-
|
|
76
|
+
const root = (0, import_node_path.resolve)(config.root ?? process.cwd());
|
|
77
|
+
resolvedExtractModules = resolveExtractModulesForVite(root, extract);
|
|
78
|
+
resolvedMode = (0, import_build_runner.resolveExtractMode)(options.mode, resolvedExtractModules);
|
|
79
|
+
if (env.command === "build" && (resolvedMode === "build" || resolvedMode === "hybrid")) {
|
|
73
80
|
config.define = {
|
|
74
81
|
...config.define ?? {},
|
|
75
82
|
// Inlined by the bundler so typestyles sheet skips creating <style> in production
|
|
@@ -80,54 +87,142 @@ function typestylesPlugin(options = {}) {
|
|
|
80
87
|
},
|
|
81
88
|
configResolved(config) {
|
|
82
89
|
resolvedConfig = config;
|
|
90
|
+
isServe = config.command === "serve";
|
|
91
|
+
},
|
|
92
|
+
configureServer(server) {
|
|
93
|
+
if (resolvedExtractModules.length === 0) return;
|
|
94
|
+
if (resolvedMode === "runtime") return;
|
|
95
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
96
|
+
const invalidateDevExtract = () => {
|
|
97
|
+
devExtractCss = null;
|
|
98
|
+
devExtractInFlight = null;
|
|
99
|
+
};
|
|
100
|
+
server.watcher.on("change", invalidateDevExtract);
|
|
101
|
+
server.watcher.on("add", invalidateDevExtract);
|
|
102
|
+
server.watcher.on("unlink", invalidateDevExtract);
|
|
103
|
+
server.middlewares.use(async (req, res, next) => {
|
|
104
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
105
|
+
next();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const config = resolvedConfig;
|
|
109
|
+
if (!config) {
|
|
110
|
+
next();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const url = req.url?.split("?")[0] ?? "";
|
|
114
|
+
const base = config.base.replace(/\/$/, "") || "";
|
|
115
|
+
const expectedPath = base === "" || base === "/" ? `/${fileName.replace(/^\//, "")}` : `${base}/${fileName.replace(/^\//, "")}`;
|
|
116
|
+
if (url !== expectedPath) {
|
|
117
|
+
next();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const root = config.root ?? process.cwd();
|
|
121
|
+
try {
|
|
122
|
+
if (devExtractCss !== null) {
|
|
123
|
+
res.statusCode = 200;
|
|
124
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
125
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
126
|
+
res.end(devExtractCss);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (!devExtractInFlight) {
|
|
130
|
+
devExtractInFlight = (0, import_build_runner.runTypestylesBuild)({
|
|
131
|
+
root,
|
|
132
|
+
modules: resolvedExtractModules
|
|
133
|
+
}).then((css2) => {
|
|
134
|
+
devExtractCss = css2;
|
|
135
|
+
return css2;
|
|
136
|
+
}).finally(() => {
|
|
137
|
+
devExtractInFlight = null;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
const css = await devExtractInFlight;
|
|
141
|
+
res.statusCode = 200;
|
|
142
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
143
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
144
|
+
res.end(css);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
next(err);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
83
149
|
},
|
|
84
150
|
transform(code, id) {
|
|
85
151
|
if (!/\.[jt]sx?$/.test(id)) return null;
|
|
86
152
|
if (id.includes("node_modules")) return null;
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
if (keys.length === 0 && prefixes.length === 0) return null;
|
|
90
|
-
if (warnDuplicates) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
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 });
|
|
161
|
+
}
|
|
162
|
+
if (!isServe) {
|
|
163
|
+
return null;
|
|
102
164
|
}
|
|
103
|
-
moduleNamespaces.set(id, { keys, prefixes });
|
|
104
165
|
const keysJSON = JSON.stringify(keys);
|
|
105
166
|
const prefixesJSON = JSON.stringify(prefixes);
|
|
106
|
-
const
|
|
167
|
+
const hmrImports = overrideHmr ? `import { invalidateKeys as __typestylesInvalidateKeys, createOverrideHmrSlot as __tsCreateOvHmr } from 'typestyles/hmr';
|
|
168
|
+
` : `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
|
|
169
|
+
`;
|
|
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}
|
|
107
181
|
if (import.meta.hot) {
|
|
108
|
-
import
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
__typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
|
|
112
|
-
});
|
|
182
|
+
import.meta.hot.accept();
|
|
183
|
+
import.meta.hot.dispose(() => {
|
|
184
|
+
${disposeBody}
|
|
113
185
|
});
|
|
114
186
|
}`;
|
|
115
187
|
return {
|
|
116
|
-
code: code + hmrCode,
|
|
188
|
+
code: hmrImports + overrideBootstrap + code + hmrCode,
|
|
117
189
|
map: null
|
|
118
190
|
};
|
|
119
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
|
+
},
|
|
120
215
|
async generateBundle() {
|
|
121
216
|
if (!isBuildCommand) return;
|
|
122
|
-
if (
|
|
123
|
-
if (!
|
|
217
|
+
if (resolvedMode === "runtime") return;
|
|
218
|
+
if (!resolvedExtractModules.length) return;
|
|
124
219
|
if (!resolvedConfig) return;
|
|
125
220
|
const root = resolvedConfig.root ?? process.cwd();
|
|
126
|
-
const fileName = extract
|
|
221
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
127
222
|
try {
|
|
128
223
|
const css = await (0, import_build_runner.runTypestylesBuild)({
|
|
129
224
|
root,
|
|
130
|
-
modules:
|
|
225
|
+
modules: resolvedExtractModules
|
|
131
226
|
});
|
|
132
227
|
this.emitFile({
|
|
133
228
|
type: "asset",
|
|
@@ -144,5 +239,10 @@ if (import.meta.hot) {
|
|
|
144
239
|
}
|
|
145
240
|
// Annotate the CommonJS export names for ESM import in node:
|
|
146
241
|
0 && (module.exports = {
|
|
147
|
-
|
|
242
|
+
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
243
|
+
discoverDefaultExtractModules,
|
|
244
|
+
extractNamespaces,
|
|
245
|
+
htmlLinksTypestylesCss,
|
|
246
|
+
moduleNeedsOverrideHmr,
|
|
247
|
+
resolveTypestylesCssHref
|
|
148
248
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,46 +1,60 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
+
import { TypestylesIntegrationMode, TypestylesExtractOptions } from '@typestyles/build-runner';
|
|
3
|
+
export { DEFAULT_EXTRACT_MODULE_CANDIDATES, TypestylesExtractOptions, discoverDefaultExtractModules, extractNamespaces, moduleNeedsOverrideHmr } from '@typestyles/build-runner';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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('dark', ...) → key "theme:dark" (also createTheme('dark', ...))
|
|
13
|
+
* keyframes.create('fadeIn', ...) → key "keyframes:fadeIn"
|
|
14
|
+
* global.style('body', ...) → prefix "body"
|
|
15
|
+
* global.fontFace('Inter', ...) → prefix "font-face:Inter"
|
|
16
|
+
*/
|
|
17
|
+
interface TypestylesPluginOptions {
|
|
9
18
|
/**
|
|
10
|
-
*
|
|
19
|
+
* When true (default), fail the build if the same logical `styles.component` / `styles.class`
|
|
20
|
+
* namespace appears in more than one module. Set to `false` to skip this check.
|
|
11
21
|
*/
|
|
12
|
-
fileName?: string;
|
|
13
|
-
}
|
|
14
|
-
interface TypestylesPluginOptions {
|
|
15
|
-
/** Warn about duplicate namespaces across modules. Defaults to true. */
|
|
16
22
|
warnDuplicates?: boolean;
|
|
17
23
|
/**
|
|
18
24
|
* Mode for typestyles integration:
|
|
19
|
-
* - "runtime"
|
|
20
|
-
* - "build"
|
|
21
|
-
* - "hybrid"
|
|
25
|
+
* - `"runtime"`: HMR only, no build-time extraction.
|
|
26
|
+
* - `"build"`: emit a static CSS asset on `vite build` and disable client injection there.
|
|
27
|
+
* - `"hybrid"`: extract on build and keep runtime-compatible behavior.
|
|
28
|
+
*
|
|
29
|
+
* When omitted and at least one extraction module is resolved (explicit `extract.modules`, or a
|
|
30
|
+
* discovered convention entry file), defaults to `"build"`: `vite dev` keeps runtime
|
|
31
|
+
* injection + HMR (runtime is only disabled during `vite build`), serves the same extracted CSS
|
|
32
|
+
* at `extract.fileName` so `<link href="/typestyles.css">` is valid (avoids SPA fallback
|
|
33
|
+
* returning HTML for that URL), and production emits the CSS file. If no modules are resolved,
|
|
34
|
+
* defaults to `"runtime"`.
|
|
22
35
|
*/
|
|
23
|
-
mode?:
|
|
36
|
+
mode?: TypestylesIntegrationMode;
|
|
24
37
|
/**
|
|
25
38
|
* Options for build-time CSS extraction when mode is "build" or "hybrid".
|
|
39
|
+
*
|
|
40
|
+
* Omitted entirely: discover a convention entry under the project root (see
|
|
41
|
+
* {@link discoverDefaultExtractModules}); if none exist, stays in runtime-only mode.
|
|
26
42
|
*/
|
|
27
43
|
extract?: TypestylesExtractOptions;
|
|
28
44
|
}
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
keys: string[];
|
|
35
|
-
prefixes: string[];
|
|
36
|
-
};
|
|
45
|
+
/** Public URL path for the extracted CSS asset (respects Vite `base`). */
|
|
46
|
+
declare function resolveTypestylesCssHref(base: string, fileName: string): string;
|
|
47
|
+
/** True when `html` already links the extracted stylesheet (avoids duplicate injection). */
|
|
48
|
+
declare function htmlLinksTypestylesCss(html: string, cssHref: string, fileName: string): boolean;
|
|
49
|
+
|
|
37
50
|
/**
|
|
38
51
|
* Vite plugin for typestyles HMR support.
|
|
39
52
|
*
|
|
40
|
-
* When a module that
|
|
41
|
-
* accept/dispose handlers that invalidate
|
|
42
|
-
* before re-execution
|
|
53
|
+
* When a module that registers typestyles (e.g. `styles.component`, `styles.class`, `tokens.create`) is edited,
|
|
54
|
+
* this plugin injects HMR accept/dispose handlers that invalidate that module's registrations
|
|
55
|
+
* before re-execution. Files may import from a local re-export such as `./typestyles` rather
|
|
56
|
+
* than `from 'typestyles'` directly — we key off API call patterns in the source, not the import path.
|
|
43
57
|
*/
|
|
44
58
|
declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
|
|
45
59
|
|
|
46
|
-
export { type
|
|
60
|
+
export { type TypestylesPluginOptions, typestylesPlugin as default, htmlLinksTypestylesCss, resolveTypestylesCssHref };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,46 +1,60 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
+
import { TypestylesIntegrationMode, TypestylesExtractOptions } from '@typestyles/build-runner';
|
|
3
|
+
export { DEFAULT_EXTRACT_MODULE_CANDIDATES, TypestylesExtractOptions, discoverDefaultExtractModules, extractNamespaces, moduleNeedsOverrideHmr } from '@typestyles/build-runner';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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('dark', ...) → key "theme:dark" (also createTheme('dark', ...))
|
|
13
|
+
* keyframes.create('fadeIn', ...) → key "keyframes:fadeIn"
|
|
14
|
+
* global.style('body', ...) → prefix "body"
|
|
15
|
+
* global.fontFace('Inter', ...) → prefix "font-face:Inter"
|
|
16
|
+
*/
|
|
17
|
+
interface TypestylesPluginOptions {
|
|
9
18
|
/**
|
|
10
|
-
*
|
|
19
|
+
* When true (default), fail the build if the same logical `styles.component` / `styles.class`
|
|
20
|
+
* namespace appears in more than one module. Set to `false` to skip this check.
|
|
11
21
|
*/
|
|
12
|
-
fileName?: string;
|
|
13
|
-
}
|
|
14
|
-
interface TypestylesPluginOptions {
|
|
15
|
-
/** Warn about duplicate namespaces across modules. Defaults to true. */
|
|
16
22
|
warnDuplicates?: boolean;
|
|
17
23
|
/**
|
|
18
24
|
* Mode for typestyles integration:
|
|
19
|
-
* - "runtime"
|
|
20
|
-
* - "build"
|
|
21
|
-
* - "hybrid"
|
|
25
|
+
* - `"runtime"`: HMR only, no build-time extraction.
|
|
26
|
+
* - `"build"`: emit a static CSS asset on `vite build` and disable client injection there.
|
|
27
|
+
* - `"hybrid"`: extract on build and keep runtime-compatible behavior.
|
|
28
|
+
*
|
|
29
|
+
* When omitted and at least one extraction module is resolved (explicit `extract.modules`, or a
|
|
30
|
+
* discovered convention entry file), defaults to `"build"`: `vite dev` keeps runtime
|
|
31
|
+
* injection + HMR (runtime is only disabled during `vite build`), serves the same extracted CSS
|
|
32
|
+
* at `extract.fileName` so `<link href="/typestyles.css">` is valid (avoids SPA fallback
|
|
33
|
+
* returning HTML for that URL), and production emits the CSS file. If no modules are resolved,
|
|
34
|
+
* defaults to `"runtime"`.
|
|
22
35
|
*/
|
|
23
|
-
mode?:
|
|
36
|
+
mode?: TypestylesIntegrationMode;
|
|
24
37
|
/**
|
|
25
38
|
* Options for build-time CSS extraction when mode is "build" or "hybrid".
|
|
39
|
+
*
|
|
40
|
+
* Omitted entirely: discover a convention entry under the project root (see
|
|
41
|
+
* {@link discoverDefaultExtractModules}); if none exist, stays in runtime-only mode.
|
|
26
42
|
*/
|
|
27
43
|
extract?: TypestylesExtractOptions;
|
|
28
44
|
}
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
keys: string[];
|
|
35
|
-
prefixes: string[];
|
|
36
|
-
};
|
|
45
|
+
/** Public URL path for the extracted CSS asset (respects Vite `base`). */
|
|
46
|
+
declare function resolveTypestylesCssHref(base: string, fileName: string): string;
|
|
47
|
+
/** True when `html` already links the extracted stylesheet (avoids duplicate injection). */
|
|
48
|
+
declare function htmlLinksTypestylesCss(html: string, cssHref: string, fileName: string): boolean;
|
|
49
|
+
|
|
37
50
|
/**
|
|
38
51
|
* Vite plugin for typestyles HMR support.
|
|
39
52
|
*
|
|
40
|
-
* When a module that
|
|
41
|
-
* accept/dispose handlers that invalidate
|
|
42
|
-
* before re-execution
|
|
53
|
+
* When a module that registers typestyles (e.g. `styles.component`, `styles.class`, `tokens.create`) is edited,
|
|
54
|
+
* this plugin injects HMR accept/dispose handlers that invalidate that module's registrations
|
|
55
|
+
* before re-execution. Files may import from a local re-export such as `./typestyles` rather
|
|
56
|
+
* than `from 'typestyles'` directly — we key off API call patterns in the source, not the import path.
|
|
43
57
|
*/
|
|
44
58
|
declare function typestylesPlugin(options?: TypestylesPluginOptions): Plugin;
|
|
45
59
|
|
|
46
|
-
export { type
|
|
60
|
+
export { type TypestylesPluginOptions, typestylesPlugin as default, htmlLinksTypestylesCss, resolveTypestylesCssHref };
|
package/dist/index.js
CHANGED
|
@@ -1,50 +1,62 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
keys.push(`theme:${match[1]}`);
|
|
25
|
-
}
|
|
26
|
-
for (const match of code.matchAll(KEYFRAMES_CREATE_RE)) {
|
|
27
|
-
keys.push(`keyframes:${match[1]}`);
|
|
28
|
-
}
|
|
29
|
-
for (const match of code.matchAll(GLOBAL_STYLE_RE)) {
|
|
30
|
-
prefixes.push(match[1]);
|
|
2
|
+
import { resolve as resolvePath } from "path";
|
|
3
|
+
import {
|
|
4
|
+
extractNamespaces,
|
|
5
|
+
moduleNeedsOverrideHmr,
|
|
6
|
+
reportDuplicateNamespaces,
|
|
7
|
+
resolveExtractMode,
|
|
8
|
+
resolveExtractModules,
|
|
9
|
+
runTypestylesBuild
|
|
10
|
+
} from "@typestyles/build-runner";
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
13
|
+
discoverDefaultExtractModules
|
|
14
|
+
} from "@typestyles/build-runner";
|
|
15
|
+
import { moduleNeedsOverrideHmr as moduleNeedsOverrideHmr2 } from "@typestyles/build-runner";
|
|
16
|
+
function resolveExtractModulesForVite(root, extract) {
|
|
17
|
+
return resolveExtractModules(root, extract);
|
|
18
|
+
}
|
|
19
|
+
function resolveTypestylesCssHref(base, fileName) {
|
|
20
|
+
const normalizedFileName = fileName.replace(/^\//, "");
|
|
21
|
+
const normalizedBase = base.replace(/\/$/, "") || "";
|
|
22
|
+
if (normalizedBase === "" || normalizedBase === "/") {
|
|
23
|
+
return `/${normalizedFileName}`;
|
|
31
24
|
}
|
|
32
|
-
|
|
33
|
-
|
|
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
|
+
}
|
|
34
38
|
}
|
|
35
|
-
return
|
|
39
|
+
return false;
|
|
36
40
|
}
|
|
37
41
|
function typestylesPlugin(options = {}) {
|
|
38
|
-
const { warnDuplicates = true,
|
|
42
|
+
const { warnDuplicates = true, extract } = options;
|
|
43
|
+
let resolvedExtractModules = [];
|
|
44
|
+
let resolvedMode = "runtime";
|
|
39
45
|
const moduleNamespaces = /* @__PURE__ */ new Map();
|
|
40
46
|
let resolvedConfig = null;
|
|
41
47
|
let isBuildCommand = false;
|
|
48
|
+
let isServe = false;
|
|
49
|
+
let devExtractCss = null;
|
|
50
|
+
let devExtractInFlight = null;
|
|
42
51
|
return {
|
|
43
52
|
name: "typestyles",
|
|
44
53
|
enforce: "pre",
|
|
45
54
|
config(config, env) {
|
|
46
55
|
isBuildCommand = env.command === "build";
|
|
47
|
-
|
|
56
|
+
const root = resolvePath(config.root ?? process.cwd());
|
|
57
|
+
resolvedExtractModules = resolveExtractModulesForVite(root, extract);
|
|
58
|
+
resolvedMode = resolveExtractMode(options.mode, resolvedExtractModules);
|
|
59
|
+
if (env.command === "build" && (resolvedMode === "build" || resolvedMode === "hybrid")) {
|
|
48
60
|
config.define = {
|
|
49
61
|
...config.define ?? {},
|
|
50
62
|
// Inlined by the bundler so typestyles sheet skips creating <style> in production
|
|
@@ -55,54 +67,142 @@ function typestylesPlugin(options = {}) {
|
|
|
55
67
|
},
|
|
56
68
|
configResolved(config) {
|
|
57
69
|
resolvedConfig = config;
|
|
70
|
+
isServe = config.command === "serve";
|
|
71
|
+
},
|
|
72
|
+
configureServer(server) {
|
|
73
|
+
if (resolvedExtractModules.length === 0) return;
|
|
74
|
+
if (resolvedMode === "runtime") return;
|
|
75
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
76
|
+
const invalidateDevExtract = () => {
|
|
77
|
+
devExtractCss = null;
|
|
78
|
+
devExtractInFlight = null;
|
|
79
|
+
};
|
|
80
|
+
server.watcher.on("change", invalidateDevExtract);
|
|
81
|
+
server.watcher.on("add", invalidateDevExtract);
|
|
82
|
+
server.watcher.on("unlink", invalidateDevExtract);
|
|
83
|
+
server.middlewares.use(async (req, res, next) => {
|
|
84
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
85
|
+
next();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const config = resolvedConfig;
|
|
89
|
+
if (!config) {
|
|
90
|
+
next();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const url = req.url?.split("?")[0] ?? "";
|
|
94
|
+
const base = config.base.replace(/\/$/, "") || "";
|
|
95
|
+
const expectedPath = base === "" || base === "/" ? `/${fileName.replace(/^\//, "")}` : `${base}/${fileName.replace(/^\//, "")}`;
|
|
96
|
+
if (url !== expectedPath) {
|
|
97
|
+
next();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const root = config.root ?? process.cwd();
|
|
101
|
+
try {
|
|
102
|
+
if (devExtractCss !== null) {
|
|
103
|
+
res.statusCode = 200;
|
|
104
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
105
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
106
|
+
res.end(devExtractCss);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (!devExtractInFlight) {
|
|
110
|
+
devExtractInFlight = runTypestylesBuild({
|
|
111
|
+
root,
|
|
112
|
+
modules: resolvedExtractModules
|
|
113
|
+
}).then((css2) => {
|
|
114
|
+
devExtractCss = css2;
|
|
115
|
+
return css2;
|
|
116
|
+
}).finally(() => {
|
|
117
|
+
devExtractInFlight = null;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const css = await devExtractInFlight;
|
|
121
|
+
res.statusCode = 200;
|
|
122
|
+
res.setHeader("Content-Type", "text/css; charset=utf-8");
|
|
123
|
+
res.setHeader("Cache-Control", "no-cache");
|
|
124
|
+
res.end(css);
|
|
125
|
+
} catch (err) {
|
|
126
|
+
next(err);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
58
129
|
},
|
|
59
130
|
transform(code, id) {
|
|
60
131
|
if (!/\.[jt]sx?$/.test(id)) return null;
|
|
61
132
|
if (id.includes("node_modules")) return null;
|
|
62
|
-
if (!TYPESTYLES_IMPORT_RE.test(code)) return null;
|
|
63
133
|
const { keys, prefixes } = extractNamespaces(code);
|
|
64
|
-
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
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 });
|
|
141
|
+
}
|
|
142
|
+
if (!isServe) {
|
|
143
|
+
return null;
|
|
77
144
|
}
|
|
78
|
-
moduleNamespaces.set(id, { keys, prefixes });
|
|
79
145
|
const keysJSON = JSON.stringify(keys);
|
|
80
146
|
const prefixesJSON = JSON.stringify(prefixes);
|
|
81
|
-
const
|
|
147
|
+
const hmrImports = overrideHmr ? `import { invalidateKeys as __typestylesInvalidateKeys, createOverrideHmrSlot as __tsCreateOvHmr } from 'typestyles/hmr';
|
|
148
|
+
` : `import { invalidateKeys as __typestylesInvalidateKeys } from 'typestyles/hmr';
|
|
149
|
+
`;
|
|
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}
|
|
82
161
|
if (import.meta.hot) {
|
|
83
|
-
import
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
__typestyles_invalidateKeys(${keysJSON}, ${prefixesJSON});
|
|
87
|
-
});
|
|
162
|
+
import.meta.hot.accept();
|
|
163
|
+
import.meta.hot.dispose(() => {
|
|
164
|
+
${disposeBody}
|
|
88
165
|
});
|
|
89
166
|
}`;
|
|
90
167
|
return {
|
|
91
|
-
code: code + hmrCode,
|
|
168
|
+
code: hmrImports + overrideBootstrap + code + hmrCode,
|
|
92
169
|
map: null
|
|
93
170
|
};
|
|
94
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
|
+
},
|
|
95
195
|
async generateBundle() {
|
|
96
196
|
if (!isBuildCommand) return;
|
|
97
|
-
if (
|
|
98
|
-
if (!
|
|
197
|
+
if (resolvedMode === "runtime") return;
|
|
198
|
+
if (!resolvedExtractModules.length) return;
|
|
99
199
|
if (!resolvedConfig) return;
|
|
100
200
|
const root = resolvedConfig.root ?? process.cwd();
|
|
101
|
-
const fileName = extract
|
|
201
|
+
const fileName = extract?.fileName ?? "typestyles.css";
|
|
102
202
|
try {
|
|
103
203
|
const css = await runTypestylesBuild({
|
|
104
204
|
root,
|
|
105
|
-
modules:
|
|
205
|
+
modules: resolvedExtractModules
|
|
106
206
|
});
|
|
107
207
|
this.emitFile({
|
|
108
208
|
type: "asset",
|
|
@@ -118,6 +218,11 @@ if (import.meta.hot) {
|
|
|
118
218
|
};
|
|
119
219
|
}
|
|
120
220
|
export {
|
|
221
|
+
DEFAULT_EXTRACT_MODULE_CANDIDATES,
|
|
121
222
|
typestylesPlugin as default,
|
|
122
|
-
|
|
223
|
+
discoverDefaultExtractModules,
|
|
224
|
+
extractNamespaces,
|
|
225
|
+
htmlLinksTypestylesCss,
|
|
226
|
+
moduleNeedsOverrideHmr2 as moduleNeedsOverrideHmr,
|
|
227
|
+
resolveTypestylesCssHref
|
|
123
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.b8d219d17c27",
|
|
4
4
|
"description": "Vite plugin for typestyles HMR support",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -31,25 +31,27 @@
|
|
|
31
31
|
"license": "Apache-2.0",
|
|
32
32
|
"repository": {
|
|
33
33
|
"type": "git",
|
|
34
|
-
"url": "https://github.com/
|
|
34
|
+
"url": "https://github.com/type-styles/typestyles",
|
|
35
35
|
"directory": "packages/vite"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
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.
|
|
44
|
+
"typestyles": "0.0.0-unstable.b8d219d17c27",
|
|
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.
|
|
49
|
+
"@typestyles/build-runner": "0.0.0-unstable.b8d219d17c27"
|
|
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
|
}
|