-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Node ESM Loader and Register Entrypoints (#20274)
* 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. * Add Node CommonJS regiter build This adds a build as a first-class export for legacy CommonJS registration in Node.js. This will grow in complexity so it deserves its own module. * Simplify fixture a bit to easier show usage with or without esm * Bump es version We leave async function in here which are newer than ES2015.
- Loading branch information
1 parent
bf7b7ae
commit 82e99e1
Showing
18 changed files
with
181 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {resolve, getSource} from 'react-transport-dom-webpack/node-loader'; | ||
|
||
export {resolve, getSource}; | ||
|
||
import babel from '@babel/core'; | ||
|
||
const babelOptions = { | ||
babelrc: false, | ||
ignore: [/\/(build|node_modules)\//], | ||
plugins: [ | ||
'@babel/plugin-syntax-import-meta', | ||
'@babel/plugin-transform-react-jsx', | ||
], | ||
}; | ||
|
||
export async function transformSource(source, context, defaultTransformSource) { | ||
const {format} = context; | ||
if (format === 'module') { | ||
const opt = Object.assign({filename: context.url}, babelOptions); | ||
const {code} = await babel.transformAsync(source, opt); | ||
return {source: code}; | ||
} | ||
return defaultTransformSource(source, context, defaultTransformSource); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/react-transport-dom-webpack/esm/react-transport-dom-webpack-node-loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/react-transport-dom-webpack/src/ReactFlightWebpackNodeLoader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/react-transport-dom-webpack/src/ReactFlightWebpackNodeRegister.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters