@elliots/unplugin-typical 0.1.9 → 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.
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./index-cR_y1bPs.mjs";
1
+ import { t as Typical } from "./index-CnuworeF.mjs";
2
2
 
3
3
  //#region src/esbuild.d.ts
4
4
 
package/dist/esbuild.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./src-DfYOX15z.mjs";
1
+ import { t as Typical } from "./src-DKwWaQcb.mjs";
2
2
 
3
3
  //#region src/esbuild.ts
4
4
  /**
package/dist/farm.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./index-cR_y1bPs.mjs";
1
+ import { t as Typical } from "./index-CnuworeF.mjs";
2
2
 
3
3
  //#region src/farm.d.ts
4
4
 
package/dist/farm.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./src-DfYOX15z.mjs";
1
+ import { t as Typical } from "./src-DKwWaQcb.mjs";
2
2
 
3
3
  //#region src/farm.ts
4
4
  /**
@@ -1,10 +1,26 @@
1
1
  import { FilterPattern, UnpluginInstance } from "unplugin";
2
- import ts from "typescript";
3
2
 
4
3
  //#region ../../dist/src/config.d.ts
5
4
  interface TypicalDebugConfig {
6
5
  writeIntermediateFiles?: boolean;
7
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
+ }
8
24
  interface TypicalConfig {
9
25
  include?: string[];
10
26
  exclude?: string[];
@@ -30,6 +46,11 @@ interface TypicalConfig {
30
46
  * Default: true
31
47
  */
32
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;
33
54
  }
34
55
  //#endregion
35
56
  //#region src/core/options.d.ts
package/dist/index.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { n as Options, t as Typical } from "./index-cR_y1bPs.mjs";
1
+ import { n as Options, t as Typical } from "./index-CnuworeF.mjs";
2
2
  export { Options, Typical };
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { t as Typical } from "./src-DfYOX15z.mjs";
1
+ import { t as Typical } from "./src-DKwWaQcb.mjs";
2
2
 
3
3
  export { Typical };
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./index-cR_y1bPs.mjs";
1
+ import { t as Typical } from "./index-CnuworeF.mjs";
2
2
 
3
3
  //#region src/rolldown.d.ts
4
4
 
package/dist/rolldown.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./src-DfYOX15z.mjs";
1
+ import { t as Typical } from "./src-DKwWaQcb.mjs";
2
2
 
3
3
  //#region src/rolldown.ts
