Tried importing svgs in an esbuild plugin

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 22:41:31 -07:00
parent 0adde063d9
commit 16fb0dab2e

View File

@ -13,11 +13,79 @@ const colors = {
yellow: '\x1b[33m', yellow: '\x1b[33m',
} }
const regex = new RegExp('mapsheets\..+\.svg'); let version;
const importSvg = {
name: 'importSvg',
setup(build) {
const regex = new RegExp(/\.svg$/);
build.onResolve({ filter: /\*\.svg$/ }, args => {
console.log('onresolve', args);
return {
path: path.join('public', 'assets', args.path),
namespace: 'svg-stub'
}
});
build.onLoad({ filter: /\.svg$/, namespace: 'svg-stub' }, async (args) => {
console.log('onload', args);
const svgs = fs.readdirSync(path.resolve(path.dirname(args.path))).filter(fn => regex.test(fn));;
console.log(svgs);
console.log('resolved path', path.join(path.dirname(args.path), 'mapsheets.svg'));
console.log('cwd', process.cwd());
// const contents = `import mapsheets from ./${path.join(path.dirname(args.path), 'mapsheets.svg')};
// console.log('mapsheets', mapsheets);
// export default mapsheets;`;
// const contents = `
// import svg from '/usr/src/app/public/assets/images/scenario-side_show.svg';
// export default svg;
// `;
// const contents = `
// import svg from '/usr/src/app/public/assets/images/scenario-side_show.svg';
// export default svg;
// `;
// const contents = `
// export { default as scenario_sideShow } from '/usr/src/app/public/assets/images/scenario-side_show.svg';
// export { default as mapsheets } from '/usr/src/app/public/assets/images/mapsheets.svg';
// `;
const contents = `
// export { default as countorLines } from './contour-lines.svg';
export { default as mapsheets } from './mapsheets.svg';
`;
console.log('resolveDir', path.dirname(args.path));
console.log('contents', contents);
return {
contents: contents,
resolveDir: path.dirname(args.path) //'./public/assets/images'
}
});
// build.onLoad({ filter: /.*/, namespace: 'svg-stub' }, async (args) => ({
// contents: `import svg from ${JSON.stringify(args.path)}
// export default (imports) =>
// WebAssembly.instantiate(wasm, imports).then(
// result => result.instance.exports)`,
// }));
}
};
const svgUseCacheBust = { const svgUseCacheBust = {
name: 'svgUseCacheBust', name: 'svgUseCacheBust',
setup(build) { setup(build) {
const regex = new RegExp('mapsheets\..+\.svg');
build.onStart(() => { build.onStart(() => {
console.log("BUILD START"); console.log("BUILD START");
@ -38,7 +106,10 @@ const ctx = await esbuild.context({
entryPoints: ['src/*.js'], entryPoints: ['src/*.js'],
bundle: true, bundle: true,
outdir: 'build', outdir: 'build',
plugins: [svgUseCacheBust], plugins: [svgUseCacheBust, importSvg],
loader: {
'.svg': 'file'
},
}); });
await ctx.watch(); await ctx.watch();
@ -92,6 +163,8 @@ http.createServer((req, res) => {
path.join(dir, url.pathname === '/' ? 'index.html' : url.pathname) path.join(dir, url.pathname === '/' ? 'index.html' : url.pathname)
); );
console.log('filepath', filePath);
for (const k in res.headers) { for (const k in res.headers) {
res.setHeader(k, res.headers[k]); res.setHeader(k, res.headers[k]);
} }