Skip to content

Commit e855f91

Browse files
authored
[Flight] Expand the fixture to use require.extensions (#20209)
* Expand fixture Use .server convention. /server/index.js should really change too so it can be compiled but for now we treat it as bootstrapping code outside the compiled code. Move App.server. It's part of the application code rather than the infra. Add hybrid component used in both server/client and an extra component shared by multiple entry points. * Use require.extensions to replace .client imports The simplest server doesn't need AOT compilation. Instead we can just configure require.extensions. This is probably not the best idea to use in prod but is enough to show the set up.
1 parent c896cf9 commit e855f91

File tree

8 files changed

+59
-21
lines changed

8 files changed

+59
-21
lines changed

fixtures/flight/server/App.server.js

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

fixtures/flight/server/handler.js renamed to fixtures/flight/server/handler.server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
import {pipeToNodeWritable} from 'react-transport-dom-webpack/server';
44
import * as React from 'react';
5-
import App from './App.server';
5+
import App from '../src/App.server';
66

77
module.exports = function(req, res) {
88
res.setHeader('Access-Control-Allow-Origin', '*');
99
pipeToNodeWritable(<App />, res, {
1010
// TODO: Read from a map on the disk.
11-
'./src/Counter.client.js': {
11+
[require.resolve('../src/Counter.client.js')]: {
1212
id: './src/Counter.client.js',
13+
chunks: ['1'],
14+
name: 'default',
15+
},
16+
[require.resolve('../src/ShowMore.client.js')]: {
17+
id: './src/ShowMore.client.js',
1318
chunks: ['2'],
1419
name: 'default',
1520
},

fixtures/flight/server/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
'use strict';
22

3+
require.extensions['.client.js'] = function(module, path) {
4+
module.exports = {
5+
$$typeof: Symbol.for('react.module.reference'),
6+
name: path,
7+
};
8+
};
9+
310
const babelRegister = require('@babel/register');
411

512
babelRegister({
@@ -18,7 +25,7 @@ app.get('/', function(req, res) {
1825
delete require.cache[key];
1926
}
2027
}
21-
require('./handler')(req, res);
28+
require('./handler.server')(req, res);
2229
});
2330

2431
app.listen(3001, () => {

fixtures/flight/src/App.server.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as React from 'react';
2+
3+
import Container from './Container';
4+
5+
import Counter from './Counter.client';
6+
7+
import ShowMore from './ShowMore.client';
8+
9+
export default function App() {
10+
return (
11+
<Container>
12+
<h1>Hello, world</h1>
13+
<Counter />
14+
<ShowMore>
15+
<p>Lorem ipsum</p>
16+
</ShowMore>
17+
</Container>
18+
);
19+
}

fixtures/flight/src/Container.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as React from 'react';
2+
3+
export default function Container({children}) {
4+
return <div>{children}</div>;
5+
}

fixtures/flight/src/Counter.client.js

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

3+
import Container from './Container';
4+
35
export default function Counter() {
46
const [count, setCount] = React.useState(0);
5-
return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>;
7+
return (
8+
<Container>
9+
<button onClick={() => setCount(c => c + 1)}>Count: {count}</button>
10+
</Container>
11+
);
612
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react';
2+
3+
import Container from './Container';
4+
5+
export default function ShowMore({children}) {
6+
const [show, setShow] = React.useState(false);
7+
if (!show) {
8+
return <button onClick={() => setShow(true)}>Show More</button>;
9+
}
10+
return <Container>{children}</Container>;
11+
}

fixtures/flight/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, {Suspense} from 'react';
1+
import * as React from 'react';
2+
import {Suspense} from 'react';
23
import ReactDOM from 'react-dom';
34
import ReactTransportDOMClient from 'react-transport-dom-webpack';
45

0 commit comments

Comments
 (0)