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