From 9718837784f374ba6b357e5c4b4f233c0c8cfab6 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 9 Nov 2022 11:23:47 -0500 Subject: [PATCH] Simpify by using webpack to create a commonjs package Signed-off-by: Sebastian Malton --- package.json | 4 --- .../fetch.global-override-for-injectable.ts | 9 ++++++ src/common/fetch/fetch.injectable.ts | 22 ++++---------- ...opment-bundled-libraries-dir.injectable.ts | 19 ------------ ...tch-impl.global-override-for-injectable.ts | 13 -------- src/main/fetch/fetch-impl.injectable.ts | 18 ----------- .../routes/files/development.injectable.ts | 30 ------------------- ...tch-impl.global-override-for-injectable.ts | 13 -------- src/renderer/fetch/fetch-impl.injectable.ts | 18 ----------- webpack/node-fetch.ts | 11 +++---- 10 files changed, 18 insertions(+), 139 deletions(-) create mode 100644 src/common/fetch/fetch.global-override-for-injectable.ts delete mode 100644 src/common/vars/development-bundled-libraries-dir.injectable.ts delete mode 100644 src/main/fetch/fetch-impl.global-override-for-injectable.ts delete mode 100644 src/main/fetch/fetch-impl.injectable.ts delete mode 100644 src/renderer/fetch/fetch-impl.global-override-for-injectable.ts delete mode 100644 src/renderer/fetch/fetch-impl.injectable.ts diff --git a/package.json b/package.json index 5c5dd8ee56..bfe7dc934c 100644 --- a/package.json +++ b/package.json @@ -127,10 +127,6 @@ "to": "./templates/", "filter": "**/*.yaml" }, - { - "from": "build/webpack", - "to": "static/bundles" - }, "LICENSE" ], "linux": { diff --git a/src/common/fetch/fetch.global-override-for-injectable.ts b/src/common/fetch/fetch.global-override-for-injectable.ts new file mode 100644 index 0000000000..1a5f80735c --- /dev/null +++ b/src/common/fetch/fetch.global-override-for-injectable.ts @@ -0,0 +1,9 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getGlobalOverrideForFunction } from "../test-utils/get-global-override-for-function"; +import fetchInjectable from "./fetch.injectable"; + +export default getGlobalOverrideForFunction(fetchInjectable); diff --git a/src/common/fetch/fetch.injectable.ts b/src/common/fetch/fetch.injectable.ts index 46b8127cf7..8f0cec3c2e 100644 --- a/src/common/fetch/fetch.injectable.ts +++ b/src/common/fetch/fetch.injectable.ts @@ -2,32 +2,20 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { getInjectionToken, getInjectable } from "@ogre-tools/injectable"; +import { getInjectable } from "@ogre-tools/injectable"; import type * as FetchModule from "node-fetch"; +const { NodeFetch: { default: fetch }} = require("../../../build/webpack/node-fetch.bundle") as { NodeFetch: typeof FetchModule }; + type Response = FetchModule.Response; type RequestInit = FetchModule.RequestInit; export type Fetch = (url: string, init?: RequestInit) => Promise; -export const fetchImplInjectionToken = getInjectionToken>({ - id: "fetch-impl-token", -}); - const fetchInjectable = getInjectable({ id: "fetch", - instantiate: (di): Fetch => { - let fetchP: Promise | undefined; - - return async (url, init) => { - /** - * This is done so that there are no timing issues with the first use of `fetchInjectable`. - */ - const fetch = (await (fetchP ??= di.inject(fetchImplInjectionToken))).default; - - return fetch(url, init); - }; - }, + instantiate: () => fetch, + causesSideEffects: true, }); export default fetchInjectable; diff --git a/src/common/vars/development-bundled-libraries-dir.injectable.ts b/src/common/vars/development-bundled-libraries-dir.injectable.ts deleted file mode 100644 index 0b76000db1..0000000000 --- a/src/common/vars/development-bundled-libraries-dir.injectable.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { getInjectable } from "@ogre-tools/injectable"; -import joinPathsInjectable from "../path/join-paths.injectable"; -import lensResourcesDirInjectable from "./lens-resources-dir.injectable"; - -const developmentBundledLibrariesDirectoryInjectable = getInjectable({ - id: "development-bundled-libraries-directory", - instantiate: (di) => { - const lensResourcesDir = di.inject(lensResourcesDirInjectable); - const joinPaths = di.inject(joinPathsInjectable); - - return joinPaths(lensResourcesDir, "build", "webpack"); - }, -}); - -export default developmentBundledLibrariesDirectoryInjectable; diff --git a/src/main/fetch/fetch-impl.global-override-for-injectable.ts b/src/main/fetch/fetch-impl.global-override-for-injectable.ts deleted file mode 100644 index e98463c98b..0000000000 --- a/src/main/fetch/fetch-impl.global-override-for-injectable.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ - -import { getGlobalOverride } from "../../common/test-utils/get-global-override"; -import fetchImplInjectable from "./fetch-impl.injectable"; - -export default getGlobalOverride(fetchImplInjectable, async () => ({ - default: async () => { - throw new Error("tried to fetch resource without override"); - }, -} as any)); diff --git a/src/main/fetch/fetch-impl.injectable.ts b/src/main/fetch/fetch-impl.injectable.ts deleted file mode 100644 index 4b6533acc7..0000000000 --- a/src/main/fetch/fetch-impl.injectable.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { getInjectable } from "@ogre-tools/injectable"; -import { fetchImplInjectionToken } from "../../common/fetch/fetch.injectable"; -import type * as FetchModule from "node-fetch"; - -const importFetchModule = new Function("return import('node-fetch')") as () => Promise; - -const fetchImplInjectable = getInjectable({ - id: "fetch-impl", - instantiate: () => importFetchModule(), - injectionToken: fetchImplInjectionToken, - causesSideEffects: true, -}); - -export default fetchImplInjectable; diff --git a/src/main/routes/files/development.injectable.ts b/src/main/routes/files/development.injectable.ts index a051dd041c..dcc6fce649 100644 --- a/src/main/routes/files/development.injectable.ts +++ b/src/main/routes/files/development.injectable.ts @@ -4,51 +4,21 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import httpProxy from "http-proxy"; -import path from "path"; import { webpackDevServerPort } from "../../../../webpack/vars"; -import readFileBufferInjectable from "../../../common/fs/read-file-buffer.injectable"; -import joinPathsInjectable from "../../../common/path/join-paths.injectable"; import { publicPath } from "../../../common/vars"; -import developmentBundledLibrariesDirectoryInjectable from "../../../common/vars/development-bundled-libraries-dir.injectable"; import appNameInjectable from "../../../common/vars/app-name.injectable"; import type { LensApiRequest, RouteResponse } from "../../router/route"; -import type { SupportedFileExtension } from "../../router/router-content-types"; -import { contentTypes } from "../../router/router-content-types"; const devStaticFileRouteHandlerInjectable = getInjectable({ id: "dev-static-file-route-handler", instantiate: (di) => { const proxy = httpProxy.createProxy(); const appName = di.inject(appNameInjectable); - const readFileBuffer = di.inject(readFileBufferInjectable); const proxyTarget = `http://127.0.0.1:${webpackDevServerPort}`; - const bundledLibrariesDirectory = di.inject(developmentBundledLibrariesDirectoryInjectable); - const joinPaths = di.inject(joinPathsInjectable); return async ({ raw: { req, res }}: LensApiRequest<"/{path*}">): Promise> => { if (req.url === "/" || !req.url) { req.url = `${publicPath}/${appName}.html`; - } else if (req.url.startsWith("/bundles/")) { - const bundledLibraryFilePath = joinPaths(bundledLibrariesDirectory, req.url.replace("/bundles/", "")); - - if (!bundledLibraryFilePath.startsWith(bundledLibrariesDirectory)) { - return { statusCode: 404 }; - } - - try { - const fileExtension = path - .extname(bundledLibraryFilePath) - .slice(1) as SupportedFileExtension; - - const contentType = contentTypes[fileExtension] || contentTypes.txt; - - return { - response: await readFileBuffer(bundledLibraryFilePath), - contentType, - }; - } catch { - return { statusCode: 404 }; - } } else if (!req.url.startsWith("/build/")) { return { statusCode: 404 }; } diff --git a/src/renderer/fetch/fetch-impl.global-override-for-injectable.ts b/src/renderer/fetch/fetch-impl.global-override-for-injectable.ts deleted file mode 100644 index e98463c98b..0000000000 --- a/src/renderer/fetch/fetch-impl.global-override-for-injectable.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ - -import { getGlobalOverride } from "../../common/test-utils/get-global-override"; -import fetchImplInjectable from "./fetch-impl.injectable"; - -export default getGlobalOverride(fetchImplInjectable, async () => ({ - default: async () => { - throw new Error("tried to fetch resource without override"); - }, -} as any)); diff --git a/src/renderer/fetch/fetch-impl.injectable.ts b/src/renderer/fetch/fetch-impl.injectable.ts deleted file mode 100644 index 6fde6740e9..0000000000 --- a/src/renderer/fetch/fetch-impl.injectable.ts +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { getInjectable } from "@ogre-tools/injectable"; -import { fetchImplInjectionToken } from "../../common/fetch/fetch.injectable"; -import type * as FetchModule from "node-fetch"; - -const importFetchModule = new Function("return import('/bundles/node-fetch.bundle.js')") as () => Promise; - -const fetchImplInjectable = getInjectable({ - id: "fetch-impl", - instantiate: () => importFetchModule(), - injectionToken: fetchImplInjectionToken, - causesSideEffects: true, -}); - -export default fetchImplInjectable; diff --git a/webpack/node-fetch.ts b/webpack/node-fetch.ts index 971dac1723..856811e05f 100644 --- a/webpack/node-fetch.ts +++ b/webpack/node-fetch.ts @@ -7,18 +7,15 @@ import path from "path"; export default { entry: "./node_modules/node-fetch/src/index.js", output: { - library: { - type: "module", - }, path: path.resolve(__dirname, "..", "build", "webpack"), filename: "node-fetch.bundle.js", - module: true, + library: { + name: "NodeFetch", + type: "commonjs", + }, clean: true, asyncChunks: false, // This is required so that only one file is created }, - experiments: { - outputModule: true, - }, mode: "production", target: "electron-renderer", optimization: {