Common utils for esbuild plugins
npm install esbuild-plugin-utils

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