mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix: loading svg icons inline as data-url (workaround for "?raw" as it fails in tests and "!!raw-loader!" seems doesn't work at all in webpack@5)
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
ad31d3e8a2
commit
98b516f290
@ -11,6 +11,7 @@ import { app } from "electron";
|
|||||||
import type { CatalogEntitySpec } from "../catalog/catalog-entity";
|
import type { CatalogEntitySpec } from "../catalog/catalog-entity";
|
||||||
import { IpcRendererNavigationEvents } from "../../renderer/navigation/events";
|
import { IpcRendererNavigationEvents } from "../../renderer/navigation/events";
|
||||||
import { requestClusterActivation, requestClusterDisconnection } from "../../renderer/ipc";
|
import { requestClusterActivation, requestClusterDisconnection } from "../../renderer/ipc";
|
||||||
|
import KubeClusterCategoryIcon from "./icons/kubernetes.svg";
|
||||||
|
|
||||||
export interface KubernetesClusterPrometheusMetrics {
|
export interface KubernetesClusterPrometheusMetrics {
|
||||||
address?: {
|
address?: {
|
||||||
@ -134,7 +135,7 @@ class KubernetesClusterCategory extends CatalogCategory {
|
|||||||
public readonly kind = "CatalogCategory";
|
public readonly kind = "CatalogCategory";
|
||||||
public metadata = {
|
public metadata = {
|
||||||
name: "Clusters",
|
name: "Clusters",
|
||||||
icon: require("./icons/kubernetes.svg?raw"),
|
icon: KubeClusterCategoryIcon,
|
||||||
};
|
};
|
||||||
public spec: CatalogCategorySpec = {
|
public spec: CatalogCategorySpec = {
|
||||||
group: "entity.k8slens.dev",
|
group: "entity.k8slens.dev",
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import type { LocationDescriptor } from "history";
|
|||||||
import { boundMethod, cssNames } from "../../utils";
|
import { boundMethod, cssNames } from "../../utils";
|
||||||
import { TooltipDecoratorProps, withTooltip } from "../tooltip";
|
import { TooltipDecoratorProps, withTooltip } from "../tooltip";
|
||||||
import isNumber from "lodash/isNumber";
|
import isNumber from "lodash/isNumber";
|
||||||
|
import { decode } from "../../../common/utils/base64";
|
||||||
|
|
||||||
export interface IconProps extends React.HTMLAttributes<any>, TooltipDecoratorProps {
|
export interface IconProps extends React.HTMLAttributes<any>, TooltipDecoratorProps {
|
||||||
material?: string; // material-icon, see available names at https://material.io/icons/
|
material?: string; // material-icon, see available names at https://material.io/icons/
|
||||||
@ -98,7 +99,9 @@ export class Icon extends React.PureComponent<IconProps> {
|
|||||||
|
|
||||||
// render as inline svg-icon
|
// render as inline svg-icon
|
||||||
if (typeof svg === "string") {
|
if (typeof svg === "string") {
|
||||||
const svgIconText = svg.includes("<svg") ? svg : require(`./${svg}.svg?raw`);
|
const dataUrlPrefix = "data:image/svg+xml;base64,";
|
||||||
|
const svgIconDataUrl: string = svg.startsWith(dataUrlPrefix) ? svg : require(`./${svg}.svg`);
|
||||||
|
const svgIconText = decode(svgIconDataUrl.replace(dataUrlPrefix, "")); // get raw xml
|
||||||
|
|
||||||
iconContent = <span className="icon" dangerouslySetInnerHTML={{ __html: svgIconText }}/>;
|
iconContent = <span className="icon" dangerouslySetInnerHTML={{ __html: svgIconText }}/>;
|
||||||
}
|
}
|
||||||
|
|||||||
7
types/mocks.d.ts
vendored
7
types/mocks.d.ts
vendored
@ -27,12 +27,7 @@ declare module "*.scss" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Declare everything what's bundled as webpack's type="asset/resource"
|
// Declare everything what's bundled as webpack's type="asset/resource"
|
||||||
declare module "*.svg?raw" {
|
declare module "*.svg";
|
||||||
export = "";
|
|
||||||
}
|
|
||||||
declare module "*.svg" {
|
|
||||||
export = "";
|
|
||||||
}
|
|
||||||
declare module "*.jpg";
|
declare module "*.jpg";
|
||||||
declare module "*.png";
|
declare module "*.png";
|
||||||
declare module "*.eot";
|
declare module "*.eot";
|
||||||
|
|||||||
@ -7,11 +7,7 @@
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import type webpack from "webpack";
|
import type webpack from "webpack";
|
||||||
import * as vars from "./src/common/vars";
|
import * as vars from "./src/common/vars";
|
||||||
import {
|
import { cssModulesWebpackRule, fontsLoaderWebpackRules, iconsAndImagesWebpackRules } from "./webpack.renderer";
|
||||||
cssModulesWebpackRule,
|
|
||||||
filesAndIconsWebpackRule,
|
|
||||||
fontsLoaderWebpackRule,
|
|
||||||
} from "./webpack.renderer";
|
|
||||||
|
|
||||||
export default function generateExtensionTypes(): webpack.Configuration {
|
export default function generateExtensionTypes(): webpack.Configuration {
|
||||||
const { isDevelopment, isProduction } = vars;
|
const { isDevelopment, isProduction } = vars;
|
||||||
@ -58,10 +54,9 @@ export default function generateExtensionTypes(): webpack.Configuration {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
fontsLoaderWebpackRule(),
|
|
||||||
filesAndIconsWebpackRule(),
|
|
||||||
cssModulesWebpackRule({ styleLoader: "style-loader" }),
|
cssModulesWebpackRule({ styleLoader: "style-loader" }),
|
||||||
{ resourceQuery: /raw/, type: "asset/source" },
|
...fontsLoaderWebpackRules(),
|
||||||
|
...iconsAndImagesWebpackRules(),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import nodeExternals from "webpack-node-externals";
|
|||||||
import * as vars from "./src/common/vars";
|
import * as vars from "./src/common/vars";
|
||||||
import getTSLoader from "./src/common/getTSLoader";
|
import getTSLoader from "./src/common/getTSLoader";
|
||||||
import CircularDependencyPlugin from "circular-dependency-plugin";
|
import CircularDependencyPlugin from "circular-dependency-plugin";
|
||||||
|
import { iconsAndImagesWebpackRules } from "./webpack.renderer";
|
||||||
|
|
||||||
const configs: { (): webpack.Configuration }[] = [];
|
const configs: { (): webpack.Configuration }[] = [];
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ configs.push((): webpack.Configuration => {
|
|||||||
context: __dirname,
|
context: __dirname,
|
||||||
target: "electron-main",
|
target: "electron-main",
|
||||||
mode: isDevelopment ? "development" : "production",
|
mode: isDevelopment ? "development" : "production",
|
||||||
devtool: isDevelopment ? "cheap-module-source-map" : "source-map",
|
devtool: isDevelopment ? "eval-cheap-source-map" : "source-map",
|
||||||
cache: isDevelopment,
|
cache: isDevelopment,
|
||||||
entry: {
|
entry: {
|
||||||
main: path.resolve(mainDir, "index.ts"),
|
main: path.resolve(mainDir, "index.ts"),
|
||||||
@ -43,10 +44,7 @@ configs.push((): webpack.Configuration => {
|
|||||||
use: "node-loader",
|
use: "node-loader",
|
||||||
},
|
},
|
||||||
getTSLoader({}, /\.ts$/),
|
getTSLoader({}, /\.ts$/),
|
||||||
{
|
...iconsAndImagesWebpackRules(),
|
||||||
resourceQuery: /raw/, // embed file as plain-text, e.g. require("./some-file.svg?raw")
|
|
||||||
type: "asset/source",
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
|
|||||||
@ -30,7 +30,7 @@ export function webpackLensRenderer(): webpack.Configuration {
|
|||||||
target: "electron-renderer",
|
target: "electron-renderer",
|
||||||
name: "lens-app",
|
name: "lens-app",
|
||||||
mode: isDevelopment ? "development" : "production",
|
mode: isDevelopment ? "development" : "production",
|
||||||
devtool: isDevelopment ? "cheap-module-source-map" : "source-map",
|
devtool: isDevelopment ? "eval-cheap-source-map" : "source-map",
|
||||||
cache: isDevelopment,
|
cache: isDevelopment,
|
||||||
entry: {
|
entry: {
|
||||||
[appName]: path.resolve(rendererDir, "bootstrap.tsx"),
|
[appName]: path.resolve(rendererDir, "bootstrap.tsx"),
|
||||||
@ -68,14 +68,8 @@ export function webpackLensRenderer(): webpack.Configuration {
|
|||||||
},
|
},
|
||||||
getTSLoader(),
|
getTSLoader(),
|
||||||
cssModulesWebpackRule(),
|
cssModulesWebpackRule(),
|
||||||
filesAndIconsWebpackRule(),
|
...iconsAndImagesWebpackRules(),
|
||||||
fontsLoaderWebpackRule(),
|
...fontsLoaderWebpackRules(),
|
||||||
{
|
|
||||||
// Allows to import/require() resource as plain text with suffix `?raw`
|
|
||||||
// To make it work must be listed in the end of `config.module.rules`
|
|
||||||
resourceQuery: /raw/,
|
|
||||||
type: "asset/source",
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -111,23 +105,32 @@ export function webpackLensRenderer(): webpack.Configuration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Import content of svg-icons, images and text files
|
* Import icons and image files.
|
||||||
|
* Read more about asset types: https://webpack.js.org/guides/asset-modules/
|
||||||
*/
|
*/
|
||||||
export function filesAndIconsWebpackRule(): webpack.RuleSetRule {
|
export function iconsAndImagesWebpackRules(): webpack.RuleSetRule[] {
|
||||||
return {
|
return [
|
||||||
test: /\.(jpg|png|svg|map|ico)$/,
|
{
|
||||||
type: "asset/resource", // https://webpack.js.org/guides/asset-modules/
|
test: /\.svg$/,
|
||||||
};
|
type: "asset/inline", // data:image/svg+xml;base64,...
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.(jpg|png|ico)$/,
|
||||||
|
type: "asset/resource", // path to file, e.g. "/static/assets/*"
|
||||||
|
},
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Import custom fonts as URL
|
* Import custom fonts as URL.
|
||||||
*/
|
*/
|
||||||
export function fontsLoaderWebpackRule(): webpack.RuleSetRule {
|
export function fontsLoaderWebpackRules(): webpack.RuleSetRule[] {
|
||||||
return {
|
return [
|
||||||
test: /\.(ttf|eot|woff2?)$/,
|
{
|
||||||
type: "asset/resource",
|
test: /\.(ttf|eot|woff2?)$/,
|
||||||
};
|
type: "asset/resource",
|
||||||
|
}
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user