From 08f3294a1d0ffef990b3986841ee0af1e0e12e45 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 23 Aug 2021 11:11:18 +0200 Subject: [PATCH] web: add ESM to generated Client https://github.com/OpenAPITools/openapi-generator/issues/8881 Signed-off-by: Jens Langhammer --- Makefile | 1 + scripts/web_api_esm.py | 29 +++++++++++++++++++++++++++++ web/package-lock.json | 14 +++++++------- web/package.json | 2 +- web/rollup.config.js | 16 +++++----------- 5 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 scripts/web_api_esm.py diff --git a/Makefile b/Makefile index 11101ef88..316d67e18 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,7 @@ gen-web: -o /local/web-api \ --additional-properties=typescriptThreePlus=true,supportsES6=true,npmName=@goauthentik/api,npmVersion=${NPM_VERSION} mkdir -p web/node_modules/@goauthentik/api + python -m scripts.web_api_esm cd web-api && npm i \cp -rfv web-api/* web/node_modules/@goauthentik/api diff --git a/scripts/web_api_esm.py b/scripts/web_api_esm.py new file mode 100644 index 000000000..d9ad1acd6 --- /dev/null +++ b/scripts/web_api_esm.py @@ -0,0 +1,29 @@ +"""Enable ESM Modules for generated Web API""" +from json import loads, dumps + +TSCONFIG_ESM = { + "compilerOptions": { + "declaration": True, + "target": "es6", + "module": "esnext", + "moduleResolution": "node", + "outDir": "./dist/esm", + "typeRoots": [ + "node_modules/@types" + ] + }, + "exclude": [ + "dist", + "node_modules" + ] +} + + +with open("web-api/package.json", encoding="utf-8") as _package: + package = loads(_package.read()) + package["module"] = "./dist/esm/index.js" + package["sideEffects"] = False + package["scripts"]["build"] = "tsc && tsc --project tsconfig.esm.json" + +open("web-api/package.json", "w+", encoding="utf-8").write(dumps(package)) +open("web-api/tsconfig.esm.json", "w+", encoding="utf-8").write(dumps(TSCONFIG_ESM)) diff --git a/web/package-lock.json b/web/package-lock.json index f167d8b0a..fb0382c0c 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -15,7 +15,7 @@ "@babel/preset-env": "^7.15.0", "@babel/preset-typescript": "^7.15.0", "@fortawesome/fontawesome-free": "^5.15.4", - "@goauthentik/api": "^1629704636.0.0", + "@goauthentik/api": "^2021.8.1-rc1-1629709535", "@lingui/cli": "^3.10.2", "@lingui/core": "^3.10.4", "@lingui/macro": "^3.10.2", @@ -1690,9 +1690,9 @@ } }, "node_modules/@goauthentik/api": { - "version": "1629704636.0.0", - "resolved": "https://registry.npmjs.org/@goauthentik/api/-/api-1629704636.0.0.tgz", - "integrity": "sha512-rjUjp7Gbf/Tzw0hGPx2ig5InXa4bPqj4FkFnIEgR6T1w6jbHV+q2BghztVFDc2hK2ib2NnQUQp6LWIAcYLAdSw==" + "version": "2021.8.1-rc1-1629709535", + "resolved": "https://registry.npmjs.org/@goauthentik/api/-/api-2021.8.1-rc1-1629709535.tgz", + "integrity": "sha512-rlOt63gF3ALGuRIn8RiWRT5cZ4JhKu9koCtdFA7FDOYYU9jWBJ3e/YlFLLOLQrL8Cyl2g553We98t0n8AyDOBQ==" }, "node_modules/@humanwhocodes/config-array": { "version": "0.5.0", @@ -9567,9 +9567,9 @@ "integrity": "sha512-eYm8vijH/hpzr/6/1CJ/V/Eb1xQFW2nnUKArb3z+yUWv7HTwj6M7SP957oMjfZjAHU6qpoNc2wQvIxBLWYa/Jg==" }, "@goauthentik/api": { - "version": "1629704636.0.0", - "resolved": "https://registry.npmjs.org/@goauthentik/api/-/api-1629704636.0.0.tgz", - "integrity": "sha512-rjUjp7Gbf/Tzw0hGPx2ig5InXa4bPqj4FkFnIEgR6T1w6jbHV+q2BghztVFDc2hK2ib2NnQUQp6LWIAcYLAdSw==" + "version": "2021.8.1-rc1-1629709535", + "resolved": "https://registry.npmjs.org/@goauthentik/api/-/api-2021.8.1-rc1-1629709535.tgz", + "integrity": "sha512-rlOt63gF3ALGuRIn8RiWRT5cZ4JhKu9koCtdFA7FDOYYU9jWBJ3e/YlFLLOLQrL8Cyl2g553We98t0n8AyDOBQ==" }, "@humanwhocodes/config-array": { "version": "0.5.0", diff --git a/web/package.json b/web/package.json index e57620c7c..27ace2e48 100644 --- a/web/package.json +++ b/web/package.json @@ -46,7 +46,7 @@ "@babel/preset-env": "^7.15.0", "@babel/preset-typescript": "^7.15.0", "@fortawesome/fontawesome-free": "^5.15.4", - "@goauthentik/api": "^2021.8.1-rc1+1629705955", + "@goauthentik/api": "^2021.8.1-rc1-1629709535", "@lingui/cli": "^3.10.2", "@lingui/core": "^3.10.4", "@lingui/macro": "^3.10.2", diff --git a/web/rollup.config.js b/web/rollup.config.js index f74706ca5..89f19ddf0 100644 --- a/web/rollup.config.js +++ b/web/rollup.config.js @@ -6,7 +6,6 @@ import cssimport from "rollup-plugin-cssimport"; import copy from "rollup-plugin-copy"; import babel from "@rollup/plugin-babel"; import replace from "@rollup/plugin-replace"; -import * as authentik from "@goauthentik/api"; const extensions = [".js", ".jsx", ".ts", ".tsx"]; @@ -47,6 +46,9 @@ const resources = [ const isProdBuild = process.env.NODE_ENV === "production"; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types function manualChunks(id) { + if (id.includes("@goauthentik/api")) { + return "api"; + } if (id.includes("locales")) { const parts = id.split("/"); const file = parts[parts.length - 1]; @@ -100,11 +102,7 @@ export default [ plugins: [ cssimport(), resolve({ extensions, browser: true }), - // Because the API Client uses star exports from typescript, we have to explicitly tell rollup - // Which classes can be imported - commonjs({ - namedExports: { "@goauthentik/api": Object.keys(authentik) }, - }), + commonjs(), babel({ extensions, babelHelpers: "runtime", @@ -136,11 +134,7 @@ export default [ plugins: [ cssimport(), resolve({ extensions, browser: true }), - // Because the API Client uses star exports from typescript, we have to explicitly tell rollup - // Which classes can be imported - commonjs({ - namedExports: { "@goauthentik/api": Object.keys(authentik) }, - }), + commonjs(), babel({ extensions, babelHelpers: "runtime",