๐ You can help the author become a full-time open-source maintainer by sponsoring him on GitHub.
Projects like Vite need to load config files provided by the user, but you can't do it with just require()
because it's not necessarily a CommonJS module, it could also be a .mjs
or even be written in TypeScript, and that's where the bundle-require
package comes in, it loads the config file regardless what module format it is.
This is implemented by pre-bundling the file with esbuild and then loading it with import()
or require()
.
npm i bundle-require esbuild
esbuild
is a peer dependency.
import { bundleRequire } from 'bundle-require'
const { mod } = await bundleRequire({
filepath: './project/vite.config.ts',
})
interface Options {
/**
* The filepath to bundle and require
*/
filepath: string
/**
* The `require` function that is used to load the output file
* Default to the global `require` function
* This function can be asynchronous, i.e. returns a Promise
*/
require?: (outfile: string) => any
/**
* esbuild options
*/
esbuildOptions?: EsbuildOptions
}
interface Result {
/** Loaded module */
mod: any
/** Files imported by the module */
dependencies: string[]
}
MIT ยฉ EGOIST