@zod-to-form/vite 0.1.1
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 +21 -0
- package/README.md +40 -0
- package/dist/cache.d.ts +39 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +56 -0
- package/dist/cache.js.map +1 -0
- package/dist/config/load.d.ts +59 -0
- package/dist/config/load.d.ts.map +1 -0
- package/dist/config/load.js +117 -0
- package/dist/config/load.js.map +1 -0
- package/dist/errors.d.ts +50 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +62 -0
- package/dist/errors.js.map +1 -0
- package/dist/generate-mode/babel-traverse.d.ts +5 -0
- package/dist/generate-mode/babel-traverse.d.ts.map +1 -0
- package/dist/generate-mode/babel-traverse.js +16 -0
- package/dist/generate-mode/babel-traverse.js.map +1 -0
- package/dist/generate-mode/generate-source.d.ts +51 -0
- package/dist/generate-mode/generate-source.d.ts.map +1 -0
- package/dist/generate-mode/generate-source.js +187 -0
- package/dist/generate-mode/generate-source.js.map +1 -0
- package/dist/generate-mode/resolve-schema.d.ts +43 -0
- package/dist/generate-mode/resolve-schema.d.ts.map +1 -0
- package/dist/generate-mode/resolve-schema.js +182 -0
- package/dist/generate-mode/resolve-schema.js.map +1 -0
- package/dist/generate-mode/scan-jsx.d.ts +86 -0
- package/dist/generate-mode/scan-jsx.d.ts.map +1 -0
- package/dist/generate-mode/scan-jsx.js +161 -0
- package/dist/generate-mode/scan-jsx.js.map +1 -0
- package/dist/hmr.d.ts +44 -0
- package/dist/hmr.d.ts.map +1 -0
- package/dist/hmr.js +28 -0
- package/dist/hmr.js.map +1 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +27 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +46 -0
- package/dist/logger.js.map +1 -0
- package/dist/plugin.d.ts +61 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +650 -0
- package/dist/plugin.js.map +1 -0
- package/dist/query-mode/parse-specifier.d.ts +23 -0
- package/dist/query-mode/parse-specifier.d.ts.map +1 -0
- package/dist/query-mode/parse-specifier.js +83 -0
- package/dist/query-mode/parse-specifier.js.map +1 -0
- package/dist/query-mode/resolve-id.d.ts +12 -0
- package/dist/query-mode/resolve-id.d.ts.map +1 -0
- package/dist/query-mode/resolve-id.js +61 -0
- package/dist/query-mode/resolve-id.js.map +1 -0
- package/dist/query-mode/transform.d.ts +33 -0
- package/dist/query-mode/transform.d.ts.map +1 -0
- package/dist/query-mode/transform.js +90 -0
- package/dist/query-mode/transform.js.map +1 -0
- package/dist/resolver-strip.d.ts +58 -0
- package/dist/resolver-strip.d.ts.map +1 -0
- package/dist/resolver-strip.js +141 -0
- package/dist/resolver-strip.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts +242 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/write-guard.d.ts +26 -0
- package/dist/write-guard.d.ts.map +1 -0
- package/dist/write-guard.js +32 -0
- package/dist/write-guard.js.map +1 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Pradeep Mouli
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# @zod-to-form/vite
|
|
2
|
+
|
|
3
|
+
Vite plugin for [zod-to-form](https://github.com/pradeepmouli/zod-to-form). Transforms `?z2f` imports into generated form components and (opt-in) rewrites `<ZodForm>` JSX call sites into generated components at build time — all with no separate codegen CLI step.
|
|
4
|
+
|
|
5
|
+
> **Status**: In active development. See [`specs/007-vite-codegen-plugin/`](../../specs/007-vite-codegen-plugin/) for the full specification, plan, and implementation tasks.
|
|
6
|
+
|
|
7
|
+
## Quickstart
|
|
8
|
+
|
|
9
|
+
See the full walkthrough in [quickstart.md](../../specs/007-vite-codegen-plugin/quickstart.md).
|
|
10
|
+
|
|
11
|
+
Minimal usage:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// vite.config.ts
|
|
15
|
+
import { defineConfig } from 'vite';
|
|
16
|
+
import react from '@vitejs/plugin-react';
|
|
17
|
+
import z2fVite from '@zod-to-form/vite';
|
|
18
|
+
|
|
19
|
+
export default defineConfig({
|
|
20
|
+
plugins: [z2fVite(), react()]
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
// src/App.tsx
|
|
26
|
+
import SignupForm from './schemas/signup.ts?z2f';
|
|
27
|
+
|
|
28
|
+
export default function App() {
|
|
29
|
+
return <SignupForm onSubmit={(data) => console.log(data)} />;
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Modes
|
|
34
|
+
|
|
35
|
+
- **Query-string mode** (default): imports carrying `?z2f` become generated form components.
|
|
36
|
+
- **Generate mode** (opt-in via `generate: {}`): scans source for `<ZodForm schema={X}>` usages and replaces statically resolvable ones with generated components at build time. Unresolvable sites are left alone and fall through to the runtime path. Presence of the `generate` object — even empty — enables the mode; pass `generate: { include: [...], exclude: [...] }` to constrain which files are scanned. The name mirrors the CLI's `zod-to-form generate` command: same codegen, driven by static analysis instead of an explicit CLI step.
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
MIT
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CompilationCache — entity #3 from `data-model.md`.
|
|
3
|
+
*
|
|
4
|
+
* Stores the result of compiling each (schemaFile, variant, configHash)
|
|
5
|
+
* triple to a generated form module. Maintains a reverse index so HMR can
|
|
6
|
+
* invalidate every entry that depends on a given schema file in O(1).
|
|
7
|
+
*
|
|
8
|
+
* The cache is single-threaded: Vite runs plugin hooks sequentially on the
|
|
9
|
+
* main event loop, so no locking is needed.
|
|
10
|
+
*/
|
|
11
|
+
import type { CompilationEntry, GenerationTarget } from './types.js';
|
|
12
|
+
export interface CacheStats {
|
|
13
|
+
/** Number of entries currently in the cache. */
|
|
14
|
+
size: number;
|
|
15
|
+
/** Number of cache hits since the cache was created. */
|
|
16
|
+
hits: number;
|
|
17
|
+
/** Number of cache misses since the cache was created. */
|
|
18
|
+
misses: number;
|
|
19
|
+
}
|
|
20
|
+
export interface CompilationCache {
|
|
21
|
+
/** Look up a cached compilation by target. Returns undefined on miss. */
|
|
22
|
+
get(target: GenerationTarget): CompilationEntry | undefined;
|
|
23
|
+
/** Store a compilation result. Updates the reverse index. */
|
|
24
|
+
set(target: GenerationTarget, entry: CompilationEntry): void;
|
|
25
|
+
/**
|
|
26
|
+
* Evict every entry whose `schemaFile` equals the given path.
|
|
27
|
+
* Returns the list of evicted cache keys for HMR fan-out.
|
|
28
|
+
*/
|
|
29
|
+
invalidateByFile(schemaFile: string): string[];
|
|
30
|
+
/**
|
|
31
|
+
* Evict every cache entry. Used when the config file changes.
|
|
32
|
+
* Returns the list of evicted cache keys for HMR fan-out.
|
|
33
|
+
*/
|
|
34
|
+
invalidateAll(): string[];
|
|
35
|
+
/** Snapshot of cache size and hit/miss counters. */
|
|
36
|
+
stats(): CacheStats;
|
|
37
|
+
}
|
|
38
|
+
export declare function createCompilationCache(): CompilationCache;
|
|
39
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAErE,MAAM,WAAW,UAAU;IACzB,gDAAgD;IAChD,IAAI,EAAE,MAAM,CAAC;IACb,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,0DAA0D;IAC1D,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,yEAAyE;IACzE,GAAG,CAAC,MAAM,EAAE,gBAAgB,GAAG,gBAAgB,GAAG,SAAS,CAAC;IAE5D,6DAA6D;IAC7D,GAAG,CAAC,MAAM,EAAE,gBAAgB,EAAE,KAAK,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAE7D;;;OAGG;IACH,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE/C;;;OAGG;IACH,aAAa,IAAI,MAAM,EAAE,CAAC;IAE1B,oDAAoD;IACpD,KAAK,IAAI,UAAU,CAAC;CACrB;AAOD,wBAAgB,sBAAsB,IAAI,gBAAgB,CAwDzD"}
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** Build a stable key from a target. */
|
|
2
|
+
function keyFor(target) {
|
|
3
|
+
return `${target.schemaFile}::${target.variant}::${target.configHash}`;
|
|
4
|
+
}
|
|
5
|
+
export function createCompilationCache() {
|
|
6
|
+
const entries = new Map();
|
|
7
|
+
const byFile = new Map();
|
|
8
|
+
let hits = 0;
|
|
9
|
+
let misses = 0;
|
|
10
|
+
return {
|
|
11
|
+
get(target) {
|
|
12
|
+
const key = keyFor(target);
|
|
13
|
+
const entry = entries.get(key);
|
|
14
|
+
if (entry === undefined) {
|
|
15
|
+
misses += 1;
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
hits += 1;
|
|
19
|
+
return entry;
|
|
20
|
+
},
|
|
21
|
+
set(target, entry) {
|
|
22
|
+
const key = keyFor(target);
|
|
23
|
+
entries.set(key, entry);
|
|
24
|
+
let group = byFile.get(target.schemaFile);
|
|
25
|
+
if (group === undefined) {
|
|
26
|
+
group = new Set();
|
|
27
|
+
byFile.set(target.schemaFile, group);
|
|
28
|
+
}
|
|
29
|
+
group.add(key);
|
|
30
|
+
},
|
|
31
|
+
invalidateByFile(schemaFile) {
|
|
32
|
+
const group = byFile.get(schemaFile);
|
|
33
|
+
if (group === undefined || group.size === 0) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
const evicted = [];
|
|
37
|
+
for (const key of group) {
|
|
38
|
+
if (entries.delete(key)) {
|
|
39
|
+
evicted.push(key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
byFile.delete(schemaFile);
|
|
43
|
+
return evicted;
|
|
44
|
+
},
|
|
45
|
+
invalidateAll() {
|
|
46
|
+
const evicted = Array.from(entries.keys());
|
|
47
|
+
entries.clear();
|
|
48
|
+
byFile.clear();
|
|
49
|
+
return evicted;
|
|
50
|
+
},
|
|
51
|
+
stats() {
|
|
52
|
+
return { size: entries.size, hits, misses };
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AA4CA,wCAAwC;AACxC,SAAS,MAAM,CAAC,MAAwB;IACtC,OAAO,GAAG,MAAM,CAAC,UAAU,KAAK,MAAM,CAAC,OAAO,KAAK,MAAM,CAAC,UAAU,EAAE,CAAC;AACzE,CAAC;AAED,MAAM,UAAU,sBAAsB;IACpC,MAAM,OAAO,GAAG,IAAI,GAAG,EAA4B,CAAC;IACpD,MAAM,MAAM,GAAG,IAAI,GAAG,EAAuB,CAAC;IAC9C,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,MAAM,GAAG,CAAC,CAAC;IAEf,OAAO;QACL,GAAG,CAAC,MAAM;YACR,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;YAC3B,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC/B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxB,MAAM,IAAI,CAAC,CAAC;gBACZ,OAAO,SAAS,CAAC;YACnB,CAAC;YACD,IAAI,IAAI,CAAC,CAAC;YACV,OAAO,KAAK,CAAC;QACf,CAAC;QAED,GAAG,CAAC,MAAM,EAAE,KAAK;YACf,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;YAC3B,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAExB,IAAI,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAC1C,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxB,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;gBAC1B,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;YACvC,CAAC;YACD,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,CAAC;QAED,gBAAgB,CAAC,UAAU;YACzB,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YACrC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;gBAC5C,OAAO,EAAE,CAAC;YACZ,CAAC;YACD,MAAM,OAAO,GAAa,EAAE,CAAC;YAC7B,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;gBACxB,IAAI,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;oBACxB,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBACpB,CAAC;YACH,CAAC;YACD,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAC1B,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,aAAa;YACX,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;YAC3C,OAAO,CAAC,KAAK,EAAE,CAAC;YAChB,MAAM,CAAC,KAAK,EAAE,CAAC;YACf,OAAO,OAAO,CAAC;QACjB,CAAC;QAED,KAAK;YACH,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;QAC9C,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { CodegenConfig } from '@zod-to-form/core';
|
|
2
|
+
import type { $ZodType } from 'zod/v4/core';
|
|
3
|
+
import type { Z2FViteConfig } from '../types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Build the effective `CodegenConfig` for a given (config, variant) pair.
|
|
6
|
+
*
|
|
7
|
+
* The bare default variant (`''`) returns the config as-is (minus the
|
|
8
|
+
* `variants` field, which has no business in `generateFormComponent`'s
|
|
9
|
+
* input). A named variant looks up `config.variants[variant]` and merges
|
|
10
|
+
* it on top of the global fields. Unknown variants throw.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* The plugin-side effective config — same shape as `CodegenConfig` except
|
|
14
|
+
* `exportName` may still be empty (auto-detect). `compileTarget` promotes
|
|
15
|
+
* it to a real name via `selectExport` before handing off to codegen.
|
|
16
|
+
*/
|
|
17
|
+
export type EffectiveZ2FConfig = Omit<CodegenConfig, 'exportName'> & {
|
|
18
|
+
exportName?: string;
|
|
19
|
+
};
|
|
20
|
+
export declare function buildEffectiveConfig(config: Z2FViteConfig, variant: string): EffectiveZ2FConfig;
|
|
21
|
+
/**
|
|
22
|
+
* Module namespace returned by Vite's `ssrLoadModule`. The shape is
|
|
23
|
+
* `Record<string, unknown>` — each named export is a property whose value
|
|
24
|
+
* is whatever the user wrote.
|
|
25
|
+
*/
|
|
26
|
+
export type ModuleNamespace = Record<string, unknown>;
|
|
27
|
+
/**
|
|
28
|
+
* Pick the named export from a module namespace that should be compiled
|
|
29
|
+
* into a form. Resolution order:
|
|
30
|
+
*
|
|
31
|
+
* 1. If `expectedName` is provided, return that export (or throw if it
|
|
32
|
+
* doesn't exist or isn't a Zod schema).
|
|
33
|
+
* 2. Otherwise, find every named export that looks like a Zod schema.
|
|
34
|
+
* If exactly one matches, return it. If zero or more than one match,
|
|
35
|
+
* throw with a clear diagnostic — the user must disambiguate via
|
|
36
|
+
* `config.exportName` or `config.variants[name].exportName`.
|
|
37
|
+
*
|
|
38
|
+
* Default exports are NOT considered — the contract is "named export
|
|
39
|
+
* matching the convention". This avoids surprising the user when they
|
|
40
|
+
* have both a default-exported helper and a named schema.
|
|
41
|
+
*/
|
|
42
|
+
export declare function selectExport(namespace: ModuleNamespace, schemaFile: string, expectedName?: string): {
|
|
43
|
+
name: string;
|
|
44
|
+
schema: $ZodType;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Compute a stable cache key from a config-shaped value. Consumers (the
|
|
48
|
+
* `CompilationCache` keying logic in `plugin.ts`) should call this rather
|
|
49
|
+
* than canonicalizing themselves so the hashing strategy stays in one
|
|
50
|
+
* place. The parameter is widened beyond `CodegenConfig` because the
|
|
51
|
+
* plugin hashes its own `Z2FViteConfig` (which adds `variants`) — and
|
|
52
|
+
* `canonicalizeConfig` walks structurally, so either shape round-trips.
|
|
53
|
+
*
|
|
54
|
+
* Truncated to 16 hex chars (64 bits): collision-resistant enough for a
|
|
55
|
+
* per-project cache keyed by (schemaFile, variant) pairs and keeps the
|
|
56
|
+
* resulting virtual-module query strings short.
|
|
57
|
+
*/
|
|
58
|
+
export declare function configHash(config: CodegenConfig | Record<string, unknown>): string;
|
|
59
|
+
//# sourceMappingURL=load.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../../src/config/load.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAIjD;;;;;;;GAOG;AACH;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,IAAI,CAAC,aAAa,EAAE,YAAY,CAAC,GAAG;IACnE,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,GAAG,kBAAkB,CA0B/F;AAID;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAoBtD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAC1B,SAAS,EAAE,eAAe,EAC1B,UAAU,EAAE,MAAM,EAClB,YAAY,CAAC,EAAE,MAAM,GACpB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,QAAQ,CAAA;CAAE,CA8CpC;AAID;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAGlF"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config loading + variant merging.
|
|
3
|
+
*
|
|
4
|
+
* The Vite plugin uses Vite's own module loaders (`server.ssrLoadModule`
|
|
5
|
+
* in dev, `this.load` in build) to load `z2f.config.ts` — see research R2.
|
|
6
|
+
* This file's responsibility is the pure parts of that pipeline:
|
|
7
|
+
*
|
|
8
|
+
* - Build the effective config for a given variant by merging the variant
|
|
9
|
+
* override on top of the global config
|
|
10
|
+
* - Validate that the loaded config has the shape of a `CodegenConfig`
|
|
11
|
+
* - `selectExport` — pick the right Zod schema from a module namespace,
|
|
12
|
+
* throwing on ambiguity
|
|
13
|
+
* - `canonicalizeForCache` — produce the SHA-256 cache key for a target
|
|
14
|
+
*/
|
|
15
|
+
import { createHash } from 'node:crypto';
|
|
16
|
+
import { canonicalizeConfig } from '@zod-to-form/core';
|
|
17
|
+
import { Z2FViteError } from '../errors.js';
|
|
18
|
+
export function buildEffectiveConfig(config, variant) {
|
|
19
|
+
// Strip the plugin-only `variants` field — generateFormComponent doesn't
|
|
20
|
+
// know about it and would copy it through.
|
|
21
|
+
const { variants, ...base } = config;
|
|
22
|
+
// Default variant + plugin-internal `__generate_<n>` variants always
|
|
23
|
+
// resolve to the base config. Generate-mode synthesizes one variant per
|
|
24
|
+
// `<ZodForm>` site to give each generated component its own cache
|
|
25
|
+
// entry, but they share the user's global config — there's no per-site
|
|
26
|
+
// override mechanism in v1.
|
|
27
|
+
if (variant === '' || /^__generate_\d+$/.test(variant)) {
|
|
28
|
+
return base;
|
|
29
|
+
}
|
|
30
|
+
if (variants === undefined || variants[variant] === undefined) {
|
|
31
|
+
const known = variants ? Object.keys(variants).join(', ') : '(none declared)';
|
|
32
|
+
throw new Z2FViteError('Z2F_VITE_UNKNOWN_VARIANT', `Variant '${variant}' is not declared in z2f.config.ts. Known variants: ${known}.`);
|
|
33
|
+
}
|
|
34
|
+
// Per-variant overrides win over global fields. Only top-level keys are
|
|
35
|
+
// merged — there's no deep merge of nested objects like componentConfig,
|
|
36
|
+
// because variants typically swap whole sub-objects rather than patch them.
|
|
37
|
+
return { ...base, ...variants[variant] };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Structural Zod-v4 check: the value is a non-null object whose `_zod`
|
|
41
|
+
* property is itself a non-null object. Zod v4 attaches its internal
|
|
42
|
+
* machinery under `_zod`, so this is the standard duck-type.
|
|
43
|
+
*
|
|
44
|
+
* The predicate narrows to `$ZodType` because `walkSchema` from core and
|
|
45
|
+
* all downstream codegen helpers consume that interface — tightening here
|
|
46
|
+
* eliminates `as $ZodType` casts at every call site.
|
|
47
|
+
*
|
|
48
|
+
* We deliberately avoid a runtime `import` of zod: the user's project owns
|
|
49
|
+
* the zod version, and the plugin mustn't couple to a specific copy.
|
|
50
|
+
*/
|
|
51
|
+
function isZodSchema(value) {
|
|
52
|
+
if (typeof value !== 'object' || value === null)
|
|
53
|
+
return false;
|
|
54
|
+
const zodInternal = value._zod;
|
|
55
|
+
return typeof zodInternal === 'object' && zodInternal !== null;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Pick the named export from a module namespace that should be compiled
|
|
59
|
+
* into a form. Resolution order:
|
|
60
|
+
*
|
|
61
|
+
* 1. If `expectedName` is provided, return that export (or throw if it
|
|
62
|
+
* doesn't exist or isn't a Zod schema).
|
|
63
|
+
* 2. Otherwise, find every named export that looks like a Zod schema.
|
|
64
|
+
* If exactly one matches, return it. If zero or more than one match,
|
|
65
|
+
* throw with a clear diagnostic — the user must disambiguate via
|
|
66
|
+
* `config.exportName` or `config.variants[name].exportName`.
|
|
67
|
+
*
|
|
68
|
+
* Default exports are NOT considered — the contract is "named export
|
|
69
|
+
* matching the convention". This avoids surprising the user when they
|
|
70
|
+
* have both a default-exported helper and a named schema.
|
|
71
|
+
*/
|
|
72
|
+
export function selectExport(namespace, schemaFile, expectedName) {
|
|
73
|
+
if (expectedName !== undefined) {
|
|
74
|
+
const value = namespace[expectedName];
|
|
75
|
+
if (value === undefined) {
|
|
76
|
+
throw new Z2FViteError('Z2F_VITE_SCHEMA_NOT_FOUND', `Schema export '${expectedName}' not found in '${schemaFile}'. Available named exports: ${Object.keys(namespace).join(', ') || '(none)'}.`, { file: schemaFile });
|
|
77
|
+
}
|
|
78
|
+
if (!isZodSchema(value)) {
|
|
79
|
+
throw new Z2FViteError('Z2F_VITE_SCHEMA_NOT_ZOD', `Export '${expectedName}' from '${schemaFile}' is not a Zod v4 schema (no '_zod' property). Make sure the export is a Zod schema instance, not a constructor or plain object.`, { file: schemaFile });
|
|
80
|
+
}
|
|
81
|
+
return { name: expectedName, schema: value };
|
|
82
|
+
}
|
|
83
|
+
// Auto-detect mode: find every Zod-shaped named export.
|
|
84
|
+
const candidates = [];
|
|
85
|
+
for (const [name, value] of Object.entries(namespace)) {
|
|
86
|
+
if (name === 'default')
|
|
87
|
+
continue; // skip default exports per the contract above
|
|
88
|
+
if (isZodSchema(value)) {
|
|
89
|
+
candidates.push({ name, schema: value });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (candidates.length === 0) {
|
|
93
|
+
throw new Z2FViteError('Z2F_VITE_SCHEMA_NOT_FOUND', `No Zod v4 schema exports found in '${schemaFile}'. Default exports are not considered — re-export as a named export (e.g. 'export const signupSchema = z.object({ ... });').`, { file: schemaFile });
|
|
94
|
+
}
|
|
95
|
+
if (candidates.length > 1) {
|
|
96
|
+
throw new Z2FViteError('Z2F_VITE_AMBIGUOUS_EXPORT', `Schema file '${schemaFile}' has multiple Zod schema exports (${candidates.map((c) => c.name).join(', ')}) and no 'exportName' is configured. Set 'exportName' in z2f.config.ts (or in a variant) to disambiguate.`, { file: schemaFile });
|
|
97
|
+
}
|
|
98
|
+
return candidates[0];
|
|
99
|
+
}
|
|
100
|
+
// ─── Cache key helper ────────────────────────────────────────────────
|
|
101
|
+
/**
|
|
102
|
+
* Compute a stable cache key from a config-shaped value. Consumers (the
|
|
103
|
+
* `CompilationCache` keying logic in `plugin.ts`) should call this rather
|
|
104
|
+
* than canonicalizing themselves so the hashing strategy stays in one
|
|
105
|
+
* place. The parameter is widened beyond `CodegenConfig` because the
|
|
106
|
+
* plugin hashes its own `Z2FViteConfig` (which adds `variants`) — and
|
|
107
|
+
* `canonicalizeConfig` walks structurally, so either shape round-trips.
|
|
108
|
+
*
|
|
109
|
+
* Truncated to 16 hex chars (64 bits): collision-resistant enough for a
|
|
110
|
+
* per-project cache keyed by (schemaFile, variant) pairs and keeps the
|
|
111
|
+
* resulting virtual-module query strings short.
|
|
112
|
+
*/
|
|
113
|
+
export function configHash(config) {
|
|
114
|
+
const canonical = canonicalizeConfig(config);
|
|
115
|
+
return createHash('sha256').update(canonical).digest('hex').slice(0, 16);
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=load.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load.js","sourceRoot":"","sources":["../../src/config/load.ts"],"names":[],"mappings":"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"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin error classes.
|
|
3
|
+
*
|
|
4
|
+
* Every plugin error carries a stable `code` string and an optional `location`
|
|
5
|
+
* (file path + line/column). Codes are part of the public contract — see
|
|
6
|
+
* `specs/007-vite-codegen-plugin/contracts/plugin-options.md`.
|
|
7
|
+
*
|
|
8
|
+
* Errors are recoverable during `vite dev` (the plugin catches and reports
|
|
9
|
+
* them via the dev server's error collector without crashing). On `vite build`
|
|
10
|
+
* they propagate normally and abort the build.
|
|
11
|
+
*/
|
|
12
|
+
export type Z2FViteErrorCode = 'Z2F_VITE_CONFIG_NOT_FOUND' | 'Z2F_VITE_CONFIG_INVALID' | 'Z2F_VITE_SCHEMA_NOT_FOUND' | 'Z2F_VITE_SCHEMA_OUTSIDE_ROOT' | 'Z2F_VITE_SCHEMA_NOT_ZOD' | 'Z2F_VITE_AMBIGUOUS_EXPORT' | 'Z2F_VITE_UNKNOWN_VARIANT' | 'Z2F_VITE_QUERY_COMPOSITION_UNSUPPORTED' | 'Z2F_VITE_INVALID_VARIANT_NAME' | 'Z2F_VITE_CODEGEN_FAILURE' | 'Z2F_VITE_GENERATE_PARSE_ERROR' | 'Z2F_VITE_WOULD_CLOBBER_FILE' | 'Z2F_VITE_INVALID_OPTIONS' | 'Z2F_VITE_NOT_IMPLEMENTED' | 'Z2F_VITE_RESOLVER_STRIP_FAILED';
|
|
13
|
+
export interface Z2FViteErrorLocation {
|
|
14
|
+
file?: string;
|
|
15
|
+
line?: number;
|
|
16
|
+
column?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Structured error thrown by the `@zod-to-form/vite` plugin.
|
|
20
|
+
*
|
|
21
|
+
* Every error carries a stable `code` (see `Z2FViteErrorCode`) and an optional
|
|
22
|
+
* file location. During `vite dev` the plugin catches these and reports them via
|
|
23
|
+
* the dev-server error overlay. During `vite build` they propagate and abort the
|
|
24
|
+
* build with the code as a breadcrumb.
|
|
25
|
+
*
|
|
26
|
+
* @useWhen
|
|
27
|
+
* - Catching plugin errors in integration tests: `expect(fn).toThrow(/Z2F_VITE_/))`
|
|
28
|
+
* - Wrapping plugin calls in error handlers that need to branch on specific error codes
|
|
29
|
+
*
|
|
30
|
+
* @avoidWhen
|
|
31
|
+
* - General application error handling — this class is specific to plugin-level failures
|
|
32
|
+
*
|
|
33
|
+
* @pitfalls
|
|
34
|
+
* - NEVER compare `error.message` to detect error type — the message format may change.
|
|
35
|
+
* Use `error.code` (e.g. `error.code === 'Z2F_VITE_SCHEMA_NOT_FOUND'`) for stable matching
|
|
36
|
+
*
|
|
37
|
+
* @category Errors
|
|
38
|
+
*/
|
|
39
|
+
export declare class Z2FViteError extends Error {
|
|
40
|
+
readonly code: Z2FViteErrorCode;
|
|
41
|
+
readonly location?: Z2FViteErrorLocation;
|
|
42
|
+
constructor(code: Z2FViteErrorCode, message: string, location?: Z2FViteErrorLocation);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Format an error for inclusion in a Vite error overlay or terminal output.
|
|
46
|
+
* The error's `message` already includes the code prefix; this function
|
|
47
|
+
* appends the location line when available.
|
|
48
|
+
*/
|
|
49
|
+
export declare function formatZ2FViteError(error: Z2FViteError): string;
|
|
50
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,MAAM,gBAAgB,GACxB,2BAA2B,GAC3B,yBAAyB,GACzB,2BAA2B,GAC3B,8BAA8B,GAC9B,yBAAyB,GACzB,2BAA2B,GAC3B,0BAA0B,GAC1B,wCAAwC,GACxC,+BAA+B,GAC/B,0BAA0B,GAC1B,+BAA+B,GAC/B,6BAA6B,GAC7B,0BAA0B,GAC1B,0BAA0B,GAC1B,gCAAgC,CAAC;AAErC,MAAM,WAAW,oBAAoB;IACnC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,YAAa,SAAQ,KAAK;IACrC,SAAgB,IAAI,EAAE,gBAAgB,CAAC;IACvC,SAAgB,QAAQ,CAAC,EAAE,oBAAoB,CAAC;IAEhD,YAAY,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,oBAAoB,EAUnF;CACF;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAQ9D"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin error classes.
|
|
3
|
+
*
|
|
4
|
+
* Every plugin error carries a stable `code` string and an optional `location`
|
|
5
|
+
* (file path + line/column). Codes are part of the public contract — see
|
|
6
|
+
* `specs/007-vite-codegen-plugin/contracts/plugin-options.md`.
|
|
7
|
+
*
|
|
8
|
+
* Errors are recoverable during `vite dev` (the plugin catches and reports
|
|
9
|
+
* them via the dev server's error collector without crashing). On `vite build`
|
|
10
|
+
* they propagate normally and abort the build.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Structured error thrown by the `@zod-to-form/vite` plugin.
|
|
14
|
+
*
|
|
15
|
+
* Every error carries a stable `code` (see `Z2FViteErrorCode`) and an optional
|
|
16
|
+
* file location. During `vite dev` the plugin catches these and reports them via
|
|
17
|
+
* the dev-server error overlay. During `vite build` they propagate and abort the
|
|
18
|
+
* build with the code as a breadcrumb.
|
|
19
|
+
*
|
|
20
|
+
* @useWhen
|
|
21
|
+
* - Catching plugin errors in integration tests: `expect(fn).toThrow(/Z2F_VITE_/))`
|
|
22
|
+
* - Wrapping plugin calls in error handlers that need to branch on specific error codes
|
|
23
|
+
*
|
|
24
|
+
* @avoidWhen
|
|
25
|
+
* - General application error handling — this class is specific to plugin-level failures
|
|
26
|
+
*
|
|
27
|
+
* @pitfalls
|
|
28
|
+
* - NEVER compare `error.message` to detect error type — the message format may change.
|
|
29
|
+
* Use `error.code` (e.g. `error.code === 'Z2F_VITE_SCHEMA_NOT_FOUND'`) for stable matching
|
|
30
|
+
*
|
|
31
|
+
* @category Errors
|
|
32
|
+
*/
|
|
33
|
+
export class Z2FViteError extends Error {
|
|
34
|
+
code;
|
|
35
|
+
location;
|
|
36
|
+
constructor(code, message, location) {
|
|
37
|
+
// Prefix the message with the code so error.message, error.toString(),
|
|
38
|
+
// and Vitest's `expect.toThrow(/CODE/)` matchers all surface the code
|
|
39
|
+
// without callers having to inspect `error.code` separately.
|
|
40
|
+
super(`[${code}] ${message}`);
|
|
41
|
+
this.name = 'Z2FViteError';
|
|
42
|
+
this.code = code;
|
|
43
|
+
if (location !== undefined) {
|
|
44
|
+
this.location = location;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Format an error for inclusion in a Vite error overlay or terminal output.
|
|
50
|
+
* The error's `message` already includes the code prefix; this function
|
|
51
|
+
* appends the location line when available.
|
|
52
|
+
*/
|
|
53
|
+
export function formatZ2FViteError(error) {
|
|
54
|
+
const parts = [error.message];
|
|
55
|
+
if (error.location?.file) {
|
|
56
|
+
const line = error.location.line ?? 0;
|
|
57
|
+
const column = error.location.column ?? 0;
|
|
58
|
+
parts.push(` at ${error.location.file}:${line}:${column}`);
|
|
59
|
+
}
|
|
60
|
+
return parts.join('\n');
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAyBH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrB,IAAI,CAAmB;IACvB,QAAQ,CAAwB;IAEhD,YAAY,IAAsB,EAAE,OAAe,EAAE,QAA+B;QAClF,uEAAuE;QACvE,sEAAsE;QACtE,6DAA6D;QAC7D,KAAK,CAAC,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC,CAAC;QAC9B,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAC3B,CAAC;IACH,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAmB;IACpD,MAAM,KAAK,GAAa,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACxC,IAAI,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC;QACzB,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,CAAC;QACtC,MAAM,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,CAAC;QAC1C,KAAK,CAAC,IAAI,CAAC,QAAQ,KAAK,CAAC,QAAQ,CAAC,IAAI,IAAI,IAAI,IAAI,MAAM,EAAE,CAAC,CAAC;IAC9D,CAAC;IACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"babel-traverse.d.ts","sourceRoot":"","sources":["../../src/generate-mode/babel-traverse.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvD,KAAK,UAAU,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;AAIhF,eAAO,MAAM,QAAQ,EAAE,UACgD,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@babel/traverse` ESM/CJS interop shim.
|
|
3
|
+
*
|
|
4
|
+
* The package's published types model the entry point as a namespace,
|
|
5
|
+
* but at runtime it's either the function itself (modern bundlers) or
|
|
6
|
+
* a `{ default: fn }` object (CJS interop). Importing as a namespace
|
|
7
|
+
* and then unwrapping at runtime is the only shape that works under
|
|
8
|
+
* tsgo, vitest, AND esbuild's bundler at the same time.
|
|
9
|
+
*
|
|
10
|
+
* This file owns the cast so neither `scan-jsx.ts` nor `resolve-schema.ts`
|
|
11
|
+
* has to repeat the same five-line dance.
|
|
12
|
+
*/
|
|
13
|
+
import * as traverseModule from '@babel/traverse';
|
|
14
|
+
const traverseAny = traverseModule;
|
|
15
|
+
export const traverse = typeof traverseAny === 'function' ? traverseAny : traverseAny.default;
|
|
16
|
+
//# sourceMappingURL=babel-traverse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"babel-traverse.js","sourceRoot":"","sources":["../../src/generate-mode/babel-traverse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,cAAc,MAAM,iBAAiB,CAAC;AAKlD,MAAM,WAAW,GAAG,cAAiE,CAAC;AAEtF,MAAM,CAAC,MAAM,QAAQ,GACnB,OAAO,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import MagicString from 'magic-string';
|
|
2
|
+
import type { ResolvedSite } from './resolve-schema.js';
|
|
3
|
+
export interface GenerateSourceInput {
|
|
4
|
+
source: string;
|
|
5
|
+
resolved: ResolvedSite[];
|
|
6
|
+
/**
|
|
7
|
+
* Optional diagnostic callback. Called with an error message (never
|
|
8
|
+
* an Error object) when an internal invariant is violated — e.g., the
|
|
9
|
+
* insertion-point scanner's Babel re-parse fails even though scanJsx
|
|
10
|
+
* already parsed the same source successfully. Wired to the plugin's
|
|
11
|
+
* logger at the call site so these land in the buildEnd summary
|
|
12
|
+
* instead of being lost.
|
|
13
|
+
*/
|
|
14
|
+
onWarn?: (message: string) => void;
|
|
15
|
+
}
|
|
16
|
+
export interface GenerateSourceOutput {
|
|
17
|
+
/** The transformed source (or the original, if there was nothing to do). */
|
|
18
|
+
code: string;
|
|
19
|
+
/** Hi-res sourcemap from magic-string, suitable for Vite. */
|
|
20
|
+
map: ReturnType<MagicString['generateMap']>;
|
|
21
|
+
/** Number of sites actually rewritten (== `resolved.length`). */
|
|
22
|
+
rewritten: number;
|
|
23
|
+
}
|
|
24
|
+
export declare function generateSource(input: GenerateSourceInput): GenerateSourceOutput;
|
|
25
|
+
/**
|
|
26
|
+
* Find the byte offset at which to insert generated imports. The target
|
|
27
|
+
* is "immediately after the last thing that must come first": the file's
|
|
28
|
+
* shebang, its leading string directives (`'use client'`, `'use strict'`),
|
|
29
|
+
* and any existing top-level `import` statements. If none of those exist,
|
|
30
|
+
* the result is byte 0 (the file starts with user code and the generated
|
|
31
|
+
* imports go at the very top).
|
|
32
|
+
*
|
|
33
|
+
* Uses Babel's parser — the same dependency scan-jsx and resolve-schema
|
|
34
|
+
* already rely on — so directive detection, multi-line imports,
|
|
35
|
+
* TypeScript-specific forms (`import type`), side-effect-only imports,
|
|
36
|
+
* BOM handling, and escape sequences inside string literals are all
|
|
37
|
+
* handled by the production-grade parser instead of a hand-rolled
|
|
38
|
+
* textual walker.
|
|
39
|
+
*
|
|
40
|
+
* On parse failure (e.g. the user is mid-save with broken syntax) we
|
|
41
|
+
* warn via `onWarn` and return 0. This is defense-in-depth: `scanJsx`'s
|
|
42
|
+
* upstream parse normally filters broken sources before this code runs,
|
|
43
|
+
* but a future Babel plugin-flag drift between the two parsers could
|
|
44
|
+
* make them disagree, and silent corruption is worse than a warn line.
|
|
45
|
+
*
|
|
46
|
+
* `onWarn` MUST NOT throw — exceptions in the callback would kill the
|
|
47
|
+
* transform and turn a recoverable warning into a hard build failure.
|
|
48
|
+
* Any throw is caught and ignored below.
|
|
49
|
+
*/
|
|
50
|
+
export declare function findImportInsertionPoint(source: string, onWarn?: (message: string) => void): number;
|
|
51
|
+
//# sourceMappingURL=generate-source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-source.d.ts","sourceRoot":"","sources":["../../src/generate-mode/generate-source.ts"],"names":[],"mappings":"AAoBA,OAAO,WAAW,MAAM,cAAc,CAAC;AAEvC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACpC;AAED,MAAM,WAAW,oBAAoB;IACnC,4EAA4E;IAC5E,IAAI,EAAE,MAAM,CAAC;IACb,6DAA6D;IAC7D,GAAG,EAAE,UAAU,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC,CAAC;IAC5C,iEAAiE;IACjE,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,mBAAmB,GAAG,oBAAoB,CAyE/E;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,GACjC,MAAM,CAqER"}
|