| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -112,8 +112,7 @@ const { | |||
| 112 | 112 | } = require('internal/util/types'); | |
| 113 | 113 | ||
| 114 | 114 | const asyncESM = require('internal/process/esm_loader'); | |
| 115 | - const ModuleJob = require('internal/modules/esm/module_job'); | ||
| 116 | - const { ModuleWrap, kInstantiated } = internalBinding('module_wrap'); | ||
| 115 | + const { kEvaluated } = internalBinding('module_wrap'); | ||
| 117 | 116 | const { | |
| 118 | 117 | encodedSepRegEx, | |
| 119 | 118 | packageInternalResolve | |
@@ -1101,29 +1100,13 @@ Module.prototype.load = function(filename) { | |||
| 1101 | 1100 | this.loaded = true; | |
| 1102 | 1101 | ||
| 1103 | 1102 | const ESMLoader = asyncESM.ESMLoader; | |
| 1104 | - const url = `${pathToFileURL(filename)}`; | ||
| 1105 | - const module = ESMLoader.moduleMap.get(url); | ||
| 1106 | 1103 | // Create module entry at load time to snapshot exports correctly | |
| 1107 | 1104 | const exports = this.exports; | |
| 1108 | - // Called from cjs translator | ||
| 1109 | - if (module !== undefined && module.module !== undefined) { | ||
| 1110 | - if (module.module.getStatus() >= kInstantiated) | ||
| 1111 | - module.module.setExport('default', exports); | ||
| 1112 | - } else { | ||
| 1113 | - // Preemptively cache | ||
| 1114 | - // We use a function to defer promise creation for async hooks. | ||
| 1115 | - ESMLoader.moduleMap.set( | ||
| 1116 | - url, | ||
| 1117 | - // Module job creation will start promises. | ||
| 1118 | - // We make it a function to lazily trigger those promises | ||
| 1119 | - // for async hooks compatibility. | ||
| 1120 | - () => new ModuleJob(ESMLoader, url, () => | ||
| 1121 | - new ModuleWrap(url, undefined, ['default'], function() { | ||
| 1122 | - this.setExport('default', exports); | ||
| 1123 | - }) | ||
| 1124 | - , false /* isMain */, false /* inspectBrk */) | ||
| 1125 | - ); | ||
| 1126 | - } | ||
| 1105 | + // Preemptively cache | ||
| 1106 | + if ((module?.module === undefined || | ||
| 1107 | + module.module.getStatus() < kEvaluated) && | ||
| 1108 | + !ESMLoader.cjsCache.has(this)) | ||
| 1109 | + ESMLoader.cjsCache.set(this, exports); | ||
| 1127 | 1110 | }; | |
| 1128 | 1111 | ||
| 1129 | 1112 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,7 @@ require('internal/modules/cjs/loader'); | |||
| 6 | 6 | const { | |
| 7 | 7 | FunctionPrototypeBind, | |
| 8 | 8 | ObjectSetPrototypeOf, | |
| 9 | - SafeMap, | ||
| 9 | + SafeWeakMap, | ||
| 10 | 10 | } = primordials; | |
| 11 | 11 | ||
| 12 | 12 | const { | |
@@ -47,7 +47,7 @@ class Loader { | |||
| 47 | 47 | this.moduleMap = new ModuleMap(); | |
| 48 | 48 | ||
| 49 | 49 | // Map of already-loaded CJS modules to use | |
| 50 | - this.cjsCache = new SafeMap(); | ||
| 50 | + this.cjsCache = new SafeWeakMap(); | ||
| 51 | 51 | ||
| 52 | 52 | // This hook is called before the first root module is imported. It's a | |
| 53 | 53 | // function that returns a piece of code that runs as a sloppy-mode script. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,6 +48,8 @@ const experimentalImportMetaResolve = | |||
| 48 | 48 | const translators = new SafeMap(); | |
| 49 | 49 | exports.translators = translators; | |
| 50 | 50 | ||
| 51 | + const asyncESM = require('internal/process/esm_loader'); | ||
| 52 | + | ||
| 51 | 53 | let DECODER = null; | |
| 52 | 54 | function assertBufferSource(body, allowString, hookName) { | |
| 53 | 55 | if (allowString && typeof body === 'string') { | |
@@ -80,21 +82,14 @@ function errPath(url) { | |||
| 80 | 82 | return url; | |
| 81 | 83 | } | |
| 82 | 84 | ||
| 83 | - let esmLoader; | ||
| 84 | 85 | async function importModuleDynamically(specifier, { url }) { | |
| 85 | - if (!esmLoader) { | ||
| 86 | - esmLoader = require('internal/process/esm_loader').ESMLoader; | ||
| 87 | - } | ||
| 88 | - return esmLoader.import(specifier, url); | ||
| 86 | + return asyncESM.ESMLoader.import(specifier, url); | ||
| 89 | 87 | } | |
| 90 | 88 | ||
| 91 | 89 | function createImportMetaResolve(defaultParentUrl) { | |
| 92 | 90 | return async function resolve(specifier, parentUrl = defaultParentUrl) { | |
| 93 | - if (!esmLoader) { | ||
| 94 | - esmLoader = require('internal/process/esm_loader').ESMLoader; | ||
| 95 | - } | ||
| 96 | 91 | return PromisePrototypeCatch( | |
| 97 | - esmLoader.resolve(specifier, parentUrl), | ||
| 92 | + asyncESM.ESMLoader.resolve(specifier, parentUrl), | ||
| 98 | 93 | (error) => ( | |
| 99 | 94 | error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ? | |
| 100 | 95 | error.url : PromiseReject(error)) | |
@@ -132,27 +127,18 @@ const isWindows = process.platform === 'win32'; | |||
| 132 | 127 | const winSepRegEx = /\//g; | |
| 133 | 128 | translators.set('commonjs', function commonjsStrategy(url, isMain) { | |
| 134 | 129 | debug(`Translating CJSModule ${url}`); | |
| 135 | - const pathname = internalURLModule.fileURLToPath(new URL(url)); | ||
| 136 | - const cached = this.cjsCache.get(url); | ||
| 137 | - if (cached) { | ||
| 138 | - this.cjsCache.delete(url); | ||
| 139 | - return cached; | ||
| 140 | - } | ||
| 141 | - const module = CJSModule._cache[ | ||
| 142 | - isWindows ? StringPrototypeReplace(pathname, winSepRegEx, '\\') : pathname | ||
| 143 | - ]; | ||
| 144 | - if (module && module.loaded) { | ||
| 145 | - const exports = module.exports; | ||
| 146 | - return new ModuleWrap(url, undefined, ['default'], function() { | ||
| 147 | - this.setExport('default', exports); | ||
| 148 | - }); | ||
| 149 | - } | ||
| 150 | 130 | return new ModuleWrap(url, undefined, ['default'], function() { | |
| 151 | 131 | debug(`Loading CJSModule ${url}`); | |
| 152 | - // We don't care about the return val of _load here because Module#load | ||
| 153 | - // will handle it for us by checking the loader registry and filling the | ||
| 154 | - // exports like above | ||
| 155 | - CJSModule._load(pathname, undefined, isMain); | ||
| 132 | + const pathname = internalURLModule.fileURLToPath(new URL(url)); | ||
| 133 | + let exports; | ||
| 134 | + const cachedModule = CJSModule._cache[pathname]; | ||
| 135 | + if (cachedModule && asyncESM.ESMLoader.cjsCache.has(cachedModule)) { | ||
| 136 | + exports = asyncESM.ESMLoader.cjsCache.get(cachedModule); | ||
| 137 | + asyncESM.ESMLoader.cjsCache.delete(cachedModule); | ||
| 138 | + } else { | ||
| 139 | + exports = CJSModule._load(pathname, undefined, isMain); | ||
| 140 | + } | ||
| 141 | + this.setExport('default', exports); | ||
| 156 | 142 | }); | |
| 157 | 143 | }); | |
| 158 | 144 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments