Skip to content

Commit fdb90ec

Browse files
committed
Add Node ESM loader build
This adds a loader build as a first-class export. This will grow in complexity so it deserves its own module.
1 parent bf7b7ae commit fdb90ec

File tree

10 files changed

+112
-28
lines changed

10 files changed

+112
-28
lines changed

fixtures/flight/server/handler.server.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {pipeToNodeWritable} from 'react-transport-dom-webpack/server.js';
1+
import {pipeToNodeWritable} from 'react-transport-dom-webpack/server';
22
import * as React from 'react';
33
import App from '../src/App.server.js';
44

@@ -10,8 +10,10 @@ function resolve(relative) {
1010
}
1111

1212
export default function(req, res) {
13+
// In case this was a transpiled CommonJS import.
14+
const AppOrDefault = App.default || App;
1315
res.setHeader('Access-Control-Allow-Origin', '*');
14-
pipeToNodeWritable(<App />, res, {
16+
pipeToNodeWritable(<AppOrDefault />, res, {
1517
// TODO: Read from a map on the disk.
1618
[resolve('../src/Counter.client.js')]: {
1719
id: './src/Counter.client.js',

fixtures/flight/server/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

3+
const url = require('url');
4+
35
require.extensions['.client.js'] = function(module, path) {
46
module.exports = {
57
$$typeof: Symbol.for('react.module.reference'),
6-
name: path,
8+
name: url.pathToFileURL(path).href,
79
};
810
};
911

@@ -26,7 +28,7 @@ app.get('/', function(req, res) {
2628
}
2729
}
2830
import('./handler.server.mjs').then(m => m.default(req, res));
29-
// require('./handler.server.js')(req, res);
31+
// require('./handler.server.js')(req, res);
3032
});
3133

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

fixtures/flight/server/loader.mjs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import {
2+
resolve,
3+
getSource,
4+
} from 'react-transport-dom-webpack/node-loader';
5+
6+
export {resolve, getSource};
7+
18
import babel from '@babel/core';
29

3-
const options = {
10+
const babelOptions = {
411
babelrc: false,
512
ignore: [/\/(build|node_modules)\//],
613
plugins: [
@@ -9,34 +16,15 @@ const options = {
916
],
1017
};
1118

12-
const optionsCommonJS = {
13-
ignore: [/\/(build|node_modules)\//],
14-
presets: ['react-app'],
15-
plugins: ['@babel/transform-modules-commonjs'],
16-
};
17-
1819
export async function transformSource(source, context, defaultTransformSource) {
1920
const {format} = context;
20-
if (format === 'module' || format === 'commonjs') {
21+
if (format === 'module') {
2122
const opt = Object.assign(
2223
{filename: context.url},
23-
format === 'commonjs' ? optionsCommonJS : options
24+
babelOptions
2425
);
2526
const {code} = await babel.transformAsync(source, opt);
2627
return {source: code};
2728
}
28-
return defaultTransformSource(source, context);
29-
}
30-
31-
export async function getSource(url, context, defaultGetSource) {
32-
if (url.endsWith('.client.js')) {
33-
const name = url;
34-
return {
35-
source:
36-
"export default { $$typeof: Symbol.for('react.module.reference'), name: " +
37-
JSON.stringify(name) +
38-
'}',
39-
};
40-
}
41-
return defaultGetSource(url, context, defaultGetSource);
29+
return defaultTransformSource(source, context, defaultTransformSource);
4230
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export * from '../src/ReactFlightWebpackNodeLoader.js';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@
1818
"server.browser.js",
1919
"server.node.js",
2020
"cjs/",
21-
"umd/"
21+
"esm/"
2222
],
23+
"exports": {
24+
".": "./index.js",
25+
"./plugin": "./plugin.js",
26+
"./server": "./server.js",
27+
"./server.browser": "./server.browser.js",
28+
"./server.node": "./server.node.js",
29+
"./node-loader": "./esm/react-transport-dom-webpack-node-loader.js",
30+
"./package.json": "./package.json"
31+
},
2332
"browser": {
2433
"./server.js": "./server.browser.js"
2534
},
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
type ResolveContext = {
11+
conditions: Array<string>,
12+
parentURL: string | void,
13+
};
14+
15+
type ResolveFunction = (
16+
string,
17+
ResolveContext,
18+
ResolveFunction,
19+
) => Promise<string>;
20+
21+
type GetSourceContext = {
22+
format: string,
23+
url: string,
24+
};
25+
26+
type GetSourceFunction = (
27+
string,
28+
GetSourceContext,
29+
GetSourceFunction,
30+
) => Promise<{source: Source}>;
31+
32+
type Source = string | ArrayBuffer | Uint8Array;
33+
34+
export async function resolve(
35+
specifier: string,
36+
context: ResolveContext,
37+
defaultResolve: ResolveFunction,
38+
): Promise<string> {
39+
// TODO: Resolve server-only files.
40+
return defaultResolve(specifier, context, defaultResolve);
41+
}
42+
43+
export async function getSource(
44+
url: string,
45+
context: GetSourceContext,
46+
defaultGetSource: GetSourceFunction,
47+
): Promise<{source: Source}> {
48+
if (url.endsWith('.client.js')) {
49+
// TODO: Named exports.
50+
const src =
51+
"export default { $$typeof: Symbol.for('react.module.reference'), name: " +
52+
JSON.stringify(url) +
53+
'}';
54+
return {source: src};
55+
}
56+
return defaultGetSource(url, context, defaultGetSource);
57+
}

scripts/rollup/bundles.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@ const bundles = [
286286
externals: [],
287287
},
288288

289+
/******* React Transport DOM Webpack Node.js Loader *******/
290+
{
291+
bundleTypes: [NODE_ESM],
292+
moduleType: RENDERER_UTILS,
293+
entry: 'react-transport-dom-webpack/node-loader',
294+
global: 'ReactFlightWebpackNodeLoader',
295+
externals: [],
296+
},
297+
289298
/******* React Transport DOM Server Relay *******/
290299
{
291300
bundleTypes: [FB_WWW_DEV, FB_WWW_PROD],

scripts/shared/pathsByLanguageVersion.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
const esNextPaths = [
1111
// Internal forwarding modules
1212
'packages/*/*.js',
13+
'packages/*/esm/*.js',
1314
// Source files
1415
'packages/*/src/**/*.js',
1516
'packages/dom-event-testing-library/**/*.js',

0 commit comments

Comments
 (0)