@openpolicy/vite-auto-collect 0.0.19 → 0.0.20
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/package.json +7 -4
- package/skills/vite-setup/SKILL.md +209 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openpolicy/vite-auto-collect",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Vite plugin that scans source files for @openpolicy/sdk collecting() calls and populates autoCollected() at build time",
|
|
6
6
|
"license": "GPL-3.0-only",
|
|
@@ -10,11 +10,13 @@
|
|
|
10
10
|
"directory": "packages/vite-auto-collect"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
|
-
"vite-plugin"
|
|
13
|
+
"vite-plugin",
|
|
14
|
+
"tanstack-intent"
|
|
14
15
|
],
|
|
15
16
|
"files": [
|
|
16
17
|
"dist",
|
|
17
|
-
"README.md"
|
|
18
|
+
"README.md",
|
|
19
|
+
"skills"
|
|
18
20
|
],
|
|
19
21
|
"exports": {
|
|
20
22
|
".": {
|
|
@@ -37,6 +39,7 @@
|
|
|
37
39
|
"devDependencies": {
|
|
38
40
|
"@openpolicy/sdk": "workspace:*",
|
|
39
41
|
"@openpolicy/tooling": "workspace:*",
|
|
40
|
-
"vite": "catalog:"
|
|
42
|
+
"vite": "catalog:",
|
|
43
|
+
"@tanstack/intent": "^0.0.29"
|
|
41
44
|
}
|
|
42
45
|
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: vite-setup
|
|
3
|
+
description: >
|
|
4
|
+
Configuring the autoCollect() Vite plugin from @openpolicy/vite-auto-collect to populate
|
|
5
|
+
the dataCollected and thirdParties sentinels at build time by scanning source files for
|
|
6
|
+
collecting() and thirdParty() calls. Covers AutoCollectOptions (srcDir, extensions, ignore,
|
|
7
|
+
thirdParties.usePackageJson), enforce: "pre" plugin ordering, virtual module interception,
|
|
8
|
+
and dev-server hot reload behaviour.
|
|
9
|
+
type: framework
|
|
10
|
+
library: openpolicy
|
|
11
|
+
framework: vite
|
|
12
|
+
library_version: "0.0.19"
|
|
13
|
+
requires:
|
|
14
|
+
- openpolicy/define-config
|
|
15
|
+
sources:
|
|
16
|
+
- jamiedavenport/openpolicy:packages/vite-auto-collect/src/index.ts
|
|
17
|
+
- jamiedavenport/openpolicy:packages/vite-auto-collect/src/scan.ts
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
This skill builds on openpolicy/define-config. Read it first.
|
|
21
|
+
|
|
22
|
+
## Overview
|
|
23
|
+
|
|
24
|
+
`autoCollect()` is a Vite plugin that scans your source files at build time, extracts
|
|
25
|
+
`collecting()` and `thirdParty()` call metadata via static AST analysis (oxc-parser), and
|
|
26
|
+
injects the results into the `@openpolicy/sdk` module graph as a virtual module. The sentinels
|
|
27
|
+
`dataCollected` and `thirdParties` exported from `@openpolicy/sdk` are then populated with the
|
|
28
|
+
scanned data when your policy config is evaluated.
|
|
29
|
+
|
|
30
|
+
The plugin uses `enforce: "pre"` so it always runs before other plugins. In dev mode it watches
|
|
31
|
+
the `srcDir` tree and triggers a full page reload when annotations change.
|
|
32
|
+
|
|
33
|
+
## AutoCollectOptions type
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
type AutoCollectOptions = {
|
|
37
|
+
// Directory walked for collecting() calls. Resolved relative to the Vite
|
|
38
|
+
// project root. Defaults to "src".
|
|
39
|
+
srcDir?: string;
|
|
40
|
+
|
|
41
|
+
// File extensions scanned. Defaults to [".ts", ".tsx"].
|
|
42
|
+
extensions?: string[];
|
|
43
|
+
|
|
44
|
+
// Extra directory names skipped during the walk. Appended to the built-in
|
|
45
|
+
// defaults: node_modules, dist, .git, .next, .output, .svelte-kit, .cache
|
|
46
|
+
ignore?: string[];
|
|
47
|
+
|
|
48
|
+
thirdParties?: {
|
|
49
|
+
// When true, reads package.json dependencies and devDependencies and
|
|
50
|
+
// matches them against the built-in KNOWN_PACKAGES registry (~30 common
|
|
51
|
+
// npm packages). Matched entries are added to thirdParties automatically.
|
|
52
|
+
usePackageJson?: boolean;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Setup
|
|
58
|
+
|
|
59
|
+
Install the package:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
bun add -D @openpolicy/vite-auto-collect
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Core Patterns
|
|
66
|
+
|
|
67
|
+
### 1. Basic setup
|
|
68
|
+
|
|
69
|
+
The minimum configuration — no options required:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
// vite.config.ts
|
|
73
|
+
import { defineConfig } from "vite";
|
|
74
|
+
import { autoCollect } from "@openpolicy/vite-auto-collect";
|
|
75
|
+
|
|
76
|
+
export default defineConfig({
|
|
77
|
+
plugins: [autoCollect()],
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The plugin scans `<root>/src/**/*.{ts,tsx}` by default.
|
|
82
|
+
|
|
83
|
+
### 2. With usePackageJson
|
|
84
|
+
|
|
85
|
+
Enable automatic third-party detection from `package.json`. The plugin reads all
|
|
86
|
+
`dependencies` and `devDependencies` and cross-references them against the built-in
|
|
87
|
+
`KNOWN_PACKAGES` registry. Matched entries are merged into the `thirdParties` sentinel
|
|
88
|
+
alongside any `thirdParty()` call results.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
// vite.config.ts
|
|
92
|
+
import { defineConfig } from "vite";
|
|
93
|
+
import { autoCollect } from "@openpolicy/vite-auto-collect";
|
|
94
|
+
|
|
95
|
+
export default defineConfig({
|
|
96
|
+
plugins: [
|
|
97
|
+
autoCollect({
|
|
98
|
+
thirdParties: {
|
|
99
|
+
usePackageJson: true,
|
|
100
|
+
},
|
|
101
|
+
}),
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 3. Custom srcDir, extensions, and ignore
|
|
107
|
+
|
|
108
|
+
Use `srcDir` when source files live outside the default `src/` directory (e.g. Next.js `app/`,
|
|
109
|
+
Nuxt `pages/`). Use `extensions` to include `.js` or `.jsx` files. Use `ignore` to skip
|
|
110
|
+
additional directories that the built-in defaults do not cover.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// vite.config.ts
|
|
114
|
+
import { defineConfig } from "vite";
|
|
115
|
+
import { autoCollect } from "@openpolicy/vite-auto-collect";
|
|
116
|
+
|
|
117
|
+
export default defineConfig({
|
|
118
|
+
plugins: [
|
|
119
|
+
autoCollect({
|
|
120
|
+
srcDir: "app",
|
|
121
|
+
extensions: [".ts", ".tsx", ".js", ".jsx"],
|
|
122
|
+
ignore: ["__tests__", "storybook-static"],
|
|
123
|
+
thirdParties: {
|
|
124
|
+
usePackageJson: true,
|
|
125
|
+
},
|
|
126
|
+
}),
|
|
127
|
+
],
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Wiring sentinels in openpolicy.ts
|
|
132
|
+
|
|
133
|
+
The plugin only has an effect if the `dataCollected` and `thirdParties` sentinels from
|
|
134
|
+
`@openpolicy/sdk` are spread into the policy config. Without this, the scan results are
|
|
135
|
+
discarded:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
// openpolicy.ts
|
|
139
|
+
import { defineConfig, dataCollected, thirdParties } from "@openpolicy/sdk";
|
|
140
|
+
|
|
141
|
+
export default defineConfig({
|
|
142
|
+
company: {
|
|
143
|
+
name: "Acme",
|
|
144
|
+
legalName: "Acme, Inc.",
|
|
145
|
+
address: "123 Main St, Springfield, USA",
|
|
146
|
+
contact: "privacy@acme.com",
|
|
147
|
+
},
|
|
148
|
+
privacy: {
|
|
149
|
+
effectiveDate: "2026-01-01",
|
|
150
|
+
dataCollected: {
|
|
151
|
+
...dataCollected,
|
|
152
|
+
// Manually declared categories merge alongside auto-collected ones
|
|
153
|
+
"Account Information": ["Email address", "Display name"],
|
|
154
|
+
},
|
|
155
|
+
thirdParties: [...thirdParties],
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Common Mistakes
|
|
161
|
+
|
|
162
|
+
### CRITICAL — Using `openPolicy()` from `@openpolicy/vite` instead of `autoCollect()`
|
|
163
|
+
|
|
164
|
+
`openPolicy()` and `autoCollect()` are different plugins with different purposes:
|
|
165
|
+
|
|
166
|
+
- `openPolicy()` (from `@openpolicy/vite`) generates static `.md`, `.html`, or `.pdf` files
|
|
167
|
+
at build time. It does not populate sentinels and does not integrate with React rendering.
|
|
168
|
+
- `autoCollect()` (from `@openpolicy/vite-auto-collect`) populates `dataCollected` and
|
|
169
|
+
`thirdParties` sentinels at build time for use with `@openpolicy/react` components.
|
|
170
|
+
|
|
171
|
+
Using `openPolicy()` when you intend to render with React produces stale static files that
|
|
172
|
+
the React components never read. The sentinels remain empty.
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
// WRONG — old static generation pattern
|
|
176
|
+
import { openPolicy } from "@openpolicy/vite";
|
|
177
|
+
|
|
178
|
+
export default defineConfig({
|
|
179
|
+
plugins: [openPolicy({ formats: ["markdown"], outDir: "public/policies" })],
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
// CORRECT — sentinel population for React rendering
|
|
185
|
+
import { autoCollect } from "@openpolicy/vite-auto-collect";
|
|
186
|
+
|
|
187
|
+
export default defineConfig({
|
|
188
|
+
plugins: [autoCollect({ thirdParties: { usePackageJson: true } })],
|
|
189
|
+
});
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### MEDIUM — Expecting autoCollect to scan files outside the default `srcDir`
|
|
193
|
+
|
|
194
|
+
`autoCollect()` only walks the directory specified by `srcDir` (default: `"src"` relative to
|
|
195
|
+
the Vite project root). Any `collecting()` or `thirdParty()` calls placed in config files,
|
|
196
|
+
scripts, `test/`, or other top-level directories are silently ignored — no warning is emitted.
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
// WRONG — collecting() in scripts/seed.ts is outside "src", silently ignored
|
|
200
|
+
// autoCollect() with default options never scans it
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
// CORRECT — either move annotations into src/, or configure srcDir to cover the directory:
|
|
205
|
+
autoCollect({ srcDir: "app" })
|
|
206
|
+
|
|
207
|
+
// Or add a second directory by using a broader common ancestor:
|
|
208
|
+
autoCollect({ srcDir: "." , ignore: ["node_modules", "dist", "public"] })
|
|
209
|
+
```
|