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 aa11c24
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))

Check failure on line 76 in lib/internal/modules/esm/initialize_import_meta.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
},
get __filename() {
filePath ??= fileURLToPath(url)

Check failure on line 79 in lib/internal/modules/esm/initialize_import_meta.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
dirPath ??= dirname(filePath)

Check failure on line 80 in lib/internal/modules/esm/initialize_import_meta.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
return filePath

Check failure on line 81 in lib/internal/modules/esm/initialize_import_meta.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
}

Check failure on line 82 in lib/internal/modules/esm/initialize_import_meta.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
};
}

Expand Down

0 comments on commit aa11c24

Please sign in to comment.