What is bundle-require?
The bundle-require npm package allows you to require modules that are bundled using various bundlers like Webpack, Rollup, or esbuild. It simplifies the process of loading and executing bundled code in Node.js environments.
What are bundle-require's main functionalities?
Require Bundled Code
This feature allows you to require a bundled JavaScript file using a specified bundler. In this example, the bundled file is loaded using Webpack.
const { bundleRequire } = require('bundle-require');
(async () => {
const result = await bundleRequire({
filepath: './path/to/your/bundled/file.js',
bundler: 'webpack'
});
console.log(result);
})();
Support for Multiple Bundlers
This feature demonstrates the ability to use different bundlers like Rollup. The bundler option can be changed to 'webpack', 'rollup', or 'esbuild' based on your needs.
const { bundleRequire } = require('bundle-require');
(async () => {
const result = await bundleRequire({
filepath: './path/to/your/bundled/file.js',
bundler: 'rollup'
});
console.log(result);
})();
Dynamic Import of Bundled Code
This feature shows how to dynamically import a bundled module using esbuild. The default export of the bundled module is accessed and logged.
const { bundleRequire } = require('bundle-require');
(async () => {
const result = await bundleRequire({
filepath: './path/to/your/bundled/file.js',
bundler: 'esbuild'
});
const { default: module } = result;
console.log(module);
})();
Other packages similar to bundle-require
require-from-string
The require-from-string package allows you to require a module from a string of code. Unlike bundle-require, it does not support bundlers directly but can be used to load code dynamically from a string.
esm
The esm package enables ES module support in Node.js. While it does not directly handle bundling, it allows you to use ES modules seamlessly, which can be useful in conjunction with bundlers.
import-fresh
The import-fresh package allows you to import a module while bypassing the require cache. This can be useful for dynamically loading modules, but it does not handle bundling like bundle-require.
💛 You can help the author become a full-time open-source maintainer by sponsoring him on GitHub.
bundle-require
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: :memo: Minimalistic Vue-powered static site generator.
License
MIT © EGOIST