1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _assert = _interopRequireDefault(require("assert"));
|
9 |
|
10 | var _path = _interopRequireDefault(require("path"));
|
11 |
|
12 | var _nullthrows = _interopRequireDefault(require("nullthrows"));
|
13 |
|
14 | var _logger = require("@parcel/logger");
|
15 |
|
16 | var _diagnostic = _interopRequireWildcard(require("@parcel/diagnostic"));
|
17 |
|
18 | var _AssetGraph = _interopRequireDefault(require("./AssetGraph"));
|
19 |
|
20 | var _BundleGraph = _interopRequireDefault(require("./public/BundleGraph"));
|
21 |
|
22 | var _BundleGraph2 = _interopRequireWildcard(require("./BundleGraph"));
|
23 |
|
24 | var _MutableBundleGraph = _interopRequireDefault(require("./public/MutableBundleGraph"));
|
25 |
|
26 | var _Bundle = require("./public/Bundle");
|
27 |
|
28 | var _ReporterRunner = require("./ReporterRunner");
|
29 |
|
30 | var _dumpGraphToGraphViz = _interopRequireDefault(require("./dumpGraphToGraphViz"));
|
31 |
|
32 | var _utils = require("@parcel/utils");
|
33 |
|
34 | var _PluginOptions = _interopRequireDefault(require("./public/PluginOptions"));
|
35 |
|
36 | var _applyRuntimes = _interopRequireDefault(require("./applyRuntimes"));
|
37 |
|
38 | var _constants = require("./constants");
|
39 |
|
40 | var _utils2 = require("./utils");
|
41 |
|
42 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
43 |
|
44 | 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; }
|
45 |
|
46 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
47 |
|
48 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
49 |
|
50 | class BundlerRunner {
|
51 | constructor(opts) {
|
52 | _defineProperty(this, "options", void 0);
|
53 |
|
54 | _defineProperty(this, "config", void 0);
|
55 |
|
56 | _defineProperty(this, "pluginOptions", void 0);
|
57 |
|
58 | _defineProperty(this, "farm", void 0);
|
59 |
|
60 | _defineProperty(this, "runtimesBuilder", void 0);
|
61 |
|
62 | _defineProperty(this, "isBundling", false);
|
63 |
|
64 | this.options = opts.options;
|
65 | this.config = opts.config;
|
66 | this.pluginOptions = new _PluginOptions.default(this.options);
|
67 | this.runtimesBuilder = opts.runtimesBuilder;
|
68 | this.farm = opts.workerFarm;
|
69 | }
|
70 |
|
71 | async bundle(graph, {
|
72 | signal
|
73 | }) {
|
74 | (0, _ReporterRunner.report)({
|
75 | type: 'buildProgress',
|
76 | phase: 'bundling'
|
77 | });
|
78 | let cacheKey;
|
79 |
|
80 | if (!this.options.disableCache) {
|
81 | cacheKey = await this.getCacheKey(graph);
|
82 | let cachedBundleGraph = await this.options.cache.get(cacheKey);
|
83 | (0, _utils2.assertSignalNotAborted)(signal);
|
84 |
|
85 | if (cachedBundleGraph) {
|
86 | return cachedBundleGraph;
|
87 | }
|
88 | }
|
89 |
|
90 | let bundleGraph = (0, _BundleGraph2.removeAssetGroups)(graph);
|
91 |
|
92 | let internalBundleGraph = new _BundleGraph2.default({
|
93 | graph: bundleGraph
|
94 | });
|
95 | await (0, _dumpGraphToGraphViz.default)(bundleGraph, 'before_bundle');
|
96 | let mutableBundleGraph = new _MutableBundleGraph.default(internalBundleGraph, this.options);
|
97 | let bundler = await this.config.getBundler();
|
98 |
|
99 | try {
|
100 | await bundler.bundle({
|
101 | bundleGraph: mutableBundleGraph,
|
102 | options: this.pluginOptions,
|
103 | logger: new _logger.PluginLogger({
|
104 | origin: this.config.bundler
|
105 | })
|
106 | });
|
107 | } catch (e) {
|
108 | throw new _diagnostic.default({
|
109 | diagnostic: (0, _diagnostic.errorToDiagnostic)(e, this.config.bundler)
|
110 | });
|
111 | }
|
112 |
|
113 | (0, _utils2.assertSignalNotAborted)(signal);
|
114 | await (0, _dumpGraphToGraphViz.default)(bundleGraph, 'after_bundle');
|
115 |
|
116 | try {
|
117 | await bundler.optimize({
|
118 | bundleGraph: mutableBundleGraph,
|
119 | options: this.pluginOptions,
|
120 | logger: new _logger.PluginLogger({
|
121 | origin: this.config.bundler
|
122 | })
|
123 | });
|
124 | } catch (e) {
|
125 | throw new _diagnostic.default({
|
126 | diagnostic: (0, _diagnostic.errorToDiagnostic)(e, this.config.bundler)
|
127 | });
|
128 | }
|
129 |
|
130 | (0, _utils2.assertSignalNotAborted)(signal);
|
131 | await (0, _dumpGraphToGraphViz.default)(bundleGraph, 'after_optimize');
|
132 | await this.nameBundles(internalBundleGraph);
|
133 | await (0, _applyRuntimes.default)({
|
134 | bundleGraph: internalBundleGraph,
|
135 | runtimesBuilder: this.runtimesBuilder,
|
136 | config: this.config,
|
137 | options: this.options,
|
138 | pluginOptions: this.pluginOptions
|
139 | });
|
140 | (0, _utils2.assertSignalNotAborted)(signal);
|
141 | await (0, _dumpGraphToGraphViz.default)(bundleGraph, 'after_runtimes');
|
142 |
|
143 | if (cacheKey != null) {
|
144 | await this.options.cache.set(cacheKey, internalBundleGraph);
|
145 | }
|
146 |
|
147 | (0, _utils2.assertSignalNotAborted)(signal);
|
148 | return internalBundleGraph;
|
149 | }
|
150 |
|
151 | async getCacheKey(assetGraph) {
|
152 | let bundler = this.config.bundler;
|
153 | let {
|
154 | pkg
|
155 | } = await this.options.packageManager.resolve(`${bundler}/package.json`, `${this.config.filePath}/index`
|
156 | );
|
157 | let version = (0, _nullthrows.default)(pkg).version;
|
158 | return (0, _utils.md5FromObject)({
|
159 | parcelVersion: _constants.PARCEL_VERSION,
|
160 | bundler,
|
161 | version,
|
162 | hash: assetGraph.getHash()
|
163 | });
|
164 | }
|
165 |
|
166 | async nameBundles(bundleGraph) {
|
167 | let namers = await this.config.getNamers();
|
168 | let bundles = bundleGraph.getBundles();
|
169 | await Promise.all(bundles.map(bundle => this.nameBundle(namers, bundle, bundleGraph)));
|
170 | let bundlePaths = bundles.map(b => b.filePath);
|
171 |
|
172 | _assert.default.deepEqual(bundlePaths, (0, _utils.unique)(bundlePaths), 'Bundles must have unique filePaths');
|
173 | }
|
174 |
|
175 | async nameBundle(namers, internalBundle, internalBundleGraph) {
|
176 | let bundle = new _Bundle.Bundle(internalBundle, internalBundleGraph, this.options);
|
177 | let bundleGraph = new _BundleGraph.default(internalBundleGraph, this.options);
|
178 |
|
179 | for (let namer of namers) {
|
180 | try {
|
181 | let name = await namer.plugin.name({
|
182 | bundle,
|
183 | bundleGraph,
|
184 | options: this.pluginOptions,
|
185 | logger: new _logger.PluginLogger({
|
186 | origin: namer.name
|
187 | })
|
188 | });
|
189 |
|
190 | if (name != null) {
|
191 | if (_path.default.extname(name).slice(1) !== bundle.type) {
|
192 | throw new Error(`Destination name ${name} extension does not match bundle type "${bundle.type}"`);
|
193 | }
|
194 |
|
195 | let target = (0, _nullthrows.default)(internalBundle.target);
|
196 | internalBundle.filePath = _path.default.join(target.distDir, (0, _utils.normalizeSeparators)(name));
|
197 | internalBundle.name = name;
|
198 | return;
|
199 | }
|
200 | } catch (e) {
|
201 | throw new _diagnostic.default({
|
202 | diagnostic: (0, _diagnostic.errorToDiagnostic)(e, namer.name)
|
203 | });
|
204 | }
|
205 | }
|
206 |
|
207 | throw new Error('Unable to name bundle');
|
208 | }
|
209 |
|
210 | }
|
211 |
|
212 | exports.default = BundlerRunner; |
\ | No newline at end of file |