main
js 597 lines 13 KB
Raw
1 'use strict';
2
3 const {bundleTypes, moduleTypes} = require('./bundles');
4
5 const {
6 NODE_ES2015,
7 ESM_DEV,
8 ESM_PROD,
9 NODE_DEV,
10 NODE_PROD,
11 NODE_PROFILING,
12 BUN_DEV,
13 BUN_PROD,
14 FB_WWW_DEV,
15 FB_WWW_PROD,
16 FB_WWW_PROFILING,
17 RN_OSS_DEV,
18 RN_OSS_PROD,
19 RN_OSS_PROFILING,
20 RN_FB_DEV,
21 RN_FB_PROD,
22 RN_FB_PROFILING,
23 BROWSER_SCRIPT,
24 CJS_DTS,
25 ESM_DTS,
26 } = bundleTypes;
27
28 const {RECONCILER} = moduleTypes;
29
30 const USE_STRICT_HEADER_REGEX = /'use strict';\n+/;
31
32 function wrapWithRegisterInternalModule(source) {
33 return `\
34 'use strict';
35 if (
36 typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
37 typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
38 'function'
39 ) {
40 __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
41 }
42 ${source}
43 if (
44 typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
45 typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
46 'function'
47 ) {
48 __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
49 }
50 `;
51 }
52
53 const license = ` * Copyright (c) Meta Platforms, Inc. and affiliates.
54 *
55 * This source code is licensed under the MIT license found in the
56 * LICENSE file in the root directory of this source tree.`;
57
58 const topLevelDefinitionWrappers = {
59 /***************** NODE_ES2015 *****************/
60 [NODE_ES2015](source, globalName, filename, moduleType) {
61 return `'use strict';
62
63 ${source}`;
64 },
65
66 /***************** ESM_DEV *****************/
67 [ESM_DEV](source, globalName, filename, moduleType) {
68 return source;
69 },
70
71 /***************** ESM_PROD *****************/
72 [ESM_PROD](source, globalName, filename, moduleType) {
73 return source;
74 },
75
76 /***************** BUN_DEV *****************/
77 [BUN_DEV](source, globalName, filename, moduleType) {
78 return source;
79 },
80
81 /***************** BUN_PROD *****************/
82 [BUN_PROD](source, globalName, filename, moduleType) {
83 return source;
84 },
85
86 /***************** NODE_DEV *****************/
87 [NODE_DEV](source, globalName, filename, moduleType) {
88 return `'use strict';
89
90 if (process.env.NODE_ENV !== "production") {
91 (function() {
92 ${source}
93 })();
94 }`;
95 },
96
97 /***************** NODE_PROD *****************/
98 [NODE_PROD](source, globalName, filename, moduleType) {
99 return source;
100 },
101
102 /***************** NODE_PROFILING *****************/
103 [NODE_PROFILING](source, globalName, filename, moduleType) {
104 return source;
105 },
106
107 /****************** FB_WWW_DEV ******************/
108 [FB_WWW_DEV](source, globalName, filename, moduleType) {
109 return `'use strict';
110
111 if (__DEV__) {
112 (function() {
113 ${source}
114 })();
115 }`;
116 },
117
118 /****************** FB_WWW_PROD ******************/
119 [FB_WWW_PROD](source, globalName, filename, moduleType) {
120 return source;
121 },
122
123 /****************** FB_WWW_PROFILING ******************/
124 [FB_WWW_PROFILING](source, globalName, filename, moduleType) {
125 return source;
126 },
127
128 /****************** RN_OSS_DEV ******************/
129 [RN_OSS_DEV](source, globalName, filename, moduleType) {
130 return `'use strict';
131
132 if (__DEV__) {
133 (function() {
134 ${source}
135 })();
136 }`;
137 },
138
139 /****************** RN_OSS_PROD ******************/
140 [RN_OSS_PROD](source, globalName, filename, moduleType) {
141 return source;
142 },
143
144 /****************** RN_OSS_PROFILING ******************/
145 [RN_OSS_PROFILING](source, globalName, filename, moduleType) {
146 return source;
147 },
148
149 /****************** RN_FB_DEV ******************/
150 [RN_FB_DEV](source, globalName, filename, moduleType) {
151 return `'use strict';
152
153 if (__DEV__) {
154 (function() {
155 ${source}
156 })();
157 }`;
158 },
159
160 /****************** RN_FB_PROD ******************/
161 [RN_FB_PROD](source, globalName, filename, moduleType) {
162 return source;
163 },
164
165 /****************** RN_FB_PROFILING ******************/
166 [RN_FB_PROFILING](source, globalName, filename, moduleType) {
167 return source;
168 },
169 };
170
171 const reconcilerWrappers = {
172 /***************** NODE_DEV (reconciler only) *****************/
173 [NODE_DEV](source, globalName, filename, moduleType) {
174 return `'use strict';
175
176 if (process.env.NODE_ENV !== "production") {
177 module.exports = function $$$reconciler($$$config) {
178 var exports = {};
179 ${source}
180 return exports;
181 };
182 module.exports.default = module.exports;
183 Object.defineProperty(module.exports, "__esModule", { value: true });
184 }
185 `;
186 },
187
188 /***************** NODE_PROD (reconciler only) *****************/
189 [NODE_PROD](source, globalName, filename, moduleType) {
190 return `module.exports = function $$$reconciler($$$config) {
191
192 var exports = {};
193 ${source}
194 return exports;
195 };
196 module.exports.default = module.exports;
197 Object.defineProperty(module.exports, "__esModule", { value: true });
198 `;
199 },
200
201 /***************** NODE_PROFILING (reconciler only) *****************/
202 [NODE_PROFILING](source, globalName, filename, moduleType) {
203 return `module.exports = function $$$reconciler($$$config) {
204 var exports = {};
205 ${source}
206 return exports;
207 };
208 module.exports.default = module.exports;
209 Object.defineProperty(module.exports, "__esModule", { value: true });
210 `;
211 },
212
213 /***************** FB_WWW_DEV (reconciler only) *****************/
214 [FB_WWW_DEV](source, globalName, filename, moduleType) {
215 return `'use strict';
216
217 if (__DEV__) {
218 module.exports = function $$$reconciler($$$config) {
219 var exports = {};
220 ${source}
221 return exports;
222 };
223 module.exports.default = module.exports;
224 Object.defineProperty(module.exports, "__esModule", { value: true });
225 }
226 `;
227 },
228
229 /***************** FB_WWW_PROD (reconciler only) *****************/
230 [FB_WWW_PROD](source, globalName, filename, moduleType) {
231 return `module.exports = function $$$reconciler($$$config) {
232
233 var exports = {};
234 ${source}
235 return exports;
236 };
237 module.exports.default = module.exports;
238 Object.defineProperty(module.exports, "__esModule", { value: true });
239 `;
240 },
241
242 /***************** FB_WWW_PROFILING (reconciler only) *****************/
243 [FB_WWW_PROFILING](source, globalName, filename, moduleType) {
244 return `module.exports = function $$$reconciler($$$config) {
245 var exports = {};
246 ${source}
247 return exports;
248 };
249 module.exports.default = module.exports;
250 Object.defineProperty(module.exports, "__esModule", { value: true });
251 `;
252 },
253
254 /***************** CJS_DTS *****************/
255 [CJS_DTS](source, globalName, filename, moduleType) {
256 return source;
257 },
258
259 /***************** ESM_DTS *****************/
260 [ESM_DTS](source, globalName, filename, moduleType) {
261 return source;
262 },
263 };
264
265 const licenseHeaderWrappers = {
266 /***************** NODE_ES2015 *****************/
267 [NODE_ES2015](source, globalName, filename, moduleType) {
268 return `/**
269 * @license React
270 * ${filename}
271 *
272 ${license}
273 */
274
275 ${source}`;
276 },
277
278 /***************** ESM_DEV *****************/
279 [ESM_DEV](source, globalName, filename, moduleType) {
280 return `/**
281 * @license React
282 * ${filename}
283 *
284 ${license}
285 */
286
287 ${source}`;
288 },
289
290 /***************** ESM_PROD *****************/
291 [ESM_PROD](source, globalName, filename, moduleType) {
292 return `/**
293 * @license React
294 * ${filename}
295 *
296 ${license}
297 */
298
299 ${source}`;
300 },
301
302 /***************** BUN_DEV *****************/
303 [BUN_DEV](source, globalName, filename, moduleType) {
304 return `/**
305 * @license React
306 * ${filename}
307 *
308 ${license}
309 */
310
311 ${source}`;
312 },
313
314 /***************** BUN_PROD *****************/
315 [BUN_PROD](source, globalName, filename, moduleType) {
316 return `/**
317 * @license React
318 * ${filename}
319 *
320 ${license}
321 */
322
323 ${source}`;
324 },
325
326 /***************** NODE_DEV *****************/
327 [NODE_DEV](source, globalName, filename, moduleType) {
328 return `/**
329 * @license React
330 * ${filename}
331 *
332 ${license}
333 */
334
335 ${source}`;
336 },
337
338 /***************** NODE_PROD *****************/
339 [NODE_PROD](source, globalName, filename, moduleType) {
340 return `/**
341 * @license React
342 * ${filename}
343 *
344 ${license}
345 */
346
347 ${source}`;
348 },
349
350 /***************** NODE_PROFILING *****************/
351 [NODE_PROFILING](source, globalName, filename, moduleType) {
352 return `/**
353 * @license React
354 * ${filename}
355 *
356 ${license}
357 */
358
359 ${source}`;
360 },
361
362 /****************** FB_WWW_DEV ******************/
363 [FB_WWW_DEV](source, globalName, filename, moduleType) {
364 return `/**
365 ${license}
366 *
367 * @noflow
368 * @nolint
369 * @preventMunge
370 * @preserve-invariant-messages
371 */
372
373 ${source}`;
374 },
375
376 /****************** FB_WWW_PROD ******************/
377 [FB_WWW_PROD](source, globalName, filename, moduleType) {
378 return `/**
379 ${license}
380 *
381 * @noflow
382 * @nolint
383 * @preventMunge
384 * @preserve-invariant-messages
385 */
386
387 ${source}`;
388 },
389
390 /****************** FB_WWW_PROFILING ******************/
391 [FB_WWW_PROFILING](source, globalName, filename, moduleType) {
392 return `/**
393 ${license}
394 *
395 * @noflow
396 * @nolint
397 * @preventMunge
398 * @preserve-invariant-messages
399 */
400
401 ${source}`;
402 },
403
404 /****************** RN_OSS_DEV ******************/
405 [RN_OSS_DEV](source, globalName, filename, moduleType) {
406 return `/**
407 ${license}
408 *
409 * @noflow
410 * @nolint
411 * @preventMunge
412 */
413
414 ${source}`;
415 },
416
417 /****************** RN_OSS_PROD ******************/
418 [RN_OSS_PROD](source, globalName, filename, moduleType) {
419 return `/**
420 ${license}
421 *
422 * @noflow
423 * @nolint
424 * @preventMunge
425 */
426
427 ${source}`;
428 },
429
430 /****************** RN_OSS_PROFILING ******************/
431 [RN_OSS_PROFILING](source, globalName, filename, moduleType) {
432 return `/**
433 ${license}
434 *
435 * @noflow
436 * @nolint
437 * @preventMunge
438 */
439
440 ${source}`;
441 },
442
443 /****************** RN_FB_DEV ******************/
444 [RN_FB_DEV](source, globalName, filename, moduleType) {
445 return `/**
446 ${license}
447 *
448 * @noflow
449 * @nolint
450 * @preventMunge
451 */
452
453 ${source}`;
454 },
455
456 /****************** RN_FB_PROD ******************/
457 [RN_FB_PROD](source, globalName, filename, moduleType) {
458 return `/**
459 ${license}
460 *
461 * @noflow
462 * @nolint
463 * @preventMunge
464 */
465
466 ${source}`;
467 },
468
469 /****************** RN_FB_PROFILING ******************/
470 [RN_FB_PROFILING](source, globalName, filename, moduleType) {
471 return `/**
472 ${license}
473 *
474 * @noflow
475 * @nolint
476 * @preventMunge
477 */
478
479 ${source}`;
480 },
481
482 /***************** CJS_DTS *****************/
483 [CJS_DTS](source, globalName, filename, moduleType) {
484 return `/**
485 * @license React
486 * ${filename}
487 *
488 ${license}
489 */
490
491 ${source}`;
492 },
493
494 /***************** ESM_DTS *****************/
495 [ESM_DTS](source, globalName, filename, moduleType) {
496 return `/**
497 * @license React
498 * ${filename}
499 *
500 ${license}
501 */
502
503 ${source}`;
504 },
505 };
506
507 function wrapWithTopLevelDefinitions(
508 source,
509 bundleType,
510 globalName,
511 filename,
512 moduleType,
513 wrapWithModuleBoundaries,
514 wrapWithNodeDevGuard
515 ) {
516 if (wrapWithModuleBoundaries) {
517 switch (bundleType) {
518 case NODE_DEV:
519 case NODE_PROFILING:
520 case FB_WWW_DEV:
521 case FB_WWW_PROFILING:
522 case RN_OSS_DEV:
523 case RN_OSS_PROFILING:
524 case RN_FB_DEV:
525 case RN_FB_PROFILING:
526 // Remove the 'use strict' directive from source.
527 // The module start wrapper will add its own.
528 // This directive is only meaningful when it is the first statement in a file or function.
529 source = source.replace(USE_STRICT_HEADER_REGEX, '');
530
531 // Certain DEV and Profiling bundles should self-register their own module boundaries with DevTools.
532 // This allows the Timeline to de-emphasize (dim) internal stack frames.
533 source = wrapWithRegisterInternalModule(source);
534 break;
535 }
536 }
537
538 if (bundleType === BROWSER_SCRIPT) {
539 // Bundles of type BROWSER_SCRIPT get sent straight to the browser without
540 // additional processing. So we should exclude any extra wrapper comments.
541 return source;
542 }
543
544 if (moduleType === RECONCILER) {
545 // Standalone reconciler is only used by third-party renderers.
546 // It is handled separately.
547 const wrapper = reconcilerWrappers[bundleType];
548 if (typeof wrapper !== 'function') {
549 throw new Error(
550 `Unsupported build type for the reconciler package: ${bundleType}.`
551 );
552 }
553
554 return wrapper(source, globalName, filename, moduleType);
555 }
556
557 // Node.js build tools (e.g. ESLint plugins) use process.env.NODE_ENV instead
558 // of __DEV__ even when building for FB_WWW, since they run in Node.js where
559 // __DEV__ is not defined.
560 const effectiveBundleType =
561 wrapWithNodeDevGuard && bundleType === FB_WWW_DEV ? NODE_DEV : bundleType;
562
563 // All the other packages.
564 const wrapper = topLevelDefinitionWrappers[effectiveBundleType];
565 if (typeof wrapper !== 'function') {
566 throw new Error(`Unsupported build type: ${bundleType}.`);
567 }
568
569 return wrapper(source, globalName, filename, moduleType);
570 }
571
572 function wrapWithLicenseHeader(
573 source,
574 bundleType,
575 globalName,
576 filename,
577 moduleType
578 ) {
579 if (bundleType === BROWSER_SCRIPT) {
580 // Bundles of type BROWSER_SCRIPT get sent straight to the browser without
581 // additional processing. So we should exclude any extra wrapper comments.
582 return source;
583 }
584
585 // All the other packages.
586 const wrapper = licenseHeaderWrappers[bundleType];
587 if (typeof wrapper !== 'function') {
588 throw new Error(`Unsupported build type: ${bundleType}.`);
589 }
590
591 return wrapper(source, globalName, filename, moduleType);
592 }
593
594 module.exports = {
595 wrapWithTopLevelDefinitions,
596 wrapWithLicenseHeader,
597 };