1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add hot-reload plugin, and make proxy redirects requests to webpackdevserver

Signed-off-by: Hung-Han (Henry) Chen <1474479+chenhunghan@users.noreply.github.com>
This commit is contained in:
Hung-Han (Henry) Chen 2020-11-06 14:55:28 +08:00
parent 4efc886142
commit 0a584acdf5
No known key found for this signature in database
GPG Key ID: 32931168425E1C95
3 changed files with 28 additions and 14 deletions

View File

@ -22,6 +22,7 @@ export const mainDir = path.join(contextDir, "src/main");
export const rendererDir = path.join(contextDir, "src/renderer"); export const rendererDir = path.join(contextDir, "src/renderer");
export const htmlTemplate = path.resolve(rendererDir, "template.html"); export const htmlTemplate = path.resolve(rendererDir, "template.html");
export const sassCommonVars = path.resolve(rendererDir, "components/vars.scss"); export const sassCommonVars = path.resolve(rendererDir, "components/vars.scss");
export const webpackDevServerPort = 9009
// Special runtime paths // Special runtime paths
defineGlobal("__static", { defineGlobal("__static", {

View File

@ -4,7 +4,7 @@ import http from "http"
import path from "path" import path from "path"
import { readFile } from "fs-extra" import { readFile } from "fs-extra"
import { Cluster } from "./cluster" import { Cluster } from "./cluster"
import { apiPrefix, appName, publicPath } from "../common/vars"; import { apiPrefix, appName, publicPath, isDevelopment, webpackDevServerPort } from "../common/vars";
import { helmRoute, kubeconfigRoute, metricsRoute, portForwardRoute, resourceApplierRoute, watchRoute } from "./routes"; import { helmRoute, kubeconfigRoute, metricsRoute, portForwardRoute, resourceApplierRoute, watchRoute } from "./routes";
export interface RouterRequestOpts { export interface RouterRequestOpts {
@ -94,13 +94,15 @@ export class Router {
return mimeTypes[path.extname(filename).slice(1)] || "text/plain" return mimeTypes[path.extname(filename).slice(1)] || "text/plain"
} }
async handleStaticFile(filePath: string, res: http.ServerResponse, requestpath?: string) { async handleStaticFile(filePath: string, res: http.ServerResponse, requestpath: string, req: http.IncomingMessage) {
const asset = path.join(__static, filePath); const asset = path.join(__static, filePath);
try { try {
if (path.basename(requestpath).includes(appName)) { const filename = path.basename(requestpath);
res.writeHead(307, const toWebpackDevServer = filename.includes(appName) || filename.includes('hot-update') || requestpath.includes('sockjs-node')
{ Location: 'http://localhost:9000' + requestpath } if (isDevelopment && toWebpackDevServer) {
); const redirectLocation = `http://localhost:${webpackDevServerPort}` + requestpath
res.statusCode = 307;
res.setHeader('Location', redirectLocation);
res.end(); res.end();
return; return;
} }
@ -109,15 +111,17 @@ export class Router {
res.write(data) res.write(data)
res.end() res.end()
} catch (err) { } catch (err) {
this.handleStaticFile(`${publicPath}/${appName}.html`, res); this.handleStaticFile(`${publicPath}/${appName}.html`, res, requestpath, req);
} }
} }
protected addRoutes() { protected addRoutes() {
// Static assets // Static assets
this.router.add({ method: 'get', path: '/{path*}' }, ({ params, response, path }: LensApiRequest) => { this.router.add(
this.handleStaticFile(params.path, response, path); { method: 'get', path: '/{path*}' },
}); ({ params, response, path, raw: { req }}: LensApiRequest) => {
this.handleStaticFile(params.path, response, path, req);
});
this.router.add({ method: "get", path: `${apiPrefix}/kubeconfig/service-account/{namespace}/{account}` }, kubeconfigRoute.routeServiceAccountRoute.bind(kubeconfigRoute)) this.router.add({ method: "get", path: `${apiPrefix}/kubeconfig/service-account/{namespace}/{account}` }, kubeconfigRoute.routeServiceAccountRoute.bind(kubeconfigRoute))

View File

@ -1,4 +1,4 @@
import { appName, buildDir, htmlTemplate, isDevelopment, isProduction, publicPath, rendererDir, sassCommonVars } from "./src/common/vars"; import { appName, buildDir, htmlTemplate, isDevelopment, isProduction, publicPath, rendererDir, sassCommonVars, webpackDevServerPort } from "./src/common/vars";
import path from "path"; import path from "path";
import webpack from "webpack"; import webpack from "webpack";
import HtmlWebpackPlugin from "html-webpack-plugin"; import HtmlWebpackPlugin from "html-webpack-plugin";
@ -7,6 +7,7 @@ import TerserPlugin from "terser-webpack-plugin";
import ForkTsCheckerPlugin from "fork-ts-checker-webpack-plugin" import ForkTsCheckerPlugin from "fork-ts-checker-webpack-plugin"
import ProgressBarPlugin from "progress-bar-webpack-plugin"; import ProgressBarPlugin from "progress-bar-webpack-plugin";
import HardSourceWebpackPlugin from 'hard-source-webpack-plugin'; import HardSourceWebpackPlugin from 'hard-source-webpack-plugin';
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin'
export default [ export default [
webpackLensRenderer webpackLensRenderer
@ -22,10 +23,12 @@ export function webpackLensRenderer({ showVars = true } = {}): webpack.Configura
devtool: "source-map", // todo: optimize in dev-mode with webpack.SourceMapDevToolPlugin devtool: "source-map", // todo: optimize in dev-mode with webpack.SourceMapDevToolPlugin
devServer: { devServer: {
contentBase: buildDir, contentBase: buildDir,
port: 9000, port: webpackDevServerPort,
host: "localhost", host: "localhost",
hot: true, hot: true,
// to avoid cors errors when requests is from iframes
disableHostCheck: true, disableHostCheck: true,
headers: { 'Access-Control-Allow-Origin': '*' },
}, },
name: "lens-app", name: "lens-app",
mode: isProduction ? "production" : "development", mode: isProduction ? "production" : "development",
@ -87,7 +90,11 @@ export function webpackLensRenderer({ showVars = true } = {}): webpack.Configura
["@babel/preset-env", { ["@babel/preset-env", {
modules: "commonjs" // ling-ui modules: "commonjs" // ling-ui
}], }],
] ],
plugins: [
// ... other plugins
isDevelopment && require.resolve('react-refresh/babel'),
].filter(Boolean),
} }
}, },
{ {
@ -181,7 +188,9 @@ export function webpackLensRenderer({ showVars = true } = {}): webpack.Configura
filename: "[name].css", filename: "[name].css",
}), }),
new HardSourceWebpackPlugin() isDevelopment && new HardSourceWebpackPlugin(),
isDevelopment && new webpack.HotModuleReplacementPlugin(),
isDevelopment && new ReactRefreshWebpackPlugin(),
], ],
} }
} }