11import { readFileSync , readdirSync } from 'fs' ;
2- import { resolve as pathResolve , dirname } from 'path' ;
2+ import { resolve as pathResolve , dirname , basename } from 'path' ;
33
44/**
55 * ESBuild plugin to handle import.meta.glob for SVG files and ?raw imports
@@ -10,10 +10,8 @@ export const globSvgPlugin = () => ({
1010 setup ( build ) {
1111 // Handle ?raw imports for SVG files
1212 build . onResolve ( { filter : / \. s v g \? r a w $ / } , ( args ) => {
13- console . log ( 'Resolving SVG raw import:' , args . path ) ;
1413 const pathWithoutQuery = args . path . replace ( '?raw' , '' ) ;
1514 const resolvedPath = pathResolve ( args . resolveDir , pathWithoutQuery ) ;
16- console . log ( 'Resolved path:' , resolvedPath ) ;
1715 return {
1816 path : resolvedPath ,
1917 namespace : 'svg-raw' ,
@@ -22,10 +20,8 @@ export const globSvgPlugin = () => ({
2220
2321 // Load SVG files as raw strings
2422 build . onLoad ( { filter : / .* / , namespace : 'svg-raw' } , ( args ) => {
25- console . log ( 'Loading SVG raw file:' , args . path ) ;
2623 try {
2724 const content = readFileSync ( args . path , 'utf8' ) ;
28- console . log ( 'Successfully loaded SVG content, length:' , content . length ) ;
2925 return {
3026 contents : `export default ${ JSON . stringify ( content ) } ;` ,
3127 loader : 'js' ,
@@ -39,83 +35,85 @@ export const globSvgPlugin = () => ({
3935 }
4036 } ) ;
4137
42- // Handle the icons.ts file specifically
38+ // Helper function to read SVG files from a directory
39+ const readSvgFiles = ( dirPath ) => {
40+ try {
41+ const files = readdirSync ( dirPath ) . filter ( ( f ) => f . endsWith ( '.svg' ) ) ;
42+ return files . reduce ( ( acc , file ) => {
43+ const key = file . replace ( '.svg' , '' ) ;
44+ const content = readFileSync ( pathResolve ( dirPath , file ) , 'utf8' ) ;
45+ acc [ key ] = content ;
46+ return acc ;
47+ } , { } ) ;
48+ } catch ( err ) {
49+ console . warn ( `Could not read directory: ${ dirPath } ` ) ;
50+ return { } ;
51+ }
52+ } ;
53+
54+ // Handle the icons.ts file - only includes standard icons
55+ // (functional/distinctive are lazy-loaded via separate chunks)
4356 build . onLoad ( { filter : / i c o n s \. t s $ / } , async ( args ) => {
4457 try {
45- // Read the original file
4658 const source = readFileSync ( args . path , 'utf8' ) ;
4759
48- // Check if this file contains import.meta.glob
4960 if ( ! source . includes ( 'import.meta.glob' ) ) {
50- return null ; // Let esbuild handle it normally
61+ return null ;
62+ }
63+
64+ const fileName = basename ( args . path ) ;
65+
66+ // Handle functional-icons.ts
67+ if ( fileName === 'functional-icons.ts' ) {
68+ const functionalDir = pathResolve ( dirname ( args . path ) , 'functional' ) ;
69+ const functionalObj = readSvgFiles ( functionalDir ) ;
70+
71+ return {
72+ contents : `export const functionalIcons = ${ JSON . stringify ( functionalObj ) } ;` ,
73+ loader : 'ts' ,
74+ resolveDir : dirname ( args . path ) ,
75+ } ;
5176 }
5277
53- // Define the icon directories relative to the icons.ts file
78+ // Handle distinctive-icons.ts
79+ if ( fileName === 'distinctive-icons.ts' ) {
80+ const distinctiveDir = pathResolve ( dirname ( args . path ) , 'distinctive' ) ;
81+ const distinctiveObj = readSvgFiles ( distinctiveDir ) ;
82+
83+ return {
84+ contents : `export const distinctiveIcons = ${ JSON . stringify ( distinctiveObj ) } ;` ,
85+ loader : 'ts' ,
86+ resolveDir : dirname ( args . path ) ,
87+ } ;
88+ }
89+
90+ // Handle main icons.ts - standard icons only, with keys/types for all sets
5491 const iconsDir = pathResolve ( dirname ( args . path ) , 'icons' ) ;
5592 const functionalDir = pathResolve ( dirname ( args . path ) , 'functional' ) ;
5693 const distinctiveDir = pathResolve ( dirname ( args . path ) , 'distinctive' ) ;
5794
58- // Helper function to read SVG files from a directory
59- const readSvgFiles = ( dirPath ) => {
60- try {
61- const files = readdirSync ( dirPath ) . filter ( ( f ) =>
62- f . endsWith ( '.svg' ) ,
63- ) ;
64- return files . reduce ( ( acc , file ) => {
65- const key = file . replace ( '.svg' , '' ) ;
66- const content = readFileSync ( pathResolve ( dirPath , file ) , 'utf8' ) ;
67- acc [ key ] = content ;
68- return acc ;
69- } , { } ) ;
70- } catch ( err ) {
71- console . warn ( `Could not read directory: ${ dirPath } ` ) ;
72- return { } ;
73- }
74- } ;
75-
76- // Read all SVG files
7795 const iconsObj = readSvgFiles ( iconsDir ) ;
78- const functionalObj = readSvgFiles ( functionalDir ) ;
79- const distinctiveObj = readSvgFiles ( distinctiveDir ) ;
80-
81- // Generate the replacement code
82- const replacementCode = `/** eslint-disable spaced-comment */
83- // This file is generated by esbuild-plugin-glob-svg for production builds
84- // The original file uses Vite's import.meta.glob for development
85-
86- const formatIcons = (imports) =>
87- Object.entries(imports).reduce<Record<string, string>>(
88- (acc, [path, content]) => {
89- const key = path
90- .replace(/\\.\\/\\(icons|functional|distinctive\\)\\//, '')
91- .replace('.svg', '');
92- return { ...acc, [key]: content as string };
93- },
94- {},
95- );
96-
97- export const icons = ${ JSON . stringify ( iconsObj , null , 2 ) } ;
98- export const functionalIcons = ${ JSON . stringify ( functionalObj , null , 2 ) } ;
99- export const distinctiveIcons = ${ JSON . stringify ( distinctiveObj , null , 2 ) } ;
96+ const functionalKeys = Object . keys ( readSvgFiles ( functionalDir ) ) ;
97+ const distinctiveKeys = Object . keys ( readSvgFiles ( distinctiveDir ) ) ;
98+
99+ const replacementCode = `// Generated by esbuild-plugin-glob-svg for production builds
100+
101+ export const icons = ${ JSON . stringify ( iconsObj ) } ;
100102
101103export const iconKeys = ${ JSON . stringify ( Object . keys ( iconsObj ) ) } as (keyof typeof icons)[];
102- export const functionalIconKeys = ${ JSON . stringify ( Object . keys ( functionalObj ) ) } as (keyof typeof functionalIcons) [];
103- export const distinctiveIconKeys = ${ JSON . stringify ( Object . keys ( distinctiveObj ) ) } as (keyof typeof distinctiveIcons) [];
104+ export const functionalIconKeys = ${ JSON . stringify ( functionalKeys ) } as string [];
105+ export const distinctiveIconKeys = ${ JSON . stringify ( distinctiveKeys ) } as string [];
104106
105107export type IconName = ${
106108 Object . keys ( iconsObj )
107109 . map ( ( k ) => `"${ k } "` )
108110 . join ( ' | ' ) || 'string'
109111 } ;
110112export type FunctionalIconName = ${
111- Object . keys ( functionalObj )
112- . map ( ( k ) => `"${ k } "` )
113- . join ( ' | ' ) || 'string'
113+ functionalKeys . map ( ( k ) => `"${ k } "` ) . join ( ' | ' ) || 'string'
114114 } ;
115115export type DistinctiveIconName = ${
116- Object . keys ( distinctiveObj )
117- . map ( ( k ) => `"${ k } "` )
118- . join ( ' | ' ) || 'string'
116+ distinctiveKeys . map ( ( k ) => `"${ k } "` ) . join ( ' | ' ) || 'string'
119117 } ;
120118
121119export default icons;
@@ -128,10 +126,10 @@ export default icons;
128126 } ;
129127 } catch ( err ) {
130128 console . warn (
131- 'Could not process icons.ts with glob-svg plugin:' ,
129+ 'Could not process icons file with glob-svg plugin:' ,
132130 err . message ,
133131 ) ;
134- return null ; // Let esbuild handle it normally
132+ return null ;
135133 }
136134 } ) ;
137135 } ,
0 commit comments