From 02ff167da22b2d4945f80e0afe0a12a21e25daba Mon Sep 17 00:00:00 2001 From: liangskyli <640634387@qq.com> Date: Fri, 15 Dec 2023 09:13:28 +0800 Subject: [PATCH 1/2] fix(openapi-typescript): build cjs support astToString export --- packages/openapi-typescript/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-typescript/package.json b/packages/openapi-typescript/package.json index 056ff0c55..ac135755e 100644 --- a/packages/openapi-typescript/package.json +++ b/packages/openapi-typescript/package.json @@ -43,7 +43,7 @@ "build": "run-s -s build:*", "build:clean": "del dist", "build:esm": "tsc -p tsconfig.build.json", - "build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:@redocly/ajv --external:@redocly/openapi-core --external:typescript src/index.ts --footer:js=\"module.exports = module.exports.default;\"", + "build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:@redocly/ajv --external:@redocly/openapi-core --external:typescript src/index.ts", "dev": "tsc -p tsconfig.build.json --watch", "download:schemas": "vite-node ./scripts/download-schemas.ts", "format": "prettier --write \"src/**/*\"", From 41dd3cdd69dc6763feb32388f475f327e3fe0603 Mon Sep 17 00:00:00 2001 From: liangskyli <640634387@qq.com> Date: Fri, 15 Dec 2023 09:23:18 +0800 Subject: [PATCH 2/2] test(openapi-typescript): modify unit test --- packages/openapi-typescript/test/cjs.test.js | 23 +------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/packages/openapi-typescript/test/cjs.test.js b/packages/openapi-typescript/test/cjs.test.js index bba6a427a..992e287a6 100644 --- a/packages/openapi-typescript/test/cjs.test.js +++ b/packages/openapi-typescript/test/cjs.test.js @@ -2,28 +2,7 @@ // important: MUST use require()! const { fileURLToPath } = require("node:url"); -const ts = require("typescript"); -const openapiTS = require("../dist/index.cjs"); - -// copy from lib/ts.ts for CJS -function astToString(ast, options) { - const sourceFile = ts.createSourceFile( - options?.fileName ?? "openapi-ts.ts", - options?.sourceText ?? "", - ts.ScriptTarget.ESNext, - false, - ts.ScriptKind.TS, - ); - sourceFile.statements = ts.factory.createNodeArray( - Array.isArray(ast) ? ast : [ast], - ); - const printer = ts.createPrinter({ - newLine: ts.NewLineKind.LineFeed, - removeComments: false, - ...options?.formatOptions, - }); - return printer.printFile(sourceFile); -} +const { astToString, default: openapiTS } = require('../dist/index.cjs'); describe("CJS bundle", () => { it("basic", async () => {