Common utils for esbuild plugins
PoC
import {
getFilesList,
getOutputFiles,
readFiles,
transformFile,
writeFile,
writeFiles,
} from 'esbuild-plugin-utils'
Scans directory and returns a list of files. Pass dir and optional recursive flag
const list = await getFilesList(process.cwd(), true) // string[]
Shortcut to convert OutputFile[]
(see esbuild.BuildResult) to TFileEntry[]
if defined or to read files from outdir
:
const entries = await getOutputFiles(buildResult.outputFiles, 'build') // TFileEntry[]
Reads files from a given directory and returns a list of TFileEntry
objects
const entries = await readFiles(['file1', 'file2']) // TFileEntry[]
Persists a given TFileEntry
to disc.
await writeFile({ contents: 'content', path: '/foo/bar.txt' })
Applies transformation hooks to a given TFileEntry
object.
const entry = await transformFile(
{
contents: 'content',
path: '/foo/bar.txt'
},
[{
pattern: /./,
transform: (contents) => contents.toUpperCase(),
rename: (path) => path.replace('bar', 'baz'),
}]
)
Resolves entry points paths to absolute paths.
const paths = resolveEntryPointsPaths(['file1', 'file2'], process.cwd()) // string[]
Formats a comma-separated list line by line.
const list = renderList(['a', 'b'], ' ')
/**
a,
b
*/
Splits script contents into layout parts: header (shebang, use strict directive) and body.
const { header, body, lines } = parseContentsLayout(`
"use strict";
console.log("Hello")
`)
// header: "use strict";
// body: console.log("Hello")\n
// lines: ['"use strict";', 'console.log("Hello")', '']