Optional
config: anyimport type { EsbuildPlugin, EsbuildPluginBuild, OnLoadArgs, OnResolveArgs } from "./typedefs.ts"
import { PLUGIN_NAMESPACE } from "./typedefs.ts"
const THIS_plugins_namespace = PLUGIN_NAMESPACE.RESOLVER_PIPELINE // == "oazmi-resolver-pipeline"
const myShinyPlugin: EsbuildPlugin = {
name: "my-shiny-plugin",
setup: (build: EsbuildPluginBuild) => {
const everything_shiny_filter = /\.shiny$/
build.onResolve({ filter: everything_shiny_filter }, async (args: OnResolveArgs) => {
const { path, ...rest_args } = args
const result = await build.resolve(path, { ...rest_args, namespace: THIS_plugins_namespace })
if (result) {
const {
path: absolute_path,
pluginData: useful_plugindata,
// there will be a temporary namespace that you MUST drop.
// the reason why it exists is because esbuild forbids the use of the default namespace,
// unless the path is explicitly an absolute filesystem path.
// so paths that begin with "http://", "file://", "jsr:", etc... are all invalidated by esbuild if `namespace = ""`.
namespace: _drop_the_temp_namespace,
...rest_result
} = result
return {
path: absolute_path,
pluginData: useful_plugindata,
namespace: "my-shiny-namespace",
...rest_result,
}
}
})
build.onLoad({ filter: RegExp(".*"), namespace: "my-shiny-namespace" }, async (args: OnLoadArgs) => {
const { path, pluginData } = args
const contents = "" // load your shiny stuff here
return { contents, loader: "ts", pluginData }
})
}
}
this is a 4-in-1 namespaced-plugin that assists in resolving esbuild paths, based on
pluginData
, esbuild's native-resolver, and relative path resolver.for details on what gets resolved an how, refer to the documentation comments of this submodule ("plugins/resolvers").
the way it is intended to be used is by being called indirectly via
build.resolve
, after specifying the correct namespace of this plugin.