UNPKG

4.23 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.replaceURLReferences = replaceURLReferences;
7exports.replaceInlineReferences = replaceInlineReferences;
8
9var _assert = _interopRequireDefault(require("assert"));
10
11var _stream = require("stream");
12
13var _nullthrows = _interopRequireDefault(require("nullthrows"));
14
15var _url = _interopRequireDefault(require("url"));
16
17var _ = require("../");
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21/*
22 * Replaces references to dependency ids for URL dependencies with:
23 * - in the case of an unresolvable url dependency, the original moduleSpecifier.
24 * These are external requests that Parcel did not bundle.
25 * - in the case of a reference to another bundle, the relative url to that
26 * bundle from the current bundle.
27 */
28function replaceURLReferences({
29 bundle,
30 bundleGraph,
31 contents,
32 map,
33 relative = true
34}) {
35 let replacements = new Map();
36 let urlDependencies = [];
37 bundle.traverse(node => {
38 if (node.type === 'dependency' && node.value.isURL) {
39 urlDependencies.push(node.value);
40 }
41 });
42
43 for (let dependency of urlDependencies) {
44 if (!dependency.isURL) {
45 continue;
46 }
47
48 let resolved = bundleGraph.resolveExternalDependency(dependency, bundle);
49
50 if (resolved == null) {
51 replacements.set(dependency.id, {
52 from: dependency.id,
53 to: dependency.moduleSpecifier
54 });
55 continue;
56 }
57
58 (0, _assert.default)(resolved.type === 'bundle_group');
59 let entryBundle = bundleGraph.getBundlesInBundleGroup(resolved.value).pop();
60
61 if (entryBundle.isInline) {
62 // If a bundle is inline, it should be replaced with inline contents,
63 // not a URL.
64 continue;
65 }
66
67 replacements.set(dependency.id, getURLReplacement({
68 dependency,
69 fromBundle: bundle,
70 toBundle: entryBundle,
71 relative
72 }));
73 }
74
75 return performReplacement(replacements, contents, map);
76}
77/*
78 * Replaces references to dependency ids for inline bundles with the packaged
79 * contents of that bundle.
80 */
81
82
83async function replaceInlineReferences({
84 bundle,
85 bundleGraph,
86 contents,
87 map,
88 getInlineReplacement,
89 getInlineBundleContents
90}) {
91 let replacements = new Map();
92 let dependencies = [];
93 bundle.traverse(node => {
94 if (node.type === 'dependency') {
95 dependencies.push(node.value);
96 }
97 });
98
99 for (let dependency of dependencies) {
100 let resolved = bundleGraph.resolveExternalDependency(dependency, bundle);
101
102 if (resolved == null || resolved.type === 'asset') {
103 continue;
104 }
105
106 let [entryBundle] = bundleGraph.getBundlesInBundleGroup(resolved.value);
107
108 if (!entryBundle.isInline) {
109 continue;
110 }
111
112 let packagedBundle = await getInlineBundleContents(entryBundle, bundleGraph);
113 let packagedContents = (packagedBundle.contents instanceof _stream.Readable ? await (0, _.bufferStream)(packagedBundle.contents) : packagedBundle.contents).toString();
114 let inlineType = (0, _nullthrows.default)(entryBundle.getMainEntry()).meta.inlineType;
115
116 if (inlineType == null || inlineType === 'string') {
117 replacements.set(dependency.id, getInlineReplacement(dependency, inlineType, packagedContents));
118 }
119 }
120
121 return performReplacement(replacements, contents, map);
122}
123
124function getURLReplacement({
125 dependency,
126 fromBundle,
127 toBundle,
128 relative
129}) {
130 let url = _url.default.parse(dependency.moduleSpecifier);
131
132 let to;
133
134 if (relative) {
135 url.pathname = (0, _.relativeBundlePath)(fromBundle, toBundle, {
136 leadingDotSlash: false
137 });
138 to = _url.default.format(url);
139 } else {
140 url.pathname = (0, _nullthrows.default)(toBundle.name);
141 to = (0, _.urlJoin)(toBundle.target.publicUrl, _url.default.format(url));
142 }
143
144 return {
145 from: dependency.id,
146 to
147 };
148}
149
150function performReplacement(replacements, contents, map) {
151 let finalContents = contents;
152
153 for (let {
154 from,
155 to
156 } of replacements.values()) {
157 // Perform replacement
158 finalContents = finalContents.split(from).join(to);
159 }
160
161 return {
162 contents: finalContents,
163 // TODO: Update sourcemap with adjusted contents
164 map
165 };
166}
\No newline at end of file