@evobug_com/safe-ts-transform 0.5.1 → 0.7.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/bun-plugin.mjs +8 -4
- package/cache.mjs +2 -2
- package/index.d.ts +20 -2
- package/index.mjs +28 -1
- package/package.json +3 -3
- package/register.mjs +6 -3
package/bun-plugin.mjs
CHANGED
|
@@ -11,7 +11,7 @@ import { dirname, resolve } from "node:path";
|
|
|
11
11
|
import { pathToFileURL } from "node:url";
|
|
12
12
|
|
|
13
13
|
import { openCache } from "./cache.mjs";
|
|
14
|
-
import { devChecksEnabled, SHARED_QUERY, sharedModule, sharedModuleId, transformWithDependencies } from "./index.mjs";
|
|
14
|
+
import { devChecksEnabled, holdPointsEnabled, SHARED_QUERY, sharedModule, sharedModuleId, transformWithDependencies } from "./index.mjs";
|
|
15
15
|
|
|
16
16
|
const require = createRequire(import.meta.url);
|
|
17
17
|
|
|
@@ -36,12 +36,15 @@ function importerDir(importer) {
|
|
|
36
36
|
* bundler). Options default to what fits where it runs: at runtime as the Node loader does
|
|
37
37
|
* (dev checks unless NODE_ENV=production, no rule checks), in `Bun.build` as `vite build` does.
|
|
38
38
|
*
|
|
39
|
-
* @param {{ check?: "warn" | "error" | false, devChecks?: boolean, sharedValidators?: boolean }} [options]
|
|
39
|
+
* @param {{ check?: "warn" | "error" | false, devChecks?: boolean, holdPoints?: boolean, sharedValidators?: boolean }} [options]
|
|
40
40
|
* check: run the safe-ts rules on every transformed file. "warn" prints problems, "error"
|
|
41
41
|
* fails the load (the build, or the import at runtime); false turns it off. Default: "warn"
|
|
42
42
|
* in `Bun.build`, false at runtime.
|
|
43
43
|
* devChecks: insert dev-only runtime checks. Default: off in `Bun.build`; at runtime, on
|
|
44
44
|
* unless NODE_ENV=production, and SAFE_TS_DEV_CHECKS=1 / =0 overrides it.
|
|
45
|
+
* holdPoints: keep hold points (`await holdPoint(name);`) where a test can arm them, instead of
|
|
46
|
+
* removing them. Default: off in `Bun.build`; at runtime on under `bun test` (NODE_ENV=test)
|
|
47
|
+
* and with SAFE_TS_HOLD_DIR set, and SAFE_TS_HOLD_POINTS=1 / =0 overrides it.
|
|
45
48
|
* sharedValidators: one validator module per named type (default), or a copy per file
|
|
46
49
|
* (false; at runtime also SAFE_TS_SHARED_VALIDATORS=0).
|
|
47
50
|
*/
|
|
@@ -56,7 +59,8 @@ export function safeTsBun(options = {}) {
|
|
|
56
59
|
const devChecks = options.devChecks ?? (bundling ? false : devChecksEnabled(env));
|
|
57
60
|
const sharedValidators =
|
|
58
61
|
options.sharedValidators ?? (bundling || !["0", "false"].includes(env.SAFE_TS_SHARED_VALIDATORS ?? ""));
|
|
59
|
-
const
|
|
62
|
+
const holdPoints = options.holdPoints ?? (bundling ? false : holdPointsEnabled(env, process.execArgv));
|
|
63
|
+
const cache = openCache(env, { devChecks, sharedValidators, holdPoints });
|
|
60
64
|
const root = process.cwd();
|
|
61
65
|
let checker;
|
|
62
66
|
// The files the runtime transformed, for the Vite plugin's config check (index.mjs), as
|
|
@@ -122,7 +126,7 @@ export function safeTsBun(options = {}) {
|
|
|
122
126
|
}
|
|
123
127
|
let contents = cache?.get(file, code);
|
|
124
128
|
if (contents === undefined) {
|
|
125
|
-
const result = transformWithDependencies(file, code, { devChecks, sharedValidators });
|
|
129
|
+
const result = transformWithDependencies(file, code, { devChecks, sharedValidators, holdPoints });
|
|
126
130
|
contents = result.code;
|
|
127
131
|
// A file with a warning isn't cached, so the warning shows on every run until it's fixed.
|
|
128
132
|
if (result.warnings.length === 0) cache?.set(file, code, result);
|
package/cache.mjs
CHANGED
|
@@ -90,7 +90,7 @@ export function cacheDir(env, cwd = process.cwd()) {
|
|
|
90
90
|
* The cache for this process, or undefined when it is turned off or no directory is writable.
|
|
91
91
|
*
|
|
92
92
|
* @param {Record<string, string | undefined>} env
|
|
93
|
-
* @param {{ devChecks: boolean, sharedValidators?: boolean }} options what the transform is asked
|
|
93
|
+
* @param {{ devChecks: boolean, sharedValidators?: boolean, holdPoints?: boolean }} options what the transform is asked
|
|
94
94
|
* for: part of each key.
|
|
95
95
|
*/
|
|
96
96
|
export function openCache(env, options) {
|
|
@@ -99,7 +99,7 @@ export function openCache(env, options) {
|
|
|
99
99
|
const dir = cacheDir(env);
|
|
100
100
|
if (dir === undefined) return undefined;
|
|
101
101
|
// Dev checks name files relative to the current directory: it is part of the key too.
|
|
102
|
-
const version = JSON.stringify([FORMAT, transformVersion(), options.devChecks === true, options.sharedValidators === true, process.cwd()]);
|
|
102
|
+
const version = JSON.stringify([FORMAT, transformVersion(), options.devChecks === true, options.sharedValidators === true, options.holdPoints === true, process.cwd()]);
|
|
103
103
|
const versionKey = digest(version).slice(0, 24);
|
|
104
104
|
const fileDir = (filename) => join(dir, digest(filename).slice(0, 32));
|
|
105
105
|
const entryPath = (filename) => join(fileDir(filename), `${versionKey}.json`);
|
package/index.d.ts
CHANGED
|
@@ -21,6 +21,12 @@ export type SafeTsOptions = {
|
|
|
21
21
|
* `vite build` and other bundlers.
|
|
22
22
|
*/
|
|
23
23
|
devChecks?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Keep hold points (`await holdPoint(name);` of `@evobug_com/safe-ts-std/hold`) where a test
|
|
26
|
+
* can arm them; they await nothing unless one is armed. Off: the statements are removed. By
|
|
27
|
+
* default on under Vitest (Vite's `test` mode) and off for every other build.
|
|
28
|
+
*/
|
|
29
|
+
holdPoints?: boolean;
|
|
24
30
|
/**
|
|
25
31
|
* Generate the validator (and test-data generator) of each named type once, in a module of
|
|
26
32
|
* its own that every file using it imports, instead of a copy per file. On by default.
|
|
@@ -99,6 +105,11 @@ export type SafeTsBunOptions = {
|
|
|
99
105
|
* `NODE_ENV=production`, and `SAFE_TS_DEV_CHECKS=1` / `=0` overrides that.
|
|
100
106
|
*/
|
|
101
107
|
devChecks?: boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Keep hold points where a test can arm them, instead of removing them. Default: off in
|
|
110
|
+
* `Bun.build`; at runtime as `holdPointsEnabled` says.
|
|
111
|
+
*/
|
|
112
|
+
holdPoints?: boolean;
|
|
102
113
|
/** One validator module per named type (default), or a copy per file (false). */
|
|
103
114
|
sharedValidators?: boolean;
|
|
104
115
|
};
|
|
@@ -130,7 +141,7 @@ export interface SafeTsBunPlugin {
|
|
|
130
141
|
export function safeTsBun(options?: SafeTsBunOptions): SafeTsBunPlugin;
|
|
131
142
|
|
|
132
143
|
/** Rewrite one file. Throws with all errors if the file can't be transformed. */
|
|
133
|
-
export function transform(filename: string, code: string, options?: { devChecks?: boolean }): string;
|
|
144
|
+
export function transform(filename: string, code: string, options?: { devChecks?: boolean; holdPoints?: boolean }): string;
|
|
134
145
|
|
|
135
146
|
/**
|
|
136
147
|
* A bundler alias with a string `find` (Vite's `resolve.alias`): `find` and anything below it
|
|
@@ -146,7 +157,7 @@ export type SafeTsAlias = { find: string; replacement: string };
|
|
|
146
157
|
export function transformWithDependencies(
|
|
147
158
|
filename: string,
|
|
148
159
|
code: string,
|
|
149
|
-
options?: { devChecks?: boolean; sharedValidators?: boolean; aliases?: readonly SafeTsAlias[] },
|
|
160
|
+
options?: { devChecks?: boolean; sharedValidators?: boolean; holdPoints?: boolean; aliases?: readonly SafeTsAlias[] },
|
|
150
161
|
): { code: string; dependencies: string[]; warnings: string[]; legacy: boolean };
|
|
151
162
|
|
|
152
163
|
/**
|
|
@@ -189,6 +200,13 @@ export function sharedModule(file: string, key: string, aliases?: readonly SafeT
|
|
|
189
200
|
*/
|
|
190
201
|
export function devChecksEnabled(env: Record<string, string | undefined>): boolean;
|
|
191
202
|
|
|
203
|
+
/**
|
|
204
|
+
* Whether the Node loader (and Bun at run time) keeps hold points where a test can arm them:
|
|
205
|
+
* under a test runner (`NODE_TEST_CONTEXT`, `--test`, `VITEST`, `NODE_ENV=test`) and with
|
|
206
|
+
* `SAFE_TS_HOLD_DIR` set; `SAFE_TS_HOLD_POINTS=1` / `=0` overrides that.
|
|
207
|
+
*/
|
|
208
|
+
export function holdPointsEnabled(env: Record<string, string | undefined>, execArgv?: readonly string[]): boolean;
|
|
209
|
+
|
|
192
210
|
/**
|
|
193
211
|
* The warning the plugin prints when the Vite config imports code that only works after the
|
|
194
212
|
* transform (Vite loads its config without plugins), or undefined.
|
package/index.mjs
CHANGED
|
@@ -36,6 +36,7 @@ export function transformWithDependencies(filename, code, options = {}) {
|
|
|
36
36
|
const result = native.transform(filename, code, {
|
|
37
37
|
devChecks: options.devChecks === true,
|
|
38
38
|
sharedValidators: options.sharedValidators === true,
|
|
39
|
+
holdPoints: options.holdPoints === true,
|
|
39
40
|
...(options.aliases?.length ? { aliases: options.aliases } : {}),
|
|
40
41
|
});
|
|
41
42
|
if (result.errors.length > 0) {
|
|
@@ -122,6 +123,26 @@ export function devChecksEnabled(env) {
|
|
|
122
123
|
return env.NODE_ENV !== "production";
|
|
123
124
|
}
|
|
124
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Whether the Node loader and Bun at run time keep hold points (`await holdPoint(name);` of
|
|
128
|
+
* `@evobug_com/safe-ts-std/hold`) where a test can arm them, instead of removing them: under a
|
|
129
|
+
* test runner (node --test: `NODE_TEST_CONTEXT` or `--test`; Vitest: `VITEST`; bun test and
|
|
130
|
+
* Jest: `NODE_ENV=test`), and in a process given `SAFE_TS_HOLD_DIR` (one a test starts, holding
|
|
131
|
+
* it across processes). `SAFE_TS_HOLD_POINTS=1` / `=0` (or `true` / `false`) overrides that.
|
|
132
|
+
*/
|
|
133
|
+
export function holdPointsEnabled(env, execArgv = []) {
|
|
134
|
+
const flag = env.SAFE_TS_HOLD_POINTS;
|
|
135
|
+
if (flag === "1" || flag === "true") return true;
|
|
136
|
+
if (flag === "0" || flag === "false") return false;
|
|
137
|
+
return (
|
|
138
|
+
env.NODE_TEST_CONTEXT !== undefined ||
|
|
139
|
+
env.VITEST !== undefined ||
|
|
140
|
+
env.NODE_ENV === "test" ||
|
|
141
|
+
(env.SAFE_TS_HOLD_DIR ?? "") !== "" ||
|
|
142
|
+
execArgv.some((arg) => arg === "--test" || arg.startsWith("--test-"))
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
125
146
|
/**
|
|
126
147
|
* Source map for a line-preserving rewrite: every generated line maps to the start of the
|
|
127
148
|
* same original line. Lines appended after the original end (generated validators) are
|
|
@@ -157,6 +178,10 @@ export function lineMap(file, original, transformed) {
|
|
|
157
178
|
* devChecks: insert dev-only runtime checks (https://safe-ts.evobug-pages.com/guide/dev-checks).
|
|
158
179
|
* By default on for Vite's dev server (`vite`, `vite dev`, `vite serve`, and tools built on
|
|
159
180
|
* it in serve mode, like Vitest) and off for `vite build`, `vite preview` and other bundlers.
|
|
181
|
+
* holdPoints: keep hold points (`await holdPoint(name);` of `@evobug_com/safe-ts-std/hold`)
|
|
182
|
+
* where a test can arm them; they await nothing unless one is armed. Off: the statements are
|
|
183
|
+
* removed, so a build outside tests neither calls nor awaits anything there. By default on
|
|
184
|
+
* under Vitest (Vite's `test` mode, or `VITEST` set) and off everywhere else.
|
|
160
185
|
* sharedValidators: generate the validator (and test-data generator) of each named type once,
|
|
161
186
|
* in a module of its own that every file using it imports, instead of a copy per file. On by
|
|
162
187
|
* default; false gives every file its own copies, as before.
|
|
@@ -175,6 +200,7 @@ export function safeTs(options = {}) {
|
|
|
175
200
|
const hints = options.compileHints ?? false;
|
|
176
201
|
const wantsHint = (chunk) => (hints === true ? chunk.isEntry : Array.isArray(hints) && hints.includes(chunk.name));
|
|
177
202
|
let devChecks = options.devChecks ?? false;
|
|
203
|
+
let holdPoints = options.holdPoints ?? false;
|
|
178
204
|
const sharedValidators = options.sharedValidators ?? true;
|
|
179
205
|
let root = process.cwd();
|
|
180
206
|
let checker;
|
|
@@ -193,6 +219,7 @@ export function safeTs(options = {}) {
|
|
|
193
219
|
command = config.command;
|
|
194
220
|
aliases = stringAliases(config.resolve?.alias);
|
|
195
221
|
devChecks = options.devChecks ?? config.command === "serve";
|
|
222
|
+
holdPoints = options.holdPoints ?? (config.mode === "test" || process.env.VITEST !== undefined);
|
|
196
223
|
const warning = configFileWarning(config.configFile);
|
|
197
224
|
if (warning !== undefined) config.logger.warn(warning);
|
|
198
225
|
},
|
|
@@ -258,7 +285,7 @@ export function safeTs(options = {}) {
|
|
|
258
285
|
}
|
|
259
286
|
}
|
|
260
287
|
}
|
|
261
|
-
const { code: out, dependencies, warnings, legacy } = transformWithDependencies(file, code, { devChecks, sharedValidators, aliases });
|
|
288
|
+
const { code: out, dependencies, warnings, legacy } = transformWithDependencies(file, code, { devChecks, sharedValidators, holdPoints, aliases });
|
|
262
289
|
if (legacy) stats.legacy++;
|
|
263
290
|
for (const warning of warnings) this.warn(`safe-ts: ${warning}`);
|
|
264
291
|
// Generated validators follow the types they were made from, wherever those are declared.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evobug_com/safe-ts-transform",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "SAFE-TS build-time transform: a Vite/Rolldown/Rollup plugin, a Node loader and a Bun plugin that rewrite q(), match(), parseJson<T>(), loops and @inline, and run the SAFE-TS checks.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"safe-ts",
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"node": ">=22.18.0"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@evobug_com/safe-ts-native": "0.
|
|
50
|
+
"@evobug_com/safe-ts-native": "0.7.0",
|
|
51
51
|
"magic-string": "^1.4.5"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"@evobug_com/safe-ts-std": "^0.
|
|
54
|
+
"@evobug_com/safe-ts-std": "^0.7.0",
|
|
55
55
|
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
56
56
|
},
|
|
57
57
|
"peerDependenciesMeta": {
|
package/register.mjs
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// node --import ./packages/safe-ts-transform/register.mjs src/main.ts
|
|
3
3
|
// Dev checks (https://safe-ts.evobug-pages.com/guide/dev-checks) are on unless
|
|
4
4
|
// NODE_ENV=production; SAFE_TS_DEV_CHECKS=1 or =0 overrides that.
|
|
5
|
+
// Hold points (`await holdPoint(name);`) stay, as points a test can arm, under a test runner and
|
|
6
|
+
// with SAFE_TS_HOLD_DIR set, and are removed elsewhere; SAFE_TS_HOLD_POINTS=1 or =0 overrides that.
|
|
5
7
|
// Transformed files are cached on disk (cache.mjs); SAFE_TS_CACHE=0 turns that off.
|
|
6
8
|
// A file that uses prelude names where no tsconfig.json turns the prelude on (a copy of a script
|
|
7
9
|
// outside the project) is still loaded, with a warning: those names aren't defined there.
|
|
@@ -19,7 +21,7 @@ import { join } from "node:path";
|
|
|
19
21
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
20
22
|
|
|
21
23
|
import { openCache } from "./cache.mjs";
|
|
22
|
-
import { devChecksEnabled, isLibraryFile, sharedModule, sharedModuleId, transformWithDependencies } from "./index.mjs";
|
|
24
|
+
import { devChecksEnabled, holdPointsEnabled, isLibraryFile, sharedModule, sharedModuleId, transformWithDependencies } from "./index.mjs";
|
|
23
25
|
|
|
24
26
|
const native = nodeModule.createRequire(import.meta.url)("@evobug_com/safe-ts-native");
|
|
25
27
|
const root = process.cwd();
|
|
@@ -39,7 +41,8 @@ const TS_FILE = /\.(ts|mts)$/;
|
|
|
39
41
|
const HAS_EXTENSION = /\.[cm]?[jt]sx?$/;
|
|
40
42
|
const devChecks = devChecksEnabled(process.env);
|
|
41
43
|
const sharedValidators = !["0", "false"].includes(process.env.SAFE_TS_SHARED_VALIDATORS ?? "");
|
|
42
|
-
const
|
|
44
|
+
const holdPoints = holdPointsEnabled(process.env, process.execArgv);
|
|
45
|
+
const cache = openCache(process.env, { devChecks, sharedValidators, holdPoints });
|
|
43
46
|
|
|
44
47
|
// The files this thread transformed, for the Vite plugin's config check (index.mjs).
|
|
45
48
|
const loaded = (globalThis[Symbol.for("safe-ts.loader.files")] ??= new Set());
|
|
@@ -93,7 +96,7 @@ registerHooks({
|
|
|
93
96
|
const code = readFileSync(filename, "utf8");
|
|
94
97
|
let source = cache?.get(filename, code);
|
|
95
98
|
if (source === undefined) {
|
|
96
|
-
const result = transformWithDependencies(filename, code, { devChecks, sharedValidators });
|
|
99
|
+
const result = transformWithDependencies(filename, code, { devChecks, sharedValidators, holdPoints });
|
|
97
100
|
source = result.code;
|
|
98
101
|
// A file with a warning isn't cached, so the warning shows on every run until it's fixed.
|
|
99
102
|
if (result.warnings.length === 0) cache?.set(filename, code, result);
|