Skip to content

bundle and `require` a file, the same way Vite loads `vite.config.ts`

License

Notifications You must be signed in to change notification settings

egoist/bundle-require

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8ca47fe · Dec 22, 2024

History

66 Commits
Apr 22, 2024
Dec 22, 2024
Dec 22, 2024
Nov 25, 2021
Oct 9, 2021
Oct 9, 2021
Feb 11, 2023
Jun 1, 2024
Dec 22, 2024
Jan 1, 2022
Nov 25, 2021

Repository files navigation

💛 You can help the author become a full-time open-source maintainer by sponsoring him on GitHub.


bundle-require

npm version npm downloads jsDocs.io

Use Case

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.

How it works

  • Bundle your file with esbuild, node_modules are excluded because it's problematic to try to bundle it
    • __filename, __dirname and import.meta.url are replaced with source file's value instead of the one from the temporary output file
  • Output file in esm format if possible (for .ts, .js input files)
  • Load output file with import() if possible
  • Return the loaded module and its dependencies (imported files)

Install

npm i bundle-require esbuild

esbuild is a peer dependency.

Usage

import { bundleRequire } from 'bundle-require'

const { mod } = await bundleRequire({
  filepath: './project/vite.config.ts',
})

API

https://www.jsdocs.io/package/bundle-require

Projects Using bundle-require

Projects that use bundle-require:

  • VuePress: 📝 Minimalistic Vue-powered static site generator.

Sponsors

sponsors

License

MIT © EGOIST