1. 'use strict';
    
  2. 
    
  3. const fs = require('fs');
    
  4. const ClosureCompiler = require('google-closure-compiler').compiler;
    
  5. const prettier = require('prettier');
    
  6. 
    
  7. const instructionDir =
    
  8.   './packages/react-dom-bindings/src/server/fizz-instruction-set';
    
  9. 
    
  10. // This is the name of the generated file that exports the inline instruction
    
  11. // set as strings.
    
  12. const inlineCodeStringsFilename =
    
  13.   instructionDir + '/ReactDOMFizzInstructionSetInlineCodeStrings.js';
    
  14. 
    
  15. const config = [
    
  16.   {
    
  17.     entry: 'ReactDOMFizzInlineClientRenderBoundary.js',
    
  18.     exportName: 'clientRenderBoundary',
    
  19.   },
    
  20.   {
    
  21.     entry: 'ReactDOMFizzInlineCompleteBoundary.js',
    
  22.     exportName: 'completeBoundary',
    
  23.   },
    
  24.   {
    
  25.     entry: 'ReactDOMFizzInlineCompleteBoundaryWithStyles.js',
    
  26.     exportName: 'completeBoundaryWithStyles',
    
  27.   },
    
  28.   {
    
  29.     entry: 'ReactDOMFizzInlineCompleteSegment.js',
    
  30.     exportName: 'completeSegment',
    
  31.   },
    
  32.   {
    
  33.     entry: 'ReactDOMFizzInlineFormReplaying.js',
    
  34.     exportName: 'formReplaying',
    
  35.   },
    
  36. ];
    
  37. 
    
  38. const prettierConfig = require('../../.prettierrc.js');
    
  39. 
    
  40. async function main() {
    
  41.   const exportStatements = await Promise.all(
    
  42.     config.map(async ({entry, exportName}) => {
    
  43.       const fullEntryPath = instructionDir + '/' + entry;
    
  44.       const compiler = new ClosureCompiler({
    
  45.         entry_point: fullEntryPath,
    
  46.         js: [
    
  47.           require.resolve('./externs/closure-externs.js'),
    
  48.           fullEntryPath,
    
  49.           instructionDir + '/ReactDOMFizzInstructionSetInlineSource.js',
    
  50.           instructionDir + '/ReactDOMFizzInstructionSetShared.js',
    
  51.         ],
    
  52.         compilation_level: 'ADVANCED',
    
  53.         language_in: 'ECMASCRIPT_2020',
    
  54.         language_out: 'ECMASCRIPT5_STRICT',
    
  55.         module_resolution: 'NODE',
    
  56.         // This is necessary to prevent Closure from inlining a Promise polyfill
    
  57.         rewrite_polyfills: false,
    
  58.       });
    
  59. 
    
  60.       const code = await new Promise((resolve, reject) => {
    
  61.         compiler.run((exitCode, stdOut, stdErr) => {
    
  62.           if (exitCode !== 0) {
    
  63.             reject(new Error(stdErr));
    
  64.           } else {
    
  65.             resolve(stdOut);
    
  66.           }
    
  67.         });
    
  68.       });
    
  69. 
    
  70.       return `export const ${exportName} = ${JSON.stringify(code.trim())};`;
    
  71.     })
    
  72.   );
    
  73. 
    
  74.   let outputCode = [
    
  75.     '// This is a generated file. The source files are in react-dom-bindings/src/server/fizz-instruction-set.',
    
  76.     '// The build script is at scripts/rollup/generate-inline-fizz-runtime.js.',
    
  77.     '// Run `yarn generate-inline-fizz-runtime` to generate.',
    
  78.     ...exportStatements,
    
  79.   ].join('\n');
    
  80. 
    
  81.   // This replaces "window.$globalVar" with "$globalVar". There's probably a
    
  82.   // better way to do this with Closure, with externs or something, but I
    
  83.   // couldn't figure it out. Good enough for now. This only affects the inline
    
  84.   // Fizz runtime, and should break immediately if there were a mistake, so I'm
    
  85.   // not too worried about it.
    
  86.   outputCode = outputCode.replace(
    
  87.     /window\.(\$[A-z0-9_]*|matchMedia)/g,
    
  88.     (_, variableName) => variableName
    
  89.   );
    
  90. 
    
  91.   const prettyOutputCode = await prettier.format(outputCode, prettierConfig);
    
  92. 
    
  93.   fs.writeFileSync(inlineCodeStringsFilename, prettyOutputCode, 'utf8');
    
  94. }
    
  95. 
    
  96. main().catch(err => {
    
  97.   console.error(err);
    
  98.   process.exit(1);
    
  99. });