1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = applyRuntimes;
|
7 |
|
8 | var _assert = _interopRequireDefault(require("assert"));
|
9 |
|
10 | var _nullthrows = _interopRequireDefault(require("nullthrows"));
|
11 |
|
12 | var _AssetGraph = _interopRequireWildcard(require("./AssetGraph"));
|
13 |
|
14 | var _BundleGraph = _interopRequireDefault(require("./public/BundleGraph"));
|
15 |
|
16 | var _BundleGraph2 = require("./BundleGraph");
|
17 |
|
18 | var _Bundle = require("./public/Bundle");
|
19 |
|
20 | var _utils = require("@parcel/utils");
|
21 |
|
22 | var _logger = require("@parcel/logger");
|
23 |
|
24 | var _diagnostic = _interopRequireWildcard(require("@parcel/diagnostic"));
|
25 |
|
26 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
27 |
|
28 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
29 |
|
30 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
31 |
|
32 | async function applyRuntimes({
|
33 | bundleGraph,
|
34 | config,
|
35 | options,
|
36 | pluginOptions,
|
37 | runtimesBuilder
|
38 | }) {
|
39 | let connections = [];
|
40 |
|
41 | for (let bundle of bundleGraph.getBundles()) {
|
42 | let runtimes = await config.getRuntimes(bundle.env.context);
|
43 |
|
44 | for (let runtime of runtimes) {
|
45 | try {
|
46 | let applied = await runtime.plugin.apply({
|
47 | bundle: new _Bundle.NamedBundle(bundle, bundleGraph, options),
|
48 | bundleGraph: new _BundleGraph.default(bundleGraph, options),
|
49 | options: pluginOptions,
|
50 | logger: new _logger.PluginLogger({
|
51 | origin: runtime.name
|
52 | })
|
53 | });
|
54 |
|
55 | if (applied) {
|
56 | let runtimeAssets = Array.isArray(applied) ? applied : [applied];
|
57 |
|
58 | for (let {
|
59 | code,
|
60 | dependency,
|
61 | filePath,
|
62 | isEntry
|
63 | } of runtimeAssets) {
|
64 | let assetRequest = {
|
65 | code,
|
66 | filePath,
|
67 | env: bundle.env
|
68 | };
|
69 | connections.push({
|
70 | bundle,
|
71 | assetRequest,
|
72 | dependency: dependency,
|
73 | isEntry
|
74 | });
|
75 | }
|
76 | }
|
77 | } catch (e) {
|
78 | throw new _diagnostic.default({
|
79 | diagnostic: (0, _diagnostic.errorToDiagnostic)(e, runtime.name)
|
80 | });
|
81 | }
|
82 | }
|
83 | }
|
84 |
|
85 | let runtimesAssetGraph = await reconcileNewRuntimes(runtimesBuilder, connections);
|
86 | let runtimesGraph = (0, _BundleGraph2.removeAssetGroups)(runtimesAssetGraph);
|
87 |
|
88 |
|
89 | bundleGraph._graph.merge(runtimesGraph);
|
90 |
|
91 | for (let {
|
92 | bundle,
|
93 | assetRequest,
|
94 | dependency,
|
95 | isEntry
|
96 | } of connections) {
|
97 | let assetGroupNode = (0, _AssetGraph.nodeFromAssetGroup)(assetRequest);
|
98 | let assetGroupAssets = runtimesAssetGraph.getNodesConnectedFrom(assetGroupNode);
|
99 | (0, _assert.default)(assetGroupAssets.length === 1);
|
100 | let runtimeNode = assetGroupAssets[0];
|
101 | (0, _assert.default)(runtimeNode.type === 'asset');
|
102 | let duplicatedAssetIds = new Set();
|
103 | runtimesGraph.traverse((node, _, actions) => {
|
104 | if (node.type !== 'dependency') {
|
105 | return;
|
106 | }
|
107 |
|
108 | let assets = runtimesGraph.getNodesConnectedFrom(node).map(assetNode => {
|
109 | (0, _assert.default)(assetNode.type === 'asset');
|
110 | return assetNode.value;
|
111 | });
|
112 |
|
113 | for (let asset of assets) {
|
114 | if (bundleGraph.isAssetInAncestorBundles(bundle, asset)) {
|
115 | duplicatedAssetIds.add(asset.id);
|
116 | actions.skipChildren();
|
117 | }
|
118 | }
|
119 | }, runtimeNode);
|
120 | runtimesGraph.traverse((node, _, actions) => {
|
121 | if (node.type === 'asset' || node.type === 'dependency') {
|
122 | if (duplicatedAssetIds.has(node.id)) {
|
123 | actions.skipChildren();
|
124 | return;
|
125 | }
|
126 |
|
127 | bundleGraph._graph.addEdge(bundle.id, node.id, 'contains');
|
128 | }
|
129 | }, runtimeNode);
|
130 |
|
131 | bundleGraph._graph.addEdge(dependency ? dependency.id : (0, _nullthrows.default)(bundleGraph._graph.getNode(bundle.id)).id, runtimeNode.id);
|
132 |
|
133 | if (isEntry) {
|
134 | bundle.entryAssetIds.unshift(runtimeNode.id);
|
135 | }
|
136 | }
|
137 | }
|
138 |
|
139 | async function reconcileNewRuntimes(runtimesBuilder, connections) {
|
140 | let {
|
141 | assetGraph
|
142 | } = runtimesBuilder;
|
143 | let assetRequestNodesById = new Map(connections.map(t => t.assetRequest).map(request => {
|
144 | let node = (0, _AssetGraph.nodeFromAssetGroup)(request);
|
145 | return [node.id, node];
|
146 | }));
|
147 | let newRequestIds = new Set(assetRequestNodesById.keys());
|
148 | let oldRequestIds = new Set(assetGraph.getEntryAssetGroupNodes().map(node => node.id));
|
149 | let toAdd = (0, _utils.setDifference)(newRequestIds, oldRequestIds);
|
150 | let toRemove = (0, _utils.setDifference)(oldRequestIds, newRequestIds);
|
151 | assetGraph.replaceNodesConnectedTo((0, _nullthrows.default)(assetGraph.getRootNode()), [...toAdd].map(requestId => (0, _nullthrows.default)(assetRequestNodesById.get(requestId))), node => toRemove.has(node.id));
|
152 |
|
153 | return (await runtimesBuilder.build()).assetGraph;
|
154 | } |
\ | No newline at end of file |