-
Notifications
You must be signed in to change notification settings - Fork 46.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test the node-register hooks in unit tests (#25132)
- Loading branch information
1 parent
3f70e68
commit 38c5d8a
Showing
7 changed files
with
94 additions
and
62 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
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
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
67 changes: 67 additions & 0 deletions
67
packages/react-server-dom-webpack/src/__tests__/utils/WebpackMock.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,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; | ||
}; |
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