-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
[Flight] Test the node-register hooks in unit tests #25132
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ module.exports = function register() { | |
}, | ||
}; | ||
|
||
(require: any).extensions['.client.js'] = function(module, path) { | ||
Module._extensions['.client.js'] = function(module, path) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this change only because of tests. jest annoyingly overrides So I moved it to use We currently override |
||
const moduleId = url.pathToFileURL(path).href; | ||
const moduleReference: {[string]: any} = { | ||
$$typeof: MODULE_REFERENCE, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const url = require('url'); | ||
const Module = require('module'); | ||
|
||
let webpackModuleIdx = 0; | ||
const webpackModules = {}; | ||
const webpackMap = {}; | ||
global.__webpack_require__ = function(id) { | ||
return webpackModules[id]; | ||
}; | ||
|
||
const previousLoader = Module._extensions['.client.js']; | ||
|
||
const register = require('react-server-dom-webpack/node-register'); | ||
// Register node loader | ||
register(); | ||
|
||
const nodeLoader = Module._extensions['.client.js']; | ||
|
||
if (previousLoader === nodeLoader) { | ||
throw new Error( | ||
'Expected the Node loader to register the .client.js extension', | ||
); | ||
} | ||
|
||
Module._extensions['.client.js'] = previousLoader; | ||
|
||
exports.webpackMap = webpackMap; | ||
exports.webpackModules = webpackModules; | ||
|
||
exports.clientExports = function clientExports(moduleExports) { | ||
const idx = '' + webpackModuleIdx++; | ||
webpackModules[idx] = moduleExports; | ||
const path = url.pathToFileURL(idx).href; | ||
webpackMap[path] = { | ||
'': { | ||
id: idx, | ||
chunks: [], | ||
name: '', | ||
}, | ||
'*': { | ||
id: idx, | ||
chunks: [], | ||
name: '*', | ||
}, | ||
}; | ||
for (const name in moduleExports) { | ||
webpackMap[path] = { | ||
[name]: { | ||
id: idx, | ||
chunks: [], | ||
name: name, | ||
}, | ||
}; | ||
} | ||
const mod = {exports: {}}; | ||
nodeLoader(mod, idx); | ||
return mod.exports; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just because jest's treatment of ESM is not the same as rollup. So I switched this to use the jest way, and then just skip this file in the rollup bundling.