Skip to content

Commit 909c6da

Browse files
authored
Update Rollup to 3.x (#26442)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn test --debug --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary This PR: - Updates Rollup from 2.x to latest 3.x, and updates associated plugins - Updates deprecated / altered config settings in the Rollup plugin pipeline - Fixes some file extension and import issues related to use of ESM in `react-dom-webpack-server` - Removes a now-obsolete `strip-unused-imports` Rollup plugin - <s>Fixes an _existing_ bug with the Rollup 2.x plugin pipeline on `main` that was causing parts of `DOMProperty.js` to get left out of the `react-dom-webpack-server` JS bundles, by adding a new plugin to tell Rollup to treat that file as if it as side effects</s> This PR should be functionally identical to the other existing "Rollup 3 upgrade" PR at #26078 . I'm filing this as a near-duplicate because I'm ready to push this change through ASAP so that I can follow it up with a PR that adds sourcemap support, that PR's artifact diffing seems like it's possibly stuck and I want to compare the build results, and I've got this set up against latest `main`. <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? --> This gets React's build setup updated to the latest Rollup version, which is generally a good practice, but also ensures that any further Rollup config tweaks can be done using the current Rollup docs as a reference. ## How did you test this change? <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. How exactly did you verify that your PR solves the issue you wanted to solve? If you leave this empty, your PR will very likely be closed. --> - Made builds from the latest `main` - Updated Rollup package versions and cross-compared the changes I needed to make locally to get successful builds vs #26078 - Diffed the output folders between `main` and this PR, and confirmed that the bundle contents are identical (with the exception of version strings and the `react-dom-webpack-server` bundle fix re-adding missing `DOMProperty.js` content)
1 parent 9c54b29 commit 909c6da

File tree

8 files changed

+142
-127
lines changed

8 files changed

+142
-127
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636
"@babel/preset-flow": "^7.10.4",
3737
"@babel/preset-react": "^7.10.4",
3838
"@babel/traverse": "^7.11.0",
39-
"@rollup/plugin-babel": "^5.3.1",
40-
"@rollup/plugin-commonjs": "^22.0.1",
41-
"@rollup/plugin-node-resolve": "^13.3.0",
42-
"@rollup/plugin-replace": "^4.0.0",
39+
"@rollup/plugin-babel": "^6.0.3",
40+
"@rollup/plugin-commonjs": "^24.0.1",
41+
"@rollup/plugin-node-resolve": "^15.0.1",
42+
"@rollup/plugin-replace": "^5.0.2",
4343
"abortcontroller-polyfill": "^1.7.5",
4444
"art": "0.10.1",
4545
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
@@ -89,9 +89,9 @@
8989
"random-seed": "^0.3.0",
9090
"react-lifecycles-compat": "^3.0.4",
9191
"rimraf": "^3.0.0",
92-
"rollup": "^2.76.0",
92+
"rollup": "^3.17.1",
9393
"rollup-plugin-prettier": "^3.0.0",
94-
"rollup-plugin-strip-banner": "^2.0.0",
94+
"rollup-plugin-strip-banner": "^3.0.0",
9595
"semver": "^7.1.1",
9696
"targz": "^1.0.1",
9797
"through2": "^3.0.1",

packages/react-devtools-extensions/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
2828
"@babel/plugin-transform-react-jsx-source": "^7.10.5",
2929
"@babel/preset-react": "^7.10.4",
30-
"@rollup/plugin-babel": "^5.3.1",
31-
"@rollup/plugin-commonjs": "^22.0.1",
32-
"@rollup/plugin-node-resolve": "^13.3.0",
3330
"acorn-jsx": "^5.2.0",
3431
"archiver": "^3.0.0",
3532
"babel-core": "^7.0.0-bridge",
@@ -58,7 +55,6 @@
5855
"os-name": "^3.1.0",
5956
"parse-filepath": "^1.0.2",
6057
"raw-loader": "^3.1.0",
61-
"rollup": "^2.76.0",
6258
"source-map-js": "^0.6.2",
6359
"sourcemap-codec": "^1.4.8",
6460
"style-loader": "^0.23.1",

packages/react-server-dom-webpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"./server.node.unbundled": "./server.node.unbundled.js",
6767
"./node-loader": "./esm/react-server-dom-webpack-node-loader.production.min.js",
6868
"./node-register": "./node-register.js",
69-
"./src/*": "./src/*",
69+
"./src/*": "./src/*.js",
7070
"./package.json": "./package.json"
7171
},
7272
"main": "index.js",

