@elliots/bun-plugin-typical 0.2.4 → 0.3.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/dist/index.d.mts +36 -101
- package/dist/index.mjs +121 -1
- package/package.json +2 -2
- package/dist/preload.d.mts +0 -1
- package/dist/preload.mjs +0 -34
- package/dist/src-CBBPzozy.mjs +0 -125
package/dist/index.d.mts
CHANGED
|
@@ -1,123 +1,58 @@
|
|
|
1
|
+
import { TypicalConfig } from "@elliots/typical";
|
|
1
2
|
import { BunPlugin } from "bun";
|
|
2
3
|
|
|
3
|
-
//#region
|
|
4
|
-
interface
|
|
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
|
-
validateCasts?: boolean;
|
|
28
|
-
hoistRegex?: boolean;
|
|
29
|
-
debug?: TypicalDebugConfig;
|
|
30
|
-
/**
|
|
31
|
-
* Type patterns to skip validation for (supports wildcards).
|
|
32
|
-
* Use this for types that typia cannot process (e.g., React event types).
|
|
33
|
-
* Example: ["React.*", "Express.Request", "*.Event"]
|
|
34
|
-
*/
|
|
35
|
-
ignoreTypes?: string[];
|
|
36
|
-
/**
|
|
37
|
-
* Validate function parameters and return types at runtime.
|
|
38
|
-
* When enabled, typed function parameters get runtime validation calls injected.
|
|
39
|
-
* Default: true
|
|
40
|
-
*/
|
|
41
|
-
validateFunctions?: boolean;
|
|
4
|
+
//#region src/core/options.d.ts
|
|
5
|
+
interface Options {
|
|
42
6
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* Default: true
|
|
7
|
+
* Bun plugin target.
|
|
8
|
+
* @default 'bun'
|
|
46
9
|
*/
|
|
47
|
-
|
|
10
|
+
target?: "bun" | "browser" | "node";
|
|
48
11
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* Default: true
|
|
12
|
+
* Patterns to include for transformation.
|
|
13
|
+
* If empty, all TypeScript files are included.
|
|
52
14
|
*/
|
|
53
|
-
|
|
15
|
+
include?: (string | RegExp)[];
|
|
54
16
|
/**
|
|
55
|
-
*
|
|
56
|
-
* Controls whether and how source maps are generated for transformed code.
|
|
17
|
+
* Patterns to exclude from transformation.
|
|
57
18
|
*/
|
|
58
|
-
|
|
19
|
+
exclude?: (string | RegExp)[];
|
|
59
20
|
/**
|
|
60
|
-
*
|
|
61
|
-
* for a single type before erroring. Complex DOM types or library types can
|
|
62
|
-
* generate hundreds of functions which indicates a type that should be excluded.
|
|
63
|
-
* Set to 0 to disable the limit.
|
|
64
|
-
* Default: 50
|
|
21
|
+
* Typical configuration overrides.
|
|
65
22
|
*/
|
|
66
|
-
maxGeneratedFunctions?: number;
|
|
67
|
-
}
|
|
68
|
-
//#endregion
|
|
69
|
-
//#region src/core/options.d.ts
|
|
70
|
-
type FilterPattern = string | RegExp | (string | RegExp)[];
|
|
71
|
-
interface Options {
|
|
72
|
-
/**
|
|
73
|
-
* Files to include. Defaults to all .ts/.tsx/.mts/.cts files.
|
|
74
|
-
*/
|
|
75
|
-
include?: FilterPattern;
|
|
76
|
-
/**
|
|
77
|
-
* Files to exclude. Defaults to node_modules.
|
|
78
|
-
*/
|
|
79
|
-
exclude?: FilterPattern;
|
|
80
|
-
/**
|
|
81
|
-
* Bun target environment.
|
|
82
|
-
* @default 'bun'
|
|
83
|
-
*/
|
|
84
|
-
target?: "bun" | "browser" | "node";
|
|
85
|
-
/**
|
|
86
|
-
* Typical configuration overrides.
|
|
87
|
-
*/
|
|
88
23
|
typical?: Partial<TypicalConfig>;
|
|
89
24
|
}
|
|
90
25
|
//#endregion
|
|
91
26
|
//#region src/core/transform.d.ts
|
|
92
27
|
/**
|
|
93
|
-
* Close the shared transformer and release resources.
|
|
94
|
-
*/
|
|
28
|
+
* Close the shared transformer and release resources.
|
|
29
|
+
*/
|
|
95
30
|
declare function closeTransformer(): Promise<void>;
|
|
96
31
|
//#endregion
|
|
97
32
|
//#region src/index.d.ts
|
|
98
33
|
/**
|
|
99
|
-
* Create a Bun plugin for Typical transformation.
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* ```ts
|
|
103
|
-
* // For Bun.build()
|
|
104
|
-
* import typicalPlugin from '@elliots/bun-plugin-typical'
|
|
105
|
-
*
|
|
106
|
-
* await Bun.build({
|
|
107
|
-
* entrypoints: ['./src/index.ts'],
|
|
108
|
-
* outdir: './dist',
|
|
109
|
-
* plugins: [typicalPlugin()],
|
|
110
|
-
* })
|
|
111
|
-
* ```
|
|
112
|
-
*
|
|
113
|
-
* @example
|
|
114
|
-
* ```ts
|
|
115
|
-
* // For runtime (bunfig.toml preload)
|
|
116
|
-
* import typicalPlugin from '@elliots/bun-plugin-typical'
|
|
117
|
-
*
|
|
118
|
-
* Bun.plugin(typicalPlugin())
|
|
119
|
-
* ```
|
|
120
|
-
*/
|
|
34
|
+
* Create a Bun plugin for Typical transformation.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* // For Bun.build()
|
|
39
|
+
* import typicalPlugin from '@elliots/bun-plugin-typical'
|
|
40
|
+
*
|
|
41
|
+
* await Bun.build({
|
|
42
|
+
* entrypoints: ['./src/index.ts'],
|
|
43
|
+
* outdir: './dist',
|
|
44
|
+
* plugins: [typicalPlugin()],
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* // For runtime (bunfig.toml preload)
|
|
51
|
+
* import typicalPlugin from '@elliots/bun-plugin-typical'
|
|
52
|
+
*
|
|
53
|
+
* Bun.plugin(typicalPlugin())
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
121
56
|
declare function typicalPlugin(rawOptions?: Options): BunPlugin;
|
|
122
57
|
//#endregion
|
|
123
58
|
export { type Options, closeTransformer, typicalPlugin as default, typicalPlugin };
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,123 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TypicalTransformer, buildTimer, loadConfig, validateConfig } from "@elliots/typical";
|
|
2
|
+
import { extname, resolve } from "path";
|
|
2
3
|
|
|
4
|
+
//#region src/core/options.ts
|
|
5
|
+
/**
|
|
6
|
+
* Resolve plugin options with defaults.
|
|
7
|
+
*/
|
|
8
|
+
function resolveOptions(options = {}) {
|
|
9
|
+
return {
|
|
10
|
+
target: options.target ?? "bun",
|
|
11
|
+
include: options.include ?? [],
|
|
12
|
+
exclude: options.exclude ?? [/node_modules/],
|
|
13
|
+
typical: options.typical ?? {}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/core/transform.ts
|
|
19
|
+
const TRANSFORM_EXTENSIONS = new Set([
|
|
20
|
+
".ts",
|
|
21
|
+
".tsx",
|
|
22
|
+
".mts",
|
|
23
|
+
".cts"
|
|
24
|
+
]);
|
|
25
|
+
const LOADER_MAP = {
|
|
26
|
+
".ts": "ts",
|
|
27
|
+
".mts": "ts",
|
|
28
|
+
".cts": "ts",
|
|
29
|
+
".tsx": "tsx"
|
|
30
|
+
};
|
|
31
|
+
let sharedTransformer = null;
|
|
32
|
+
/**
|
|
33
|
+
* Transform a TypeScript file with Typical.
|
|
34
|
+
*
|
|
35
|
+
* Returns TypeScript code with validation injected.
|
|
36
|
+
* Bun handles the final transpilation to JavaScript.
|
|
37
|
+
*/
|
|
38
|
+
async function transformFile(filePath, config) {
|
|
39
|
+
buildTimer.start("total-transform");
|
|
40
|
+
const ext = extname(filePath).toLowerCase();
|
|
41
|
+
if (!TRANSFORM_EXTENSIONS.has(ext)) {
|
|
42
|
+
buildTimer.end("total-transform");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const resolvedPath = resolve(filePath);
|
|
46
|
+
buildTimer.start("create-transformer");
|
|
47
|
+
if (!sharedTransformer) sharedTransformer = new TypicalTransformer(validateConfig(config));
|
|
48
|
+
buildTimer.end("create-transformer");
|
|
49
|
+
buildTimer.start("transform");
|
|
50
|
+
const result = await sharedTransformer.transform(resolvedPath, "ts");
|
|
51
|
+
buildTimer.end("transform");
|
|
52
|
+
buildTimer.end("total-transform");
|
|
53
|
+
if (process.env.DEBUG) console.log(`[bun-plugin-typical] Transformed: ${filePath}`);
|
|
54
|
+
return {
|
|
55
|
+
code: result.code,
|
|
56
|
+
loader: LOADER_MAP[ext] ?? "ts"
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Close the shared transformer and release resources.
|
|
61
|
+
*/
|
|
62
|
+
async function closeTransformer() {
|
|
63
|
+
if (sharedTransformer) {
|
|
64
|
+
await sharedTransformer.close();
|
|
65
|
+
sharedTransformer = null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/index.ts
|
|
71
|
+
const TS_FILTER = /\.(ts|tsx|mts|cts)$/;
|
|
72
|
+
/**
|
|
73
|
+
* Create a Bun plugin for Typical transformation.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* // For Bun.build()
|
|
78
|
+
* import typicalPlugin from '@elliots/bun-plugin-typical'
|
|
79
|
+
*
|
|
80
|
+
* await Bun.build({
|
|
81
|
+
* entrypoints: ['./src/index.ts'],
|
|
82
|
+
* outdir: './dist',
|
|
83
|
+
* plugins: [typicalPlugin()],
|
|
84
|
+
* })
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* // For runtime (bunfig.toml preload)
|
|
90
|
+
* import typicalPlugin from '@elliots/bun-plugin-typical'
|
|
91
|
+
*
|
|
92
|
+
* Bun.plugin(typicalPlugin())
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
function typicalPlugin(rawOptions = {}) {
|
|
96
|
+
const options = resolveOptions(rawOptions);
|
|
97
|
+
const typicalConfig = {
|
|
98
|
+
...loadConfig(),
|
|
99
|
+
...options.typical
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
name: "bun-plugin-typical",
|
|
103
|
+
target: options.target,
|
|
104
|
+
setup(build) {
|
|
105
|
+
buildTimer.reset();
|
|
106
|
+
build.onLoad({ filter: TS_FILTER }, async (args) => {
|
|
107
|
+
if (options.exclude.some((pattern) => typeof pattern === "string" ? args.path.includes(pattern) : pattern.test(args.path))) return;
|
|
108
|
+
if (options.include.length > 0 && !options.include.some((pattern) => typeof pattern === "string" ? args.path.includes(pattern) : pattern.test(args.path))) return;
|
|
109
|
+
const result = await transformFile(args.path, typicalConfig);
|
|
110
|
+
if (!result) return;
|
|
111
|
+
if (process.env.DEBUG) buildTimer.report("[bun-plugin-typical]");
|
|
112
|
+
return {
|
|
113
|
+
contents: result.code,
|
|
114
|
+
loader: result.loader
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
var src_default = typicalPlugin;
|
|
121
|
+
|
|
122
|
+
//#endregion
|
|
3
123
|
export { closeTransformer, src_default as default, typicalPlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elliots/bun-plugin-typical",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Bun plugin for typical - runtime safe TypeScript transformer",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bun",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@elliots/typical": "0.
|
|
39
|
+
"@elliots/typical": "0.3.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/bun": "^1.0.0",
|
package/dist/preload.d.mts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/preload.mjs
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
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
|
-
* validateCasts: true,
|
|
27
|
-
* }
|
|
28
|
-
* }))
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
Bun.plugin(typicalPlugin());
|
|
32
|
-
|
|
33
|
-
//#endregion
|
|
34
|
-
export { };
|
package/dist/src-CBBPzozy.mjs
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
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 };
|