Skip to content

Commit 41c5d00

Browse files
authored
[Flight] Minimal webpack plugin (#20228)
1 parent e23673b commit 41c5d00

File tree

6 files changed

+52
-44
lines changed

6 files changed

+52
-44
lines changed

fixtures/flight/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# production
1212
/build
13+
/dist
1314

1415
# misc
1516
.DS_Store
+10-41
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,20 @@
11
'use strict';
22

33
import {pipeToNodeWritable} from 'react-transport-dom-webpack/server';
4+
import {readFileSync} from 'fs';
5+
import {resolve} from 'path';
46
import * as React from 'react';
57

6-
import url from 'url';
7-
8-
function resolve(path) {
9-
return url.pathToFileURL(require.resolve(path)).href;
10-
}
11-
128
module.exports = async function(req, res) {
13-
res.setHeader('Access-Control-Allow-Origin', '*');
149
const m = await import('../src/App.server.js');
1510
// const m = require('../src/App.server.js');
1611
const App = m.default.default || m.default;
17-
pipeToNodeWritable(<App />, res, {
18-
// TODO: Read from a map on the disk.
19-
[resolve('../src/Counter.client.js')]: {
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-
},
32-
},
33-
[resolve('../src/ShowMore.client.js')]: {
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-
},
49-
},
50-
});
12+
res.setHeader('Access-Control-Allow-Origin', '*');
13+
const moduleMap = JSON.parse(
14+
readFileSync(
15+
resolve(__dirname, '../dist/react-transport-manifest.json'),
16+
'utf8'
17+
)
18+
);
19+
pipeToNodeWritable(<App />, res, moduleMap);
5120
};

fixtures/flight/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ ReactDOM.render(
1919
);
2020

2121
// Create entry points for Client Components.
22-
// TODO: Webpack plugin should do this and write a map to disk.
22+
// TODO: Webpack plugin should do this.
2323
require.context('./', true, /\.client\.js$/, 'lazy');

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

+36-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,42 @@
77
* @flow
88
*/
99

10+
import {mkdirSync, writeFileSync} from 'fs';
11+
import {dirname, resolve} from 'path';
12+
import {pathToFileURL} from 'url';
13+
1014
export default class ReactFlightWebpackPlugin {
1115
constructor(options: {isServer: boolean}) {}
12-
apply(compiler: any) {}
16+
17+
apply(compiler: any) {
18+
compiler.hooks.emit.tap('React Transport Plugin', compilation => {
19+
const json = {};
20+
compilation.chunks.forEach(chunk => {
21+
chunk.getModules().forEach(mod => {
22+
if (!/\.client\.js$/.test(mod.resource)) {
23+
return;
24+
}
25+
const moduleExports = {};
26+
['', '*'].concat(mod.buildMeta.providedExports).forEach(name => {
27+
moduleExports[name] = {
28+
id: mod.id,
29+
chunks: chunk.ids,
30+
name: name,
31+
};
32+
});
33+
const href = pathToFileURL(mod.resource).href;
34+
if (href !== undefined) {
35+
json[href] = moduleExports;
36+
}
37+
});
38+
});
39+
const output = JSON.stringify(json, null, 2);
40+
const filename = resolve(
41+
compiler.options.output.path,
42+
'react-transport-manifest.json',
43+
);
44+
mkdirSync(dirname(filename), {recursive: true});
45+
writeFileSync(filename, output);
46+
});
47+
}
1348
}

scripts/rollup/bundles.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ const bundles = [
283283
moduleType: RENDERER_UTILS,
284284
entry: 'react-transport-dom-webpack/plugin',
285285
global: 'ReactFlightWebpackPlugin',
286-
externals: [],
286+
externals: ['fs', 'path', 'url'],
287287
},
288288

289289
/******* React Transport DOM Webpack Node.js Loader *******/

scripts/rollup/modules.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const {UMD_DEV, UMD_PROD, UMD_PROFILING} = require('./bundles').bundleTypes;
99
const HAS_NO_SIDE_EFFECTS_ON_IMPORT = false;
1010
// const HAS_SIDE_EFFECTS_ON_IMPORT = true;
1111
const importSideEffects = Object.freeze({
12+
fs: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
13+
path: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
1214
'prop-types/checkPropTypes': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
1315
'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
1416
scheduler: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
@@ -17,6 +19,7 @@ const importSideEffects = Object.freeze({
1719
'react/jsx-dev-runtime': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
1820
'react-fetch/node': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
1921
'react-dom': HAS_NO_SIDE_EFFECTS_ON_IMPORT,
22+
url: HAS_NO_SIDE_EFFECTS_ON_IMPORT,
2023
});
2124

2225
// Bundles exporting globals that other modules rely on.

0 commit comments

Comments
 (0)