MCPcopy Index your code
hub / github.com/nodejs/node / createCJSModuleWrap

Function createCJSModuleWrap

lib/internal/modules/esm/translators.js:204–276  ·  view source on GitHub ↗

* Creates a ModuleWrap object for a CommonJS module. * @param {string} url - The URL of the module. * @param {import('./loader').TranslateContext} translateContext Context for the translator * @param {string|undefined} parentURL URL of the module initiating the module loading for the first time.

(url, translateContext, parentURL)

Source from the content-addressed store, hash-verified

202 * @returns {ModuleWrap} The ModuleWrap object for the CommonJS module.
203 */
204function createCJSModuleWrap(url, translateContext, parentURL) {
205 debug(`Translating CJSModule ${url}`, translateContext);
206
207 const { format: sourceFormat } = translateContext;
208 let { source } = translateContext;
209 const isMain = (parentURL === undefined);
210 const filename = urlToFilename(url);
211 try {
212 // In case the source was not provided by the `load` step, we need fetch it now.
213 source = stringify(source ?? getSourceSync(new URL(url)).source);
214 } catch {
215 // Continue regardless of error.
216 }
217
218 const { exportNames, module } = cjsPreparseModuleExports(filename, source, sourceFormat);
219 cjsCache.set(url, module);
220
221 const wrapperNames = [...exportNames];
222 if (!exportNames.has('default')) {
223 ArrayPrototypePush(wrapperNames, 'default');
224 }
225 if (!exportNames.has('module.exports')) {
226 ArrayPrototypePush(wrapperNames, 'module.exports');
227 }
228
229 if (isMain) {
230 setOwnProperty(process, 'mainModule', module);
231 }
232
233 return new ModuleWrap(url, undefined, wrapperNames, function() {
234 debug(`Loading CJSModule ${url}`);
235
236 if (!module.loaded) {
237 // For backward-compatibility, it's possible for async hooks to return a nullish value for
238 // CJS source associated with a `file:` URL - that usually means the source is not
239 // customized (is loaded by default load) or the hook author wants it to be reloaded
240 // through CJS routine. In this case, the source is obtained by calling the
241 // Module._load().
242 if (translateContext.translatorKey === 'commonjs-sync' ||
243 translateContext.isSourceLoadedSynchronously ||
244 translateContext.source == null) {
245 loadCJSModuleWithModuleLoad(module, source, url, filename, !!isMain, translateContext);
246 } else { // CommonJS with source customized by async hooks
247 // This may be eventually removed when module.register() reaches end-of-life.
248 loadCJSModuleWithSpecialRequire(module, source, url, filename, !!isMain, translateContext);
249 }
250 }
251
252 let exports;
253 if (module[kModuleExport] !== undefined) {
254 exports = module[kModuleExport];
255 module[kModuleExport] = undefined;
256 } else {
257 ({ exports } = module);
258 }
259 for (const exportName of exportNames) {
260 if (exportName === 'default' || exportName === 'module.exports' ||
261 !ObjectPrototypeHasOwnProperty(exports, exportName)) {

Callers 1

translators.jsFile · 0.85

Calls 11

urlToFilenameFunction · 0.85
getSourceSyncFunction · 0.85
cjsPreparseModuleExportsFunction · 0.85
setOwnPropertyFunction · 0.85
setExportMethod · 0.80
hasMethod · 0.65
debugFunction · 0.50
stringifyFunction · 0.50
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…