scripts/rollup/build.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const Stats = require('./stats');
1818
const Sync = require('./sync');
1919
const sizes = require('./plugins/sizes-plugin');
2020
const useForks = require('./plugins/use-forks-plugin');
21-
const stripUnusedImports = require('./plugins/strip-unused-imports');
2221
const dynamicImports = require('./plugins/dynamic-imports');
2322
const Packaging = require('./packaging');
2423
const {asyncRimRaf} = require('./utils');
@@ -174,6 +173,31 @@ function getBabelConfig(
174173
return options;
175174
}
176175

176+
let getRollupInteropValue = id => {
177+
// We're setting Rollup to assume that imports are ES modules unless otherwise specified.
178+
// However, we also compile ES import syntax to `require()` using Babel.
179+
// This causes Rollup to turn uses of `import SomeDefaultImport from 'some-module' into
180+
// references to `SomeDefaultImport.default` due to CJS/ESM interop.
181+
// Some CJS modules don't have a `.default` export, and the rewritten import is incorrect.
182+
// Specifying `interop: 'default'` instead will have Rollup use the imported variable as-is,
183+
// without adding a `.default` to the reference.
184+
const modulesWithCommonJsExports = [
185+
'JSResourceReferenceImpl',
186+
'error-stack-parser',
187+
'art/core/transform',
188+
'art/modes/current',
189+
'art/modes/fast-noSideEffects',
190+
'art/modes/svg',
191+
];
192+
193+
if (modulesWithCommonJsExports.includes(id)) {
194+
return 'default';
195+
}
196+
197+
// For all other modules, handle imports without any import helper utils
198+
return 'esModule';
199+
};
200+
177201
function getRollupOutputOptions(
178202
outputPath,
179203
format,
@@ -188,7 +212,7 @@ function getRollupOutputOptions(
188212
format,
189213
globals,
190214
freeze: !isProduction,
191-
interop: false,
215+
interop: getRollupInteropValue,
192216
name: globalName,
193217
sourcemap: false,
194218
esModule: false,
@@ -420,9 +444,6 @@ function getPlugins(
420444
assume_function_wrapper: !isUMDBundle,
421445
renaming: !shouldStayReadable,
422446
}),
423-
// HACK to work around the fact that Rollup isn't removing unused, pure-module imports.
424-
// Note that this plugin must be called after closure applies DCE.
425-
isProduction && stripUnusedImports(pureExternalModules),
426447
// Add the whitespace back if necessary.
427448
shouldStayReadable &&
428449
prettier({
@@ -616,7 +637,7 @@ async function createBundle(bundle, bundleType) {
616637
output: {
617638
externalLiveBindings: false,
618639
freeze: false,
619-
interop: false,
640+
interop: getRollupInteropValue,
620641
esModule: false,
621642
},
622643
};

scripts/rollup/bundles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ const bundles = [
442442
{
443443
bundleTypes: [NODE_ES2015],
444444
moduleType: RENDERER_UTILS,
445-
entry: 'react-server-dom-webpack/src/ReactFlightWebpackNodeRegister.js',
445+
entry: 'react-server-dom-webpack/src/ReactFlightWebpackNodeRegister',
446446
name: 'react-server-dom-webpack-node-register',
447447
global: 'ReactFlightWebpackNodeRegister',
448448
minifyWithProdErrorCodes: false,

scripts/rollup/plugins/strip-unused-imports.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

scripts/rollup/wrappers.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,10 @@ if (process.env.NODE_ENV !== "production") {
354354
${source}
355355
return exports;
356356
};
357-
}`;
357+
module.exports.default = module.exports;
358+
Object.defineProperty(module.exports, "__esModule", { value: true });
359+
}
360+
`;
358361
},
359362

360363
/***************** NODE_PROD (reconciler only) *****************/
@@ -366,10 +369,14 @@ ${source}
366369
${license}
367370
*/
368371
module.exports = function $$$reconciler($$$hostConfig) {
372+
369373
var exports = {};
370374
${source}
371375
return exports;
372-
};`;
376+
};
377+
module.exports.default = module.exports;
378+
Object.defineProperty(module.exports, "__esModule", { value: true });
379+
`;
373380
},
374381

375382
/***************** NODE_PROFILING (reconciler only) *****************/
@@ -384,7 +391,10 @@ module.exports = function $$$reconciler($$$hostConfig) {
384391
var exports = {};
385392
${source}
386393
return exports;
387-
};`;
394+
};
395+
module.exports.default = module.exports;
396+
Object.defineProperty(module.exports, "__esModule", { value: true });
397+
`;
388398
},
389399
};
390400

0 commit comments

Comments
 (0)