You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a Question. I would like to dynamically generate a svg file using js/ts. In attempting to do this, I created a project plugin + favicon.svg.ts, which dynamically generates svg. The problem with using the file loader is it will generate the file with a .ts extension...but I want it to generate a file with the .svg extension. Does the file loader support this behavior with some configuration? If not, could I use a text loader & somehow add the generated file into the build from the plugin?
constplugin={name: 'esmsvg',setup(build){build.onResolve({filter: /\.svg\.(js|ts)$/},async({ path, ...args})=>{// Avoid recursion in resolve() belowif(args.pluginData==='esmsvg')return// Tell esbuild to resolve the pathconstresult=awaitbuild.resolve(path,{ ...args,pluginData: 'esmsvg'})if(result.errors.length>0)return{errors: result.errors}return{path: result.path.slice(0,-3),// ".svg.js" => ".svg"pluginData: 'esmsvg:'+result.path,// Save the original path}})build.onLoad({filter: /\.svg$/},async({ pluginData })=>{// Load the original pathif(!pluginData.startsWith('esmsvg:'))returnconstpath=pluginData.slice('esmsvg:'.length)constcontents=awaitimport(path).then(mod=>mod.default())return{ contents,loader: 'file'}})},}
Use an onResolve callback to change the file extension, then use an onLoad callback to load it.
This is a Question. I would like to dynamically generate a svg file using js/ts. In attempting to do this, I created a project plugin +
favicon.svg.ts
, which dynamically generates svg. The problem with using the file loader is it will generate the file with a.ts
extension...but I want it to generate a file with the.svg
extension. Does the file loader support this behavior with some configuration? If not, could I use a text loader & somehow add the generated file into the build from the plugin?Here is the plugin using the file loader:
Here is
favicon.svg.ts
:The text was updated successfully, but these errors were encountered: