MCPcopy
hub / github.com/webpack/webpack / createCjsRunner

Method createCjsRunner

test/harness/runner/index.js:462–527  ·  view source on GitHub ↗

* @returns {(moduleInfo: ModuleInfo, context: RequireContext) => EXPECTED_ANY} cjs runner

()

Source from the content-addressed store, hash-verified

460 * @returns {(moduleInfo: ModuleInfo, context: RequireContext) => EXPECTED_ANY} cjs runner
461 */
462 createCjsRunner() {
463 const requireCache = Object.create(null);
464 return (moduleInfo, _context) => {
465 const { modulePath, subPath, content } = moduleInfo;
466 let _content = content;
467 if (modulePath in requireCache) {
468 return requireCache[modulePath].exports;
469 }
470 const mod = {
471 exports: {},
472 webpackTestSuiteModule: true
473 };
474 requireCache[modulePath] = mod;
475 const moduleScope = {
476 ...this._moduleScope,
477 require: Object.assign(
478 this.require.bind(this, path.dirname(modulePath)),
479 this.require
480 ),
481 importScripts: (/** @type {string} */ url) => {
482 expect(url).toMatch(/^https:\/\/test\.cases\/path\//);
483 this.require(this.outputDirectory, urlToRelativePath(url));
484 },
485 module: mod,
486 exports: mod.exports,
487 __dirname: path.dirname(modulePath),
488 __filename: modulePath,
489 _globalAssign: { expect, it: this._moduleScope.it }
490 };
491 // Call again because some tests rely on `scope.module`
492 if (this.testConfig.moduleScope) {
493 this.testConfig.moduleScope(
494 moduleScope,
495 this.webpackOptions,
496 this.target
497 );
498 }
499
500 const args = Object.keys(moduleScope);
501 const argValues = args.map((arg) => moduleScope[arg]);
502
503 if (!this._runInNewContext) {
504 _content = `Object.assign(global, _globalAssign); ${content}`;
505 }
506 const code = `(function(${args.join(", ")}) {${_content}\n})`;
507
508 const fn = this._runInNewContext
509 ? vm.runInNewContext(code, this._globalContext, modulePath)
510 : vm.runInThisContext(code, modulePath);
511 const call = () => {
512 fn.call(
513 this.testConfig.nonEsmThis
514 ? this.testConfig.nonEsmThis(module)
515 : mod.exports,
516 ...argValues
517 );
518 };
519

Callers 1

createModuleRunnersMethod · 0.95

Calls 7

requireMethod · 0.95
hasWebTargetMethod · 0.95
urlToRelativePathFunction · 0.85
keysMethod · 0.65
callFunction · 0.50
createMethod · 0.45

Tested by

no test coverage detected