Skip to content

Commit

Permalink
fix: 🐛 add vite "?react" postfix to adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorPulzz committed Mar 25, 2024
1 parent e5f6fac commit 50bb06b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/core/generateAdapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import path from "path";
import { alerts, BuilderEnum, CoreOptions, FileObject } from "../shared";
import { getGenericPaths } from "../shared/utils/getGenericPaths";

function genSvgPath(relative: string, file: FileObject[string], isVite: boolean) {
function genSvgPath(relative: string, file: FileObject[string], vitePostfix: string) {
/**
* Add postfix for handle vite-plugin-svgr package
*/
const folderPathSvg = file.dir.split(path.sep).slice(1).join(path.sep);
return path.join(relative, folderPathSvg, file.base) + (isVite ? "?react" : "");
return path.join(relative, folderPathSvg, file.base) + vitePostfix;
}

export async function generateDynamicAdapter(
Expand All @@ -18,6 +18,7 @@ export async function generateDynamicAdapter(
{ typescript, builder }: CoreOptions,
) {
const filesList = Object.entries(filesObject);
const vitePostfix = builder === BuilderEnum.VITE ? "?react" : "";

const mappedSvgTypes = filesList
.map(([, values]) => {
Expand All @@ -27,7 +28,7 @@ export async function generateDynamicAdapter(

const mappedSvgSwitch = filesList
.map(([, values]) => {
const svgPath = genSvgPath(relativeSvgPath, values, builder === BuilderEnum.VITE);
const svgPath = genSvgPath(relativeSvgPath, values, vitePostfix);
return `
case "${values.fileName}":
svgComponent = await import("${svgPath}");
Expand All @@ -44,6 +45,8 @@ export async function generateDynamicAdapter(

generateData = generateData.replace("{svgSwitch}", mappedSvgSwitch);

generateData = generateData.replace("{vitePostfix}", vitePostfix);

await fs.writeFile(adapterFilePath, generateData, {
encoding: "utf8",
flag: "w",
Expand All @@ -59,10 +62,11 @@ export async function generateStaticAdapter(
{ builder }: CoreOptions,
) {
const filesList = Object.entries(filesObject);
const vitePostfix = builder === BuilderEnum.VITE ? "?react" : "";

const mappedSvgImports = filesList
.map(([, values], index) => {
const svgPath = genSvgPath(relativeSvgPath, values, builder === BuilderEnum.VITE);
const svgPath = genSvgPath(relativeSvgPath, values, vitePostfix);
return `import ${values.fileName} from "${svgPath}";${filesList.length - 1 !== index ? "\n" : ""}`;
})
.join("");
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/adapter/dynamic.ts.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type SvgIconsType = {svgNames};
export type SvgComponentType = typeof import("*.svg") | null;
export type SvgComponentType = typeof import("*.svg{vitePostfix}") | null;

export const getSvgComponent = async (
name: SvgIconsType,
Expand Down

0 comments on commit 50bb06b

Please sign in to comment.