Skip to content

Add Node ESM Loader and Register Entrypoints #20274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions fixtures/flight/server/handler.server.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {pipeToNodeWritable} from 'react-transport-dom-webpack/server.js';
import {pipeToNodeWritable} from 'react-transport-dom-webpack/server';
import * as React from 'react';
import App from '../src/App.server.js';

Expand All @@ -10,8 +10,10 @@ function resolve(relative) {
}

export default function(req, res) {
// In case this was a transpiled CommonJS import.
const AppOrDefault = App.default || App;
res.setHeader('Access-Control-Allow-Origin', '*');
pipeToNodeWritable(<App />, res, {
pipeToNodeWritable(<AppOrDefault />, res, {
// TODO: Read from a map on the disk.
[resolve('../src/Counter.client.js')]: {
id: './src/Counter.client.js',
Expand Down
10 changes: 3 additions & 7 deletions fixtures/flight/server/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict';

require.extensions['.client.js'] = function(module, path) {
module.exports = {
$$typeof: Symbol.for('react.module.reference'),
name: path,
};
};
const register = require('react-transport-dom-webpack/node-register');
register();

const babelRegister = require('@babel/register');

Expand All @@ -26,7 +22,7 @@ app.get('/', function(req, res) {
}
}
import('./handler.server.mjs').then(m => m.default(req, res));
// require('./handler.server.js')(req, res);
// require('./handler.server.js')(req, res);
});

app.listen(3001, () => {
Expand Down
34 changes: 11 additions & 23 deletions fixtures/flight/server/loader.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import {
resolve,
getSource,
} from 'react-transport-dom-webpack/node-loader';

export {resolve, getSource};

import babel from '@babel/core';

const options = {
const babelOptions = {
babelrc: false,
ignore: [/\/(build|node_modules)\//],
plugins: [
Expand All @@ -9,34 +16,15 @@ const options = {
],
};

const optionsCommonJS = {
ignore: [/\/(build|node_modules)\//],
presets: ['react-app'],
plugins: ['@babel/transform-modules-commonjs'],
};

export async function transformSource(source, context, defaultTransformSource) {
const {format} = context;
if (format === 'module' || format === 'commonjs') {
if (format === 'module') {
const opt = Object.assign(
{filename: context.url},
format === 'commonjs' ? optionsCommonJS : options
babelOptions
);
const {code} = await babel.transformAsync(source, opt);
return {source: code};
}
return defaultTransformSource(source, context);
}

export async function getSource(url, context, defaultGetSource) {
if (url.endsWith('.client.js')) {
const name = url;
return {
source:
"export default { $$typeof: Symbol.for('react.module.reference'), name: " +
JSON.stringify(name) +
'}',
};
}
return defaultGetSource(url, context, defaultGetSource);
return defaultTransformSource(source, context, defaultTransformSource);
}
3 changes: 3 additions & 0 deletions packages/react-transport-dom-webpack/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export * from '../src/ReactFlightWebpackNodeLoader.js';
10 changes: 10 additions & 0 deletions packages/react-transport-dom-webpack/node-register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export * from './src/ReactFlightWebpackNodeRegister';
3 changes: 3 additions & 0 deletions packages/react-transport-dom-webpack/npm/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
3 changes: 3 additions & 0 deletions packages/react-transport-dom-webpack/npm/node-register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./cjs/react-transport-dom-webpack-node-register.js');
14 changes: 13 additions & 1 deletion packages/react-transport-dom-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,21 @@
"server.js",
"server.browser.js",
"server.node.js",
"node-register.js",
"cjs/",
"umd/"
"umd/",
"esm/"
],
"exports": {
".": "./index.js",
"./plugin": "./plugin.js",
"./server": "./server.js",
"./server.browser": "./server.browser.js",
"./server.node": "./server.node.js",
"./node-loader": "./esm/react-transport-dom-webpack-node-loader.js",
"./node-register": "./node-register.js",
"./package.json": "./package.json"
},
"browser": {
"./server.js": "./server.browser.js"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

type ResolveContext = {
conditions: Array<string>,
parentURL: string | void,
};

type ResolveFunction = (
string,
ResolveContext,
ResolveFunction,
) => Promise<string>;

type GetSourceContext = {
format: string,
url: string,
};

type GetSourceFunction = (
string,
GetSourceContext,
GetSourceFunction,
) => Promise<{source: Source}>;

type Source = string | ArrayBuffer | Uint8Array;

export async function resolve(
specifier: string,
context: ResolveContext,
defaultResolve: ResolveFunction,
): Promise<string> {
// TODO: Resolve server-only files.
return defaultResolve(specifier, context, defaultResolve);
}

export async function getSource(
url: string,
context: GetSourceContext,
defaultGetSource: GetSourceFunction,
): Promise<{source: Source}> {
if (url.endsWith('.client.js')) {
// TODO: Named exports.
const src =
"export default { $$typeof: Symbol.for('react.module.reference'), name: " +
JSON.stringify(url) +
'}';
return {source: src};
}
return defaultGetSource(url, context, defaultGetSource);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

const url = require('url');

module.exports = function register() {
(require: any).extensions['.client.js'] = function(module, path) {
module.exports = {
$$typeof: Symbol.for('react.module.reference'),
name: url.pathToFileURL(path).href,
};
};
};
18 changes: 18 additions & 0 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,24 @@ const bundles = [
externals: [],
},

/******* React Transport DOM Webpack Node.js Loader *******/
{
bundleTypes: [NODE_ESM],
moduleType: RENDERER_UTILS,
entry: 'react-transport-dom-webpack/node-loader',
global: 'ReactFlightWebpackNodeLoader',
externals: [],
},

/******* React Transport DOM Webpack Node.js CommonJS Loader *******/
{
bundleTypes: [NODE_ES2015],
moduleType: RENDERER_UTILS,
entry: 'react-transport-dom-webpack/node-register',
global: 'ReactFlightWebpackNodeRegister',
externals: ['url'],
},

/******* React Transport DOM Server Relay *******/
{
bundleTypes: [FB_WWW_DEV, FB_WWW_PROD],
Expand Down
1 change: 1 addition & 0 deletions scripts/shared/pathsByLanguageVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const esNextPaths = [
// Internal forwarding modules
'packages/*/*.js',
'packages/*/esm/*.js',
// Source files
'packages/*/src/**/*.js',
'packages/dom-event-testing-library/**/*.js',
Expand Down