From 53c2c66682ba48e4ec8d009a48019a77c36ff4f8 Mon Sep 17 00:00:00 2001 From: sdoeringNew Date: Wed, 2 Mar 2022 08:37:54 +0100 Subject: [PATCH] #8881: add ESM to typescript-fetch (#11720) In case of "supportsES6" an additional tsconfig.esm.json is created. That will add an ECMAScript module to the npm build. --- .../TypeScriptFetchClientCodegen.java | 4 +++ .../typescript-fetch/package.mustache | 9 ++++-- .../typescript-fetch/tsconfig.esm.mustache | 7 +++++ .../TypeScriptFetchClientCodegenTest.java | 31 +++++++++++++++++++ .../es6-target/.openapi-generator/FILES | 1 + .../builds/es6-target/package.json | 4 ++- .../builds/es6-target/tsconfig.esm.json | 7 +++++ .../.openapi-generator/FILES | 1 + .../builds/sagas-and-records/package.json | 5 ++- .../sagas-and-records/tsconfig.esm.json | 7 +++++ 10 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/typescript-fetch/tsconfig.esm.mustache create mode 100644 samples/client/petstore/typescript-fetch/builds/es6-target/tsconfig.esm.json create mode 100644 samples/client/petstore/typescript-fetch/builds/sagas-and-records/tsconfig.esm.json diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java index 0d47cdcd7ac6..ccb273a6025b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java @@ -405,6 +405,10 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); supportingFiles.add(new SupportingFile("package.mustache", "", "package.json")); supportingFiles.add(new SupportingFile("tsconfig.mustache", "", "tsconfig.json")); + // in case ECMAScript 6 is supported add another tsconfig for an ESM (ECMAScript Module) + if (supportsES6) { + supportingFiles.add(new SupportingFile("tsconfig.esm.mustache", "", "tsconfig.esm.json")); + } supportingFiles.add(new SupportingFile("npmignore.mustache", "", ".npmignore")); supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore")); } diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/package.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/package.mustache index c082bbf39828..ddb7f7e5aa86 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/package.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/package.mustache @@ -9,10 +9,13 @@ {{^packageAsSourceOnlyLibrary}} "main": "./dist/index.js", "typings": "./dist/index.d.ts", +{{#supportsES6}} + "module": "./dist/esm/index.js", + "sideEffects": false, +{{/supportsES6}} "scripts": { - "build": "tsc"{{^sagasAndRecords}}, - "prepare": "npm run build" -{{/sagasAndRecords}} + "build": "tsc{{#supportsES6}} && tsc -p tsconfig.esm.json{{/supportsES6}}"{{^sagasAndRecords}}, + "prepare": "npm run build"{{/sagasAndRecords}} }, {{/packageAsSourceOnlyLibrary}} "devDependencies": { diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/tsconfig.esm.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/tsconfig.esm.mustache new file mode 100644 index 000000000000..2c0331cce040 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/tsconfig.esm.mustache @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist/esm" + } +} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java index f6b0678a4147..379c2865a4ea 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java @@ -3,12 +3,14 @@ package org.openapitools.codegen.typescript.fetch; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.media.*; import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.SupportingFile; import org.openapitools.codegen.TestUtils; import org.openapitools.codegen.languages.TypeScriptFetchClientCodegen; import org.openapitools.codegen.utils.ModelUtils; import org.testng.Assert; import org.testng.annotations.Test; +import static org.assertj.core.api.Assertions.assertThat; public class TypeScriptFetchClientCodegenTest { @Test @@ -102,4 +104,33 @@ public class TypeScriptFetchClientCodegenTest { Assert.assertEquals(codegen.getTypeDeclaration(parentSchema), "{ [key: string]: Child; }"); } + @Test + public void containsESMTSConfigFileInCaseOfES6AndNPM() { + TypeScriptFetchClientCodegen codegen = new TypeScriptFetchClientCodegen(); + + codegen.additionalProperties().put("npmName", "@openapi/typescript-fetch-petstore"); + codegen.additionalProperties().put("snapshot", false); + codegen.additionalProperties().put("npmVersion", "1.0.0-SNAPSHOT"); + codegen.setSupportsES6(true); + + codegen.processOpts(); + + assertThat(codegen.supportingFiles()).contains(new SupportingFile("tsconfig.mustache", "", "tsconfig.json")); + assertThat(codegen.supportingFiles()).contains(new SupportingFile("tsconfig.esm.mustache", "", "tsconfig.esm.json")); + } + + @Test + public void doesNotContainESMTSConfigFileInCaseOfES5AndNPM() { + TypeScriptFetchClientCodegen codegen = new TypeScriptFetchClientCodegen(); + + codegen.additionalProperties().put("npmName", "@openapi/typescript-fetch-petstore"); + codegen.additionalProperties().put("snapshot", false); + codegen.additionalProperties().put("npmVersion", "1.0.0-SNAPSHOT"); + codegen.setSupportsES6(false); + + codegen.processOpts(); + + assertThat(codegen.supportingFiles()).contains(new SupportingFile("tsconfig.mustache", "", "tsconfig.json")); + assertThat(codegen.supportingFiles()).doesNotContain(new SupportingFile("tsconfig.esm.mustache", "", "tsconfig.esm.json")); + } } diff --git a/samples/client/petstore/typescript-fetch/builds/es6-target/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/es6-target/.openapi-generator/FILES index 38feffe8896a..cfa1534a047b 100644 --- a/samples/client/petstore/typescript-fetch/builds/es6-target/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-fetch/builds/es6-target/.openapi-generator/FILES @@ -15,4 +15,5 @@ src/models/Tag.ts src/models/User.ts src/models/index.ts src/runtime.ts +tsconfig.esm.json tsconfig.json diff --git a/samples/client/petstore/typescript-fetch/builds/es6-target/package.json b/samples/client/petstore/typescript-fetch/builds/es6-target/package.json index 478c94e3f2e2..65198767ad5c 100644 --- a/samples/client/petstore/typescript-fetch/builds/es6-target/package.json +++ b/samples/client/petstore/typescript-fetch/builds/es6-target/package.json @@ -5,8 +5,10 @@ "author": "OpenAPI-Generator", "main": "./dist/index.js", "typings": "./dist/index.d.ts", + "module": "./dist/esm/index.js", + "sideEffects": false, "scripts": { - "build": "tsc", + "build": "tsc && tsc -p tsconfig.esm.json", "prepare": "npm run build" }, "devDependencies": { diff --git a/samples/client/petstore/typescript-fetch/builds/es6-target/tsconfig.esm.json b/samples/client/petstore/typescript-fetch/builds/es6-target/tsconfig.esm.json new file mode 100644 index 000000000000..2c0331cce040 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/es6-target/tsconfig.esm.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist/esm" + } +} diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/.openapi-generator/FILES index e0b52bf6e972..c7d07ac052f6 100644 --- a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/.openapi-generator/FILES @@ -70,4 +70,5 @@ src/models/WarningCodeRecord.ts src/models/index.ts src/runtime.ts src/runtimeSagasAndRecords.ts +tsconfig.esm.json tsconfig.json diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/package.json b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/package.json index 3b5c84b1eeed..d12b372f051b 100644 --- a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/package.json +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/package.json @@ -5,8 +5,11 @@ "author": "OpenAPI-Generator", "main": "./dist/index.js", "typings": "./dist/index.d.ts", + "module": "./dist/esm/index.js", + "sideEffects": false, "scripts": { - "build": "tsc" }, + "build": "tsc && tsc -p tsconfig.esm.json" + }, "devDependencies": { "immutable": "^4.0.0-rc.12", "normalizr": "^3.6.1", diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/tsconfig.esm.json b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/tsconfig.esm.json new file mode 100644 index 000000000000..2c0331cce040 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/tsconfig.esm.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "esnext", + "outDir": "dist/esm" + } +}