mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Simpify by using webpack to create a commonjs package
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
1d763c1972
commit
9718837784
@ -127,10 +127,6 @@
|
|||||||
"to": "./templates/",
|
"to": "./templates/",
|
||||||
"filter": "**/*.yaml"
|
"filter": "**/*.yaml"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"from": "build/webpack",
|
|
||||||
"to": "static/bundles"
|
|
||||||
},
|
|
||||||
"LICENSE"
|
"LICENSE"
|
||||||
],
|
],
|
||||||
"linux": {
|
"linux": {
|
||||||
|
|||||||
9
src/common/fetch/fetch.global-override-for-injectable.ts
Normal file
9
src/common/fetch/fetch.global-override-for-injectable.ts
Normal file
@ -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);
|
||||||
@ -2,32 +2,20 @@
|
|||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* 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";
|
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 Response = FetchModule.Response;
|
||||||
type RequestInit = FetchModule.RequestInit;
|
type RequestInit = FetchModule.RequestInit;
|
||||||
|
|
||||||
export type Fetch = (url: string, init?: RequestInit) => Promise<Response>;
|
export type Fetch = (url: string, init?: RequestInit) => Promise<Response>;
|
||||||
|
|
||||||
export const fetchImplInjectionToken = getInjectionToken<Promise<typeof FetchModule>>({
|
|
||||||
id: "fetch-impl-token",
|
|
||||||
});
|
|
||||||
|
|
||||||
const fetchInjectable = getInjectable({
|
const fetchInjectable = getInjectable({
|
||||||
id: "fetch",
|
id: "fetch",
|
||||||
instantiate: (di): Fetch => {
|
instantiate: () => fetch,
|
||||||
let fetchP: Promise<typeof FetchModule> | undefined;
|
causesSideEffects: true,
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default fetchInjectable;
|
export default fetchInjectable;
|
||||||
|
|||||||
@ -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;
|
|
||||||
@ -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));
|
|
||||||
@ -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<typeof FetchModule>;
|
|
||||||
|
|
||||||
const fetchImplInjectable = getInjectable({
|
|
||||||
id: "fetch-impl",
|
|
||||||
instantiate: () => importFetchModule(),
|
|
||||||
injectionToken: fetchImplInjectionToken,
|
|
||||||
causesSideEffects: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
export default fetchImplInjectable;
|
|
||||||
@ -4,51 +4,21 @@
|
|||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import httpProxy from "http-proxy";
|
import httpProxy from "http-proxy";
|
||||||
import path from "path";
|
|
||||||
import { webpackDevServerPort } from "../../../../webpack/vars";
|
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 { publicPath } from "../../../common/vars";
|
||||||
import developmentBundledLibrariesDirectoryInjectable from "../../../common/vars/development-bundled-libraries-dir.injectable";
|
|
||||||
import appNameInjectable from "../../../common/vars/app-name.injectable";
|
import appNameInjectable from "../../../common/vars/app-name.injectable";
|
||||||
import type { LensApiRequest, RouteResponse } from "../../router/route";
|
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({
|
const devStaticFileRouteHandlerInjectable = getInjectable({
|
||||||
id: "dev-static-file-route-handler",
|
id: "dev-static-file-route-handler",
|
||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
const proxy = httpProxy.createProxy();
|
const proxy = httpProxy.createProxy();
|
||||||
const appName = di.inject(appNameInjectable);
|
const appName = di.inject(appNameInjectable);
|
||||||
const readFileBuffer = di.inject(readFileBufferInjectable);
|
|
||||||
const proxyTarget = `http://127.0.0.1:${webpackDevServerPort}`;
|
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<RouteResponse<Buffer>> => {
|
return async ({ raw: { req, res }}: LensApiRequest<"/{path*}">): Promise<RouteResponse<Buffer>> => {
|
||||||
if (req.url === "/" || !req.url) {
|
if (req.url === "/" || !req.url) {
|
||||||
req.url = `${publicPath}/${appName}.html`;
|
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/")) {
|
} else if (!req.url.startsWith("/build/")) {
|
||||||
return { statusCode: 404 };
|
return { statusCode: 404 };
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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));
|
|
||||||
@ -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<typeof FetchModule>;
|
|
||||||
|
|
||||||
const fetchImplInjectable = getInjectable({
|
|
||||||
id: "fetch-impl",
|
|
||||||
instantiate: () => importFetchModule(),
|
|
||||||
injectionToken: fetchImplInjectionToken,
|
|
||||||
causesSideEffects: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
export default fetchImplInjectable;
|
|
||||||
@ -7,18 +7,15 @@ import path from "path";
|
|||||||
export default {
|
export default {
|
||||||
entry: "./node_modules/node-fetch/src/index.js",
|
entry: "./node_modules/node-fetch/src/index.js",
|
||||||
output: {
|
output: {
|
||||||
library: {
|
|
||||||
type: "module",
|
|
||||||
},
|
|
||||||
path: path.resolve(__dirname, "..", "build", "webpack"),
|
path: path.resolve(__dirname, "..", "build", "webpack"),
|
||||||
filename: "node-fetch.bundle.js",
|
filename: "node-fetch.bundle.js",
|
||||||
module: true,
|
library: {
|
||||||
|
name: "NodeFetch",
|
||||||
|
type: "commonjs",
|
||||||
|
},
|
||||||
clean: true,
|
clean: true,
|
||||||
asyncChunks: false, // This is required so that only one file is created
|
asyncChunks: false, // This is required so that only one file is created
|
||||||
},
|
},
|
||||||
experiments: {
|
|
||||||
outputModule: true,
|
|
||||||
},
|
|
||||||
mode: "production",
|
mode: "production",
|
||||||
target: "electron-renderer",
|
target: "electron-renderer",
|
||||||
optimization: {
|
optimization: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user