Skip to content

Commit 504222d

Browse files
authored
Add Node ESM build option (#20243)
This allows exporting ESM modules for the Webpack plugin. This is necessary for making a resolver plugin. We could probably make the whole plugin use ESM instead of CJS ES2015.
1 parent 1b96ee4 commit 504222d

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed

scripts/rollup/build.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ process.on('unhandledRejection', err => {
4646

4747
const {
4848
NODE_ES2015,
49+
NODE_ESM,
4950
UMD_DEV,
5051
UMD_PROD,
5152
UMD_PROFILING,
@@ -259,12 +260,15 @@ function getFormat(bundleType) {
259260
case RN_FB_PROD:
260261
case RN_FB_PROFILING:
261262
return `cjs`;
263+
case NODE_ESM:
264+
return `es`;
262265
}
263266
}
264267

265268
function isProductionBundleType(bundleType) {
266269
switch (bundleType) {
267270
case NODE_ES2015:
271+
case NODE_ESM:
268272
case UMD_DEV:
269273
case NODE_DEV:
270274
case FB_WWW_DEV:
@@ -290,6 +294,7 @@ function isProductionBundleType(bundleType) {
290294
function isProfilingBundleType(bundleType) {
291295
switch (bundleType) {
292296
case NODE_ES2015:
297+
case NODE_ESM:
293298
case FB_WWW_DEV:
294299
case FB_WWW_PROD:
295300
case NODE_DEV:
@@ -729,6 +734,7 @@ async function buildEverything() {
729734
for (const bundle of Bundles.bundles) {
730735
bundles.push(
731736
[bundle, NODE_ES2015],
737+
[bundle, NODE_ESM],
732738
[bundle, UMD_DEV],
733739
[bundle, UMD_PROD],
734740
[bundle, UMD_PROFILING],

scripts/rollup/bundles.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const __EXPERIMENTAL__ =
99

1010
const bundleTypes = {
1111
NODE_ES2015: 'NODE_ES2015',
12+
NODE_ESM: 'NODE_ESM',
1213
UMD_DEV: 'UMD_DEV',
1314
UMD_PROD: 'UMD_PROD',
1415
UMD_PROFILING: 'UMD_PROFILING',
@@ -28,6 +29,7 @@ const bundleTypes = {
2829

2930
const {
3031
NODE_ES2015,
32+
NODE_ESM,
3133
UMD_DEV,
3234
UMD_PROD,
3335
UMD_PROFILING,
@@ -777,6 +779,8 @@ function getFilename(bundle, bundleType) {
777779
switch (bundleType) {
778780
case NODE_ES2015:
779781
return `${name}.js`;
782+
case NODE_ESM:
783+
return `${name}.js`;
780784
case UMD_DEV:
781785
return `${name}.development.js`;
782786
case UMD_PROD:

scripts/rollup/packaging.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717

1818
const {
1919
NODE_ES2015,
20+
NODE_ESM,
2021
UMD_DEV,
2122
UMD_PROD,
2223
UMD_PROFILING,
@@ -45,6 +46,8 @@ function getBundleOutputPath(bundleType, filename, packageName) {
4546
switch (bundleType) {
4647
case NODE_ES2015:
4748
return `build/node_modules/${packageName}/cjs/${filename}`;
49+
case NODE_ESM:
50+
return `build/node_modules/${packageName}/esm/${filename}`;
4851
case NODE_DEV:
4952
case NODE_PROD:
5053
case NODE_PROFILING:
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
module.exports = {
4+
env: {
5+
commonjs: true,
6+
browser: true,
7+
},
8+
globals: {
9+
// ES 6
10+
Map: true,
11+
Set: true,
12+
Proxy: true,
13+
Symbol: true,
14+
WeakMap: true,
15+
WeakSet: true,
16+
Uint16Array: true,
17+
Reflect: true,
18+
// Vendor specific
19+
MSApp: true,
20+
__REACT_DEVTOOLS_GLOBAL_HOOK__: true,
21+
// CommonJS / Node
22+
process: true,
23+
setImmediate: true,
24+
Buffer: true,
25+
// Trusted Types
26+
trustedTypes: true,
27+
28+
// Scheduler profiling
29+
SharedArrayBuffer: true,
30+
Int32Array: true,
31+
ArrayBuffer: true,
32+
33+
TaskController: true,
34+
35+
// Flight
36+
Uint8Array: true,
37+
Promise: true,
38+
39+
// Flight Webpack
40+
__webpack_chunk_load__: true,
41+
__webpack_require__: true,
42+
43+
// jest
44+
expect: true,
45+
jest: true,
46+
},
47+
parserOptions: {
48+
ecmaVersion: 2015,
49+
sourceType: 'module',
50+
},
51+
rules: {
52+
'no-undef': 'error',
53+
'no-shadow-restricted-names': 'error',
54+
},
55+
56+
// These plugins aren't used, but eslint complains if an eslint-ignore comment
57+
// references unused plugins. An alternate approach could be to strip
58+
// eslint-ignore comments as part of the build.
59+
plugins: ['jest', 'no-for-of-loops', 'react', 'react-internal'],
60+
};

scripts/rollup/validate/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Packaging = require('../packaging');
99

1010
const {
1111
NODE_ES2015,
12+
NODE_ESM,
1213
UMD_DEV,
1314
UMD_PROD,
1415
UMD_PROFILING,
@@ -34,6 +35,8 @@ function getFormat(bundleType) {
3435
return 'umd';
3536
case NODE_ES2015:
3637
return 'cjs2015';
38+
case NODE_ESM:
39+
return 'esm';
3740
case NODE_DEV:
3841
case NODE_PROD:
3942
case NODE_PROFILING:
@@ -64,6 +67,7 @@ function getESLintInstance(format) {
6467
const esLints = {
6568
cjs: getESLintInstance('cjs'),
6669
cjs2015: getESLintInstance('cjs2015'),
70+
esm: getESLintInstance('esm'),
6771
rn: getESLintInstance('rn'),
6872
fb: getESLintInstance('fb'),
6973
umd: getESLintInstance('umd'),

scripts/rollup/wrappers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const reactVersion = require('../../package.json').version;
55

66
const {
77
NODE_ES2015,
8+
NODE_ESM,
89
UMD_DEV,
910
UMD_PROD,
1011
UMD_PROFILING,
@@ -40,6 +41,17 @@ ${license}
4041
4142
'use strict';
4243
44+
${source}`;
45+
},
46+
47+
/***************** NODE_ESM *****************/
48+
[NODE_ESM](source, globalName, filename, moduleType) {
49+
return `/** @license React v${reactVersion}
50+
* ${filename}
51+
*
52+
${license}
53+
*/
54+
4355
${source}`;
4456
},
4557

0 commit comments

Comments
 (0)