Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Use options param for clientExports in WebpackMock
  • Loading branch information
unstubbable committed Sep 13, 2024
commit 0a8d96d46fabbbabefd92f11a76177fb04c8c8d2
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,15 @@ describe('ReactFlightDOMBrowser', () => {
function ClientInner({children}) {
return <span>{children}</span>;
},
'42',
'/test.js',
new Promise(resolve => (resolveClientComponentChunk = resolve)),
{
chunk: {
id: '42',
filename: '/test.js',
promise: new Promise(
resolve => (resolveClientComponentChunk = resolve),
),
},
},
);

function Server() {
Expand Down Expand Up @@ -258,9 +264,15 @@ describe('ReactFlightDOMBrowser', () => {
function ClientInner({value}) {
return <pre>{JSON.stringify(value)}</pre>;
},
'42',
'/test.js',
new Promise(resolve => (resolveClientComponentChunk = resolve)),
{
chunk: {
id: '42',
filename: '/test.js',
promise: new Promise(
resolve => (resolveClientComponentChunk = resolve),
),
},
},
);

function Server({value}) {
Expand Down Expand Up @@ -309,9 +321,15 @@ describe('ReactFlightDOMBrowser', () => {
function ClientInner({value}) {
return <pre>{JSON.stringify(value)}</pre>;
},
'42',
'/test.js',
new Promise(resolve => (resolveClientComponentChunk = resolve)),
{
chunk: {
id: '42',
filename: '/test.js',
promise: new Promise(
resolve => (resolveClientComponentChunk = resolve),
),
},
},
);

function Server({value}) {
Expand Down Expand Up @@ -355,12 +373,15 @@ describe('ReactFlightDOMBrowser', () => {
it('should resolve deduped objects that are themselves blocked', async () => {
let resolveClientComponentChunk;

const Client = clientExports(
[4, 5],
'42',
'/test.js',
new Promise(resolve => (resolveClientComponentChunk = resolve)),
);
const Client = clientExports([4, 5], {
chunk: {
id: '42',
filename: '/test.js',
promise: new Promise(
resolve => (resolveClientComponentChunk = resolve),
),
},
});

const shared = [1, 2, 3, Client];

Expand Down Expand Up @@ -407,9 +428,15 @@ describe('ReactFlightDOMBrowser', () => {
function ClientOuter({children, value}) {
return children;
},
'1',
'/outer.js',
new Promise(resolve => (resolveOuterClientComponentChunk = resolve)),
{
chunk: {
id: '1',
filename: '/outer.js',
promise: new Promise(
resolve => (resolveOuterClientComponentChunk = resolve),
),
},
},
);

function PassthroughServerComponent({children}) {
Expand All @@ -420,9 +447,15 @@ describe('ReactFlightDOMBrowser', () => {
function ClientInner({children}) {
return JSON.stringify(children);
},
'2',
'/inner.js',
new Promise(resolve => (resolveInnerClientComponentChunk = resolve)),
{
chunk: {
id: '2',
filename: '/inner.js',
promise: new Promise(
resolve => (resolveInnerClientComponentChunk = resolve),
),
},
},
);

const value = {};
Expand Down Expand Up @@ -475,18 +508,30 @@ describe('ReactFlightDOMBrowser', () => {
function FooClient({children}) {
return JSON.stringify(children);
},
'1',
'/foo.js',
new Promise(resolve => (resolveFooClientComponentChunk = resolve)),
{
chunk: {
id: '1',
filename: '/foo.js',
promise: new Promise(
resolve => (resolveFooClientComponentChunk = resolve),
),
},
},
);

const BarClient = clientExports(
function BarClient() {
return 'not used';
},
'2',
'/bar.js',
new Promise(resolve => (resolveBarClientComponentChunk = resolve)),
{
chunk: {
id: '2',
filename: '/bar.js',
promise: new Promise(
resolve => (resolveBarClientComponentChunk = resolve),
),
},
},
);

const shared = {foo: 1};
Expand Down Expand Up @@ -539,9 +584,15 @@ describe('ReactFlightDOMBrowser', () => {
function Foo({children, item}) {
return children;
},
'1',
'/foo.js',
new Promise(resolve => (resolveFooClientComponentChunk = resolve)),
{
chunk: {
id: '1',
filename: '/foo.js',
promise: new Promise(
resolve => (resolveFooClientComponentChunk = resolve),
),
},
},
);

const shared = <div />;
Expand Down Expand Up @@ -590,9 +641,15 @@ describe('ReactFlightDOMBrowser', () => {
function Foo({children, item}) {
return children;
},
'1',
'/foo.js',
new Promise(resolve => (resolveFooClientComponentChunk = resolve)),
{
chunk: {
id: '1',
filename: '/foo.js',
promise: new Promise(
resolve => (resolveFooClientComponentChunk = resolve),
),
},
},
);

const shared = <div />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ describe('ReactFlightDOMNode', () => {
}
// The Client build may not have the same IDs as the Server bundles for the same
// component.
const ClientComponentOnTheClient = clientExports(
ClientComponent,
123,
'path/to/chunk.js',
);
const ClientComponentOnTheClient = clientExports(ClientComponent, {
chunk: {id: 123, filename: 'path/to/chunk.js'},
});
const ClientComponentOnTheServer = clientExports(ClientComponent);

// In the SSR bundle this module won't exist. We simulate this by deleting it.
Expand Down Expand Up @@ -236,11 +234,9 @@ describe('ReactFlightDOMNode', () => {
}
// The Client build may not have the same IDs as the Server bundles for the same
// component.
const ClientComponentOnTheClient = clientExports(
ClientComponent,
123,
'path/to/chunk.js',
);
const ClientComponentOnTheClient = clientExports(ClientComponent, {
chunk: {id: 123, filename: 'path/to/chunk.js'},
});
const ClientComponentOnTheServer = clientExports(ClientComponent);

// In the SSR bundle this module won't exist. We simulate this by deleting it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,20 @@ exports.clientModuleError = function clientModuleError(moduleError) {

exports.clientExports = function clientExports(
moduleExports,
chunkId,
chunkFilename,
blockOnChunk,
options?: {
chunk?: {
id: string | number,
filename: string,
promise?: Promise,
},
} = {},
) {
const chunks = [];
if (chunkId) {
chunks.push(chunkId, chunkFilename);
if (options.chunk) {
chunks.push(options.chunk.id, options.chunk.filename);

if (blockOnChunk) {
webpackChunkMap[chunkId] = blockOnChunk;
if (options.chunk.promise) {
webpackChunkMap[options.chunk.id] = options.chunk.promise;
}
}
const idx = '' + webpackModuleIdx++;
Expand Down