GeezerVison is showing github.com — Support named exports from client references (#20312) · react/react@3f73dce
File treeExpand file treeCollapse file tree Open diff view settings packages/react-transport-dom-webpack
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: fixtures/flight/loader/index.js +12-2Lines changed: 12 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import {resolve, getSource} from 'react-transport-dom-webpack/node-loader'; | | 1 | +import { | | 2 | + resolve, | | 3 | + getSource, | | 4 | + transformSource as reactTransformSource, | | 5 | +} from 'react-transport-dom-webpack/node-loader'; | 2 | 6 |
| 3 | 7 | export {resolve, getSource}; | 4 | 8 |
| @@ -13,7 +17,7 @@ const babelOptions = { | 13 | 17 | ], | 14 | 18 | }; | 15 | 19 |
| 16 | | -export async function transformSource(source, context, defaultTransformSource) { | | 20 | +async function babelTransformSource(source, context, defaultTransformSource) { | 17 | 21 | const {format} = context; | 18 | 22 | if (format === 'module') { | 19 | 23 | const opt = Object.assign({filename: context.url}, babelOptions); | @@ -22,3 +26,9 @@ export async function transformSource(source, context, defaultTransformSource) { | 22 | 26 | } | 23 | 27 | return defaultTransformSource(source, context, defaultTransformSource); | 24 | 28 | } | | 29 | + | | 30 | +export async function transformSource(source, context, defaultTransformSource) { | | 31 | + return reactTransformSource(source, context, (s, c) => { | | 32 | + return babelTransformSource(s, c, defaultTransformSource); | | 33 | + }); | | 34 | +} |
Collapse file Copy file name to clipboardExpand all lines: fixtures/flight/server/handler.server.js +27-6Lines changed: 27 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,14 +17,35 @@ module.exports = async function(req, res) { | 17 | 17 | pipeToNodeWritable(<App />, res, { | 18 | 18 | // TODO: Read from a map on the disk. | 19 | 19 | [resolve('../src/Counter.client.js')]: { | 20 | | - id: './src/Counter.client.js', | 21 | | - chunks: ['1'], | 22 | | - name: 'default', | | 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 | + }, | 23 | 32 | }, | 24 | 33 | [resolve('../src/ShowMore.client.js')]: { | 25 | | - id: './src/ShowMore.client.js', | 26 | | - chunks: ['2'], | 27 | | - name: 'default', | | 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 | + }, | 28 | 49 | }, | 29 | 50 | }); | 30 | 51 | }; |
Collapse file Copy file name to clipboardExpand all lines: fixtures/flight/src/App.server.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,8 @@ import * as React from 'react'; | 2 | 2 |
| 3 | 3 | import Container from './Container.js'; | 4 | 4 |
| 5 | | -import Counter from './Counter.client.js'; | | 5 | +import {Counter} from './Counter.client.js'; | | 6 | +import {Counter as Counter2} from './Counter2.client.js'; | 6 | 7 |
| 7 | 8 | import ShowMore from './ShowMore.client.js'; | 8 | 9 |
| @@ -11,6 +12,7 @@ export default function App() { | 11 | 12 | <Container> | 12 | 13 | <h1>Hello, world</h1> | 13 | 14 | <Counter /> | | 15 | + <Counter2 /> | 14 | 16 | <ShowMore> | 15 | 17 | <p>Lorem ipsum</p> | 16 | 18 | </ShowMore> | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/flight/src/Counter.client.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,7 +2,7 @@ import * as React from 'react'; | 2 | 2 |
| 3 | 3 | import Container from './Container.js'; | 4 | 4 |
| 5 | | -export default function Counter() { | | 5 | +export function Counter() { | 6 | 6 | const [count, setCount] = React.useState(0); | 7 | 7 | return ( | 8 | 8 | <Container> | |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export * from './Counter.client.js'; |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-webpack/package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 50 | 50 | "webpack": "^4.43.0" | 51 | 51 | }, | 52 | 52 | "dependencies": { | | 53 | + "acorn": "^6.2.1", | 53 | 54 | "loose-envify": "^1.1.0", | 54 | 55 | "object-assign": "^4.1.1" | 55 | 56 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-webpack/src/ReactFlightClientWebpackBundlerConfig.js +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,5 +59,16 @@ export function requireModule<T>(moduleData: ModuleReference<T>): T { | 59 | 59 | throw entry; | 60 | 60 | } | 61 | 61 | } | 62 | | - return __webpack_require__(moduleData.id)[moduleData.name]; | | 62 | + const moduleExports = __webpack_require__(moduleData.id); | | 63 | + if (moduleData.name === '*') { | | 64 | + // This is a placeholder value that represents that the caller imported this | | 65 | + // as a CommonJS module as is. | | 66 | + return moduleExports; | | 67 | + } | | 68 | + if (moduleData.name === '') { | | 69 | + // This is a placeholder value that represents that the caller accessed the | | 70 | + // default property of this if it was an ESM interop module. | | 71 | + return moduleExports.__esModule ? moduleExports.default : moduleExports; | | 72 | + } | | 73 | + return moduleExports[moduleData.name]; | 63 | 74 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-webpack/src/ReactFlightServerWebpackBundlerConfig.js +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | */ | 9 | 9 |
| 10 | 10 | type WebpackMap = { | 11 | | - [filename: string]: ModuleMetaData, | | 11 | + [filepath: string]: { | | 12 | + [name: string]: ModuleMetaData, | | 13 | + }, | 12 | 14 | }; | 13 | 15 |
| 14 | 16 | export type BundlerConfig = WebpackMap; | 15 | 17 |
| 16 | 18 | // eslint-disable-next-line no-unused-vars | 17 | 19 | export type ModuleReference<T> = { | 18 | 20 | $$typeof: Symbol, | | 21 | + filepath: string, | 19 | 22 | name: string, | 20 | 23 | }; | 21 | 24 |
| @@ -30,7 +33,7 @@ export type ModuleKey = string; | 30 | 33 | const MODULE_TAG = Symbol.for('react.module.reference'); | 31 | 34 |
| 32 | 35 | export function getModuleKey(reference: ModuleReference<any>): ModuleKey { | 33 | | - return reference.name; | | 36 | + return reference.filepath + '#' + reference.name; | 34 | 37 | } | 35 | 38 |
| 36 | 39 | export function isModuleReference(reference: Object): boolean { | @@ -41,5 +44,5 @@ export function resolveModuleMetaData<T>( | 41 | 44 | config: BundlerConfig, | 42 | 45 | moduleReference: ModuleReference<T>, | 43 | 46 | ): ModuleMetaData { | 44 | | - return config[moduleReference.name]; | | 47 | + return config[moduleReference.filepath][moduleReference.name]; | 45 | 48 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-webpack/src/ReactFlightWebpackNodeLoader.js +189-11Lines changed: 189 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| | 10 | +import acorn from 'acorn'; | | 11 | + | 10 | 12 | type ResolveContext = { | 11 | 13 | conditions: Array<string>, | 12 | 14 | parentURL: string | void, | @@ -16,11 +18,10 @@ type ResolveFunction = ( | 16 | 18 | string, | 17 | 19 | ResolveContext, | 18 | 20 | ResolveFunction, | 19 | | -) => Promise<string>; | | 21 | +) => Promise<{url: string}>; | 20 | 22 |
| 21 | 23 | type GetSourceContext = { | 22 | 24 | format: string, | 23 | | - url: string, | 24 | 25 | }; | 25 | 26 |
| 26 | 27 | type GetSourceFunction = ( | @@ -29,15 +30,32 @@ type GetSourceFunction = ( | 29 | 30 | GetSourceFunction, | 30 | 31 | ) => Promise<{source: Source}>; | 31 | 32 |
| | 33 | +type TransformSourceContext = { | | 34 | + format: string, | | 35 | + url: string, | | 36 | +}; | | 37 | + | | 38 | +type TransformSourceFunction = ( | | 39 | + Source, | | 40 | + TransformSourceContext, | | 41 | + TransformSourceFunction, | | 42 | +) => Promise<{source: Source}>; | | 43 | + | 32 | 44 | type Source = string | ArrayBuffer | Uint8Array; | 33 | 45 |
| 34 | 46 | let warnedAboutConditionsFlag = false; | 35 | 47 |
| | 48 | +let stashedGetSource: null | GetSourceFunction = null; | | 49 | +let stashedResolve: null | ResolveFunction = null; | | 50 | + | 36 | 51 | export async function resolve( | 37 | 52 | specifier: string, | 38 | 53 | context: ResolveContext, | 39 | 54 | defaultResolve: ResolveFunction, | 40 | | -): Promise<string> { | | 55 | +): Promise<{url: string}> { | | 56 | + // We stash this in case we end up needing to resolve export * statements later. | | 57 | + stashedResolve = defaultResolve; | | 58 | + | 41 | 59 | if (!context.conditions.includes('react-server')) { | 42 | 60 | context = { | 43 | 61 | ...context, | @@ -71,14 +89,174 @@ export async function getSource( | 71 | 89 | url: string, | 72 | 90 | context: GetSourceContext, | 73 | 91 | defaultGetSource: GetSourceFunction, | | 92 | +) { | | 93 | + // We stash this in case we end up needing to resolve export * statements later. | | 94 | + stashedGetSource = defaultGetSource; | | 95 | + return defaultGetSource(url, context, defaultGetSource); | | 96 | +} | | 97 | + | | 98 | +function addExportNames(names, node) { | | 99 | + switch (node.type) { | | 100 | + case 'Identifier': | | 101 | + names.push(node.name); | | 102 | + return; | | 103 | + case 'ObjectPattern': | | 104 | + for (let i = 0; i < node.properties.length; i++) | | 105 | + addExportNames(names, node.properties[i]); | | 106 | + return; | | 107 | + case 'ArrayPattern': | | 108 | + for (let i = 0; i < node.elements.length; i++) { | | 109 | + const element = node.elements[i]; | | 110 | + if (element) addExportNames(names, element); | | 111 | + } | | 112 | + return; | | 113 | + case 'Property': | | 114 | + addExportNames(names, node.value); | | 115 | + return; | | 116 | + case 'AssignmentPattern': | | 117 | + addExportNames(names, node.left); | | 118 | + return; | | 119 | + case 'RestElement': | | 120 | + addExportNames(names, node.argument); | | 121 | + return; | | 122 | + case 'ParenthesizedExpression': | | 123 | + addExportNames(names, node.expression); | | 124 | + return; | | 125 | + } | | 126 | +} | | 127 | + | | 128 | +function resolveClientImport( | | 129 | + specifier: string, | | 130 | + parentURL: string, | | 131 | +): Promise<{url: string}> { | | 132 | + // Resolve an import specifier as if it was loaded by the client. This doesn't use | | 133 | + // the overrides that this loader does but instead reverts to the default. | | 134 | + // This resolution algorithm will not necessarily have the same configuration | | 135 | + // as the actual client loader. It should mostly work and if it doesn't you can | | 136 | + // always convert to explicit exported names instead. | | 137 | + const conditions = ['node', 'import']; | | 138 | + if (stashedResolve === null) { | | 139 | + throw new Error( | | 140 | + 'Expected resolve to have been called before transformSource', | | 141 | + ); | | 142 | + } | | 143 | + return stashedResolve(specifier, {conditions, parentURL}, stashedResolve); | | 144 | +} | | 145 | + | | 146 | +async function loadClientImport( | | 147 | + url: string, | | 148 | + defaultTransformSource: TransformSourceFunction, | 74 | 149 | ): Promise<{source: Source}> { | 75 | | - if (url.endsWith('.client.js')) { | 76 | | - // TODO: Named exports. | 77 | | - const src = | 78 | | - "export default { $$typeof: Symbol.for('react.module.reference'), name: " + | 79 | | - JSON.stringify(url) + | 80 | | - '}'; | 81 | | - return {source: src}; | | 150 | + if (stashedGetSource === null) { | | 151 | + throw new Error( | | 152 | + 'Expected getSource to have been called before transformSource', | | 153 | + ); | 82 | 154 | } | 83 | | - return defaultGetSource(url, context, defaultGetSource); | | 155 | + // TODO: Validate that this is another module by calling getFormat. | | 156 | + const {source} = await stashedGetSource( | | 157 | + url, | | 158 | + {format: 'module'}, | | 159 | + stashedGetSource, | | 160 | + ); | | 161 | + return defaultTransformSource( | | 162 | + source, | | 163 | + {format: 'module', url}, | | 164 | + defaultTransformSource, | | 165 | + ); | | 166 | +} | | 167 | + | | 168 | +async function parseExportNamesInto( | | 169 | + transformedSource: string, | | 170 | + names: Array<string>, | | 171 | + parentURL: string, | | 172 | + defaultTransformSource, | | 173 | +): Promise<void> { | | 174 | + const {body} = acorn.parse(transformedSource, { | | 175 | + ecmaVersion: '2019', | | 176 | + sourceType: 'module', | | 177 | + }); | | 178 | + for (let i = 0; i < body.length; i++) { | | 179 | + const node = body[i]; | | 180 | + switch (node.type) { | | 181 | + case 'ExportAllDeclaration': | | 182 | + if (node.exported) { | | 183 | + addExportNames(names, node.exported); | | 184 | + continue; | | 185 | + } else { | | 186 | + const {url} = await resolveClientImport(node.source.value, parentURL); | | 187 | + const {source} = await loadClientImport(url, defaultTransformSource); | | 188 | + if (typeof source !== 'string') { | | 189 | + throw new Error('Expected the transformed source to be a string.'); | | 190 | + } | | 191 | + parseExportNamesInto(source, names, url, defaultTransformSource); | | 192 | + continue; | | 193 | + } | | 194 | + case 'ExportDefaultDeclaration': | | 195 | + names.push('default'); | | 196 | + continue; | | 197 | + case 'ExportNamedDeclaration': | | 198 | + if (node.declaration) { | | 199 | + if (node.declaration.type === 'VariableDeclaration') { | | 200 | + const declarations = node.declaration.declarations; | | 201 | + for (let j = 0; j < declarations.length; j++) { | | 202 | + addExportNames(names, declarations[j].id); | | 203 | + } | | 204 | + } else { | | 205 | + addExportNames(names, node.declaration.id); | | 206 | + } | | 207 | + } | | 208 | + if (node.specificers) { | | 209 | + const specificers = node.specificers; | | 210 | + for (let j = 0; j < specificers.length; j++) { | | 211 | + addExportNames(names, specificers[j].exported); | | 212 | + } | | 213 | + } | | 214 | + continue; | | 215 | + } | | 216 | + } | | 217 | +} | | 218 | + | | 219 | +export async function transformSource( | | 220 | + source: Source, | | 221 | + context: TransformSourceContext, | | 222 | + defaultTransformSource: TransformSourceFunction, | | 223 | +): Promise<{source: Source}> { | | 224 | + const transformed = await defaultTransformSource( | | 225 | + source, | | 226 | + context, | | 227 | + defaultTransformSource, | | 228 | + ); | | 229 | + if (context.format === 'module' && context.url.endsWith('.client.js')) { | | 230 | + const transformedSource = transformed.source; | | 231 | + if (typeof transformedSource !== 'string') { | | 232 | + throw new Error('Expected source to have been transformed to a string.'); | | 233 | + } | | 234 | + | | 235 | + const names = []; | | 236 | + await parseExportNamesInto( | | 237 | + transformedSource, | | 238 | + names, | | 239 | + context.url, | | 240 | + defaultTransformSource, | | 241 | + ); | | 242 | + | | 243 | + let newSrc = | | 244 | + "const MODULE_REFERENCE = Symbol.for('react.module.reference');\n"; | | 245 | + for (let i = 0; i < names.length; i++) { | | 246 | + const name = names[i]; | | 247 | + if (name === 'default') { | | 248 | + newSrc += 'export default '; | | 249 | + } else { | | 250 | + newSrc += 'export const ' + name + ' = '; | | 251 | + } | | 252 | + newSrc += '{ $$typeof: MODULE_REFERENCE, filepath: '; | | 253 | + newSrc += JSON.stringify(context.url); | | 254 | + newSrc += ', name: '; | | 255 | + newSrc += JSON.stringify(name); | | 256 | + newSrc += '};\n'; | | 257 | + } | | 258 | + | | 259 | + return {source: newSrc}; | | 260 | + } | | 261 | + return transformed; | 84 | 262 | } |
|