esbuild-plugin-utils
v0.1.0
Published
Common utils for esbuild plugins
Downloads
2,182
Readme
esbuild-plugin-utils
Common utils for esbuild plugins
Status
PoC
Usage
import {
getFilesList,
getOutputFiles,
readFiles,
transformFile,
writeFile,
writeFiles,
} from 'esbuild-plugin-utils'
getFilesList
Scans directory and returns a list of files. Pass dir and optional recursive flag
const list = await getFilesList(process.cwd(), true) // string[]
getOutputFiles
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[]
readFiles
Reads files from a given directory and returns a list of TFileEntry
objects
const entries = await readFiles(['file1', 'file2']) // TFileEntry[]
writeFile
Persists a given TFileEntry
to disc.
await writeFile({ contents: 'content', path: '/foo/bar.txt' })
transformFile
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'),
}]
)
resolveEntryPointsPaths
Resolves entry points paths to absolute paths.
const paths = resolveEntryPointsPaths(['file1', 'file2'], process.cwd()) // string[]
renderList
Formats a comma-separated list line by line.
const list = renderList(['a', 'b'], ' ')
/**
a,
b
*/
parseContentsLayout
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")', '']