#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.
This commit is contained in:
sdoeringNew
2022-03-02 08:37:54 +01:00
committed by GitHub
parent 6a7fc38fb9
commit 53c2c66682
10 changed files with 71 additions and 5 deletions

View File

@@ -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"));
}

View File

@@ -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": {

View File

@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/esm"
}
}

View File

@@ -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"));
}
}

View File

@@ -15,4 +15,5 @@ src/models/Tag.ts
src/models/User.ts
src/models/index.ts
src/runtime.ts
tsconfig.esm.json
tsconfig.json

View File

@@ -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": {

View File

@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/esm"
}
}

View File

@@ -70,4 +70,5 @@ src/models/WarningCodeRecord.ts
src/models/index.ts
src/runtime.ts
src/runtimeSagasAndRecords.ts
tsconfig.esm.json
tsconfig.json

View File

@@ -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",

View File

@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/esm"
}
}