Skip to content

Commit 8e4548f

Browse files
sebmarkbageAndyPengc12
authored andcommitted
[Flight] Instrument the Promise for Async Module instead of using a Module Cache (facebook#26985)
Currently, since we use a module cache for async modules, it doesn't automatically get updated when the module registry gets updated (HMR). This technique ensures that if Webpack replaces the module (HMR) then we'll get the new Promise when we require it again. This technique doesn't work for ESM and probably not Vite since ESM will provide a new Promise each time you call `import()` but in the Webpack/CJS approach this Promise is an entry in the module cache and not a promise for the entry. I tried to replicate the original issue in the fixture but it's tricky to replicate because 1) we can't really use async modules the same way without compiling both server and client 2) even then I'm not quite sure how to repro the HMR issue.
1 parent b1a59ea commit 8e4548f

13 files changed

+1154
-1375
lines changed

fixtures/flight/config/jest/babelTransform.js

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

fixtures/flight/config/jest/cssTransform.js

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

fixtures/flight/config/jest/fileTransform.js

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

fixtures/flight/config/webpack.config.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
1515
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
1616
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
1717
const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
18-
const ESLintPlugin = require('eslint-webpack-plugin');
1918
const paths = require('./paths');
2019
const modules = require('./modules');
2120
const getClientEnvironment = require('./env');
@@ -54,9 +53,6 @@ const babelRuntimeRegenerator = require.resolve('@babel/runtime/regenerator', {
5453
// makes for a smoother build process.
5554
const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
5655

57-
const emitErrorsAsWarnings = process.env.ESLINT_NO_DEV_ERRORS === 'true';
58-
const disableESLintPlugin = process.env.DISABLE_ESLINT_PLUGIN === 'true';
59-
6056
const imageInlineSizeLimit = parseInt(
6157
process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'
6258
);
@@ -687,31 +683,6 @@ module.exports = function (webpackEnv) {
687683
infrastructure: 'silent',
688684
},
689685
}),
690-
// !disableESLintPlugin &&
691-
// new ESLintPlugin({
692-
// // Plugin options
693-
// extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
694-
// formatter: require.resolve('react-dev-utils/eslintFormatter'),
695-
// eslintPath: require.resolve('eslint'),
696-
// failOnError: !(isEnvDevelopment && emitErrorsAsWarnings),
697-
// context: paths.appSrc,
698-
// cache: true,
699-
// cacheLocation: path.resolve(
700-
// paths.appNodeModules,
701-
// '.cache/.eslintcache'
702-
// ),
703-
// // ESLint class options
704-
// cwd: paths.appPath,
705-
// resolvePluginsRelativeTo: __dirname,
706-
// baseConfig: {
707-
// extends: [require.resolve('eslint-config-react-app/base')],
708-
// rules: {
709-
// ...(!hasJsxRuntime && {
710-
// 'react/react-in-jsx-scope': 'error',
711-
// }),
712-
// },
713-
// },
714-
// }),
715686
// Fork Start
716687
new ReactFlightWebpackPlugin({
717688
isServer: false,
@@ -723,6 +694,9 @@ module.exports = function (webpackEnv) {
723694
}),
724695
// Fork End
725696
].filter(Boolean),
697+
experiments: {
698+
topLevelAwait: true,
699+
},
726700
// Turn off performance processing because we utilize
727701
// our own hints via the FileSizeReporter
728702
performance: false,

fixtures/flight/package.json

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"@babel/core": "^7.16.0",
88
"@babel/plugin-proposal-private-property-in-object": "^7.18.6",
9+
"@babel/preset-react": "^7.22.5",
910
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.3",
1011
"@svgr/webpack": "^5.5.0",
1112
"@testing-library/jest-dom": "^5.14.1",
@@ -26,9 +27,6 @@
2627
"css-minimizer-webpack-plugin": "^3.2.0",
2728
"dotenv": "^10.0.0",
2829
"dotenv-expand": "^5.1.0",
29-
"eslint": "^8.3.0",
30-
"eslint-config-react-app": "^7.0.1",
31-
"eslint-webpack-plugin": "^3.1.1",
3230
"file-loader": "^6.2.0",
3331
"fs-extra": "^10.0.0",
3432
"html-webpack-plugin": "^5.5.0",
@@ -45,7 +43,6 @@
4543
"postcss-preset-env": "^7.0.1",
4644
"prompts": "^2.4.2",
4745
"react": "experimental",
48-
"react-app-polyfill": "^3.0.0",
4946
"react-dev-utils": "^12.0.1",
5047
"react-dom": "experimental",
5148
"react-refresh": "^0.11.0",
@@ -75,12 +72,6 @@
7572
"build": "node scripts/build.js",
7673
"test": "node scripts/test.js --env=jsdom"
7774
},
78-
"eslintConfig": {
79-
"extends": [
80-
"react-app",
81-
"react-app/jest"
82-
]
83-
},
8475
"browserslist": {
8576
"production": [
8677
">0.2%",

fixtures/flight/server/global.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ const path = require('path');
77
// Do this as the first thing so that any code reading it knows the right env.
88
process.env.BABEL_ENV = process.env.NODE_ENV;
99

10-
const register = require('react-server-dom-webpack/node-register');
11-
register();
12-
1310
const babelRegister = require('@babel/register');
1411
babelRegister({
1512
babelrc: false,
@@ -25,8 +22,7 @@ babelRegister({
2522
return false;
2623
},
2724
],
28-
presets: ['react-app'],
29-
plugins: ['@babel/transform-modules-commonjs'],
25+
presets: ['@babel/preset-react'],
3026
});
3127

3228
// Ensure environment variables are read.

fixtures/flight/server/region.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ babelRegister({
2222
return false;
2323
},
2424
],
25-
presets: ['react-app'],
25+
presets: ['@babel/preset-react'],
2626
plugins: ['@babel/transform-modules-commonjs'],
2727
});
2828

fixtures/flight/src/App.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Container from './Container.js';
44

55
import {Counter} from './Counter.js';
66
import {Counter as Counter2} from './Counter2.js';
7+
import AsyncModule from './cjs/Counter3.js';
8+
const Counter3 = await(AsyncModule);
79

810
import ShowMore from './ShowMore.js';
911
import Button from './Button.js';
@@ -28,6 +30,7 @@ export default async function App() {
2830
<h1>{getServerState()}</h1>
2931
<Counter />
3032
<Counter2 />
33+
<Counter3 />
3134
<ul>
3235
{todos.map(todo => (
3336
<li key={todo.id}>{todo.text}</li>

fixtures/flight/src/cjs/Counter3.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use client";
2+
// CJS-ESM async module
3+
module.exports = import('../Counter.js').then(m => {
4+
return m.Counter
5+
});

fixtures/flight/src/cjs/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type":"commonjs"
3+
}

0 commit comments

Comments
 (0)