aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/module.exports.js
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-06-24 06:59:47 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-06-24 06:59:47 -0700
commit7bb75f55530e52447b9c68bc5b0908bf734ba184 (patch)
treee30b431d6f257824f2821c56a2ec01136938cc5e /src/bun.js/module.exports.js
parent6d6a89780b10816de38c465b1e6bb583979feacd (diff)
downloadbun-7bb75f55530e52447b9c68bc5b0908bf734ba184.tar.gz
bun-7bb75f55530e52447b9c68bc5b0908bf734ba184.tar.zst
bun-7bb75f55530e52447b9c68bc5b0908bf734ba184.zip
Add dynamic require support
Diffstat (limited to 'src/bun.js/module.exports.js')
-rw-r--r--src/bun.js/module.exports.js24
1 files changed, 7 insertions, 17 deletions
diff --git a/src/bun.js/module.exports.js b/src/bun.js/module.exports.js
index f0817a059..6873748e2 100644
--- a/src/bun.js/module.exports.js
+++ b/src/bun.js/module.exports.js
@@ -12,16 +12,6 @@ resolve.paths = () => [];
function require(pathString) {
// this refers to an ImportMeta instance
const resolved = this.resolveSync(pathString);
- if (
- !resolved.endsWith(".node") &&
- !resolved.endsWith(".json") &&
- !resolved.endsWith(".toml")
- ) {
- throw new Error(
- "Dynamic require() in Bun.js currently only supports .node, .json, and .toml files.\n\tConsider using ESM import() instead."
- );
- }
-
return this.require(resolved);
}
@@ -49,13 +39,13 @@ export function createRequire(filename) {
// but we don't support windows yet
process.platform !== "win32" ? "/" : "\\"
);
- var customImportMeta = {
- ...import.meta,
- path: filenameString,
- file:
- lastSlash > -1 ? filenameString.substring(lastSlash + 1) : filenameString,
- dir: lastSlash > -1 ? filenameString.substring(0, lastSlash) : "",
- };
+
+ var customImportMeta = Object.create(import.meta);
+ customImportMeta.path = filenameString;
+ customImportMeta.file =
+ lastSlash > -1 ? filenameString.substring(lastSlash + 1) : filenameString;
+ customImportMeta.dir =
+ lastSlash > -1 ? filenameString.substring(0, lastSlash) : "";
if (isURL) {
customImportMeta.url = filename;