Skip to content

Commit

Permalink
Add Node ESM build option (facebook#20243)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sebmarkbage authored and koto committed Jun 15, 2021
1 parent d05d467 commit 8007266
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scripts/rollup/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ process.on('unhandledRejection', err => {

const {
NODE_ES2015,
NODE_ESM,
UMD_DEV,
UMD_PROD,
UMD_PROFILING,
Expand Down Expand Up @@ -259,12 +260,15 @@ function getFormat(bundleType) {
case RN_FB_PROD:
case RN_FB_PROFILING:
return `cjs`;
case NODE_ESM:
return `es`;
}
}

function isProductionBundleType(bundleType) {
switch (bundleType) {
case NODE_ES2015:
case NODE_ESM:
case UMD_DEV:
case NODE_DEV:
case FB_WWW_DEV:
Expand All @@ -290,6 +294,7 @@ function isProductionBundleType(bundleType) {
function isProfilingBundleType(bundleType) {
switch (bundleType) {
case NODE_ES2015:
case NODE_ESM:
case FB_WWW_DEV:
case FB_WWW_PROD:
case NODE_DEV:
Expand Down Expand Up @@ -729,6 +734,7 @@ async function buildEverything() {
for (const bundle of Bundles.bundles) {
bundles.push(
[bundle, NODE_ES2015],
[bundle, NODE_ESM],
[bundle, UMD_DEV],
[bundle, UMD_PROD],
[bundle, UMD_PROFILING],
Expand Down
4 changes: 4 additions & 0 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const __EXPERIMENTAL__ =

const bundleTypes = {
NODE_ES2015: 'NODE_ES2015',
NODE_ESM: 'NODE_ESM',
UMD_DEV: 'UMD_DEV',
UMD_PROD: 'UMD_PROD',
UMD_PROFILING: 'UMD_PROFILING',
Expand All @@ -28,6 +29,7 @@ const bundleTypes = {

const {
NODE_ES2015,
NODE_ESM,
UMD_DEV,
UMD_PROD,
UMD_PROFILING,
Expand Down Expand Up @@ -777,6 +779,8 @@ function getFilename(bundle, bundleType) {
switch (bundleType) {
case NODE_ES2015:
return `${name}.js`;
case NODE_ESM:
return `${name}.js`;
case UMD_DEV:
return `${name}.development.js`;
case UMD_PROD:
Expand Down
3 changes: 3 additions & 0 deletions scripts/rollup/packaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {

const {
NODE_ES2015,
NODE_ESM,
UMD_DEV,
UMD_PROD,
UMD_PROFILING,
Expand Down Expand Up @@ -45,6 +46,8 @@ function getBundleOutputPath(bundleType, filename, packageName) {
switch (bundleType) {
case NODE_ES2015:
return `build/node_modules/${packageName}/cjs/${filename}`;
case NODE_ESM:
return `build/node_modules/${packageName}/esm/${filename}`;
case NODE_DEV:
case NODE_PROD:
case NODE_PROFILING:
Expand Down
60 changes: 60 additions & 0 deletions scripts/rollup/validate/eslintrc.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

module.exports = {
env: {
commonjs: true,
browser: true,
},
globals: {
// ES 6
Map: true,
Set: true,
Proxy: true,
Symbol: true,
WeakMap: true,
WeakSet: true,
Uint16Array: true,
Reflect: true,
// Vendor specific
MSApp: true,
__REACT_DEVTOOLS_GLOBAL_HOOK__: true,
// CommonJS / Node
process: true,
setImmediate: true,
Buffer: true,
// Trusted Types
trustedTypes: true,

// Scheduler profiling
SharedArrayBuffer: true,
Int32Array: true,
ArrayBuffer: true,

TaskController: true,

// Flight
Uint8Array: true,
Promise: true,

// Flight Webpack
__webpack_chunk_load__: true,
__webpack_require__: true,

// jest
expect: true,
jest: true,
},
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
},
rules: {
'no-undef': 'error',
'no-shadow-restricted-names': 'error',
},

// These plugins aren't used, but eslint complains if an eslint-ignore comment
// references unused plugins. An alternate approach could be to strip
// eslint-ignore comments as part of the build.
plugins: ['jest', 'no-for-of-loops', 'react', 'react-internal'],
};
4 changes: 4 additions & 0 deletions scripts/rollup/validate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Packaging = require('../packaging');

const {
NODE_ES2015,
NODE_ESM,
UMD_DEV,
UMD_PROD,
UMD_PROFILING,
Expand All @@ -34,6 +35,8 @@ function getFormat(bundleType) {
return 'umd';
case NODE_ES2015:
return 'cjs2015';
case NODE_ESM:
return 'esm';
case NODE_DEV:
case NODE_PROD:
case NODE_PROFILING:
Expand Down Expand Up @@ -64,6 +67,7 @@ function getESLintInstance(format) {
const esLints = {
cjs: getESLintInstance('cjs'),
cjs2015: getESLintInstance('cjs2015'),
esm: getESLintInstance('esm'),
rn: getESLintInstance('rn'),
fb: getESLintInstance('fb'),
umd: getESLintInstance('umd'),
Expand Down
12 changes: 12 additions & 0 deletions scripts/rollup/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const reactVersion = require('../../package.json').version;

const {
NODE_ES2015,
NODE_ESM,
UMD_DEV,
UMD_PROD,
UMD_PROFILING,
Expand Down Expand Up @@ -40,6 +41,17 @@ ${license}
'use strict';
${source}`;
},

/***************** NODE_ESM *****************/
[NODE_ESM](source, globalName, filename, moduleType) {
return `/** @license React v${reactVersion}
* ${filename}
*
${license}
*/
${source}`;
},

Expand Down

0 comments on commit 8007266

Please sign in to comment.