Skip to content

Commit

Permalink
Lazily evaluate the properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Jul 12, 2023
1 parent 71f4e6b commit d864bbc
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/internal/modules/esm/initialize_import_meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,20 @@ function resolveModuleMeta(url) {
return { __dirname: undefined, __filename: undefined };
}

const filePath = fileURLToPath(url);
// We return an object with getters to reduce memory usage in modules that
// do not use these properties, i.e. most modules don't need the values
// to be resolved until the first time the property is accessed.
let filePath;
let dirPath;
return {
__dirname: dirname(filePath),
__filename: filePath,
get __dirname() {
return dirname(filePath ??= fileURLToPath(url));
},
get __filename() {
filePath ??= fileURLToPath(url);
dirPath ??= dirname(filePath);
return filePath;
},
};
}

Expand Down

0 comments on commit d864bbc

Please sign in to comment.