@atlaspack/packager-html 2.14.5-canary.36 → 2.14.5-canary.360
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +412 -0
- package/dist/HTMLPackager.js +228 -0
- package/lib/HTMLPackager.js +19 -1
- package/lib/types/HTMLPackager.d.ts +3 -0
- package/package.json +13 -8
- package/src/{HTMLPackager.js → HTMLPackager.ts} +84 -29
- package/tsconfig.json +21 -0
- package/tsconfig.tsbuildinfo +1 -0
package/lib/HTMLPackager.js
CHANGED
|
@@ -88,7 +88,7 @@ var _default = exports.default = new (_plugin().Packager)({
|
|
|
88
88
|
let referencedBundles = [...(0, _utils().setSymmetricDifference)(new Set(referencedBundlesRecursive), new Set(bundleGraph.getReferencedBundles(bundle, {
|
|
89
89
|
recursive: false
|
|
90
90
|
})))];
|
|
91
|
-
let conditionalBundles = config.evaluateRootConditionalBundles ? (0, _utils().setDifference)(
|
|
91
|
+
let conditionalBundles = config.evaluateRootConditionalBundles ? (0, _utils().setDifference)(getReferencedConditionalScripts(bundleGraph, referencedBundlesRecursive), new Set(referencedBundles)) : new Set();
|
|
92
92
|
let renderConfig = config === null || config === void 0 ? void 0 : config.render;
|
|
93
93
|
let {
|
|
94
94
|
html
|
|
@@ -208,12 +208,14 @@ function insertBundleReferences(siblingBundles, tree, conditionalBundles) {
|
|
|
208
208
|
}
|
|
209
209
|
function addBundlesToTree(bundles, tree) {
|
|
210
210
|
const main = find(tree, 'head') || find(tree, 'html');
|
|
211
|
+
// @ts-expect-error TS2339
|
|
211
212
|
const content = main ? main.content || (main.content = []) : tree;
|
|
212
213
|
const index = findBundleInsertIndex(content);
|
|
213
214
|
content.splice(index, 0, ...bundles);
|
|
214
215
|
}
|
|
215
216
|
function find(tree, tag) {
|
|
216
217
|
let res;
|
|
218
|
+
// @ts-expect-error TS7006
|
|
217
219
|
tree.match({
|
|
218
220
|
tag
|
|
219
221
|
}, node => {
|
|
@@ -243,4 +245,20 @@ function findBundleInsertIndex(content) {
|
|
|
243
245
|
}
|
|
244
246
|
}
|
|
245
247
|
return doctypeIndex ? doctypeIndex + 1 : 0;
|
|
248
|
+
}
|
|
249
|
+
function getReferencedConditionalScripts(bundleGraph, referencedBundles) {
|
|
250
|
+
const conditionalBundleMapping = bundleGraph.getConditionalBundleMapping();
|
|
251
|
+
const bundles = [];
|
|
252
|
+
for (const bundle of referencedBundles) {
|
|
253
|
+
const conditions = conditionalBundleMapping.get(bundle.id);
|
|
254
|
+
if (conditions) {
|
|
255
|
+
for (const [, cond] of conditions) {
|
|
256
|
+
// Reverse so dependent bundles are loaded first
|
|
257
|
+
const dependentBundles = [...cond.ifTrueBundles.reverse(), ...cond.ifFalseBundles.reverse()];
|
|
258
|
+
bundles.push(...dependentBundles);
|
|
259
|
+
referencedBundles.push(...dependentBundles);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return new Set(bundles);
|
|
246
264
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/packager-html",
|
|
3
|
-
"version": "2.14.5-canary.
|
|
3
|
+
"version": "2.14.5-canary.360+fc3adc098",
|
|
4
4
|
"license": "(MIT OR Apache-2.0)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -9,18 +9,23 @@
|
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/atlassian-labs/atlaspack.git"
|
|
11
11
|
},
|
|
12
|
-
"main": "lib/HTMLPackager.js",
|
|
13
|
-
"source": "src/HTMLPackager.
|
|
12
|
+
"main": "./lib/HTMLPackager.js",
|
|
13
|
+
"source": "./src/HTMLPackager.ts",
|
|
14
|
+
"types": "./lib/types/HTMLPackager.d.ts",
|
|
14
15
|
"engines": {
|
|
15
16
|
"node": ">= 16.0.0"
|
|
16
17
|
},
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"@atlaspack/
|
|
19
|
-
"@atlaspack/
|
|
20
|
-
"@atlaspack/
|
|
19
|
+
"@atlaspack/feature-flags": "2.14.1-canary.428+fc3adc098",
|
|
20
|
+
"@atlaspack/plugin": "2.14.5-canary.360+fc3adc098",
|
|
21
|
+
"@atlaspack/types": "2.14.5-canary.360+fc3adc098",
|
|
22
|
+
"@atlaspack/utils": "2.14.5-canary.360+fc3adc098",
|
|
21
23
|
"nullthrows": "^1.1.1",
|
|
22
24
|
"posthtml": "^0.16.5"
|
|
23
25
|
},
|
|
24
26
|
"type": "commonjs",
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
29
|
+
},
|
|
30
|
+
"gitHead": "fc3adc098f583e40d6d7687412cac6dde7cbb3f3"
|
|
31
|
+
}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
Asset,
|
|
3
|
+
Async,
|
|
4
|
+
Blob,
|
|
5
|
+
Bundle,
|
|
6
|
+
BundleGraph,
|
|
7
|
+
NamedBundle,
|
|
8
|
+
} from '@atlaspack/types';
|
|
3
9
|
|
|
4
10
|
import assert from 'assert';
|
|
5
11
|
import {Readable} from 'stream';
|
|
@@ -26,9 +32,15 @@ const metadataContent = new Set([
|
|
|
26
32
|
'title',
|
|
27
33
|
]);
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
type HTMLPackagerConfig = {
|
|
36
|
+
evaluateRootConditionalBundles: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default new Packager({
|
|
30
40
|
async loadConfig({config, options}) {
|
|
31
|
-
let posthtmlConfig = await config.getConfig
|
|
41
|
+
let posthtmlConfig = await config.getConfig<{
|
|
42
|
+
render: any;
|
|
43
|
+
}>(
|
|
32
44
|
[
|
|
33
45
|
'.posthtmlrc',
|
|
34
46
|
'.posthtmlrc.js',
|
|
@@ -43,9 +55,13 @@ export default (new Packager({
|
|
|
43
55
|
},
|
|
44
56
|
);
|
|
45
57
|
|
|
46
|
-
let conf = await config.getConfigFrom(
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
let conf = await config.getConfigFrom<HTMLPackagerConfig>(
|
|
59
|
+
options.projectRoot + '/index',
|
|
60
|
+
[],
|
|
61
|
+
{
|
|
62
|
+
packageKey: '@atlaspack/packager-html',
|
|
63
|
+
},
|
|
64
|
+
);
|
|
49
65
|
|
|
50
66
|
return {
|
|
51
67
|
render: posthtmlConfig?.contents?.render,
|
|
@@ -55,7 +71,7 @@ export default (new Packager({
|
|
|
55
71
|
};
|
|
56
72
|
},
|
|
57
73
|
async package({bundle, bundleGraph, getInlineBundleContents, config}) {
|
|
58
|
-
let assets = [];
|
|
74
|
+
let assets: Array<Asset> = [];
|
|
59
75
|
bundle.traverseAssets((asset) => {
|
|
60
76
|
assets.push(asset);
|
|
61
77
|
});
|
|
@@ -77,25 +93,23 @@ export default (new Packager({
|
|
|
77
93
|
|
|
78
94
|
let conditionalBundles = config.evaluateRootConditionalBundles
|
|
79
95
|
? setDifference(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
]),
|
|
96
|
+
getReferencedConditionalScripts(
|
|
97
|
+
bundleGraph,
|
|
98
|
+
referencedBundlesRecursive,
|
|
99
|
+
),
|
|
85
100
|
new Set(referencedBundles),
|
|
86
101
|
)
|
|
87
|
-
: new Set();
|
|
88
|
-
|
|
102
|
+
: new Set<NamedBundle>();
|
|
89
103
|
let renderConfig = config?.render;
|
|
90
104
|
|
|
91
105
|
let {html} = await posthtml([
|
|
92
|
-
(tree) =>
|
|
106
|
+
(tree: any) =>
|
|
93
107
|
insertBundleReferences(
|
|
94
108
|
[...conditionalBundles, ...referencedBundles],
|
|
95
109
|
tree,
|
|
96
110
|
conditionalBundles,
|
|
97
111
|
),
|
|
98
|
-
(tree) =>
|
|
112
|
+
(tree: any) =>
|
|
99
113
|
replaceInlineAssetContent(bundleGraph, getInlineBundleContents, tree),
|
|
100
114
|
]).process(code, {
|
|
101
115
|
...renderConfig,
|
|
@@ -123,14 +137,19 @@ export default (new Packager({
|
|
|
123
137
|
map,
|
|
124
138
|
});
|
|
125
139
|
},
|
|
126
|
-
})
|
|
140
|
+
}) as Packager<unknown, unknown>;
|
|
127
141
|
|
|
128
142
|
async function getAssetContent(
|
|
129
143
|
bundleGraph: BundleGraph<NamedBundle>,
|
|
130
|
-
getInlineBundleContents
|
|
131
|
-
|
|
144
|
+
getInlineBundleContents: (
|
|
145
|
+
arg1: Bundle,
|
|
146
|
+
arg2: BundleGraph<NamedBundle>,
|
|
147
|
+
) => Async<{
|
|
148
|
+
contents: Blob;
|
|
149
|
+
}>,
|
|
150
|
+
assetId: any,
|
|
132
151
|
) {
|
|
133
|
-
let inlineBundle:
|
|
152
|
+
let inlineBundle: Bundle | null | undefined;
|
|
134
153
|
bundleGraph.traverseBundles((bundle, context, {stop}) => {
|
|
135
154
|
let entryAssets = bundle.getEntryAssets();
|
|
136
155
|
if (entryAssets.some((a) => a.uniqueKey === assetId)) {
|
|
@@ -153,11 +172,16 @@ async function getAssetContent(
|
|
|
153
172
|
|
|
154
173
|
async function replaceInlineAssetContent(
|
|
155
174
|
bundleGraph: BundleGraph<NamedBundle>,
|
|
156
|
-
getInlineBundleContents
|
|
157
|
-
|
|
175
|
+
getInlineBundleContents: (
|
|
176
|
+
arg1: Bundle,
|
|
177
|
+
arg2: BundleGraph<NamedBundle>,
|
|
178
|
+
) => Async<{
|
|
179
|
+
contents: Blob;
|
|
180
|
+
}>,
|
|
181
|
+
tree: any,
|
|
158
182
|
) {
|
|
159
|
-
const inlineNodes = [];
|
|
160
|
-
tree.walk((node) => {
|
|
183
|
+
const inlineNodes: Array<any> = [];
|
|
184
|
+
tree.walk((node: any) => {
|
|
161
185
|
if (node.attrs && node.attrs['data-parcel-key']) {
|
|
162
186
|
inlineNodes.push(node);
|
|
163
187
|
}
|
|
@@ -207,7 +231,11 @@ async function replaceInlineAssetContent(
|
|
|
207
231
|
return tree;
|
|
208
232
|
}
|
|
209
233
|
|
|
210
|
-
function insertBundleReferences(
|
|
234
|
+
function insertBundleReferences(
|
|
235
|
+
siblingBundles: Array<NamedBundle>,
|
|
236
|
+
tree: any,
|
|
237
|
+
conditionalBundles: Set<NamedBundle>,
|
|
238
|
+
) {
|
|
211
239
|
const bundles = [];
|
|
212
240
|
|
|
213
241
|
for (let bundle of siblingBundles) {
|
|
@@ -240,16 +268,18 @@ function insertBundleReferences(siblingBundles, tree, conditionalBundles) {
|
|
|
240
268
|
addBundlesToTree(bundles, tree);
|
|
241
269
|
}
|
|
242
270
|
|
|
243
|
-
function addBundlesToTree(bundles, tree) {
|
|
271
|
+
function addBundlesToTree(bundles: any[], tree: any) {
|
|
244
272
|
const main = find(tree, 'head') || find(tree, 'html');
|
|
273
|
+
// @ts-expect-error TS2339
|
|
245
274
|
const content = main ? main.content || (main.content = []) : tree;
|
|
246
275
|
const index = findBundleInsertIndex(content);
|
|
247
276
|
|
|
248
277
|
content.splice(index, 0, ...bundles);
|
|
249
278
|
}
|
|
250
279
|
|
|
251
|
-
function find(tree, tag) {
|
|
280
|
+
function find(tree: any, tag: string) {
|
|
252
281
|
let res;
|
|
282
|
+
// @ts-expect-error TS7006
|
|
253
283
|
tree.match({tag}, (node) => {
|
|
254
284
|
res = node;
|
|
255
285
|
return node;
|
|
@@ -258,7 +288,7 @@ function find(tree, tag) {
|
|
|
258
288
|
return res;
|
|
259
289
|
}
|
|
260
290
|
|
|
261
|
-
function findBundleInsertIndex(content) {
|
|
291
|
+
function findBundleInsertIndex(content: any) {
|
|
262
292
|
// HTML document order (https://html.spec.whatwg.org/multipage/syntax.html#writing)
|
|
263
293
|
// - Any number of comments and ASCII whitespace.
|
|
264
294
|
// - A DOCTYPE.
|
|
@@ -284,3 +314,28 @@ function findBundleInsertIndex(content) {
|
|
|
284
314
|
|
|
285
315
|
return doctypeIndex ? doctypeIndex + 1 : 0;
|
|
286
316
|
}
|
|
317
|
+
|
|
318
|
+
function getReferencedConditionalScripts(
|
|
319
|
+
bundleGraph: BundleGraph<NamedBundle>,
|
|
320
|
+
referencedBundles: NamedBundle[],
|
|
321
|
+
): Set<NamedBundle> {
|
|
322
|
+
const conditionalBundleMapping = bundleGraph.getConditionalBundleMapping();
|
|
323
|
+
|
|
324
|
+
const bundles: Array<NamedBundle> = [];
|
|
325
|
+
for (const bundle of referencedBundles) {
|
|
326
|
+
const conditions = conditionalBundleMapping.get(bundle.id);
|
|
327
|
+
if (conditions) {
|
|
328
|
+
for (const [, cond] of conditions) {
|
|
329
|
+
// Reverse so dependent bundles are loaded first
|
|
330
|
+
const dependentBundles = [
|
|
331
|
+
...cond.ifTrueBundles.reverse(),
|
|
332
|
+
...cond.ifFalseBundles.reverse(),
|
|
333
|
+
];
|
|
334
|
+
bundles.push(...dependentBundles);
|
|
335
|
+
referencedBundles.push(...dependentBundles);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return new Set(bundles);
|
|
341
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../tsconfig.base.json",
|
|
3
|
+
"include": ["src"],
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"composite": true
|
|
6
|
+
},
|
|
7
|
+
"references": [
|
|
8
|
+
{
|
|
9
|
+
"path": "../../core/feature-flags/tsconfig.json"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"path": "../../core/plugin/tsconfig.json"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"path": "../../core/types/tsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"path": "../../core/utils/tsconfig.json"
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}
|