@stetcms/vite 0.1.0
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 +202 -0
- package/README.md +73 -0
- package/dist/config.d.mts +28 -0
- package/dist/config.mjs +75 -0
- package/dist/index.d.mts +46 -0
- package/dist/index.mjs +96 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @stetcms/vite
|
|
2
|
+
|
|
3
|
+
Vite plugin for products built on [Stet](https://github.com/jamiedavenport/stet). It does two jobs from one config file:
|
|
4
|
+
|
|
5
|
+
- **Generates a typed content client** before every build and dev-server start: the collections and maps your content team shapes in the Stet UI become `stet.<slug>.list()` / `.get()` calls your editor autocompletes. While the dev server runs it keeps watching, so a field added in the Stet UI reaches your types moments later without a restart.
|
|
6
|
+
- **Publishes your analytics tracking plan**, so the events your code declares can be charted in Stet before anyone has fired one.
|
|
7
|
+
|
|
8
|
+
Building without Vite? [`stet generate`](https://github.com/jamiedavenport/stet/tree/main/published/cli#stet-generate) and [`stet sync`](https://github.com/jamiedavenport/stet/tree/main/published/cli#stet-sync) run the same two jobs from the command line, for Next.js apps and CI.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install -D @stetcms/vite
|
|
14
|
+
npm install @stetcms/client
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// vite.config.ts
|
|
21
|
+
import { stet } from '@stetcms/vite';
|
|
22
|
+
import { defineConfig } from 'vite';
|
|
23
|
+
|
|
24
|
+
export default defineConfig({
|
|
25
|
+
plugins: [stet()],
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Everything is configured in `stet.config.ts` (see [`@stetcms/config`](https://github.com/jamiedavenport/stet/tree/main/published/config)), which the CLI reads too:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// stet.config.ts
|
|
33
|
+
import { defineStet } from '@stetcms/config';
|
|
34
|
+
|
|
35
|
+
export default defineStet({ output: 'src/stet.gen.ts' });
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
With `STET_API_KEY` set, the plugin fetches `/api/v1/model` and writes `src/stet.gen.ts`:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { stet } from './stet.gen';
|
|
42
|
+
|
|
43
|
+
const posts = await stet.posts.list(); // a collection
|
|
44
|
+
const post = await stet.posts.get('hello-world'); // one entry
|
|
45
|
+
const landing = await stet.landing.get(); // a map
|
|
46
|
+
// all fully typed from the model marketing built
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The generated file never contains the key: at runtime the client reads `STET_API_KEY` from the environment again, so the file is safe to commit — and committing it keeps type checks working without a running Stet. It reads `STET_ORIGIN` at runtime too, falling back to the origin it was generated against, so one committed file works in every environment rather than pinning your deployment to whatever origin last regenerated it.
|
|
50
|
+
|
|
51
|
+
Neither job can fail your build. Without a key, or with the API unreachable, the plugin warns and leaves the previous generated file in place, writing an empty model only when no file exists yet. A field deleted in the Stet UI keeps its key in the generated types, marked `@deprecated`: your editor strikes it through wherever you read it, and your build keeps passing while you migrate.
|
|
52
|
+
|
|
53
|
+
## Analytics
|
|
54
|
+
|
|
55
|
+
A config that declares `analytics` has its tracking plan published on every dev-server and build start, so the dashboard can offer an event before it has ever been recorded. Dropping an event from your code drops it from that list on the next sync, while anything already recorded under that name keeps its history. See [`@stetcms/analytics`](https://github.com/jamiedavenport/stet/tree/main/published/analytics) for the plan itself and the route it feeds.
|
|
56
|
+
|
|
57
|
+
## Options
|
|
58
|
+
|
|
59
|
+
Each option overrides the same key in `stet.config.ts`; leave them off and the config file decides.
|
|
60
|
+
|
|
61
|
+
| Option | Default | Description |
|
|
62
|
+
| -------- | --------------------------------- | ----------------------------------------------------------------------------- |
|
|
63
|
+
| `origin` | `STET_ORIGIN` or the hosted cloud | The Stet deployment to generate from and call. |
|
|
64
|
+
| `apiKey` | `STET_API_KEY` | Organization API key used to fetch the model. |
|
|
65
|
+
| `output` | `src/stet.gen.ts` | Where the generated module goes, relative to root. |
|
|
66
|
+
| `watch` | `true` | Regenerate every few seconds while the dev server runs. Never affects builds. |
|
|
67
|
+
| `config` | auto-detected | Path to `stet.config.ts`, relative to root. |
|
|
68
|
+
|
|
69
|
+
See [`examples/tanstack`](https://github.com/jamiedavenport/stet/tree/main/examples/tanstack) for a complete TanStack Start app built on the generated client.
|
|
70
|
+
|
|
71
|
+
## License
|
|
72
|
+
|
|
73
|
+
Apache-2.0
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { StetConfig } from "@stetcms/config";
|
|
2
|
+
import { EventsShape } from "@stetcms/analytics";
|
|
3
|
+
|
|
4
|
+
//#region src/config.d.ts
|
|
5
|
+
/** Paths tried, relative to the project root, when none is given. */
|
|
6
|
+
declare const CONFIG_CANDIDATES: string[];
|
|
7
|
+
/** Finds a config by trying {@link CONFIG_CANDIDATES} under `root`. */
|
|
8
|
+
declare function findStetConfig(root: string): string | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Loads a config file and returns its `defineStet(...)` result, or undefined
|
|
11
|
+
* when the file exports none.
|
|
12
|
+
*
|
|
13
|
+
* Uses jiti so a TypeScript config loads under plain Node whatever the
|
|
14
|
+
* project's own build setup is. `cloudflare:workers` only exists inside
|
|
15
|
+
* workerd, so it resolves to a virtual module whose `env` is `process.env` —
|
|
16
|
+
* the same values Workers expose there on recent compatibility dates.
|
|
17
|
+
*/
|
|
18
|
+
declare function loadStetConfig(path: string): Promise<StetConfig | undefined>;
|
|
19
|
+
/**
|
|
20
|
+
* The tracking plan's events, if the config declares any.
|
|
21
|
+
*
|
|
22
|
+
* `StetConfig` keeps `analytics` opaque so `@stetcms/config` never depends on
|
|
23
|
+
* `@stetcms/analytics`; this is where the two meet, and the only place the
|
|
24
|
+
* shape is asserted rather than inferred.
|
|
25
|
+
*/
|
|
26
|
+
declare function trackingPlanEvents(config: StetConfig | undefined): EventsShape | undefined;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { CONFIG_CANDIDATES, findStetConfig, loadStetConfig, trackingPlanEvents };
|
package/dist/config.mjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { createJiti } from "jiti";
|
|
4
|
+
//#region src/config.ts
|
|
5
|
+
/**
|
|
6
|
+
* Loading a project's `stet.config.ts`. Node-only, and kept out of
|
|
7
|
+
* `index.ts` so the CLI can reuse it without pulling in Vite.
|
|
8
|
+
*/
|
|
9
|
+
/** Paths tried, relative to the project root, when none is given. */
|
|
10
|
+
const CONFIG_CANDIDATES = [
|
|
11
|
+
"stet.config.ts",
|
|
12
|
+
"stet.config.js",
|
|
13
|
+
"stet.config.mjs",
|
|
14
|
+
"src/stet.config.ts"
|
|
15
|
+
];
|
|
16
|
+
/** Finds a config by trying {@link CONFIG_CANDIDATES} under `root`. */
|
|
17
|
+
function findStetConfig(root) {
|
|
18
|
+
for (const candidate of CONFIG_CANDIDATES) {
|
|
19
|
+
const absolute = resolve(root, candidate);
|
|
20
|
+
if (existsSync(absolute)) return absolute;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function isStetConfig(value) {
|
|
24
|
+
if (typeof value !== "object" || value === null) return false;
|
|
25
|
+
return [
|
|
26
|
+
"origin",
|
|
27
|
+
"apiKey",
|
|
28
|
+
"output",
|
|
29
|
+
"watch",
|
|
30
|
+
"analytics"
|
|
31
|
+
].some((key) => key in value);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Loads a config file and returns its `defineStet(...)` result, or undefined
|
|
35
|
+
* when the file exports none.
|
|
36
|
+
*
|
|
37
|
+
* Uses jiti so a TypeScript config loads under plain Node whatever the
|
|
38
|
+
* project's own build setup is. `cloudflare:workers` only exists inside
|
|
39
|
+
* workerd, so it resolves to a virtual module whose `env` is `process.env` —
|
|
40
|
+
* the same values Workers expose there on recent compatibility dates.
|
|
41
|
+
*/
|
|
42
|
+
async function loadStetConfig(path) {
|
|
43
|
+
const absolute = resolve(path);
|
|
44
|
+
if (!existsSync(absolute)) return;
|
|
45
|
+
const module = await createJiti(import.meta.url, {
|
|
46
|
+
interopDefault: true,
|
|
47
|
+
virtualModules: { "cloudflare:workers": { env: process.env } }
|
|
48
|
+
}).import(absolute);
|
|
49
|
+
for (const exported of Object.values(module)) {
|
|
50
|
+
let value = exported;
|
|
51
|
+
if (typeof value === "function" && value.length === 0) try {
|
|
52
|
+
value = value();
|
|
53
|
+
} catch {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (value instanceof Promise) try {
|
|
57
|
+
value = await value;
|
|
58
|
+
} catch {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (isStetConfig(value)) return value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The tracking plan's events, if the config declares any.
|
|
66
|
+
*
|
|
67
|
+
* `StetConfig` keeps `analytics` opaque so `@stetcms/config` never depends on
|
|
68
|
+
* `@stetcms/analytics`; this is where the two meet, and the only place the
|
|
69
|
+
* shape is asserted rather than inferred.
|
|
70
|
+
*/
|
|
71
|
+
function trackingPlanEvents(config) {
|
|
72
|
+
return (config?.analytics)?.events;
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { CONFIG_CANDIDATES, findStetConfig, loadStetConfig, trackingPlanEvents };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
/** Options for {@link stet}. Each one overrides the same key in `stet.config.ts`. */
|
|
5
|
+
type StetPluginOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* The Stet deployment to generate from. Defaults to the config file, then
|
|
8
|
+
* `STET_ORIGIN`, then the hosted cloud.
|
|
9
|
+
*/
|
|
10
|
+
origin?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Organization API key used to fetch the model at codegen time. Defaults to
|
|
13
|
+
* `STET_API_KEY`. Never written into the generated file: at runtime the
|
|
14
|
+
* client reads `STET_API_KEY` from the environment again.
|
|
15
|
+
*/
|
|
16
|
+
apiKey?: string; /** Where the generated module goes, relative to the project root. */
|
|
17
|
+
output?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Regenerate while the dev server runs, so model changes made in the Stet
|
|
20
|
+
* UI reach the app without a restart. On by default; never affects builds.
|
|
21
|
+
*/
|
|
22
|
+
watch?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Path to `stet.config.ts`, relative to the project root. Auto-detected
|
|
25
|
+
* when omitted; a project without one runs on this plugin's options alone.
|
|
26
|
+
*/
|
|
27
|
+
config?: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Generates a typed content client from your organization's content model
|
|
31
|
+
* before every build and dev-server start: the collections and maps your
|
|
32
|
+
* content team shapes become `stet.<slug>.list()` / `.get()` calls your
|
|
33
|
+
* editor autocompletes. While the dev server runs it keeps watching, so a
|
|
34
|
+
* field added in the Stet UI shows up in your types moments later.
|
|
35
|
+
*
|
|
36
|
+
* It also publishes the analytics tracking plan from `stet.config.ts`, so the
|
|
37
|
+
* events your code declares can be charted in Stet before anyone has fired
|
|
38
|
+
* one.
|
|
39
|
+
*
|
|
40
|
+
* Neither ever fails the build. Without a key or with the API unreachable it
|
|
41
|
+
* warns and leaves the previous generated file in place, writing an empty
|
|
42
|
+
* model only when no file exists yet.
|
|
43
|
+
*/
|
|
44
|
+
declare function stet(options?: StetPluginOptions): Plugin;
|
|
45
|
+
//#endregion
|
|
46
|
+
export { StetPluginOptions, stet };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { findStetConfig, loadStetConfig, trackingPlanEvents } from "./config.mjs";
|
|
2
|
+
import { access, mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { syncTrackingPlan } from "@stetcms/analytics/sync";
|
|
5
|
+
import { fetchContentModel, renderContentModule } from "@stetcms/client/codegen";
|
|
6
|
+
import { resolveStetConfig } from "@stetcms/config";
|
|
7
|
+
//#region src/index.ts
|
|
8
|
+
/** How often the dev server checks the model for changes. */
|
|
9
|
+
const watchIntervalMs = 3e3;
|
|
10
|
+
/**
|
|
11
|
+
* Generates a typed content client from your organization's content model
|
|
12
|
+
* before every build and dev-server start: the collections and maps your
|
|
13
|
+
* content team shapes become `stet.<slug>.list()` / `.get()` calls your
|
|
14
|
+
* editor autocompletes. While the dev server runs it keeps watching, so a
|
|
15
|
+
* field added in the Stet UI shows up in your types moments later.
|
|
16
|
+
*
|
|
17
|
+
* It also publishes the analytics tracking plan from `stet.config.ts`, so the
|
|
18
|
+
* events your code declares can be charted in Stet before anyone has fired
|
|
19
|
+
* one.
|
|
20
|
+
*
|
|
21
|
+
* Neither ever fails the build. Without a key or with the API unreachable it
|
|
22
|
+
* warns and leaves the previous generated file in place, writing an empty
|
|
23
|
+
* model only when no file exists yet.
|
|
24
|
+
*/
|
|
25
|
+
function stet(options = {}) {
|
|
26
|
+
let resolvedConfig;
|
|
27
|
+
let lastRendered;
|
|
28
|
+
const configPath = () => options.config === void 0 ? findStetConfig(resolvedConfig.root) : resolve(resolvedConfig.root, options.config);
|
|
29
|
+
/**
|
|
30
|
+
* Read on every pass rather than cached, so editing the config during a
|
|
31
|
+
* watch run takes effect without restarting the dev server.
|
|
32
|
+
*/
|
|
33
|
+
const settle = async () => {
|
|
34
|
+
const path = configPath();
|
|
35
|
+
const file = path === void 0 ? void 0 : await loadStetConfig(path);
|
|
36
|
+
const resolved = resolveStetConfig(file, options);
|
|
37
|
+
return {
|
|
38
|
+
...resolved,
|
|
39
|
+
output: resolve(resolvedConfig.root, resolved.output),
|
|
40
|
+
events: trackingPlanEvents(file)
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
const write = async (output, code) => {
|
|
44
|
+
await mkdir(dirname(output), { recursive: true });
|
|
45
|
+
await writeFile(output, code, "utf8");
|
|
46
|
+
};
|
|
47
|
+
const exists = (output) => access(output).then(() => true, () => false);
|
|
48
|
+
return {
|
|
49
|
+
name: "stet",
|
|
50
|
+
configResolved(config) {
|
|
51
|
+
resolvedConfig = config;
|
|
52
|
+
},
|
|
53
|
+
async buildStart() {
|
|
54
|
+
const { origin, apiKey, output, events } = await settle();
|
|
55
|
+
if (apiKey === void 0) {
|
|
56
|
+
this.warn("[stet] STET_API_KEY is not set; skipping content client generation.");
|
|
57
|
+
if (!await exists(output)) await write(output, renderContentModule({ types: [] }, origin));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
lastRendered = renderContentModule(await fetchContentModel(origin, apiKey), origin);
|
|
62
|
+
await write(output, lastRendered);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
this.warn(`[stet] Could not refresh the content client from ${origin}: ${String(error)}`);
|
|
65
|
+
if (!await exists(output)) await write(output, renderContentModule({ types: [] }, origin));
|
|
66
|
+
}
|
|
67
|
+
if (events !== void 0) try {
|
|
68
|
+
await syncTrackingPlan({
|
|
69
|
+
events,
|
|
70
|
+
origin,
|
|
71
|
+
apiKey
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
this.warn(`[stet] Could not publish the tracking plan to ${origin}: ${String(error)}`);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
configureServer(server) {
|
|
78
|
+
const tick = async () => {
|
|
79
|
+
try {
|
|
80
|
+
const { origin, apiKey, output, watch } = await settle();
|
|
81
|
+
if (!watch || apiKey === void 0) return;
|
|
82
|
+
const code = renderContentModule(await fetchContentModel(origin, apiKey), origin);
|
|
83
|
+
if (code !== lastRendered) {
|
|
84
|
+
lastRendered = code;
|
|
85
|
+
await write(output, code);
|
|
86
|
+
server.config.logger.info(`[stet] Content model changed; regenerated ${output}`);
|
|
87
|
+
}
|
|
88
|
+
} catch {}
|
|
89
|
+
};
|
|
90
|
+
const timer = setInterval(() => void tick(), watchIntervalMs);
|
|
91
|
+
server.httpServer?.on("close", () => clearInterval(timer));
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { stet };
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stetcms/vite",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vite plugin for products built on Stet: typed options, a virtual config module, and a build-time codegen hook.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"plugin",
|
|
7
|
+
"stet",
|
|
8
|
+
"vite",
|
|
9
|
+
"vite-plugin"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/jamiedavenport/stet/tree/main/published/vite#readme",
|
|
12
|
+
"bugs": "https://github.com/jamiedavenport/stet/issues",
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"author": "Jamie Davenport (https://jxd.dev)",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/jamiedavenport/stet.git",
|
|
18
|
+
"directory": "published/vite"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.mts",
|
|
27
|
+
"default": "./dist/index.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./config": {
|
|
30
|
+
"types": "./dist/config.d.mts",
|
|
31
|
+
"default": "./dist/config.mjs"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"jiti": "^2.7.0",
|
|
39
|
+
"@stetcms/client": "0.1.0",
|
|
40
|
+
"@stetcms/analytics": "0.1.0",
|
|
41
|
+
"@stetcms/config": "0.1.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^26.1.1",
|
|
45
|
+
"publint": "^0.3.21",
|
|
46
|
+
"typescript": "^5.9.3",
|
|
47
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.2",
|
|
48
|
+
"vite-plus": "0.2.2",
|
|
49
|
+
"vitest": "^4.1.9"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"vite": ">=6.0.0"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=20"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "vp pack",
|
|
59
|
+
"tc": "tsc --noEmit",
|
|
60
|
+
"test": "vp test run"
|
|
61
|
+
}
|
|
62
|
+
}
|