@elliots/bun-plugin-typical 0.2.0-beta.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.
@@ -0,0 +1,110 @@
1
+ import { BunPlugin } from "bun";
2
+
3
+ //#region ../../dist/src/config.d.ts
4
+ interface TypicalDebugConfig {
5
+ writeIntermediateFiles?: boolean;
6
+ }
7
+ /**
8
+ * Configuration options for source map generation.
9
+ */
10
+ interface TypicalSourceMapConfig {
11
+ /**
12
+ * Generate source maps. Default: true
13
+ */
14
+ enabled?: boolean;
15
+ /**
16
+ * Include original source content in the map. Default: true
17
+ */
18
+ includeContent?: boolean;
19
+ /**
20
+ * Use inline source maps (data URL) instead of external files. Default: false
21
+ */
22
+ inline?: boolean;
23
+ }
24
+ interface TypicalConfig {
25
+ include?: string[];
26
+ exclude?: string[];
27
+ reusableValidators?: boolean;
28
+ validateCasts?: boolean;
29
+ hoistRegex?: boolean;
30
+ debug?: TypicalDebugConfig;
31
+ /**
32
+ * Type patterns to skip validation for (supports wildcards).
33
+ * Use this for types that typia cannot process (e.g., React event types).
34
+ * Example: ["React.*", "Express.Request", "*.Event"]
35
+ */
36
+ ignoreTypes?: string[];
37
+ /**
38
+ * Skip validation for DOM types (Document, Element, Node, etc.) and their subclasses.
39
+ * These types have complex Window intersections that typia cannot process.
40
+ * Default: true
41
+ */
42
+ ignoreDOMTypes?: boolean;
43
+ /**
44
+ * Validate function parameters and return types at runtime.
45
+ * When enabled, typed function parameters get runtime validation calls injected.
46
+ * Default: true
47
+ */
48
+ validateFunctions?: boolean;
49
+ /**
50
+ * Source map generation settings.
51
+ * Controls whether and how source maps are generated for transformed code.
52
+ */
53
+ sourceMap?: TypicalSourceMapConfig;
54
+ }
55
+ //#endregion
56
+ //#region src/core/options.d.ts
57
+ type FilterPattern = string | RegExp | (string | RegExp)[];
58
+ interface Options {
59
+ /**
60
+ * Files to include. Defaults to all .ts/.tsx/.mts/.cts files.
61
+ */
62
+ include?: FilterPattern;
63
+ /**
64
+ * Files to exclude. Defaults to node_modules.
65
+ */
66
+ exclude?: FilterPattern;
67
+ /**
68
+ * Bun target environment.
69
+ * @default 'bun'
70
+ */
71
+ target?: "bun" | "browser" | "node";
72
+ /**
73
+ * Typical configuration overrides.
74
+ */
75
+ typical?: Partial<TypicalConfig>;
76
+ }
77
+ //#endregion
78
+ //#region src/core/transform.d.ts
79
+ /**
80
+ * Close the shared transformer and release resources.
81
+ */
82
+ declare function closeTransformer(): Promise<void>;
83
+ //#endregion
84
+ //#region src/index.d.ts
85
+ /**
86
+ * Create a Bun plugin for Typical transformation.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * // For Bun.build()
91
+ * import typicalPlugin from '@elliots/bun-plugin-typical'
92
+ *
93
+ * await Bun.build({
94
+ * entrypoints: ['./src/index.ts'],
95
+ * outdir: './dist',
96
+ * plugins: [typicalPlugin()],
97
+ * })
98
+ * ```
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * // For runtime (bunfig.toml preload)
103
+ * import typicalPlugin from '@elliots/bun-plugin-typical'
104
+ *
105
+ * Bun.plugin(typicalPlugin())
106
+ * ```
107
+ */
108
+ declare function typicalPlugin(rawOptions?: Options): BunPlugin;
109
+ //#endregion
110
+ export { type Options, closeTransformer, typicalPlugin as default, typicalPlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import { n as typicalPlugin, r as closeTransformer, t as src_default } from "./src-CBBPzozy.mjs";
2
+
3
+ export { closeTransformer, src_default as default, typicalPlugin };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,34 @@
1
+ import { n as typicalPlugin } from "./src-CBBPzozy.mjs";
2
+
3
+ //#region src/preload.ts
4
+ /**
5
+ * Convenience module for preloading the Typical plugin.
6
+ *
7
+ * @example
8
+ * ```toml
9
+ * # bunfig.toml
10
+ * preload = ["./preload.ts"]
11
+ * ```
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // preload.ts
16
+ * import '@elliots/bun-plugin-typical/preload'
17
+ * ```
18
+ *
19
+ * Or with custom options:
20
+ * ```ts
21
+ * // preload.ts
22
+ * import { typicalPlugin } from '@elliots/bun-plugin-typical'
23
+ *
24
+ * Bun.plugin(typicalPlugin({
25
+ * typical: {
26
+ * reusableValidators: true,
27
+ * }
28
+ * }))
29
+ * ```
30
+ */
31
+ Bun.plugin(typicalPlugin());
32
+
33
+ //#endregion
34
+ export { };
@@ -0,0 +1,125 @@
1
+ import { TypicalTransformer, buildTimer, loadConfig, validateConfig } from "@elliots/typical";
2
+ import { extname, resolve } from "path";
3
+
4
+ //#region src/core/options.ts
5
+ function normalizePattern(pattern) {
6
+ if (!pattern) return [];
7
+ if (Array.isArray(pattern)) return pattern;
8
+ return [pattern];
9
+ }
10
+ function resolveOptions(options) {
11
+ return {
12
+ include: normalizePattern(options.include),
13
+ exclude: options.exclude ? normalizePattern(options.exclude) : [/node_modules/],
14
+ target: options.target ?? "bun",
15
+ typical: options.typical
16
+ };
17
+ }
18
+
19
+ //#endregion
20
+ //#region src/core/transform.ts
21
+ const TRANSFORM_EXTENSIONS = new Set([
22
+ ".ts",
23
+ ".tsx",
24
+ ".mts",
25
+ ".cts"
26
+ ]);
27
+ const LOADER_MAP = {
28
+ ".ts": "ts",
29
+ ".mts": "ts",
30
+ ".cts": "ts",
31
+ ".tsx": "tsx"
32
+ };
33
+ let sharedTransformer = null;
34
+ /**
35
+ * Transform a TypeScript file with Typical.
36
+ *
37
+ * Returns TypeScript code with validation injected.
38
+ * Bun handles the final transpilation to JavaScript.
39
+ */
40
+ async function transformFile(filePath, config) {
41
+ buildTimer.start("total-transform");
42
+ const ext = extname(filePath).toLowerCase();
43
+ if (!TRANSFORM_EXTENSIONS.has(ext)) {
44
+ buildTimer.end("total-transform");
45
+ return;
46
+ }
47
+ const resolvedPath = resolve(filePath);
48
+ buildTimer.start("create-transformer");
49
+ if (!sharedTransformer) sharedTransformer = new TypicalTransformer(validateConfig(config));
50
+ buildTimer.end("create-transformer");
51
+ buildTimer.start("transform");
52
+ const result = await sharedTransformer.transform(resolvedPath, "ts");
53
+ buildTimer.end("transform");
54
+ buildTimer.end("total-transform");
55
+ if (process.env.DEBUG) console.log(`[bun-plugin-typical] Transformed: ${filePath}`);
56
+ return {
57
+ code: result.code,
58
+ loader: LOADER_MAP[ext] ?? "ts"
59
+ };
60
+ }
61
+ /**
62
+ * Close the shared transformer and release resources.
63
+ */
64
+ async function closeTransformer() {
65
+ if (sharedTransformer) {
66
+ await sharedTransformer.close();
67
+ sharedTransformer = null;
68
+ }
69
+ }
70
+
71
+ //#endregion
72
+ //#region src/index.ts
73
+ const TS_FILTER = /\.(ts|tsx|mts|cts)$/;
74
+ /**
75
+ * Create a Bun plugin for Typical transformation.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * // For Bun.build()
80
+ * import typicalPlugin from '@elliots/bun-plugin-typical'
81
+ *
82
+ * await Bun.build({
83
+ * entrypoints: ['./src/index.ts'],
84
+ * outdir: './dist',
85
+ * plugins: [typicalPlugin()],
86
+ * })
87
+ * ```
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * // For runtime (bunfig.toml preload)
92
+ * import typicalPlugin from '@elliots/bun-plugin-typical'
93
+ *
94
+ * Bun.plugin(typicalPlugin())
95
+ * ```
96
+ */
97
+ function typicalPlugin(rawOptions = {}) {
98
+ const options = resolveOptions(rawOptions);
99
+ const typicalConfig = {
100
+ ...loadConfig(),
101
+ ...options.typical
102
+ };
103
+ return {
104
+ name: "bun-plugin-typical",
105
+ target: options.target,
106
+ setup(build) {
107
+ buildTimer.reset();
108
+ build.onLoad({ filter: TS_FILTER }, async (args) => {
109
+ if (options.exclude.some((pattern) => typeof pattern === "string" ? args.path.includes(pattern) : pattern.test(args.path))) return;
110
+ if (options.include.length > 0 && !options.include.some((pattern) => typeof pattern === "string" ? args.path.includes(pattern) : pattern.test(args.path))) return;
111
+ const result = await transformFile(args.path, typicalConfig);
112
+ if (!result) return;
113
+ if (process.env.DEBUG) buildTimer.report("[bun-plugin-typical]");
114
+ return {
115
+ contents: result.code,
116
+ loader: result.loader
117
+ };
118
+ });
119
+ }
120
+ };
121
+ }
122
+ var src_default = typicalPlugin;
123
+
124
+ //#endregion
125
+ export { typicalPlugin as n, closeTransformer as r, src_default as t };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@elliots/bun-plugin-typical",
3
+ "version": "0.2.0-beta.1",
4
+ "description": "Bun plugin for typical - runtime safe TypeScript transformer",
5
+ "keywords": [
6
+ "bun",
7
+ "runtime",
8
+ "typescript",
9
+ "typical",
10
+ "validation"
11
+ ],
12
+ "homepage": "https://github.com/elliots/typical#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/elliots/typical/issues"
15
+ },
16
+ "license": "MIT",
17
+ "author": "Elliot Shepherd <elliot@jarofworms.com>",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/elliots/typical.git",
21
+ "directory": "packages/bun-plugin"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "main": "./dist/index.mjs",
28
+ "module": "./dist/index.mjs",
29
+ "types": "./dist/index.d.mts",
30
+ "exports": {
31
+ ".": "./dist/index.mjs",
32
+ "./preload": "./dist/preload.mjs",
33
+ "./package.json": "./package.json"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "dependencies": {
39
+ "@elliots/typical": "0.2.0-beta.1"
40
+ },
41
+ "devDependencies": {
42
+ "@types/bun": "^1.0.0",
43
+ "@types/node": "^22.0.0",
44
+ "tsdown": "^0.18.3"
45
+ },
46
+ "peerDependencies": {
47
+ "typescript": "^5.0.0"
48
+ },
49
+ "scripts": {
50
+ "build": "tsdown",
51
+ "dev": "tsdown --watch",
52
+ "typecheck": "tsc --noEmit"
53
+ }
54
+ }