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 turbopackModuleIdx = 0;
    
  14. const turbopackServerModules = {};
    
  15. const turbopackClientModules = {};
    
  16. const turbopackErroredModules = {};
    
  17. const turbopackServerMap = {};
    
  18. const turbopackClientMap = {};
    
  19. global.__turbopack_require__ = function (id) {
    
  20.   if (turbopackErroredModules[id]) {
    
  21.     throw turbopackErroredModules[id];
    
  22.   }
    
  23.   return turbopackClientModules[id] || turbopackServerModules[id];
    
  24. };
    
  25. 
    
  26. const previousCompile = Module.prototype._compile;
    
  27. 
    
  28. const register = require('react-server-dom-turbopack/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.turbopackMap = turbopackClientMap;
    
  43. exports.turbopackModules = turbopackClientModules;
    
  44. exports.turbopackServerMap = turbopackServerMap;
    
  45. exports.moduleLoading = {
    
  46.   prefix: '/prefix/',
    
  47. };
    
  48. 
    
  49. exports.clientModuleError = function clientModuleError(moduleError) {
    
  50.   const idx = '' + turbopackModuleIdx++;
    
  51.   turbopackErroredModules[idx] = moduleError;
    
  52.   const path = url.pathToFileURL(idx).href;
    
  53.   turbopackClientMap[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(moduleExports, chunkUrl) {
    
  64.   const chunks = [];
    
  65.   if (chunkUrl !== undefined) {
    
  66.     chunks.push(chunkUrl);
    
  67.   }
    
  68.   const idx = '' + turbopackModuleIdx++;
    
  69.   turbopackClientModules[idx] = moduleExports;
    
  70.   const path = url.pathToFileURL(idx).href;
    
  71.   turbopackClientMap[path] = {
    
  72.     id: idx,
    
  73.     chunks,
    
  74.     name: '*',
    
  75.   };
    
  76.   // We only add this if this test is testing ESM compat.
    
  77.   if ('__esModule' in moduleExports) {
    
  78.     turbopackClientMap[path + '#'] = {
    
  79.       id: idx,
    
  80.       chunks,
    
  81.       name: '',
    
  82.     };
    
  83.   }
    
  84.   if (typeof moduleExports.then === 'function') {
    
  85.     moduleExports.then(
    
  86.       asyncModuleExports => {
    
  87.         for (const name in asyncModuleExports) {
    
  88.           turbopackClientMap[path + '#' + name] = {
    
  89.             id: idx,
    
  90.             chunks,
    
  91.             name: name,
    
  92.           };
    
  93.         }
    
  94.       },
    
  95.       () => {},
    
  96.     );
    
  97.   }
    
  98.   if ('split' in moduleExports) {
    
  99.     // If we're testing module splitting, we encode this name in a separate module id.
    
  100.     const splitIdx = '' + turbopackModuleIdx++;
    
  101.     turbopackClientModules[splitIdx] = {
    
  102.       s: moduleExports.split,
    
  103.     };
    
  104.     turbopackClientMap[path + '#split'] = {
    
  105.       id: splitIdx,
    
  106.       chunks,
    
  107.       name: 's',
    
  108.     };
    
  109.   }
    
  110.   const mod = {exports: {}};
    
  111.   nodeCompile.call(mod, '"use client"', idx);
    
  112.   return mod.exports;
    
  113. };
    
  114. 
    
  115. // This tests server to server references. There's another case of client to server references.
    
  116. exports.serverExports = function serverExports(moduleExports) {
    
  117.   const idx = '' + turbopackModuleIdx++;
    
  118.   turbopackServerModules[idx] = moduleExports;
    
  119.   const path = url.pathToFileURL(idx).href;
    
  120.   turbopackServerMap[path] = {
    
  121.     id: idx,
    
  122.     chunks: [],
    
  123.     name: '*',
    
  124.   };
    
  125.   // We only add this if this test is testing ESM compat.
    
  126.   if ('__esModule' in moduleExports) {
    
  127.     turbopackServerMap[path + '#'] = {
    
  128.       id: idx,
    
  129.       chunks: [],
    
  130.       name: '',
    
  131.     };
    
  132.   }
    
  133.   if ('split' in moduleExports) {
    
  134.     // If we're testing module splitting, we encode this name in a separate module id.
    
  135.     const splitIdx = '' + turbopackModuleIdx++;
    
  136.     turbopackServerModules[splitIdx] = {
    
  137.       s: moduleExports.split,
    
  138.     };
    
  139.     turbopackServerMap[path + '#split'] = {
    
  140.       id: splitIdx,
    
  141.       chunks: [],
    
  142.       name: 's',
    
  143.     };
    
  144.   }
    
  145.   const mod = {exports: moduleExports};
    
  146.   nodeCompile.call(mod, '"use server"', idx);
    
  147.   return mod.exports;
    
  148. };