Skip to content

Commit 3f73dce

Browse files
Support named exports from client references (#20312)
* Rename "name"->"filepath" field on Webpack module references This field name will get confused with the imported name or the module id. * Switch back to transformSource instead of getSource getSource would be more efficient in the cases where we don't need to read the original file but we'll need to most of the time. Even then, we can't return a JS file if we're trying to support non-JS loader because it'll end up being transformed. Similarly, we'll need to parse the file and we can't parse it before it's transformed. So we need to chain with other loaders that know how. * Add acorn dependency This should be the version used by Webpack since we have a dependency on Webpack anyway. * Parse exported names of ESM modules We need to statically resolve the names that a client component will export so that we can export a module reference for each of the names. For export * from, this gets tricky because we need to also load the source of the next file to parse that. We don't know exactly how the client is built so we guess it's somewhat default. * Handle imported names one level deep in CommonJS using a Proxy We use a proxy to see what property the server access and that will tell us which property we'll want to import on the client. * Add export name to module reference and Webpack map To support named exports each name needs to be encoded as a separate reference. It's possible with module splitting that different exports end up in different chunks. It's also possible that the export is renamed as part of minification. So the map also includes a map from the original to the bundled name. * Special case plain CJS requires and conditional imports using __esModule This models if the server tries to import .default or a plain require. We should replicate the same thing on the client when we load that module reference. * Dedupe acorn-related deps Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
1 parent 565148d commit 3f73dce

14 files changed

Lines changed: 322 additions & 61 deletions

fixtures/flight/loader/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import {resolve, getSource} from 'react-transport-dom-webpack/node-loader';
1+
import {
2+
resolve,
3+
getSource,
4+
transformSource as reactTransformSource,
5+
} from 'react-transport-dom-webpack/node-loader';
26

37
export {resolve, getSource};
48

@@ -13,7 +17,7 @@ const babelOptions = {
1317
],
1418
};
1519

16-
export async function transformSource(source, context, defaultTransformSource) {
20+
async function babelTransformSource(source, context, defaultTransformSource) {
1721
const {format} = context;
1822
if (format === 'module') {
1923
const opt = Object.assign({filename: context.url}, babelOptions);
@@ -22,3 +26,9 @@ export async function transformSource(source, context, defaultTransformSource) {
2226
}
2327
return defaultTransformSource(source, context, defaultTransformSource);
2428
}
29+
30+
export async function transformSource(source, context, defaultTransformSource) {
31+
return reactTransformSource(source, context, (s, c) => {
32+
return babelTransformSource(s, c, defaultTransformSource);
33+
});
34+
}

fixtures/flight/server/handler.server.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,35 @@ module.exports = async function(req, res) {
1717
pipeToNodeWritable(<App />, res, {
1818
// TODO: Read from a map on the disk.
1919
[resolve('../src/Counter.client.js')]: {
20-
id: './src/Counter.client.js',
21-
chunks: ['1'],
22-
name: 'default',
20+
Counter: {
21+
id: './src/Counter.client.js',
22+
chunks: ['2'],
23+
name: 'Counter',
24+
},
25+
},
26+
[resolve('../src/Counter2.client.js')]: {
27+
Counter: {
28+
id: './src/Counter2.client.js',
29+
chunks: ['1'],
30+
name: 'Counter',
31+
},
2332
},
2433
[resolve('../src/ShowMore.client.js')]: {
25-
id: './src/ShowMore.client.js',
26-
chunks: ['2'],
27-
name: 'default',
34+
default: {
35+
id: './src/ShowMore.client.js',
36+
chunks: ['3'],
37+
name: 'default',
38+
},
39+
'': {
40+
id: './src/ShowMore.client.js',
41+
chunks: ['3'],
42+
name: '',
43+
},
44+
'*': {
45+
id: './src/ShowMore.client.js',
46+
chunks: ['3'],
47+
name: '*',
48+
},
2849
},
2950
});
3051
};

fixtures/flight/src/App.server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as React from 'react';
22

33
import Container from './Container.js';
44

5-
import Counter from './Counter.client.js';
5+
import {Counter} from './Counter.client.js';
6+
import {Counter as Counter2} from './Counter2.client.js';
67

78
import ShowMore from './ShowMore.client.js';
89

@@ -11,6 +12,7 @@ export default function App() {
1112
<Container>
1213
<h1>Hello, world</h1>
1314
<Counter />
15+
<Counter2 />
1416
<ShowMore>
1517
<p>Lorem ipsum</p>
1618
</ShowMore>

fixtures/flight/src/Counter.client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22

33
import Container from './Container.js';
44

5-
export default function Counter() {
5+
export function Counter() {
66
const [count, setCount] = React.useState(0);
77
return (
88
<Container>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Counter.client.js';

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"webpack": "^4.43.0"
5151
},
5252
"dependencies": {
53+
"acorn": "^6.2.1",
5354
"loose-envify": "^1.1.0",
5455
"object-assign": "^4.1.1"
5556
},

packages/react-transport-dom-webpack/src/ReactFlightClientWebpackBundlerConfig.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,16 @@ export function requireModule<T>(moduleData: ModuleReference<T>): T {
5959
throw entry;
6060
}
6161
}
62-
return __webpack_require__(moduleData.id)[moduleData.name];
62+
const moduleExports = __webpack_require__(moduleData.id);
63+
if (moduleData.name === '*') {
64+
// This is a placeholder value that represents that the caller imported this
65+
// as a CommonJS module as is.
66+
return moduleExports;
67+
}
68+
if (moduleData.name === '') {
69+
// This is a placeholder value that represents that the caller accessed the
70+
// default property of this if it was an ESM interop module.
71+
return moduleExports.__esModule ? moduleExports.default : moduleExports;
72+
}
73+
return moduleExports[moduleData.name];
6374
}

packages/react-transport-dom-webpack/src/ReactFlightServerWebpackBundlerConfig.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
*/