diff options
author | 2022-05-11 02:57:51 -0700 | |
---|---|---|
committer | 2022-05-11 02:57:51 -0700 | |
commit | 7d1eced8c6d76a64f505b20473ba65d4c80e10cc (patch) | |
tree | a2d40e15ce0f7096404658c73a58e0b51ea51237 | |
parent | 123267685f57968e3515841a8c2d6991c44dcfa8 (diff) | |
download | bun-7d1eced8c6d76a64f505b20473ba65d4c80e10cc.tar.gz bun-7d1eced8c6d76a64f505b20473ba65d4c80e10cc.tar.zst bun-7d1eced8c6d76a64f505b20473ba65d4c80e10cc.zip |
[bun.js] eagerly convert to import.meta.require
-rw-r--r-- | src/javascript/jsc/bindings/ZigGlobalObject.cpp | 2 | ||||
-rw-r--r-- | src/js_parser/js_parser.zig | 25 |
2 files changed, 24 insertions, 3 deletions
diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp index a3c84bcfc..45f4533c2 100644 --- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp +++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp @@ -1173,7 +1173,7 @@ JSC::JSObject* GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje auto index = view.reverseFind('/', view.length()); if (index != WTF::notFound) { metaProperties->putDirect(vm, clientData->builtinNames().dirPublicName(), - JSC::jsSubstring(globalObject, keyString, 0, index + 1)); + JSC::jsSubstring(globalObject, keyString, 0, index)); metaProperties->putDirect( vm, clientData->builtinNames().filePublicName(), JSC::jsSubstring(globalObject, keyString, index + 1, keyString->length() - index - 1)); diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index 6189f9f7c..f8ad08009 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -3614,13 +3614,34 @@ fn NewParser_( const pathname = str.string(p.allocator) catch unreachable; + // When we know that we support dynamically requiring this file type + // we can avoid eager loading it + // instead, we can just use the require() function directly. if (p.options.features.dynamic_require and !p.options.enable_bundling and (strings.endsWithComptime(pathname, ".json") or - strings.endsWithComptime(pathname, ".toml") or + // strings.endsWithComptime(pathname, ".toml") or strings.endsWithComptime(pathname, ".node"))) { - return arg; + p.ignoreUsage(p.require_ref); + var args = p.allocator.alloc(Expr, 1) catch unreachable; + args[0] = arg; + + return p.e( + E.Call{ + .target = p.e( + E.Dot{ + .target = p.e(E.ImportMeta{}, arg.loc), + .name = "require", + .name_loc = arg.loc, + }, + arg.loc, + ), + .args = js_ast.ExprNodeList.init(args), + .close_paren_loc = arg.loc, + }, + arg.loc, + ); } const import_record_index = p.addImportRecord(.require, arg.loc, pathname); |