33 * Licensed under the MIT License. See LICENSE file in the project root for license information.
44 *-----------------------------------------------------------------------------------------------*/
55
6+ import { transform } from '@svgr/core' ;
67import * as esbuild from 'esbuild' ;
7- import svgr from 'esbuild-plugin-svgr' ;
88import { sassPlugin } from 'esbuild-sass-plugin' ;
9- import * as fs from 'fs/promises' ;
109import * as glob from 'glob' ;
1110import { createRequire } from 'module' ;
11+ import { cp , mkdir , readFile , stat } from 'node:fs/promises' ;
1212import * as path from 'path' ;
1313import { fileURLToPath } from 'url' ;
1414
@@ -115,6 +115,57 @@ const nativeNodeModulesPlugin = {
115115 } ,
116116} ;
117117
118+ // The following 'svgrPlugin' const have been stolen from 'esbuild-plugin-scgr' due to lack of support of the latest 'esbuild' versions
119+ // by the plugin itself.
120+ // See: https://github.com/kazijawad/esbuild-plugin-svgr/issues/20
121+ //
122+ const svgrPlugin = ( options = {
123+ markExternal : true
124+ } ) => ( {
125+ name : 'svgr' ,
126+ setup ( build ) {
127+ build . onResolve ( { filter : / \. s v g $ / } , async ( args ) => {
128+ switch ( args . kind ) {
129+ case 'import-statement' :
130+ case 'require-call' :
131+ case 'dynamic-import' :
132+ case 'require-resolve' :
133+ return
134+ default :
135+ if ( options . markExternal ) {
136+ return {
137+ external : true ,
138+ }
139+ }
140+ }
141+ } )
142+
143+ build . onLoad ( { filter : / \. s v g $ / } , async ( args ) => {
144+ const svg = await readFile ( args . path , { encoding : 'utf8' } )
145+
146+ if ( options . plugins && ! options . plugins . includes ( '@svgr/plugin-jsx' ) ) {
147+ options . plugins . push ( '@svgr/plugin-jsx' )
148+ } else if ( ! options . plugins ) {
149+ options . plugins = [ '@svgr/plugin-jsx' ]
150+ }
151+
152+ const contents = await transform ( svg , { ...options } , { filePath : args . path } )
153+
154+ if ( args . suffix === '?url' ) {
155+ return {
156+ contents : args . path ,
157+ loader : 'text' ,
158+ }
159+ }
160+
161+ return {
162+ contents,
163+ loader : options . typescript ? 'tsx' : 'jsx' ,
164+ }
165+ } )
166+ } ,
167+ } ) ;
168+
118169const baseConfig = {
119170 bundle : true ,
120171 target : 'chrome108' ,
@@ -149,9 +200,7 @@ if (production) {
149200 } ,
150201 plugins : [
151202 sassPlugin ( ) ,
152- svgr ( {
153- plugins : [ '@svgr/plugin-jsx' ]
154- } ) ,
203+ svgrPlugin ( { plugins : [ '@svgr/plugin-jsx' ] } ) ,
155204 esbuildProblemMatcherPlugin // this one is to be added to the end of plugins array
156205 ]
157206 } ;
@@ -168,9 +217,7 @@ if (production) {
168217 } ,
169218 plugins : [
170219 sassPlugin ( ) ,
171- svgr ( {
172- plugins : [ '@svgr/plugin-jsx' ]
173- } ) ,
220+ svgrPlugin ( { plugins : [ '@svgr/plugin-jsx' ] } ) ,
174221 esbuildProblemMatcherPlugin // this one is to be added to the end of plugins array
175222 ]
176223 } ;
@@ -179,7 +226,7 @@ if (production) {
179226
180227async function dirExists ( path ) {
181228 try {
182- if ( ( await fs . stat ( path ) ) . isDirectory ( ) ) {
229+ if ( ( await stat ( path ) ) . isDirectory ( ) ) {
183230 return true ;
184231 }
185232 } catch {
@@ -193,10 +240,10 @@ await Promise.all([
193240 ...webviews . map ( async webview => {
194241 const targetDir = path . join ( __dirname , `${ outDir } /${ webview } /app` ) ;
195242 if ( ! dirExists ( targetDir ) ) {
196- await fs . mkdir ( targetDir , { recursive : true , mode : 0o750 } ) ;
243+ await mkdir ( targetDir , { recursive : true , mode : 0o750 } ) ;
197244 }
198245 glob . sync ( [ `${ srcDir } /webview/${ webview } /app/index.html` ] ) . map ( async srcFile => {
199- await fs . cp ( path . join ( __dirname , srcFile ) , path . join ( targetDir , `${ path . basename ( srcFile ) } ` ) )
246+ await cp ( path . join ( __dirname , srcFile ) , path . join ( targetDir , `${ path . basename ( srcFile ) } ` ) )
200247 } ) ;
201248 } )
202- ] ) ;
249+ ] ) ;
0 commit comments