1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  */
    
  7. 
    
  8. 'use strict';
    
  9. 
    
  10. const url = require('url');
    
  11. const Module = require('module');
    
  12. 
    
  13. let webpackModuleIdx = 0;
    
  14. const webpackServerModules = {};
    
  15. const webpackClientModules = {};
    
  16. const webpackErroredModules = {};
    
  17. const webpackServerMap = {};
    
  18. const webpackClientMap = {};
    
  19. global.__webpack_require__ = function (id) {
    
  20.   if (webpackErroredModules[id]) {
    
  21.     throw webpackErroredModules[id];
    
  22.   }
    
  23.   return webpackClientModules[id] || webpackServerModules[id];
    
  24. };
    
  25. 
    
  26. const previousCompile = Module.prototype._compile;
    
  27. 
    
  28. const register = require('react-server-dom-webpack/node-register');
    
  29. // Register node compile
    
  30. register();
    
  31. 
    
  32. const nodeCompile = Module.prototype._compile;
    
  33. 
    
  34. if (previousCompile === nodeCompile) {
    
  35.   throw new Error(
    
  36.     'Expected the Node loader to register the _compile extension',
    
  37.   );
    
  38. }
    
  39. 
    
  40. Module.prototype._compile = previousCompile;
    
  41. 
    
  42. exports.webpackMap = webpackClientMap;
    
  43. exports.webpackModules = webpackClientModules;
    
  44. exports.webpackServerMap = webpackServerMap;
    
  45. exports.moduleLoading = {
    
  46.   prefix: '/',
    
  47. };
    
  48. 
    
  49. exports.clientModuleError = function clientModuleError(moduleError) {
    
  50.   const idx = '' + webpackModuleIdx++;
    
  51.   webpackErroredModules[idx] = moduleError;
    
  52.   const path = url.pathToFileURL(idx).href;
    
  53.   webpackClientMap[path] = {
    
  54.     id: idx,
    
  55.     chunks: [],
    
  56.     name: '*',
    
  57.   };
    
  58.   const mod = {exports: {}};
    
  59.   nodeCompile.call(mod, '"use client"', idx);
    
  60.   return mod.exports;
    
  61. };
    
  62. 
    
  63. exports.clientExports = function clientExports(
    
  64.   moduleExports,
    
  65.   chunkId,
    
  66.   chunkFilename,
    
  67. ) {
    
  68.   const chunks = [];
    
  69.   if (chunkId) {
    
  70.     chunks.push(chunkId, chunkFilename);
    
  71.   }
    
  72.   const idx = '' + webpackModuleIdx++;
    
  73.   webpackClientModules[idx] = moduleExports;
    
  74.   const path = url.pathToFileURL(idx).href;
    
  75.   webpackClientMap[path] = {
    
  76.     id: idx,
    
  77.     chunks,
    
  78.     name: '*',
    
  79.   };
    
  80.   // We only add this if this test is testing ESM compat.
    
  81.   if ('__esModule' in moduleExports) {
    
  82.     webpackClientMap[path + '#'] = {
    
  83.       id: idx,
    
  84.       chunks,
    
  85.       name: '',
    
  86.     };
    
  87.   }
    
  88.   if (typeof moduleExports.then === 'function') {
    
  89.     moduleExports.then(
    
  90.       asyncModuleExports => {
    
  91.         for (const name in asyncModuleExports) {
    
  92.           webpackClientMap[path + '#' + name] = {
    
  93.             id: idx,
    
  94.             chunks,
    
  95.             name: name,
    
  96.           };
    
  97.         }
    
  98.       },
    
  99.       () => {},
    
  100.     );
    
  101.   }
    
  102.   if ('split' in moduleExports) {
    
  103.     // If we're testing module splitting, we encode this name in a separate module id.
    
  104.     const splitIdx = '' + webpackModuleIdx++;
    
  105.     webpackClientModules[splitIdx] = {
    
  106.       s: moduleExports.split,
    
  107.     };
    
  108.     webpackClientMap[path + '#split'] = {
    
  109.       id: splitIdx,
    
  110.       chunks,
    
  111.       name: 's',
    
  112.     };
    
  113.   }
    
  114.   const mod = {exports: {}};
    
  115.   nodeCompile.call(mod, '"use client"', idx);
    
  116.   return mod.exports;
    
  117. };
    
  118. 
    
  119. // This tests server to server references. There's another case of client to server references.
    
  120. exports.serverExports = function serverExports(moduleExports) {
    
  121.   const idx = '' + webpackModuleIdx++;
    
  122.   webpackServerModules[idx] = moduleExports;
    
  123.   const path = url.pathToFileURL(idx).href;
    
  124.   webpackServerMap[path] = {
    
  125.     id: idx,
    
  126.     chunks: [],
    
  127.     name: '*',
    
  128.   };
    
  129.   // We only add this if this test is testing ESM compat.
    
  130.   if ('__esModule' in moduleExports) {
    
  131.     webpackServerMap[path + '#'] = {
    
  132.       id: idx,
    
  133.       chunks: [],
    
  134.       name: '',
    
  135.     };
    
  136.   }
    
  137.   if ('split' in moduleExports) {
    
  138.     // If we're testing module splitting, we encode this name in a separate module id.
    
  139.     const splitIdx = '' + webpackModuleIdx++;
    
  140.     webpackServerModules[splitIdx] = {
    
  141.       s: moduleExports.split,
    
  142.     };
    
  143.     webpackServerMap[path + '#split'] = {
    
  144.       id: splitIdx,
    
  145.       chunks: [],
    
  146.       name: 's',
    
  147.     };
    
  148.   }
    
  149.   const mod = {exports: moduleExports};
    
  150.   nodeCompile.call(mod, '"use server"', idx);
    
  151.   return mod.exports;
    
  152. };