MCPcopy
hub / github.com/webpack/webpack / require

Method require

test/harness/runner/index.js:408–457  ·  view source on GitHub ↗

* @param {string} currentDirectory current directory * @param {string | string[]} module module * @param {RequireContext=} context context * @param {Record<string, string>=} importAttributes import attributes * @returns {EXPECTED_ANY} require result

(
		currentDirectory,
		module,
		context = /** @type {RequireContext} */ ({}),
		importAttributes = {}
	)

Source from the content-addressed store, hash-verified

406 * @returns {EXPECTED_ANY} require result
407 */
408 require(
409 currentDirectory,
410 module,
411 context = /** @type {RequireContext} */ ({}),
412 importAttributes = {}
413 ) {
414 if (
415 /** @type {EXPECTED_ANY} */ (this.testConfig).modules &&
416 /** @type {string} */ (module) in
417 /** @type {EXPECTED_ANY} */ (this.testConfig).modules
418 ) {
419 return /** @type {EXPECTED_ANY} */ (this.testConfig).modules[
420 /** @type {string} */ (module)
421 ];
422 }
423 if (this.testConfig.resolveModule) {
424 module = this.testConfig.resolveModule(
425 module,
426 this.testMeta.round || 0,
427 this.webpackOptions
428 );
429 }
430 const moduleInfo = this._resolveModule(currentDirectory, module);
431 if (!moduleInfo) {
432 // node v12.2.0+ has Module.createRequire
433 const rawRequire = Module.createRequire
434 ? Module.createRequire(currentDirectory)
435 : require;
436 const mod = /** @type {string} */ (module);
437 return rawRequire(mod.startsWith("node:") ? mod.slice(5) : mod);
438 }
439 const { modulePath } = moduleInfo;
440 if (importAttributes && importAttributes.type === "bytes") {
441 return this._moduleRunners.bytes(moduleInfo, context);
442 }
443 if (
444 modulePath.endsWith(".mjs") &&
445 this.webpackOptions.experiments &&
446 this.webpackOptions.experiments.outputModule
447 ) {
448 return this._moduleRunners.esm(moduleInfo, context);
449 }
450 if (modulePath.endsWith(".json")) {
451 return this._moduleRunners.json(moduleInfo, context);
452 }
453 if (modulePath.endsWith(".css")) {
454 return this._moduleRunners.css(moduleInfo, context);
455 }
456 return this._moduleRunners.cjs(moduleInfo, context);
457 }
458
459 /**
460 * @returns {(moduleInfo: ModuleInfo, context: RequireContext) => EXPECTED_ANY} cjs runner

Callers 9

runBundlesMethod · 0.95
createCjsRunnerMethod · 0.95
getModuleInstanceMethod · 0.95
linkMethod · 0.95
setupEnvMethod · 0.95
onAcceptedFunction · 0.80
index.jsFile · 0.80

Calls 3

_resolveModuleMethod · 0.95
sliceMethod · 0.80
cssMethod · 0.80

Tested by

no test coverage detected