aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/javascript/jsc/bindings/ZigGlobalObject.cpp2
-rw-r--r--src/js_parser/js_parser.zig25
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);