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

fix: raw-loader / <Icon svg="./*">, updated mocked types for importing resources via webpack

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2022-01-26 16:15:06 +02:00
parent f7a53e6c6b
commit cae184883b
6 changed files with 27 additions and 12 deletions

View File

@ -11,6 +11,7 @@ import { broadcastMessage, requestMain } from "../ipc";
import { app } from "electron"; 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 KubeClusterCategoryIcon from "./icons/kubernetes.svg?raw";
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`).default, // eslint-disable-line icon: KubeClusterCategoryIcon,
}; };
public spec: CatalogCategorySpec = { public spec: CatalogCategorySpec = {
group: "entity.k8slens.dev", group: "entity.k8slens.dev",

View File

@ -100,7 +100,7 @@ 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`).default; const svgIconText = svg.includes("<svg") ? svg : require(`./${svg}.svg?raw`);
iconContent = <span className="icon" dangerouslySetInnerHTML={{ __html: svgIconText }}/>; iconContent = <span className="icon" dangerouslySetInnerHTML={{ __html: svgIconText }}/>;
} }

16
types/mocks.d.ts vendored
View File

@ -25,7 +25,17 @@ declare module "*.scss" {
const content: string; const content: string;
export = content; export = content;
} }
declare module "*.ttf" {
const content: string; // Declare everything what's bundled as webpack's type="asset/resource"
export = content; declare module "*.svg?raw" {
export = "";
} }
declare module "*.svg" {
export = "";
}
declare module "*.jpg";
declare module "*.png";
declare module "*.eot";
declare module "*.woff";
declare module "*.woff2";
declare module "*.ttf";

View File

@ -57,6 +57,7 @@ export default function generateExtensionTypes(): webpack.Configuration {
fontsLoaderWebpackRule(), fontsLoaderWebpackRule(),
filesAndIconsWebpackRule(), filesAndIconsWebpackRule(),
cssModulesWebpackRule({ styleLoader: "style-loader" }), cssModulesWebpackRule({ styleLoader: "style-loader" }),
{ resourceQuery: /raw/, type: "asset/source" }, // import with "?raw"
], ],
}, },
resolve: { resolve: {

View File

@ -42,11 +42,12 @@ configs.push((): webpack.Configuration => {
test: /\.node$/, test: /\.node$/,
use: "node-loader", use: "node-loader",
}, },
getTSLoader({}, /\.ts$/),
{ {
// import SvgRawTextFile from "./some-file.svg?raw"
resourceQuery: /raw/, resourceQuery: /raw/,
type: "asset/source", type: "asset/source",
}, },
getTSLoader({}, /\.ts$/),
], ],
}, },
plugins: [ plugins: [

View File

@ -86,14 +86,16 @@ export function webpackLensRenderer(): webpack.Configuration {
test: /\.node$/, test: /\.node$/,
use: "node-loader", use: "node-loader",
}, },
{
resourceQuery: /raw/,
type: "asset/source",
},
getTSLoader(), getTSLoader(),
cssModulesWebpackRule(), cssModulesWebpackRule(),
filesAndIconsWebpackRule(), filesAndIconsWebpackRule(),
fontsLoaderWebpackRule(), fontsLoaderWebpackRule(),
{
// 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",
},
], ],
}, },
@ -133,7 +135,7 @@ export function webpackLensRenderer(): webpack.Configuration {
export function filesAndIconsWebpackRule(): webpack.RuleSetRule { export function filesAndIconsWebpackRule(): webpack.RuleSetRule {
return { return {
test: /\.(jpg|png|svg|map|ico)$/, test: /\.(jpg|png|svg|map|ico)$/,
type: "asset/resource", type: "asset/resource", // https://webpack.js.org/guides/asset-modules/
}; };
} }
@ -143,7 +145,7 @@ export function filesAndIconsWebpackRule(): webpack.RuleSetRule {
export function fontsLoaderWebpackRule(): webpack.RuleSetRule { export function fontsLoaderWebpackRule(): webpack.RuleSetRule {
return { return {
test: /\.(ttf|eot|woff2?)$/, test: /\.(ttf|eot|woff2?)$/,
type: "asset", type: "asset/resource",
}; };
} }