4
4
  /**
package/dist/rollup.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./index-cR_y1bPs.mjs";
1
+ import { t as Typical } from "./index-CnuworeF.mjs";
2
2
 
3
3
  //#region src/rollup.d.ts
4
4
 
package/dist/rollup.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./src-DfYOX15z.mjs";
1
+ import { t as Typical } from "./src-DKwWaQcb.mjs";
2
2
 
3
3
  //#region src/rollup.ts
4
4
  /**
package/dist/rspack.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./index-cR_y1bPs.mjs";
1
+ import { t as Typical } from "./index-CnuworeF.mjs";
2
2
 
3
3
  //#region src/rspack.d.ts
4
4
 
package/dist/rspack.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./src-DfYOX15z.mjs";
1
+ import { t as Typical } from "./src-DKwWaQcb.mjs";
2
2
 
3
3
  //#region src/rspack.ts
4
4
  /**
@@ -0,0 +1,95 @@
1
+ import { createUnplugin } from "unplugin";
2
+ import { TypicalTransformer, buildTimer, loadConfig } from "@elliots/typical";
3
+ import { extname, resolve } from "path";
4
+
5
+ //#region src/core/options.ts
6
+ function resolveOptions(options) {
7
+ return {
8
+ include: options.include || [/\.[cm]?[jt]sx?$/],
9
+ exclude: options.exclude || [/node_modules/],
10
+ enforce: "enforce" in options ? options.enforce : "pre",
11
+ typical: options.typical
12
+ };
13
+ }
14
+
15
+ //#endregion
16
+ //#region src/core/transform.ts
17
+ const TRANSFORM_EXTENSIONS = new Set([
18
+ ".ts",
19
+ ".tsx",
20
+ ".mts",
21
+ ".cts"
22
+ ]);
23
+ let transformer = null;
24
+ /**
25
+ * Transform a TypeScript file with Typical.
26
+ *
27
+ * Uses the Go compiler via TypicalCompiler for validation code generation.
28
+ * The Go server stays running and maintains the TypeScript program state.
29
+ */
30
+ async function transformTypia(id, _source, config) {
31
+ buildTimer.start("total-transform");
32
+ const ext = extname(id).toLowerCase();
33
+ if (!TRANSFORM_EXTENSIONS.has(ext)) {
34
+ buildTimer.end("total-transform");
35
+ return;
36
+ }
37
+ const resolvedId = resolve(id);
38
+ if (!transformer) {
39
+ buildTimer.start("init-transformer");
40
+ transformer = new TypicalTransformer(config);
41
+ buildTimer.end("init-transformer");
42
+ }
43
+ buildTimer.start("transform");
44
+ const result = await transformer.transform(resolvedId, "ts");
45
+ buildTimer.end("transform");
46
+ buildTimer.end("total-transform");
47
+ if (process.env.DEBUG) console.log("[unplugin-typical] Transform output (first 1000 chars):", result.code.substring(0, 1e3));
48
+ return {
49
+ code: result.code,
50
+ map: result.map
51
+ };
52
+ }
53
+ /**
54
+ * Close the transformer and release resources.
55
+ * Should be called at build end.
56
+ */
57
+ async function closeTransformer() {
58
+ if (transformer) {
59
+ await transformer.close();
60
+ transformer = null;
61
+ }
62
+ }
63
+
64
+ //#endregion
65
+ //#region src/index.ts
66
+ const Typical = createUnplugin((rawOptions = {}) => {
67
+ const options = resolveOptions(rawOptions);
68
+ const typicalConfig = {
69
+ ...loadConfig(),
70
+ ...options.typical
71
+ };
72
+ return {
73
+ name: "unplugin-typical",
74
+ enforce: options.enforce,
75
+ buildStart() {
76
+ buildTimer.reset();
77
+ },
78
+ async buildEnd() {
79
+ if (process.env.DEBUG) buildTimer.report();
80
+ await closeTransformer();
81
+ },
82
+ transform: {
83
+ filter: { id: {
84
+ include: options.include,
85
+ exclude: options.exclude
86
+ } },
87
+ handler(code, id) {
88
+ return transformTypia(id, code, typicalConfig);
89
+ }
90
+ }
91
+ };
92
+ });
93
+
94
+ //#endregion
95
+ export { Typical as t };
package/dist/vite.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./index-cR_y1bPs.mjs";
1
+ import { t as Typical } from "./index-CnuworeF.mjs";
2
2
 
3
3
  //#region src/vite.d.ts
4
4
 
package/dist/vite.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./src-DfYOX15z.mjs";
1
+ import { t as Typical } from "./src-DKwWaQcb.mjs";
2
2
 
3
3
  //#region src/vite.ts
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./index-cR_y1bPs.mjs";
1
+ import { t as Typical } from "./index-CnuworeF.mjs";
2
2
 
3
3
  //#region src/webpack.d.ts
4
4
 
package/dist/webpack.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as Typical } from "./src-DfYOX15z.mjs";
1
+ import { t as Typical } from "./src-DKwWaQcb.mjs";
2
2
 
3
3
  //#region src/webpack.ts
