-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathReactFlightWebpackPlugin.js
More file actions
526 lines (476 loc) · 17.3 KB
/
ReactFlightWebpackPlugin.js
File metadata and controls
526 lines (476 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {ImportManifestEntry} from './shared/ReactFlightImportMetadata';
import {join} from 'path';
import {pathToFileURL} from 'url';
import asyncLib from 'neo-async';
import * as acorn from 'acorn-loose';
import ModuleDependency from 'webpack/lib/dependencies/ModuleDependency';
import NullDependency from 'webpack/lib/dependencies/NullDependency';
import Template from 'webpack/lib/Template';
import {
sources,
WebpackError,
Compilation,
AsyncDependenciesBlock,
} from 'webpack';
import isArray from 'shared/isArray';
class ClientReferenceDependency extends ModuleDependency {
constructor(request: mixed) {
super(request);
}
get type(): string {
return 'client-reference';
}
}
// This is the module that will be used to anchor all client references to.
// I.e. it will have all the client files as async deps from this point on.
// We use the Flight client implementation because you can't get to these
// without the client runtime so it's the first time in the loading sequence
// you might want them.
const clientImportName = 'react-server-dom-webpack/client';
const clientFileName = require.resolve('../client.browser.js');
type ClientReferenceSearchPath = {
directory: string,
recursive?: boolean,
include: RegExp,
exclude?: RegExp,
};
type ClientReferencePath = string | ClientReferenceSearchPath;
type Options = {
isServer: boolean,
clientReferences?: ClientReferencePath | $ReadOnlyArray<ClientReferencePath>,
chunkName?: string,
clientManifestFilename?: string,
serverConsumerManifestFilename?: string,
};
const PLUGIN_NAME = 'React Server Plugin';
export default class ReactFlightWebpackPlugin {
clientReferences: $ReadOnlyArray<ClientReferencePath>;
chunkName: string;
clientManifestFilename: string;
serverConsumerManifestFilename: string;
constructor(options: Options) {
if (!options || typeof options.isServer !== 'boolean') {
throw new Error(
PLUGIN_NAME + ': You must specify the isServer option as a boolean.',
);
}
if (options.isServer) {
throw new Error('TODO: Implement the server compiler.');
}
if (!options.clientReferences) {
this.clientReferences = [
{
directory: '.',
recursive: true,
include: /\.(js|ts|jsx|tsx)$/,
},
];
} else if (
typeof options.clientReferences === 'string' ||
!isArray(options.clientReferences)
) {
this.clientReferences = [(options.clientReferences: $FlowFixMe)];
} else {
// $FlowFixMe[incompatible-type] found when upgrading Flow
this.clientReferences = options.clientReferences;
}
if (typeof options.chunkName === 'string') {
this.chunkName = options.chunkName;
if (!/\[(index|request)\]/.test(this.chunkName)) {
this.chunkName += '[index]';
}
} else {
this.chunkName = 'client[index]';
}
this.clientManifestFilename =
options.clientManifestFilename || 'react-client-manifest.json';
this.serverConsumerManifestFilename =
options.serverConsumerManifestFilename || 'react-ssr-manifest.json';
}
apply(compiler: any) {
const _this = this;
let resolvedClientReferences;
let clientFileNameFound = false;
// Find all client files on the file system
compiler.hooks.beforeCompile.tapAsync(
PLUGIN_NAME,
({contextModuleFactory}, callback) => {
const contextResolver = compiler.resolverFactory.get('context', {});
const normalResolver = compiler.resolverFactory.get('normal');
_this.resolveAllClientFiles(
compiler.context,
contextResolver,
normalResolver,
compiler.inputFileSystem,
contextModuleFactory,
function (err, resolvedClientRefs) {
if (err) {
callback(err);
return;
}
resolvedClientReferences = resolvedClientRefs;
callback();
},
);
},
);
compiler.hooks.thisCompilation.tap(
PLUGIN_NAME,
(compilation, {normalModuleFactory}) => {
compilation.dependencyFactories.set(
ClientReferenceDependency,
normalModuleFactory,
);
compilation.dependencyTemplates.set(
ClientReferenceDependency,
new NullDependency.Template(),
);
// $FlowFixMe[missing-local-annot]
const handler = parser => {
// We need to add all client references as dependency of something in the graph so
// Webpack knows which entries need to know about the relevant chunks and include the
// map in their runtime. The things that actually resolves the dependency is the Flight
// client runtime. So we add them as a dependency of the Flight client runtime.
// Anything that imports the runtime will be made aware of these chunks.
parser.hooks.program.tap(PLUGIN_NAME, () => {
const module = parser.state.module;
if (module.resource !== clientFileName) {
return;
}
clientFileNameFound = true;
if (resolvedClientReferences) {
// $FlowFixMe[incompatible-use] found when upgrading Flow
for (let i = 0; i < resolvedClientReferences.length; i++) {
// $FlowFixMe[incompatible-use] found when upgrading Flow
const dep = resolvedClientReferences[i];
const chunkName = _this.chunkName
.replace(/\[index\]/g, '' + i)
.replace(/\[request\]/g, Template.toPath(dep.userRequest));
const block = new AsyncDependenciesBlock(
{
name: chunkName,
},
null,
dep.request,
);
block.addDependency(dep);
module.addBlock(block);
}
}
});
};
normalModuleFactory.hooks.parser
.for('javascript/auto')
.tap('HarmonyModulesPlugin', handler);
normalModuleFactory.hooks.parser
.for('javascript/esm')
.tap('HarmonyModulesPlugin', handler);
normalModuleFactory.hooks.parser
.for('javascript/dynamic')
.tap('HarmonyModulesPlugin', handler);
},
);
compiler.hooks.make.tap(PLUGIN_NAME, compilation => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
stage: Compilation.PROCESS_ASSETS_STAGE_REPORT,
},
function () {
if (clientFileNameFound === false) {
compilation.warnings.push(
new WebpackError(
`Client runtime at ${clientImportName} was not found. React Server Components module map file ${_this.clientManifestFilename} was not created.`,
),
);
return;
}
const configuredCrossOriginLoading =
compilation.outputOptions.crossOriginLoading;
const crossOriginMode =
typeof configuredCrossOriginLoading === 'string'
? configuredCrossOriginLoading === 'use-credentials'
? configuredCrossOriginLoading
: 'anonymous'
: null;
const resolvedClientFiles = new Set(
(resolvedClientReferences || []).map(ref => ref.request),
);
const clientManifest: {
[string]: ImportManifestEntry,
} = {};
type ServerConsumerModuleMap = {
[string]: {
[string]: {specifier: string, name: string},
},
};
const moduleMap: ServerConsumerModuleMap = {};
const ssrBundleConfig: {
moduleLoading: {
prefix: string,
crossOrigin: string | null,
},
moduleMap: ServerConsumerModuleMap,
} = {
moduleLoading: {
prefix: compilation.outputOptions.publicPath || '',
crossOrigin: crossOriginMode,
},
moduleMap,
};
// We figure out which files are always loaded by any initial chunk (entrypoint).
// We use this to filter out chunks that Flight will never need to load
const emptySet: Set<string> = new Set();
const runtimeChunkFiles: Set<string> = emptySet;
compilation.entrypoints.forEach(entrypoint => {
const runtimeChunk = entrypoint.getRuntimeChunk();
if (runtimeChunk) {
runtimeChunk.files.forEach(runtimeFile => {
runtimeChunkFiles.add(runtimeFile);
});
}
});
compilation.chunkGroups.forEach(function (chunkGroup) {
const chunks: Array<string> = [];
chunkGroup.chunks.forEach(function (c) {
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const file of c.files) {
if (!(file.endsWith('.js') || file.endsWith('.mjs'))) {
return;
}
if (
file.endsWith('.hot-update.js') ||
file.endsWith('.hot-update.mjs')
)
return;
chunks.push(c.id, file);
break;
}
});
// $FlowFixMe[missing-local-annot]
function recordModule(id: $FlowFixMe, module) {
// TODO: Hook into deps instead of the target module.
// That way we know by the type of dep whether to include.
// It also resolves conflicts when the same module is in multiple chunks.
if (!resolvedClientFiles.has(module.resource)) {
return;
}
const href = pathToFileURL(module.resource).href;
if (href !== undefined) {
const ssrExports: {
[string]: {specifier: string, name: string},
} = {};
clientManifest[href] = {
id,
chunks,
name: '*',
};
ssrExports['*'] = {
specifier: href,
name: '*',
};
// TODO: If this module ends up split into multiple modules, then
// we should encode each the chunks needed for the specific export.
// When the module isn't split, it doesn't matter and we can just
// encode the id of the whole module. This code doesn't currently
// deal with module splitting so is likely broken from ESM anyway.
/*
clientManifest[href + '#'] = {
id,
chunks,
name: '',
};
ssrExports[''] = {
specifier: href,
name: '',
};
const moduleProvidedExports = compilation.moduleGraph
.getExportsInfo(module)
.getProvidedExports();
if (Array.isArray(moduleProvidedExports)) {
moduleProvidedExports.forEach(function (name) {
clientManifest[href + '#' + name] = {
id,
chunks,
name: name,
};
ssrExports[name] = {
specifier: href,
name: name,
};
});
}
*/
moduleMap[id] = ssrExports;
}
}
chunkGroup.chunks.forEach(function (chunk) {
const chunkModules =
compilation.chunkGraph.getChunkModulesIterable(chunk);
Array.from(chunkModules).forEach(function (module) {
const moduleId = compilation.chunkGraph.getModuleId(module);
recordModule(moduleId, module);
// If this is a concatenation, register each child to the parent ID.
if (module.modules) {
module.modules.forEach(concatenatedMod => {
recordModule(moduleId, concatenatedMod);
});
}
});
});
});
const clientOutput = JSON.stringify(clientManifest, null, 2);
compilation.emitAsset(
_this.clientManifestFilename,
new sources.RawSource(clientOutput, false),
);
const ssrOutput = JSON.stringify(ssrBundleConfig, null, 2);
compilation.emitAsset(
_this.serverConsumerManifestFilename,
new sources.RawSource(ssrOutput, false),
);
},
);
});
}
// This attempts to replicate the dynamic file path resolution used for other wildcard
// resolution in Webpack is using.
resolveAllClientFiles(
context: string,
contextResolver: any,
normalResolver: any,
fs: any,
contextModuleFactory: any,
callback: (
err: null | Error,
result?: $ReadOnlyArray<ClientReferenceDependency>,
) => void,
) {
function hasUseClientDirective(source: string): boolean {
if (source.indexOf('use client') === -1) {
return false;
}
let body;
try {
body = acorn.parse(source, {
ecmaVersion: '2024',
sourceType: 'module',
}).body;
} catch (x) {
return false;
}
for (let i = 0; i < body.length; i++) {
const node = body[i];
if (node.type !== 'ExpressionStatement' || !node.directive) {
break;
}
if (node.directive === 'use client') {
return true;
}
}
return false;
}
asyncLib.map(
this.clientReferences,
(
clientReferencePath: string | ClientReferenceSearchPath,
cb: (
err: null | Error,
result?: $ReadOnlyArray<ClientReferenceDependency>,
) => void,
): void => {
if (typeof clientReferencePath === 'string') {
cb(null, [new ClientReferenceDependency(clientReferencePath)]);
return;
}
const clientReferenceSearch: ClientReferenceSearchPath =
clientReferencePath;
contextResolver.resolve(
{},
context,
clientReferencePath.directory,
{},
(err, resolvedDirectory) => {
if (err) return cb(err);
const options = {
resource: resolvedDirectory,
resourceQuery: '',
recursive:
clientReferenceSearch.recursive === undefined
? true
: clientReferenceSearch.recursive,
regExp: clientReferenceSearch.include,
include: undefined,
exclude: clientReferenceSearch.exclude,
};
contextModuleFactory.resolveDependencies(
fs,
options,
(err2: null | Error, deps: Array<any /*ModuleDependency*/>) => {
if (err2) return cb(err2);
const clientRefDeps = deps.map(dep => {
// use userRequest instead of request. request always end with undefined which is wrong
const request = join(resolvedDirectory, dep.userRequest);
const clientRefDep = new ClientReferenceDependency(request);
clientRefDep.userRequest = dep.userRequest;
return clientRefDep;
});
asyncLib.filter(
clientRefDeps,
(
clientRefDep: ClientReferenceDependency,
filterCb: (err: null | Error, truthValue: boolean) => void,
) => {
normalResolver.resolve(
{},
context,
clientRefDep.request,
{},
(err3: null | Error, resolvedPath: mixed) => {
if (err3 || typeof resolvedPath !== 'string') {
return filterCb(null, false);
}
fs.readFile(
resolvedPath,
'utf-8',
(err4: null | Error, content: string) => {
if (err4 || typeof content !== 'string') {
return filterCb(null, false);
}
const useClient = hasUseClientDirective(content);
filterCb(null, useClient);
},
);
},
);
},
cb,
);
},
);
},
);
},
(
err: null | Error,
result: $ReadOnlyArray<$ReadOnlyArray<ClientReferenceDependency>>,
): void => {
if (err) return callback(err);
const flat: Array<any> = [];
for (let i = 0; i < result.length; i++) {
// $FlowFixMe[method-unbinding]
flat.push.apply(flat, result[i]);
}
callback(null, flat);
},
);
}
}