Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
esm: avoid import.meta setup costs for unused properties
  • Loading branch information
JakobJingleheimer committed Feb 11, 2025
commit b3575a09b6323a2edf6091f43a9f50c091956940
26 changes: 23 additions & 3 deletions lib/internal/modules/esm/initialize_import_meta.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const {
ObjectDefineProperties,
ObjectDefineProperty,
StringPrototypeStartsWith,
} = primordials;

Expand Down Expand Up @@ -60,9 +62,27 @@ function initializeImportMeta(meta, context, loader) {
if (StringPrototypeStartsWith(url, 'file:') === true) {
// These only make sense for locally loaded modules,
// i.e. network modules are not supported.
const filePath = fileURLToPath(url);
meta.dirname = dirname(filePath);
meta.filename = filePath;

// Avoid paying the cost to derive these unless they're actually accessed.
ObjectDefineProperties(meta, {
dirname: {
Comment thread
JakobJingleheimer marked this conversation as resolved.
get() {
const value = dirname(this.filename);
ObjectDefineProperty(this, 'dirname', { __proto__: null, value });
return value;
},
__proto__: null,
},
filePath: {
get() {
const value = fileURLToPath(url);
ObjectDefineProperty(this, 'filename', { __proto__: null, value });
return value;
},
__proto__: null,
},
__proto__: null,
});
}

if (!loader || loader.allowImportMetaResolve) {
Expand Down