Page MenuHomeSealhub

rewrite-asset-imports.ts
No OneTemporary

rewrite-asset-imports.ts

import { promises as fs } from "fs";
import { extname } from "node:path";
import { embeddable_file_extensions } from "../embeddable-file-extensions.js";
import { removeNamedImports } from "../../remove-named-imports.js";
import { PluginBuild } from "esbuild";
export const rewrite_asset_imports_plugin = {
name: "sealgen-rewrite-asset-imports",
setup(build: PluginBuild) {
build.onLoad({ filter: /\.tsx?$/ }, async (args) => {
let contents = await fs.readFile(args.path, "utf8");
for (const ext of embeddable_file_extensions) {
const regex_assets = new RegExp(
`^import (\\w+|{[^}]+}) from "([^"]+.${ext})";`,
"gm"
);
contents = contents.replaceAll(regex_assets, (line) => {
return line.replace(`.${ext}"`, '.js"');
});
}
contents = removeNamedImports(contents) as string;
return {
contents,
loader: extname(args.path) === ".tsx" ? "tsx" : "ts",
};
});
},
};

File Metadata

Mime Type
text/x-java
Expires
Tue, Dec 24, 14:02 (21 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557142
Default Alt Text
rewrite-asset-imports.ts (926 B)

Event Timeline