open-codetree / esbuild /plugins /unpkg-path-plugin.ts
matt HOFFNER
init
3c3f089
raw
history blame contribute delete
883 Bytes
import * as esbuild from "esbuild-wasm";
export const unpkgPathPlugin = (): esbuild.Plugin => {
return {
name: "unpkg-path-plugin",
setup(build: esbuild.PluginBuild) {
//
build.onResolve({ filter: /.*/ }, (args) => {
if (args.kind === "entry-point") {
return { path: args.path, namespace: "a" };
}
});
//match relative path in a module "./" or "../"
build.onResolve({ filter: /^\.+\// }, (args: esbuild.OnResolveArgs) => {
return {
namespace: "a",
path: new URL(args.path, `https://unpkg.com${args.resolveDir}/`).href,
};
});
//match main file in a module
build.onResolve({ filter: /.*/ }, async (args: esbuild.OnResolveArgs) => {
return {
namespace: "a",
path: `https://unpkg.com/${args.path}`,
};
});
},
};
};