4
4
  /**
package/package.json CHANGED
@@ -1,44 +1,36 @@
1
1
  {
2
2
  "name": "@elliots/unplugin-typical",
3
- "type": "module",
4
- "version": "0.1.9",
3
+ "version": "0.2.0-beta.1",
5
4
  "description": "Unplugin for typical - runtime safe TypeScript transformer",
6
- "author": "Elliot Shepherd <elliot@jarofworms.com>",
7
- "license": "MIT",
8
- "homepage": "https://github.com/elliots/typical#readme",
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/elliots/typical.git",
12
- "directory": "packages/unplugin"
13
- },
14
- "bugs": {
15
- "url": "https://github.com/elliots/typical/issues"
16
- },
17
5
  "keywords": [
18
- "unplugin",
19
- "vite",
20
- "webpack",
21
- "rspack",
22
- "rollup",
23
- "rolldown",
24
6
  "esbuild",
25
7
  "farm",
26
- "typescript",
8
+ "rolldown",
9
+ "rollup",
10
+ "rspack",
27
11
  "runtime",
12
+ "typescript",
13
+ "typical",
14
+ "unplugin",
28
15
  "validation",
29
- "typical"
16
+ "vite",
17
+ "webpack"
30
18
  ],
31
- "exports": {
32
- ".": "./dist/index.mjs",
33
- "./esbuild": "./dist/esbuild.mjs",
34
- "./farm": "./dist/farm.mjs",
35
- "./rolldown": "./dist/rolldown.mjs",
36
- "./rollup": "./dist/rollup.mjs",
37
- "./rspack": "./dist/rspack.mjs",
38
- "./vite": "./dist/vite.mjs",
39
- "./webpack": "./dist/webpack.mjs",
40
- "./package.json": "./package.json"
19
+ "homepage": "https://github.com/elliots/typical#readme",
20
+ "bugs": {
21
+ "url": "https://github.com/elliots/typical/issues"
22
+ },
23
+ "license": "MIT",
24
+ "author": "Elliot Shepherd <elliot@jarofworms.com>",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/elliots/typical.git",
28
+ "directory": "packages/unplugin"
41
29
  },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "type": "module",
42
34
  "main": "./dist/index.mjs",
43
35
  "module": "./dist/index.mjs",
44
36
  "types": "./dist/index.d.mts",
@@ -50,14 +42,22 @@
50
42
  ]
51
43
  }
52
44
  },
53
- "files": [
54
- "dist"
55
- ],
45
+ "exports": {
46
+ ".": "./dist/index.mjs",
47
+ "./esbuild": "./dist/esbuild.mjs",
48
+ "./farm": "./dist/farm.mjs",
49
+ "./rolldown": "./dist/rolldown.mjs",
50
+ "./rollup": "./dist/rollup.mjs",
51
+ "./rspack": "./dist/rspack.mjs",
52
+ "./vite": "./dist/vite.mjs",
53
+ "./webpack": "./dist/webpack.mjs",
54
+ "./package.json": "./package.json"
55
+ },
56
56
  "publishConfig": {
57
57
  "access": "public"
58
58
  },
59
59
  "dependencies": {
60
- "@elliots/typical": "0.1.9",
60
+ "@elliots/typical": "0.2.0-beta.1",
61
61
  "unplugin": "^2.3.11"
62
62
  },
63
63
  "devDependencies": {
@@ -1,250 +0,0 @@
1
- import { createUnplugin } from "unplugin";
2
- import { TypicalTransformer, loadConfig } from "@elliots/typical";
3
- import { dirname, extname, resolve } from "path";
4
- import ts from "typescript";
5
-
6
- //#region src/core/options.ts
7
- function resolveOptions(options) {
8
- return {
9
- include: options.include || [/\.[cm]?[jt]sx?$/],
10
- exclude: options.exclude || [/node_modules/],
11
- enforce: "enforce" in options ? options.enforce : "pre",
12
- typical: options.typical
13
- };
14
- }
15
-
16
- //#endregion
17
- //#region src/core/timing.ts
18
- /**
19
- * Performance instrumentation for tracking build times
20
- */
21
- var BuildTimer = class {
22
- timings = /* @__PURE__ */ new Map();
23
- starts = /* @__PURE__ */ new Map();
24
- start(stage) {
25
- this.starts.set(stage, performance.now());
26
- }
27
- end(stage) {
28
- const start = this.starts.get(stage);
29
- if (start !== void 0) {
30
- const duration = performance.now() - start;
31
- const existing = this.timings.get(stage) ?? [];
32
- existing.push(duration);
33
- this.timings.set(stage, existing);
34
- this.starts.delete(stage);
35
- }
36
- }
37
- reset() {
38
- this.timings.clear();
39
- this.starts.clear();
40
- }
41
- report() {
42
- console.log("\n[unplugin-typical] Build Performance Report:");
43
- console.log("─".repeat(60));
44
- const sortedStages = Array.from(this.timings.entries()).sort(([, a], [, b]) => b.reduce((x, y) => x + y, 0) - a.reduce((x, y) => x + y, 0));
45
- let totalTime = 0;
46
- for (const [stage, times] of sortedStages) {
47
- const total = times.reduce((a, b) => a + b, 0);
48
- const avg = total / times.length;
49
- const min = Math.min(...times);
50
- const max = Math.max(...times);
51
- const count = times.length;
52
- totalTime += total;
53
- console.log(`${stage}:`);
54
- console.log(` Count: ${count}`);
55
- console.log(` Total: ${total.toFixed(2)}ms`);
56
- console.log(` Avg: ${avg.toFixed(2)}ms`);
57
- console.log(` Min: ${min.toFixed(2)}ms`);
58
- console.log(` Max: ${max.toFixed(2)}ms`);
59
- }
60
- console.log("─".repeat(60));
61
- console.log(`Total transform time: ${totalTime.toFixed(2)}ms`);
62
- console.log("");
63
- }
64
- getTimings() {
65
- const result = /* @__PURE__ */ new Map();
66
- for (const [stage, times] of this.timings) {
67
- const total = times.reduce((a, b) => a + b, 0);
68
- result.set(stage, {
69
- count: times.length,
70
- total,
71
- avg: total / times.length,
72
- min: Math.min(...times),
73
- max: Math.max(...times)
74
- });
75
- }
76
- return result;
77
- }
78
- };
79
- const buildTimer = new BuildTimer();
80
-
81
- //#endregion
82
- //#region src/core/transform.ts
83
- const TRANSFORM_EXTENSIONS = new Set([
84
- ".ts",
85
- ".tsx",
86
- ".mts",
87
- ".cts"
88
- ]);
89
- /**
90
- * Transform a TypeScript file with Typical.
91
- *
92
- * Uses a shared ProgramManager for incremental compilation across files.
93
- */
94
- function transformTypia(id, source, config, programManager) {
95
- buildTimer.start("total-transform");
96
- const ext = extname(id).toLowerCase();
97
- if (!TRANSFORM_EXTENSIONS.has(ext)) {
98
- buildTimer.end("total-transform");
99
- return;
100
- }
101
- const resolvedId = resolve(id);
102
- buildTimer.start("get-program");
103
- const program = programManager.getProgram(resolvedId, source);
104
- buildTimer.end("get-program");
105
- buildTimer.start("get-source-file");
106
- const sourceFile = programManager.getSourceFile(resolvedId);
107
- buildTimer.end("get-source-file");
108
- if (!sourceFile) {
109
- buildTimer.end("total-transform");
110
- console.warn(`[unplugin-typical] Could not get source file for: ${id}`);
111
- return;
112
- }
113
- buildTimer.start("create-transformer");
114
- const transformer = new TypicalTransformer(config, program);
115
- buildTimer.end("create-transformer");
116
- buildTimer.start("transform");
117
- const result = transformer.transform(sourceFile, "js");
118
- buildTimer.end("transform");
119
- buildTimer.end("total-transform");
120
- if (process.env.DEBUG) console.log("[unplugin-typical] Transform output (first 1000 chars):", result.substring(0, 1e3));
121
- return result;
122
- }
123
-
124
- //#endregion
125
- //#region src/core/program-manager.ts
126
- /**
127
- * Manages a shared TypeScript program across file transformations.
128
- * This avoids the expensive cost of creating a new program for each file.
129
- */
130
- var ProgramManager = class {
131
- program;
132
- compilerOptions;
133
- sourceContents = /* @__PURE__ */ new Map();
134
- sourceFileCache = /* @__PURE__ */ new Map();
135
- host;
136
- /**
137
- * Get or create a program with the given source content for a file.
138
- * Uses incremental compilation to reuse data from previous program.
139
- */
140
- getProgram(id, source) {
141
- const resolvedId = resolve(id);
142
- this.sourceContents.set(resolvedId, source);
143
- this.sourceFileCache.delete(resolvedId);
144
- if (!this.compilerOptions) {
145
- buildTimer.start("load-compiler-options");
146
- this.compilerOptions = this.loadCompilerOptions();
147
- buildTimer.end("load-compiler-options");
148
- }
149
- if (!this.host) this.host = this.createHost();
150
- const rootFiles = this.program?.getRootFileNames() ?? [];
151
- const rootFileSet = new Set(rootFiles);
152
- if (!rootFileSet.has(resolvedId)) rootFileSet.add(resolvedId);
153
- buildTimer.start("create-program-incremental");
154
- this.program = ts.createProgram(Array.from(rootFileSet), this.compilerOptions, this.host, this.program);
155
- buildTimer.end("create-program-incremental");
156
- return this.program;
157
- }
158
- /**
159
- * Get the source file for a given ID from the current program.
160
- */
161
- getSourceFile(id) {
162
- const resolvedId = resolve(id);
163
- return this.program?.getSourceFile(resolvedId);
164
- }
165
- /**
166
- * Reset the program manager state (e.g., at build start).
167
- */
168
- reset() {
169
- this.program = void 0;
170
- this.sourceContents.clear();
171
- }
172
- loadCompilerOptions() {
173
- const configPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists, "tsconfig.json");
174
- if (!configPath) return {
175
- target: ts.ScriptTarget.ES2020,
176
- module: ts.ModuleKind.ESNext,
177
- moduleResolution: ts.ModuleResolutionKind.Bundler,
178
- esModuleInterop: true,
179
- strict: true
180
- };
181
- const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
182
- if (configFile.error) throw new Error(ts.flattenDiagnosticMessageText(configFile.error.messageText, "\n"));
183
- return ts.parseJsonConfigFileContent(configFile.config, ts.sys, dirname(configPath)).options;
184
- }
185
- createHost() {
186
- const baseHost = ts.createCompilerHost(this.compilerOptions);
187
- const originalGetSourceFile = baseHost.getSourceFile.bind(baseHost);
188
- return {
189
- ...baseHost,
190
- getSourceFile: (fileName, languageVersion, onError, shouldCreateNewSourceFile) => {
191
- const resolvedFileName = resolve(fileName);
192
- const virtualContent = this.sourceContents.get(resolvedFileName);
193
- if (virtualContent !== void 0) {
194
- const cached = this.sourceFileCache.get(resolvedFileName);
195
- if (cached && cached.text === virtualContent) return cached;
196
- const sourceFile = ts.createSourceFile(resolvedFileName, virtualContent, languageVersion, true);
197
- this.sourceFileCache.set(resolvedFileName, sourceFile);
198
- return sourceFile;
199
- }
200
- const cachedDisk = this.sourceFileCache.get(resolvedFileName);
201
- if (cachedDisk) return cachedDisk;
202
- const result = originalGetSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile);
203
- if (result) this.sourceFileCache.set(resolvedFileName, result);
204
- return result;
205
- },
206
- fileExists: (fileName) => {
207
- const resolvedFileName = resolve(fileName);
208
- return this.sourceContents.has(resolvedFileName) || baseHost.fileExists(fileName);
209
- },
210
- readFile: (fileName) => {
211
- const resolvedFileName = resolve(fileName);
212
- return this.sourceContents.get(resolvedFileName) ?? baseHost.readFile(fileName);
213
- }
214
- };
215
- }
216
- };
217
-
218
- //#endregion
219
- //#region src/index.ts
220
- const Typical = createUnplugin((rawOptions = {}) => {
221
- const options = resolveOptions(rawOptions);
222
- const typicalConfig = {
223
- ...loadConfig(),
224
- ...options.typical
225
- };
226
- const programManager = new ProgramManager();
227
- return {
228
- name: "unplugin-typical",
229
- enforce: options.enforce,
230
- buildStart() {
231
- buildTimer.reset();
232
- programManager.reset();
233
- },
234
- buildEnd() {
235
- if (process.env.DEBUG) buildTimer.report();
236
- },
237
- transform: {
238
- filter: { id: {
239
- include: options.include,
240
- exclude: options.exclude
241
- } },
242
- handler(code, id) {
243
- return transformTypia(id, code, typicalConfig, programManager);
244
- }
245
- }
246
- };
247
- });
248
-
249
- //#endregion
250
- export { Typical as t };