Public commit
This commit is contained in:
32
scripts/plugins/esbuild-string-import.ts
Normal file
32
scripts/plugins/esbuild-string-import.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { dirname } from "path"
|
||||
import { promises as fs } from "fs"
|
||||
import * as esbuild from "esbuild"
|
||||
|
||||
export const stringImport = (): esbuild.Plugin => ({
|
||||
name: "stringImport",
|
||||
setup(build) {
|
||||
build.onResolve({ filter: /\?string$/ }, async ({ path, importer }) => {
|
||||
const resolved = await build.resolve(path.replace(/\?string$/, ""), {
|
||||
kind: "import-statement",
|
||||
importer,
|
||||
resolveDir: dirname(importer),
|
||||
})
|
||||
|
||||
return {
|
||||
namespace: stringImport.name,
|
||||
path: resolved.path,
|
||||
}
|
||||
})
|
||||
|
||||
build.onLoad({ filter: /.*/, namespace: stringImport.name }, async ({ path }) => {
|
||||
let code = await fs.readFile(path, "utf-8")
|
||||
if (build.initialOptions.minify && path.endsWith(".css")) {
|
||||
;({ code } = await esbuild.transform(code, { loader: "css", minify: true }))
|
||||
}
|
||||
return {
|
||||
contents: code,
|
||||
loader: "text",
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
Reference in New Issue
Block a user