@samitouri / QOS-React-2 / commits / 504222dcd2

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.

Sebastian Markbåge committed Nov 13, 2020 at 08:57 UTC 504222dcd21c7ac43eb78df11226bab47ec03bb0
6 files changed +89
scripts/rollup/build.js
+6
@@ -46,6 +46,7 @@ process.on('unhandledRejection', err => {
46
47 const {
48 NODE_ES2015,
49 + NODE_ESM,
50 UMD_DEV,
51 UMD_PROD,
52 UMD_PROFILING,
@@ -259,12 +260,15 @@ function getFormat(bundleType) {
260 case RN_FB_PROD:
261 case RN_FB_PROFILING:
262 return `cjs`;
263 + case NODE_ESM:
264 + return `es`;
265 }
266 }
267
268 function isProductionBundleType(bundleType) {
269 switch (bundleType) {
270 case NODE_ES2015:
271 + case NODE_ESM:
272 case UMD_DEV:
273 case NODE_DEV:
274 case FB_WWW_DEV:
@@ -290,6 +294,7 @@ function isProductionBundleType(bundleType) {
294 function isProfilingBundleType(bundleType) {
295 switch (bundleType) {
296 case NODE_ES2015:
297 + case NODE_ESM:
298 case FB_WWW_DEV:
299 case FB_WWW_PROD:
300 case NODE_DEV:
@@ -729,6 +734,7 @@ async function buildEverything() {
734 for (const bundle of Bundles.bundles) {
735 bundles.push(
736 [bundle, NODE_ES2015],
737 + [bundle, NODE_ESM],
738 [bundle, UMD_DEV],
739 [bundle, UMD_PROD],
740 [bundle, UMD_PROFILING],
scripts/rollup/bundles.js
+4
@@ -9,6 +9,7 @@ const __EXPERIMENTAL__ =
9
10 const bundleTypes = {
11 NODE_ES2015: 'NODE_ES2015',
12 + NODE_ESM: 'NODE_ESM',
13 UMD_DEV: 'UMD_DEV',
14 UMD_PROD: 'UMD_PROD',
15 UMD_PROFILING: 'UMD_PROFILING',
@@ -28,6 +29,7 @@ const bundleTypes = {
29
30 const {
31 NODE_ES2015,
32 + NODE_ESM,
33 UMD_DEV,
34 UMD_PROD,
35 UMD_PROFILING,
@@ -777,6 +779,8 @@ function getFilename(bundle, bundleType) {
779 switch (bundleType) {
780 case NODE_ES2015:
781 return `${name}.js`;
782 + case NODE_ESM:
783 + return `${name}.js`;
784 case UMD_DEV:
785 return `${name}.development.js`;
786 case UMD_PROD:
scripts/rollup/packaging.js
+3
@@ -17,6 +17,7 @@ const {
17
18 const {
19 NODE_ES2015,
20 + NODE_ESM,
21 UMD_DEV,
22 UMD_PROD,
23 UMD_PROFILING,
@@ -45,6 +46,8 @@ function getBundleOutputPath(bundleType, filename, packageName) {
46 switch (bundleType) {
47 case NODE_ES2015:
48 return `build/node_modules/${packageName}/cjs/${filename}`;
49 + case NODE_ESM:
50 + return `build/node_modules/${packageName}/esm/${filename}`;
51 case NODE_DEV:
52 case NODE_PROD:
53 case NODE_PROFILING:
scripts/rollup/validate/eslintrc.esm.js new
+60
@@ -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
+4
@@ -9,6 +9,7 @@ const Packaging = require('../packaging');
9
10 const {
11 NODE_ES2015,
12 + NODE_ESM,
13 UMD_DEV,
14 UMD_PROD,
15 UMD_PROFILING,
@@ -34,6 +35,8 @@ function getFormat(bundleType) {
35 return 'umd';
36 case NODE_ES2015:
37 return 'cjs2015';
38 + case NODE_ESM:
39 + return 'esm';
40 case NODE_DEV:
41 case NODE_PROD:
42 case NODE_PROFILING:
@@ -64,6 +67,7 @@ function getESLintInstance(format) {
67 const esLints = {
68 cjs: getESLintInstance('cjs'),
69 cjs2015: getESLintInstance('cjs2015'),
70 + esm: getESLintInstance('esm'),
71 rn: getESLintInstance('rn'),
72 fb: getESLintInstance('fb'),
73 umd: getESLintInstance('umd'),
scripts/rollup/wrappers.js
+12
@@ -5,6 +5,7 @@ const reactVersion = require('../../package.json').version;
5
6 const {
7 NODE_ES2015,
8 + NODE_ESM,
9 UMD_DEV,
10 UMD_PROD,
11 UMD_PROFILING,
@@ -40,6 +41,17 @@ ${license}
41
42 'use strict';
43
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 +
55 ${source}`;
56 },
57