GeezerVison is showing github.com — Disallow *.server.js imports from any other files (#20309) · react/react@565148d
File treeExpand file treeCollapse file tree Open diff view settings packages/react-transport-dom-webpack/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboard +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | { | 2 | | - "type": "commonjs" | | 2 | + "type": "commonjs", | | 3 | + "main": "./cli.server.js" | 3 | 4 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-webpack/src/ReactFlightWebpackNodeLoader.js +28-1Lines changed: 28 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,12 +31,39 @@ type GetSourceFunction = ( | 31 | 31 |
| 32 | 32 | type Source = string | ArrayBuffer | Uint8Array; | 33 | 33 |
| | 34 | +let warnedAboutConditionsFlag = false; | | 35 | + | 34 | 36 | export async function resolve( | 35 | 37 | specifier: string, | 36 | 38 | context: ResolveContext, | 37 | 39 | defaultResolve: ResolveFunction, | 38 | 40 | ): Promise<string> { | 39 | | - // TODO: Resolve server-only files. | | 41 | + if (!context.conditions.includes('react-server')) { | | 42 | + context = { | | 43 | + ...context, | | 44 | + conditions: [...context.conditions, 'react-server'], | | 45 | + }; | | 46 | + if (!warnedAboutConditionsFlag) { | | 47 | + warnedAboutConditionsFlag = true; | | 48 | + // eslint-disable-next-line react-internal/no-production-logging | | 49 | + console.warn( | | 50 | + 'You did not run Node.js with the `--conditions react-server` flag. ' + | | 51 | + 'Any "react-server" override will only work with ESM imports.', | | 52 | + ); | | 53 | + } | | 54 | + } | | 55 | + // We intentionally check the specifier here instead of the resolved file. | | 56 | + // This allows package exports to configure non-server aliases that resolve to server files | | 57 | + // depending on environment. It's probably a bad idea to export a server file as "main" though. | | 58 | + if (specifier.endsWith('.server.js')) { | | 59 | + if (context.parentURL && !context.parentURL.endsWith('.server.js')) { | | 60 | + throw new Error( | | 61 | + `Cannot import "${specifier}" from "${context.parentURL}". ` + | | 62 | + 'By react-server convention, .server.js files can only be imported from other .server.js files. ' + | | 63 | + 'That way nobody accidentally sends these to the client by indirectly importing it.', | | 64 | + ); | | 65 | + } | | 66 | + } | 40 | 67 | return defaultResolve(specifier, context, defaultResolve); | 41 | 68 | } | 42 | 69 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-transport-dom-webpack/src/ReactFlightWebpackNodeRegister.js +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | const url = require('url'); | 11 | 11 |
| | 12 | +// $FlowFixMe | | 13 | +const Module = require('module'); | | 14 | + | 12 | 15 | module.exports = function register() { | 13 | 16 | (require: any).extensions['.client.js'] = function(module, path) { | 14 | 17 | module.exports = { | 15 | 18 | $$typeof: Symbol.for('react.module.reference'), | 16 | 19 | name: url.pathToFileURL(path).href, | 17 | 20 | }; | 18 | 21 | }; | | 22 | + | | 23 | + const originalResolveFilename = Module._resolveFilename; | | 24 | + | | 25 | + Module._resolveFilename = function(request, parent, isMain, options) { | | 26 | + // We intentionally check the request here instead of the resolved file. | | 27 | + // This allows package exports to configure non-server aliases that resolve to server files | | 28 | + // depending on environment. It's probably a bad idea to export a server file as "main" though. | | 29 | + if (request.endsWith('.server.js')) { | | 30 | + if ( | | 31 | + parent && | | 32 | + parent.filename && | | 33 | + !parent.filename.endsWith('.server.js') | | 34 | + ) { | | 35 | + throw new Error( | | 36 | + `Cannot import "${request}" from "${parent.filename}". ` + | | 37 | + 'By react-server convention, .server.js files can only be imported from other .server.js files. ' + | | 38 | + 'That way nobody accidentally sends these to the client by indirectly importing it.', | | 39 | + ); | | 40 | + } | | 41 | + } | | 42 | + return originalResolveFilename.apply(this, arguments); | | 43 | + }; | 19 | 44 | }; |
Collapse file Copy file name to clipboardExpand all lines: scripts/rollup/bundles.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -310,7 +310,7 @@ const bundles = [ | 310 | 310 | moduleType: RENDERER_UTILS, | 311 | 311 | entry: 'react-transport-dom-webpack/node-register', | 312 | 312 | global: 'ReactFlightWebpackNodeRegister', | 313 | | - externals: ['url'], | | 313 | + externals: ['url', 'module'], | 314 | 314 | }, | 315 | 315 |
| 316 | 316 | /******* React Transport DOM Server Relay *******/ | |
|