1. 'use strict';
    
  2. 
    
  3. const {resolve} = require('path');
    
  4. const {readFileSync} = require('fs');
    
  5. const {signFile, getSigningToken} = require('signedsource');
    
  6. const {bundleTypes, moduleTypes} = require('./bundles');
    
  7. 
    
  8. const {
    
  9.   NODE_ES2015,
    
  10.   ESM_DEV,
    
  11.   ESM_PROD,
    
  12.   UMD_DEV,
    
  13.   UMD_PROD,
    
  14.   UMD_PROFILING,
    
  15.   NODE_DEV,
    
  16.   NODE_PROD,
    
  17.   NODE_PROFILING,
    
  18.   BUN_DEV,
    
  19.   BUN_PROD,
    
  20.   FB_WWW_DEV,
    
  21.   FB_WWW_PROD,
    
  22.   FB_WWW_PROFILING,
    
  23.   RN_OSS_DEV,
    
  24.   RN_OSS_PROD,
    
  25.   RN_OSS_PROFILING,
    
  26.   RN_FB_DEV,
    
  27.   RN_FB_PROD,
    
  28.   RN_FB_PROFILING,
    
  29.   BROWSER_SCRIPT,
    
  30. } = bundleTypes;
    
  31. 
    
  32. const {RECONCILER} = moduleTypes;
    
  33. 
    
  34. const USE_STRICT_HEADER_REGEX = /'use strict';\n+/;
    
  35. 
    
  36. function registerInternalModuleStart(globalName) {
    
  37.   const path = resolve(__dirname, 'wrappers', 'registerInternalModuleBegin.js');
    
  38.   const file = readFileSync(path);
    
  39.   return String(file).trim();
    
  40. }
    
  41. 
    
  42. function registerInternalModuleStop(globalName) {
    
  43.   const path = resolve(__dirname, 'wrappers', 'registerInternalModuleEnd.js');
    
  44.   const file = readFileSync(path);
    
  45. 
    
  46.   // Remove the 'use strict' directive from the footer.
    
  47.   // This directive is only meaningful when it is the first statement in a file or function.
    
  48.   return String(file).replace(USE_STRICT_HEADER_REGEX, '').trim();
    
  49. }
    
  50. 
    
  51. const license = ` * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  52.  *
    
  53.  * This source code is licensed under the MIT license found in the
    
  54.  * LICENSE file in the root directory of this source tree.`;
    
  55. 
    
  56. const wrappers = {
    
  57.   /***************** NODE_ES2015 *****************/
    
  58.   [NODE_ES2015](source, globalName, filename, moduleType) {
    
  59.     return `/**
    
  60.  * @license React
    
  61.  * ${filename}
    
  62.  *
    
  63. ${license}
    
  64.  */
    
  65. 
    
  66. 'use strict';
    
  67. 
    
  68. ${source}`;
    
  69.   },
    
  70. 
    
  71.   /***************** ESM_DEV *****************/
    
  72.   [ESM_DEV](source, globalName, filename, moduleType) {
    
  73.     return `/**
    
  74. * @license React
    
  75.  * ${filename}
    
  76.  *
    
  77. ${license}
    
  78.  */
    
  79. 
    
  80. ${source}`;
    
  81.   },
    
  82. 
    
  83.   /***************** ESM_PROD *****************/
    
  84.   [ESM_PROD](source, globalName, filename, moduleType) {
    
  85.     return `/**
    
  86. * @license React
    
  87.  * ${filename}
    
  88.  *
    
  89. ${license}
    
  90.  */
    
  91. 
    
  92. ${source}`;
    
  93.   },
    
  94. 
    
  95.   /***************** BUN_DEV *****************/
    
  96.   [BUN_DEV](source, globalName, filename, moduleType) {
    
  97.     return `/**
    
  98. * @license React
    
  99.  * ${filename}
    
  100.  *
    
  101. ${license}
    
  102.  */
    
  103. 
    
  104. ${source}`;
    
  105.   },
    
  106. 
    
  107.   /***************** BUN_PROD *****************/
    
  108.   [BUN_PROD](source, globalName, filename, moduleType) {
    
  109.     return `/**
    
  110. * @license React
    
  111.  * ${filename}
    
  112.  *
    
  113. ${license}
    
  114.  */
    
  115. 
    
  116. ${source}`;
    
  117.   },
    
  118. 
    
  119.   /***************** UMD_DEV *****************/
    
  120.   [UMD_DEV](source, globalName, filename, moduleType) {
    
  121.     return `/**
    
  122.  * @license React
    
  123.  * ${filename}
    
  124.  *
    
  125. ${license}
    
  126.  */
    
  127. ${source}`;
    
  128.   },
    
  129. 
    
  130.   /***************** UMD_PROD *****************/
    
  131.   [UMD_PROD](source, globalName, filename, moduleType) {
    
  132.     return `/**
    
  133.  * @license React
    
  134.  * ${filename}
    
  135.  *
    
  136. ${license}
    
  137.  */
    
  138. (function(){${source}})();`;
    
  139.   },
    
  140. 
    
  141.   /***************** UMD_PROFILING *****************/
    
  142.   [UMD_PROFILING](source, globalName, filename, moduleType) {
    
  143.     return `/**
    
  144.  * @license React
    
  145.  * ${filename}
    
  146.  *
    
  147. ${license}
    
  148.  */
    
  149. (function(){${source}})();`;
    
  150.   },
    
  151. 
    
  152.   /***************** NODE_DEV *****************/
    
  153.   [NODE_DEV](source, globalName, filename, moduleType) {
    
  154.     return `/**
    
  155.  * @license React
    
  156.  * ${filename}
    
  157.  *
    
  158. ${license}
    
  159.  */
    
  160. 
    
  161. 'use strict';
    
  162. 
    
  163. if (process.env.NODE_ENV !== "production") {
    
  164.   (function() {
    
  165. ${source}
    
  166.   })();
    
  167. }`;
    
  168.   },
    
  169. 
    
  170.   /***************** NODE_PROD *****************/
    
  171.   [NODE_PROD](source, globalName, filename, moduleType) {
    
  172.     return `/**
    
  173.  * @license React
    
  174.  * ${filename}
    
  175.  *
    
  176. ${license}
    
  177.  */
    
  178. ${source}`;
    
  179.   },
    
  180. 
    
  181.   /***************** NODE_PROFILING *****************/
    
  182.   [NODE_PROFILING](source, globalName, filename, moduleType) {
    
  183.     return `/**
    
  184.  * @license React
    
  185.  * ${filename}
    
  186.  *
    
  187. ${license}
    
  188.  */
    
  189. ${source}`;
    
  190.   },
    
  191. 
    
  192.   /****************** FB_WWW_DEV ******************/
    
  193.   [FB_WWW_DEV](source, globalName, filename, moduleType) {
    
  194.     return `/**
    
  195. ${license}
    
  196.  *
    
  197.  * @noflow
    
  198.  * @nolint
    
  199.  * @preventMunge
    
  200.  * @preserve-invariant-messages
    
  201.  */
    
  202. 
    
  203. 'use strict';
    
  204. 
    
  205. if (__DEV__) {
    
  206.   (function() {
    
  207. ${source}
    
  208.   })();
    
  209. }`;
    
  210.   },
    
  211. 
    
  212.   /****************** FB_WWW_PROD ******************/
    
  213.   [FB_WWW_PROD](source, globalName, filename, moduleType) {
    
  214.     return `/**
    
  215. ${license}
    
  216.  *
    
  217.  * @noflow
    
  218.  * @nolint
    
  219.  * @preventMunge
    
  220.  * @preserve-invariant-messages
    
  221.  */
    
  222. 
    
  223. ${source}`;
    
  224.   },
    
  225. 
    
  226.   /****************** FB_WWW_PROFILING ******************/
    
  227.   [FB_WWW_PROFILING](source, globalName, filename, moduleType) {
    
  228.     return `/**
    
  229. ${license}
    
  230.  *
    
  231.  * @noflow
    
  232.  * @nolint
    
  233.  * @preventMunge
    
  234.  * @preserve-invariant-messages
    
  235.  */
    
  236. 
    
  237. ${source}`;
    
  238.   },
    
  239. 
    
  240.   /****************** RN_OSS_DEV ******************/
    
  241.   [RN_OSS_DEV](source, globalName, filename, moduleType) {
    
  242.     return signFile(`/**
    
  243. ${license}
    
  244.  *
    
  245.  * @noflow
    
  246.  * @nolint
    
  247.  * @providesModule ${globalName}-dev
    
  248.  * @preventMunge
    
  249.  * ${getSigningToken()}
    
  250.  */
    
  251. 
    
  252. 'use strict';
    
  253. 
    
  254. if (__DEV__) {
    
  255.   (function() {
    
  256. ${source}
    
  257.   })();
    
  258. }`);
    
  259.   },
    
  260. 
    
  261.   /****************** RN_OSS_PROD ******************/
    
  262.   [RN_OSS_PROD](source, globalName, filename, moduleType) {
    
  263.     return signFile(`/**
    
  264. ${license}
    
  265.  *
    
  266.  * @noflow
    
  267.  * @nolint
    
  268.  * @providesModule ${globalName}-prod
    
  269.  * @preventMunge
    
  270.  * ${getSigningToken()}
    
  271.  */
    
  272. 
    
  273. ${source}`);
    
  274.   },
    
  275. 
    
  276.   /****************** RN_OSS_PROFILING ******************/
    
  277.   [RN_OSS_PROFILING](source, globalName, filename, moduleType) {
    
  278.     return signFile(`/**
    
  279. ${license}
    
  280.  *
    
  281.  * @noflow
    
  282.  * @nolint
    
  283.  * @providesModule ${globalName}-profiling
    
  284.  * @preventMunge
    
  285.  * ${getSigningToken()}
    
  286.  */
    
  287. 
    
  288. ${source}`);
    
  289.   },
    
  290. 
    
  291.   /****************** RN_FB_DEV ******************/
    
  292.   [RN_FB_DEV](source, globalName, filename, moduleType) {
    
  293.     return signFile(`/**
    
  294. ${license}
    
  295.  *
    
  296.  * @noflow
    
  297.  * @nolint
    
  298.  * @preventMunge
    
  299.  * ${getSigningToken()}
    
  300.  */
    
  301. 
    
  302. 'use strict';
    
  303. 
    
  304. if (__DEV__) {
    
  305.   (function() {
    
  306. ${source}
    
  307.   })();
    
  308. }`);
    
  309.   },
    
  310. 
    
  311.   /****************** RN_FB_PROD ******************/
    
  312.   [RN_FB_PROD](source, globalName, filename, moduleType) {
    
  313.     return signFile(`/**
    
  314. ${license}
    
  315.  *
    
  316.  * @noflow
    
  317.  * @nolint
    
  318.  * @preventMunge
    
  319.  * ${getSigningToken()}
    
  320.  */
    
  321. 
    
  322. ${source}`);
    
  323.   },
    
  324. 
    
  325.   /****************** RN_FB_PROFILING ******************/
    
  326.   [RN_FB_PROFILING](source, globalName, filename, moduleType) {
    
  327.     return signFile(`/**
    
  328. ${license}
    
  329.  *
    
  330.  * @noflow
    
  331.  * @nolint
    
  332.  * @preventMunge
    
  333.  * ${getSigningToken()}
    
  334.  */
    
  335. 
    
  336. ${source}`);
    
  337.   },
    
  338. };
    
  339. 
    
  340. const reconcilerWrappers = {
    
  341.   /***************** NODE_DEV (reconciler only) *****************/
    
  342.   [NODE_DEV](source, globalName, filename, moduleType) {
    
  343.     return `/**
    
  344.  * @license React
    
  345.  * ${filename}
    
  346.  *
    
  347. ${license}
    
  348.  */
    
  349. 
    
  350. 'use strict';
    
  351. 
    
  352. if (process.env.NODE_ENV !== "production") {
    
  353.   module.exports = function $$$reconciler($$$config) {
    
  354.     var exports = {};
    
  355. ${source}
    
  356.     return exports;
    
  357.   };
    
  358.   module.exports.default = module.exports;
    
  359.   Object.defineProperty(module.exports, "__esModule", { value: true });
    
  360. }
    
  361. `;
    
  362.   },
    
  363. 
    
  364.   /***************** NODE_PROD (reconciler only) *****************/
    
  365.   [NODE_PROD](source, globalName, filename, moduleType) {
    
  366.     return `/**
    
  367.  * @license React
    
  368.  * ${filename}
    
  369.  *
    
  370. ${license}
    
  371.  */
    
  372. module.exports = function $$$reconciler($$$config) {
    
  373. 
    
  374.     var exports = {};
    
  375. ${source}
    
  376.     return exports;
    
  377. };
    
  378. module.exports.default = module.exports;
    
  379. Object.defineProperty(module.exports, "__esModule", { value: true });
    
  380. `;
    
  381.   },
    
  382. 
    
  383.   /***************** NODE_PROFILING (reconciler only) *****************/
    
  384.   [NODE_PROFILING](source, globalName, filename, moduleType) {
    
  385.     return `/**
    
  386.  * @license React
    
  387.  * ${filename}
    
  388.  *
    
  389. ${license}
    
  390.  */
    
  391. module.exports = function $$$reconciler($$$config) {
    
  392.     var exports = {};
    
  393. ${source}
    
  394.     return exports;
    
  395. };
    
  396. module.exports.default = module.exports;
    
  397. Object.defineProperty(module.exports, "__esModule", { value: true });
    
  398. `;
    
  399.   },
    
  400. };
    
  401. 
    
  402. function wrapBundle(
    
  403.   source,
    
  404.   bundleType,
    
  405.   globalName,
    
  406.   filename,
    
  407.   moduleType,
    
  408.   wrapWithModuleBoundaries
    
  409. ) {
    
  410.   if (wrapWithModuleBoundaries) {
    
  411.     switch (bundleType) {
    
  412.       case NODE_DEV:
    
  413.       case NODE_PROFILING:
    
  414.       case FB_WWW_DEV:
    
  415.       case FB_WWW_PROFILING:
    
  416.       case RN_OSS_DEV:
    
  417.       case RN_OSS_PROFILING:
    
  418.       case RN_FB_DEV:
    
  419.       case RN_FB_PROFILING:
    
  420.         // Remove the 'use strict' directive from source.
    
  421.         // The module start wrapper will add its own.
    
  422.         // This directive is only meaningful when it is the first statement in a file or function.
    
  423.         source = source.replace(USE_STRICT_HEADER_REGEX, '');
    
  424. 
    
  425.         // Certain DEV and Profiling bundles should self-register their own module boundaries with DevTools.
    
  426.         // This allows the Timeline to de-emphasize (dim) internal stack frames.
    
  427.         source = `
    
  428.           ${registerInternalModuleStart(globalName)}
    
  429.           ${source}
    
  430.           ${registerInternalModuleStop(globalName)}
    
  431.         `;
    
  432.         break;
    
  433.     }
    
  434.   }
    
  435. 
    
  436.   if (bundleType === BROWSER_SCRIPT) {
    
  437.     // Bundles of type BROWSER_SCRIPT get sent straight to the browser without
    
  438.     // additional processing. So we should exclude any extra wrapper comments.
    
  439.     return source;
    
  440.   }
    
  441. 
    
  442.   if (moduleType === RECONCILER) {
    
  443.     // Standalone reconciler is only used by third-party renderers.
    
  444.     // It is handled separately.
    
  445.     const wrapper = reconcilerWrappers[bundleType];
    
  446.     if (typeof wrapper !== 'function') {
    
  447.       throw new Error(
    
  448.         `Unsupported build type for the reconciler package: ${bundleType}.`
    
  449.       );
    
  450.     }
    
  451.     return wrapper(source, globalName, filename, moduleType);
    
  452.   }
    
  453. 
    
  454.   // All the other packages.
    
  455.   const wrapper = wrappers[bundleType];
    
  456.   if (typeof wrapper !== 'function') {
    
  457.     throw new Error(`Unsupported build type: ${bundleType}.`);
    
  458.   }
    
  459.   return wrapper(source, globalName, filename, moduleType);
    
  460. }
    
  461. 
    
  462. module.exports = {
    
  463.   wrapBundle,
    
  464. };