|
1 | | -import { ok } from "assert"; |
2 | | -import { describe, it } from "vitest"; |
| 1 | +import { mkdtemp, rm, writeFile } from "fs/promises"; |
| 2 | +import { tmpdir } from "os"; |
| 3 | +import { join } from "path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { createTypeSpecBundle } from "../src/bundler.js"; |
3 | 6 |
|
4 | 7 | describe("bundler", () => { |
5 | | - it("works", () => { |
6 | | - ok(true); |
| 8 | + /** |
| 9 | + * Regression test: TypeSpec decorator functions are identified by their `.name` property |
| 10 | + * at runtime (e.g., `d.decorator.name === "$armResourceOperations"`). |
| 11 | + * When esbuild minifies library bundles, it can rename functions, changing their `.name`. |
| 12 | + * The bundler must use `keepNames: true` to preserve function names in minified output. |
| 13 | + */ |
| 14 | + it("preserves function names when minifying", async () => { |
| 15 | + const tmpDir = await mkdtemp(join(tmpdir(), "typespec-bundler-test-")); |
| 16 | + try { |
| 17 | + // Create a minimal TypeSpec library with named decorator function exports |
| 18 | + await writeFile( |
| 19 | + join(tmpDir, "package.json"), |
| 20 | + JSON.stringify({ |
| 21 | + name: "test-lib", |
| 22 | + version: "1.0.0", |
| 23 | + main: "index.js", |
| 24 | + tspMain: "main.tsp", |
| 25 | + peerDependencies: {}, |
| 26 | + }), |
| 27 | + ); |
| 28 | + await writeFile( |
| 29 | + join(tmpDir, "main.tsp"), |
| 30 | + ['import "./index.js";', "namespace TestLib;"].join("\n"), |
| 31 | + ); |
| 32 | + await writeFile( |
| 33 | + join(tmpDir, "index.js"), |
| 34 | + [ |
| 35 | + "export function $testDecorator(context, target) { }", |
| 36 | + "export function $anotherDecorator(context, target) { }", |
| 37 | + ].join("\n"), |
| 38 | + ); |
| 39 | + |
| 40 | + const bundle = await createTypeSpecBundle(tmpDir, { minify: true }); |
| 41 | + const indexFile = bundle.files.find((f) => f.filename === "index.js"); |
| 42 | + expect(indexFile, "index.js should be in bundle output").toBeDefined(); |
| 43 | + |
| 44 | + // The bundle's jsSourceFiles should contain modules where the function |
| 45 | + // .name property is preserved. With keepNames, esbuild emits a helper |
| 46 | + // that restores the original name via Object.defineProperty. |
| 47 | + // We verify the original function names appear as string literals in the output. |
| 48 | + expect(indexFile!.content).toContain('"$testDecorator"'); |
| 49 | + expect(indexFile!.content).toContain('"$anotherDecorator"'); |
| 50 | + } finally { |
| 51 | + await rm(tmpDir, { recursive: true }); |
| 52 | + } |
7 | 53 | }); |
8 | 54 | }); |
0 commit comments