From f5394fa5e81e8bf9f4dd99db4e1e491482d7f6cf Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Wed, 25 Sep 2024 13:52:18 +0200 Subject: [PATCH] Take static dependencies from ui-plugin dependencies Instead of a separate manually tended list for static dependencies in ui-webapp we now take it from the package definitions in ui-plugins. Doing so we avoid inconsistencies between these lists. --- scm-ui/ui-plugins/generateStatic.js | 51 +++++++++++++++++++ scm-ui/ui-plugins/package.json | 16 ++++-- scm-ui/ui-webapp/package.json | 3 +- .../src/_modules/provided-modules.ts | 39 +------------- 4 files changed, 67 insertions(+), 42 deletions(-) create mode 100644 scm-ui/ui-plugins/generateStatic.js diff --git a/scm-ui/ui-plugins/generateStatic.js b/scm-ui/ui-plugins/generateStatic.js new file mode 100644 index 0000000000..3beb53db25 --- /dev/null +++ b/scm-ui/ui-plugins/generateStatic.js @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020 - present Cloudogu GmbH + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the Free + * Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +import fs from "node:fs"; + +const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf8")); + +const { lazyDependencies } = packageJson; + +let importStatements = ""; +let calls = ""; + +Object.keys(packageJson.dependencies) + .filter((dependency) => !lazyDependencies.includes(dependency)) + .forEach((dependency, index) => { + importStatements += `import * as DEP${index} from '${dependency}';\n`; + calls += ` defineStatic('${dependency}', DEP${index});\n`; + }); + +fs.mkdirSync("./build", { recursive: true }); +fs.writeFileSync( + "./build/provided-modules.d.ts", + ` +type DefineStatic = (name: string, dependency: unknown) => void; + +declare function definePluginDependencies(defineStatic: DefineStatic): void; +export {definePluginDependencies}; +` +); +fs.writeFileSync( + "./build/provided-modules.js", + ` +${importStatements} + +export function definePluginDependencies(defineStatic) { +${calls} +}` +); diff --git a/scm-ui/ui-plugins/package.json b/scm-ui/ui-plugins/package.json index b751297d92..0f6e507787 100644 --- a/scm-ui/ui-plugins/package.json +++ b/scm-ui/ui-plugins/package.json @@ -3,6 +3,12 @@ "description": "Defines the versions of SCM-Manager plugin dependencies provided by the core webapp. Exclusively used by the postinstall command of @scm-manager/plugin-scripts.", "version": "3.4.3-SNAPSHOT", "license": "AGPL-3.0-only", + "type": "module", + "main": "./build/provided-modules.js", + "types": "./build/provided-modules.d.ts", + "scripts": { + "build": "node ./generateStatic.js" + }, "dependencies": { "react": "17", "react-dom": "17", @@ -11,12 +17,13 @@ "react-i18next": "11", "styled-components": "^5.3.5", "@scm-manager/ui-api": "3.4.3-SNAPSHOT", - "@scm-manager/ui-extensions": "3.4.3-SNAPSHOT", - "@scm-manager/ui-components": "3.4.3-SNAPSHOT", - "@scm-manager/ui-forms": "3.4.3-SNAPSHOT", "@scm-manager/ui-buttons": "3.4.3-SNAPSHOT", - "@scm-manager/ui-overlays": "3.4.3-SNAPSHOT", + "@scm-manager/ui-components": "3.4.3-SNAPSHOT", + "@scm-manager/ui-core": "3.4.3-SNAPSHOT", + "@scm-manager/ui-extensions": "3.4.3-SNAPSHOT", + "@scm-manager/ui-forms": "3.4.3-SNAPSHOT", "@scm-manager/ui-layout": "3.4.3-SNAPSHOT", + "@scm-manager/ui-overlays": "3.4.3-SNAPSHOT", "classnames": "^2.2.6", "query-string": "6.14.1", "redux": "^4.0.0", @@ -24,6 +31,7 @@ "react-hook-form": "^7.5.1", "react-query": "^3.25.1" }, + "lazyDependencies": ["redux", "react-redux"], "devDependencies": { "@scm-manager/babel-preset": "^2.13.1", "@scm-manager/eslint-config": "^2.17.0", diff --git a/scm-ui/ui-webapp/package.json b/scm-ui/ui-webapp/package.json index 6dcefc4469..214f0d36f9 100644 --- a/scm-ui/ui-webapp/package.json +++ b/scm-ui/ui-webapp/package.json @@ -46,6 +46,7 @@ "@scm-manager/eslint-config": "^2.17.0", "@scm-manager/jest-preset": "^2.13.0", "@scm-manager/ui-tests": "3.4.3-SNAPSHOT", + "@scm-manager/ui-plugins": "3.4.3-SNAPSHOT", "@testing-library/react": "^12.1.5", "@types/classnames": "^2.2.9", "@types/enzyme": "^3.10.3", @@ -94,4 +95,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/scm-ui/ui-webapp/src/_modules/provided-modules.ts b/scm-ui/ui-webapp/src/_modules/provided-modules.ts index 859aeb961f..4242b034db 100644 --- a/scm-ui/ui-webapp/src/_modules/provided-modules.ts +++ b/scm-ui/ui-webapp/src/_modules/provided-modules.ts @@ -15,44 +15,9 @@ */ import { defineLazy, defineStatic } from "./define"; +import { definePluginDependencies } from "@scm-manager/ui-plugins"; -import * as React from "react"; -import * as ReactDOM from "react-dom"; -import * as ReactRouterDom from "react-router-dom"; -import * as ReactQuery from "react-query"; -import * as StyledComponents from "styled-components"; -import * as ReactHookForm from "react-hook-form"; -import * as ReactI18Next from "react-i18next"; -import * as ClassNames from "classnames"; -import * as QueryString from "query-string"; -import * as UIExtensions from "@scm-manager/ui-extensions"; -import * as UIComponents from "@scm-manager/ui-components"; -import * as UIButtons from "@scm-manager/ui-buttons"; -import * as UIForms from "@scm-manager/ui-forms"; -import * as UIOverlays from "@scm-manager/ui-overlays"; -import * as UILayout from "@scm-manager/ui-layout"; -import * as UIApi from "@scm-manager/ui-api"; -import * as UICore from "@scm-manager/ui-core"; - -// This module has side effects and is required to be imported unconditionally into the application at all times. - -defineStatic("react", React); -defineStatic("react-dom", ReactDOM); -defineStatic("react-router-dom", ReactRouterDom); -defineStatic("styled-components", StyledComponents); -defineStatic("react-i18next", ReactI18Next); -defineStatic("react-hook-form", ReactHookForm); -defineStatic("react-query", ReactQuery); -defineStatic("classnames", ClassNames); -defineStatic("query-string", QueryString); -defineStatic("@scm-manager/ui-extensions", UIExtensions); -defineStatic("@scm-manager/ui-components", UIComponents); -defineStatic("@scm-manager/ui-buttons", UIButtons); -defineStatic("@scm-manager/ui-forms", UIForms); -defineStatic("@scm-manager/ui-overlays", UIOverlays); -defineStatic("@scm-manager/ui-layout", UILayout); -defineStatic("@scm-manager/ui-api", UIApi); -defineStatic("@scm-manager/ui-core", UICore); +definePluginDependencies(defineStatic); // redux is deprecated in favor of ui-api defineLazy("redux", () => import("@scm-manager/ui-legacy").then((legacy) => legacy.Redux));