UNPKG

1.6 kBJavaScriptView Raw
1/** Copyright (c) 2018 Uber Technologies, Inc.
2 *
3 * This source code is licensed under the MIT license found in the
4 * LICENSE file in the root directory of this source tree.
5 *
6 * @flow
7 */
8/* eslint-env node */
9
10/*::
11import type {ClientChunkMetadata} from "./types.js";
12*/
13
14module.exports = mergeChunkMetadata;
15
16function mergeChunkMetadata(
17 arr /*: Array<ClientChunkMetadata> */
18) /*: ClientChunkMetadata */ {
19 const base = {
20 fileManifest: new Map(),
21 urlMap: new Map(),
22 criticalPaths: [],
23 criticalIds: [],
24 chunks: new Map(),
25 runtimeChunkIds: new Set(),
26 initialChunkIds: new Set(),
27 };
28 arr = arr.reverse();
29
30 return arr.reduce((acc, item) => {
31 for (let [key, val] of item.fileManifest) {
32 if (acc.fileManifest.has(key)) {
33 let set = acc.fileManifest.get(key);
34 for (let el of val) {
35 // $FlowFixMe
36 set.add(el);
37 }
38 } else {
39 acc.fileManifest.set(key, val);
40 }
41 }
42 for (let [key, val] of item.urlMap) {
43 acc.urlMap.set(key, val);
44 }
45 for (let [key, val] of item.chunks) {
46 acc.chunks.set(key, val);
47 }
48 for (let val of item.runtimeChunkIds) {
49 acc.runtimeChunkIds.add(val);
50 }
51 for (let val of item.initialChunkIds) {
52 acc.initialChunkIds.add(val);
53 }
54 for (let path of item.criticalPaths) {
55 if (!acc.criticalPaths.includes(path)) {
56 acc.criticalPaths.push(path);
57 }
58 }
59 for (let id of item.criticalIds) {
60 if (!acc.criticalIds.includes(id)) {
61 acc.criticalIds.push(id);
62 }
63 }
64 return acc;
65 }, base);
66}