@atlaspack/transformer-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 +345 -0
- package/dist/HTMLTransformer.js +120 -0
- package/dist/dependencies.js +262 -0
- package/dist/inline.js +162 -0
- package/lib/HTMLTransformer.js +4 -0
- package/lib/dependencies.js +10 -0
- package/lib/inline.js +14 -3
- package/lib/types/HTMLTransformer.d.ts +6 -0
- package/lib/types/dependencies.d.ts +2 -0
- package/lib/types/inline.d.ts +7 -0
- package/package.json +13 -10
- package/src/{HTMLTransformer.js → HTMLTransformer.ts} +9 -7
- package/src/{dependencies.js → dependencies.ts} +26 -15
- package/src/{inline.js → inline.ts} +21 -7
- package/test/{HTMLTransformer.test.js → HTMLTransformer.test.ts} +41 -27
- package/tsconfig.json +24 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = collectDependencies;
|
|
7
|
+
const posthtml_1 = __importDefault(require("posthtml"));
|
|
8
|
+
const srcset_1 = require("srcset");
|
|
9
|
+
// A list of all attributes that may produce a dependency
|
|
10
|
+
// Based on https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
|
|
11
|
+
const ATTRS = {
|
|
12
|
+
src: [
|
|
13
|
+
'script',
|
|
14
|
+
'img',
|
|
15
|
+
'audio',
|
|
16
|
+
'video',
|
|
17
|
+
'source',
|
|
18
|
+
'track',
|
|
19
|
+
'iframe',
|
|
20
|
+
'embed',
|
|
21
|
+
'amp-img',
|
|
22
|
+
],
|
|
23
|
+
// Using href with <script> is described here: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/script
|
|
24
|
+
href: ['link', 'a', 'use', 'script', 'image'],
|
|
25
|
+
srcset: ['img', 'source'],
|
|
26
|
+
imagesrcset: ['link'],
|
|
27
|
+
poster: ['video'],
|
|
28
|
+
'xlink:href': ['use', 'image', 'script'],
|
|
29
|
+
content: ['meta'],
|
|
30
|
+
data: ['object'],
|
|
31
|
+
};
|
|
32
|
+
// A list of metadata that should produce a dependency
|
|
33
|
+
// Based on:
|
|
34
|
+
// - http://schema.org/
|
|
35
|
+
// - http://ogp.me
|
|
36
|
+
// - https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup
|
|
37
|
+
// - https://msdn.microsoft.com/en-us/library/dn255024.aspx
|
|
38
|
+
// - https://vk.com/dev/publications
|
|
39
|
+
const META = {
|
|
40
|
+
property: [
|
|
41
|
+
'og:image',
|
|
42
|
+
'og:image:url',
|
|
43
|
+
'og:image:secure_url',
|
|
44
|
+
'og:audio',
|
|
45
|
+
'og:audio:secure_url',
|
|
46
|
+
'og:video',
|
|
47
|
+
'og:video:secure_url',
|
|
48
|
+
'vk:image',
|
|
49
|
+
],
|
|
50
|
+
name: [
|
|
51
|
+
'twitter:image',
|
|
52
|
+
'msapplication-square150x150logo',
|
|
53
|
+
'msapplication-square310x310logo',
|
|
54
|
+
'msapplication-square70x70logo',
|
|
55
|
+
'msapplication-wide310x150logo',
|
|
56
|
+
'msapplication-TileImage',
|
|
57
|
+
'msapplication-config',
|
|
58
|
+
],
|
|
59
|
+
itemprop: [
|
|
60
|
+
'image',
|
|
61
|
+
'logo',
|
|
62
|
+
'screenshot',
|
|
63
|
+
'thumbnailUrl',
|
|
64
|
+
'contentUrl',
|
|
65
|
+
'downloadUrl',
|
|
66
|
+
],
|
|
67
|
+
};
|
|
68
|
+
const FEED_TYPES = new Set(['application/rss+xml', 'application/atom+xml']);
|
|
69
|
+
// Options to be passed to `addDependency` for certain tags + attributes
|
|
70
|
+
const OPTIONS = {
|
|
71
|
+
a: {
|
|
72
|
+
href: { needsStableName: true },
|
|
73
|
+
},
|
|
74
|
+
iframe: {
|
|
75
|
+
src: { needsStableName: true },
|
|
76
|
+
},
|
|
77
|
+
link(attrs) {
|
|
78
|
+
if (attrs.rel === 'stylesheet') {
|
|
79
|
+
return {
|
|
80
|
+
// Keep in the same bundle group as the HTML.
|
|
81
|
+
priority: 'parallel',
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
function collectSrcSetDependencies(asset, srcset, opts) {
|
|
87
|
+
let parsed = (0, srcset_1.parse)(srcset).map(({ url, ...v }) => ({
|
|
88
|
+
url: asset.addURLDependency(url, opts),
|
|
89
|
+
...v,
|
|
90
|
+
}));
|
|
91
|
+
return (0, srcset_1.stringify)(parsed);
|
|
92
|
+
}
|
|
93
|
+
function getAttrDepHandler(attr) {
|
|
94
|
+
if (attr === 'srcset' || attr === 'imagesrcset') {
|
|
95
|
+
return collectSrcSetDependencies;
|
|
96
|
+
}
|
|
97
|
+
return (asset, src, opts) => asset.addURLDependency(src, opts);
|
|
98
|
+
}
|
|
99
|
+
function collectDependencies(asset, ast) {
|
|
100
|
+
let isDirty = false;
|
|
101
|
+
let hasModuleScripts = false;
|
|
102
|
+
let seen = new Set();
|
|
103
|
+
let errors = [];
|
|
104
|
+
// @ts-expect-error TS2339
|
|
105
|
+
(0, posthtml_1.default)().walk.call(ast.program, (node) => {
|
|
106
|
+
let { tag, attrs } = node;
|
|
107
|
+
if (!attrs || seen.has(node)) {
|
|
108
|
+
return node;
|
|
109
|
+
}
|
|
110
|
+
seen.add(node);
|
|
111
|
+
if (tag === 'meta') {
|
|
112
|
+
const isMetaDependency = Object.keys(attrs).some((attr) => {
|
|
113
|
+
// @ts-expect-error TS7053
|
|
114
|
+
let values = META[attr];
|
|
115
|
+
return (values &&
|
|
116
|
+
values.includes(attrs[attr]) &&
|
|
117
|
+
attrs.content !== '' &&
|
|
118
|
+
!(attrs.name === 'msapplication-config' && attrs.content === 'none'));
|
|
119
|
+
});
|
|
120
|
+
if (isMetaDependency) {
|
|
121
|
+
const metaAssetUrl = attrs.content;
|
|
122
|
+
if (metaAssetUrl) {
|
|
123
|
+
attrs.content = asset.addURLDependency(attrs.content, {
|
|
124
|
+
needsStableName: !(attrs.name && attrs.name.includes('msapplication')),
|
|
125
|
+
});
|
|
126
|
+
isDirty = true;
|
|
127
|
+
asset.setAST(ast);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return node;
|
|
131
|
+
}
|
|
132
|
+
if (tag === 'link' &&
|
|
133
|
+
(attrs.rel === 'canonical' ||
|
|
134
|
+
attrs.rel === 'manifest' ||
|
|
135
|
+
(attrs.rel === 'alternate' && FEED_TYPES.has(attrs.type))) &&
|
|
136
|
+
attrs.href) {
|
|
137
|
+
let href = attrs.href;
|
|
138
|
+
if (attrs.rel === 'manifest') {
|
|
139
|
+
// A hack to allow manifest.json rather than manifest.webmanifest.
|
|
140
|
+
// If a custom pipeline is used, it is responsible for running @atlaspack/transformer-webmanifest.
|
|
141
|
+
if (!href.includes(':')) {
|
|
142
|
+
href = 'webmanifest:' + href;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
attrs.href = asset.addURLDependency(href, {
|
|
146
|
+
needsStableName: true,
|
|
147
|
+
});
|
|
148
|
+
isDirty = true;
|
|
149
|
+
asset.setAST(ast);
|
|
150
|
+
return node;
|
|
151
|
+
}
|
|
152
|
+
if (tag === 'script' && attrs.src) {
|
|
153
|
+
let sourceType = attrs.type === 'module' ? 'module' : 'script';
|
|
154
|
+
let loc = node.location
|
|
155
|
+
? {
|
|
156
|
+
filePath: asset.filePath,
|
|
157
|
+
start: node.location.start,
|
|
158
|
+
end: {
|
|
159
|
+
line: node.location.end.line,
|
|
160
|
+
// PostHTML's location is inclusive
|
|
161
|
+
column: node.location.end.column + 1,
|
|
162
|
+
},
|
|
163
|
+
}
|
|
164
|
+
: undefined;
|
|
165
|
+
let outputFormat = 'global';
|
|
166
|
+
if (attrs.type === 'module' && asset.env.shouldScopeHoist) {
|
|
167
|
+
outputFormat = 'esmodule';
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
if (attrs.type === 'module') {
|
|
171
|
+
attrs.defer = '';
|
|
172
|
+
}
|
|
173
|
+
delete attrs.type;
|
|
174
|
+
}
|
|
175
|
+
// If this is a <script type="module">, and not all of the browser targets support ESM natively,
|
|
176
|
+
// add a copy of the script tag with a nomodule attribute.
|
|
177
|
+
let copy;
|
|
178
|
+
if (outputFormat === 'esmodule' &&
|
|
179
|
+
!asset.env.supports('esmodules', true)) {
|
|
180
|
+
let attrs = Object.assign({}, node.attrs);
|
|
181
|
+
copy = { ...node, attrs };
|
|
182
|
+
delete attrs.type;
|
|
183
|
+
attrs.nomodule = '';
|
|
184
|
+
attrs.defer = '';
|
|
185
|
+
attrs.src = asset.addURLDependency(attrs.src, {
|
|
186
|
+
// Keep in the same bundle group as the HTML.
|
|
187
|
+
priority: 'parallel',
|
|
188
|
+
bundleBehavior: sourceType === 'script' || attrs.async != null
|
|
189
|
+
? 'isolated'
|
|
190
|
+
: undefined,
|
|
191
|
+
env: {
|
|
192
|
+
// @ts-expect-error TS2322
|
|
193
|
+
sourceType,
|
|
194
|
+
outputFormat: 'global',
|
|
195
|
+
loc,
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
seen.add(copy);
|
|
199
|
+
}
|
|
200
|
+
attrs.src = asset.addURLDependency(attrs.src, {
|
|
201
|
+
// Keep in the same bundle group as the HTML.
|
|
202
|
+
priority: 'parallel',
|
|
203
|
+
// If the script is async it can be executed in any order, so it cannot depend
|
|
204
|
+
// on any sibling scripts for dependencies. Keep all dependencies together.
|
|
205
|
+
// Also, don't share dependencies between classic scripts and nomodule scripts
|
|
206
|
+
// because nomodule scripts won't run when modules are supported.
|
|
207
|
+
bundleBehavior: sourceType === 'script' || attrs.async != null
|
|
208
|
+
? 'isolated'
|
|
209
|
+
: undefined,
|
|
210
|
+
env: {
|
|
211
|
+
// @ts-expect-error TS2322
|
|
212
|
+
sourceType,
|
|
213
|
+
// @ts-expect-error TS2322
|
|
214
|
+
outputFormat,
|
|
215
|
+
loc,
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
asset.setAST(ast);
|
|
219
|
+
if (sourceType === 'module')
|
|
220
|
+
hasModuleScripts = true;
|
|
221
|
+
return copy ? [node, copy] : node;
|
|
222
|
+
}
|
|
223
|
+
for (let attr in attrs) {
|
|
224
|
+
// Check for virtual paths
|
|
225
|
+
if (tag === 'a' && attrs[attr].split('#')[0].lastIndexOf('.') < 1) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
// Check for id references
|
|
229
|
+
if (attrs[attr][0] === '#') {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
// @ts-expect-error TS7053
|
|
233
|
+
let elements = ATTRS[attr];
|
|
234
|
+
if (elements && elements.includes(node.tag)) {
|
|
235
|
+
// Check for empty string
|
|
236
|
+
if (attrs[attr].length === 0) {
|
|
237
|
+
errors.push({
|
|
238
|
+
message: `'${attr}' should not be empty string`,
|
|
239
|
+
filePath: asset.filePath,
|
|
240
|
+
loc: node.location,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
let depHandler = getAttrDepHandler(attr);
|
|
244
|
+
// @ts-expect-error TS7053
|
|
245
|
+
let depOptionsHandler = OPTIONS[node.tag];
|
|
246
|
+
let depOptions = typeof depOptionsHandler === 'function'
|
|
247
|
+
? depOptionsHandler(attrs, asset.env)
|
|
248
|
+
: depOptionsHandler && depOptionsHandler[attr];
|
|
249
|
+
attrs[attr] = depHandler(asset, attrs[attr], depOptions);
|
|
250
|
+
isDirty = true;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (isDirty) {
|
|
254
|
+
asset.setAST(ast);
|
|
255
|
+
}
|
|
256
|
+
return node;
|
|
257
|
+
});
|
|
258
|
+
if (errors.length > 0) {
|
|
259
|
+
throw errors;
|
|
260
|
+
}
|
|
261
|
+
return hasModuleScripts;
|
|
262
|
+
}
|
package/dist/inline.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = extractInlineAssets;
|
|
7
|
+
const rust_1 = require("@atlaspack/rust");
|
|
8
|
+
const posthtml_1 = __importDefault(require("posthtml"));
|
|
9
|
+
const SCRIPT_TYPES = {
|
|
10
|
+
'application/javascript': 'js',
|
|
11
|
+
'text/javascript': 'js',
|
|
12
|
+
'application/json': false,
|
|
13
|
+
'application/ld+json': 'jsonld',
|
|
14
|
+
'text/html': false,
|
|
15
|
+
module: 'js',
|
|
16
|
+
};
|
|
17
|
+
function extractInlineAssets(asset, ast) {
|
|
18
|
+
let program = ast.program;
|
|
19
|
+
let key = 0;
|
|
20
|
+
// Extract inline <script> and <style> tags for processing.
|
|
21
|
+
let parts = [];
|
|
22
|
+
let hasModuleScripts = false;
|
|
23
|
+
// @ts-expect-error TS2339
|
|
24
|
+
(0, posthtml_1.default)().walk.call(program, (node) => {
|
|
25
|
+
let parcelKey = (0, rust_1.hashString)(`${asset.id}:${key++}`);
|
|
26
|
+
if (node.tag === 'script' || node.tag === 'style') {
|
|
27
|
+
let value = node.content && node.content.join('');
|
|
28
|
+
if (value != null) {
|
|
29
|
+
let type, env;
|
|
30
|
+
if (node.tag === 'style') {
|
|
31
|
+
if (node.attrs && node.attrs.type != null) {
|
|
32
|
+
type = node.attrs.type.split('/')[1];
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
type = 'css';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (node.attrs && node.attrs.type != null) {
|
|
39
|
+
// Skip JSON
|
|
40
|
+
// @ts-expect-error TS7053
|
|
41
|
+
if (SCRIPT_TYPES[node.attrs.type] === false) {
|
|
42
|
+
return node;
|
|
43
|
+
}
|
|
44
|
+
// @ts-expect-error TS7053
|
|
45
|
+
if (SCRIPT_TYPES[node.attrs.type]) {
|
|
46
|
+
// @ts-expect-error TS7053
|
|
47
|
+
type = SCRIPT_TYPES[node.attrs.type];
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
type = node.attrs.type.split('/')[1];
|
|
51
|
+
}
|
|
52
|
+
let outputFormat = 'global';
|
|
53
|
+
let sourceType = 'script';
|
|
54
|
+
let attrs = node.attrs;
|
|
55
|
+
if (attrs && attrs.type === 'module') {
|
|
56
|
+
if (asset.env.shouldScopeHoist &&
|
|
57
|
+
asset.env.supports('esmodules', true)) {
|
|
58
|
+
outputFormat = 'esmodule';
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
delete attrs.type;
|
|
62
|
+
}
|
|
63
|
+
sourceType = 'module';
|
|
64
|
+
}
|
|
65
|
+
let loc = node.location
|
|
66
|
+
? {
|
|
67
|
+
filePath: asset.filePath,
|
|
68
|
+
start: node.location.start,
|
|
69
|
+
end: node.location.end,
|
|
70
|
+
}
|
|
71
|
+
: undefined;
|
|
72
|
+
env = {
|
|
73
|
+
sourceType,
|
|
74
|
+
outputFormat,
|
|
75
|
+
loc,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
let loc = node.location
|
|
80
|
+
? {
|
|
81
|
+
filePath: asset.filePath,
|
|
82
|
+
start: node.location.start,
|
|
83
|
+
end: node.location.end,
|
|
84
|
+
}
|
|
85
|
+
: undefined;
|
|
86
|
+
type = 'js';
|
|
87
|
+
env = {
|
|
88
|
+
sourceType: 'script',
|
|
89
|
+
loc,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
if (!type) {
|
|
93
|
+
return node;
|
|
94
|
+
}
|
|
95
|
+
if (!node.attrs) {
|
|
96
|
+
node.attrs = {};
|
|
97
|
+
}
|
|
98
|
+
// allow a script/style tag to declare its key
|
|
99
|
+
if (node.attrs['data-parcel-key']) {
|
|
100
|
+
parcelKey = node.attrs['data-parcel-key'];
|
|
101
|
+
}
|
|
102
|
+
// Inform packager to remove type, since CSS and JS are the defaults.
|
|
103
|
+
if (node.attrs?.type && node.tag === 'style') {
|
|
104
|
+
delete node.attrs.type;
|
|
105
|
+
}
|
|
106
|
+
let bundleBehavior = 'inline';
|
|
107
|
+
if (typeof node.attrs['data-atlaspack-isolated'] !== 'undefined') {
|
|
108
|
+
bundleBehavior = 'inlineIsolated';
|
|
109
|
+
delete node.attrs['data-atlaspack-isolated'];
|
|
110
|
+
}
|
|
111
|
+
// insert parcelId to allow us to retrieve node during packaging
|
|
112
|
+
node.attrs['data-parcel-key'] = parcelKey;
|
|
113
|
+
asset.setAST(ast); // mark dirty
|
|
114
|
+
asset.addDependency({
|
|
115
|
+
specifier: parcelKey,
|
|
116
|
+
specifierType: 'esm',
|
|
117
|
+
});
|
|
118
|
+
parts.push({
|
|
119
|
+
type,
|
|
120
|
+
content: value,
|
|
121
|
+
uniqueKey: parcelKey,
|
|
122
|
+
bundleBehavior,
|
|
123
|
+
// @ts-expect-error TS2322
|
|
124
|
+
env,
|
|
125
|
+
meta: {
|
|
126
|
+
type: 'tag',
|
|
127
|
+
node,
|
|
128
|
+
startLine: node.location?.start.line,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
if (env && env.sourceType === 'module') {
|
|
132
|
+
hasModuleScripts = true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Process inline style attributes.
|
|
137
|
+
let attrs = node.attrs;
|
|
138
|
+
let style = attrs?.style;
|
|
139
|
+
if (attrs != null && style != null) {
|
|
140
|
+
attrs.style = asset.addDependency({
|
|
141
|
+
specifier: parcelKey,
|
|
142
|
+
specifierType: 'esm',
|
|
143
|
+
});
|
|
144
|
+
asset.setAST(ast); // mark dirty
|
|
145
|
+
parts.push({
|
|
146
|
+
type: 'css',
|
|
147
|
+
content: style,
|
|
148
|
+
uniqueKey: parcelKey,
|
|
149
|
+
bundleBehavior: 'inline',
|
|
150
|
+
meta: {
|
|
151
|
+
type: 'attr',
|
|
152
|
+
node,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return node;
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
assets: parts,
|
|
160
|
+
hasModuleScripts,
|
|
161
|
+
};
|
|
162
|
+
}
|
package/lib/HTMLTransformer.js
CHANGED
|
@@ -58,6 +58,8 @@ function _diagnostic() {
|
|
|
58
58
|
return data;
|
|
59
59
|
}
|
|
60
60
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
61
|
+
// @ts-expect-error TS2305
|
|
62
|
+
|
|
61
63
|
function parseHTML(code, xmlMode) {
|
|
62
64
|
return {
|
|
63
65
|
type: 'posthtml',
|
|
@@ -156,6 +158,7 @@ const transformerOpts = exports.transformerOpts = {
|
|
|
156
158
|
}) {
|
|
157
159
|
return {
|
|
158
160
|
content: (0, _posthtmlRender().render)(ast.program, {
|
|
161
|
+
// @ts-expect-error TS2322
|
|
159
162
|
closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined
|
|
160
163
|
})
|
|
161
164
|
};
|
|
@@ -165,6 +168,7 @@ var _default = exports.default = new (_plugin().Transformer)(transformerOpts);
|
|
|
165
168
|
function findFirstMatch(ast, expressions) {
|
|
166
169
|
let found;
|
|
167
170
|
for (const expression of expressions) {
|
|
171
|
+
// @ts-expect-error TS2339
|
|
168
172
|
(0, _posthtml().default)().match.call(ast.program, expression, node => {
|
|
169
173
|
found = node;
|
|
170
174
|
return node;
|
package/lib/dependencies.js
CHANGED
|
@@ -19,6 +19,8 @@ function _srcset() {
|
|
|
19
19
|
return data;
|
|
20
20
|
}
|
|
21
21
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
+
// @ts-expect-error TS2724
|
|
23
|
+
|
|
22
24
|
// A list of all attributes that may produce a dependency
|
|
23
25
|
// Based on https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
|
|
24
26
|
const ATTRS = {
|
|
@@ -89,6 +91,7 @@ function collectDependencies(asset, ast) {
|
|
|
89
91
|
let hasModuleScripts = false;
|
|
90
92
|
let seen = new Set();
|
|
91
93
|
let errors = [];
|
|
94
|
+
// @ts-expect-error TS2339
|
|
92
95
|
(0, _posthtml().default)().walk.call(ast.program, node => {
|
|
93
96
|
let {
|
|
94
97
|
tag,
|
|
@@ -100,6 +103,7 @@ function collectDependencies(asset, ast) {
|
|
|
100
103
|
seen.add(node);
|
|
101
104
|
if (tag === 'meta') {
|
|
102
105
|
const isMetaDependency = Object.keys(attrs).some(attr => {
|
|
106
|
+
// @ts-expect-error TS7053
|
|
103
107
|
let values = META[attr];
|
|
104
108
|
return values && values.includes(attrs[attr]) && attrs.content !== '' && !(attrs.name === 'msapplication-config' && attrs.content === 'none');
|
|
105
109
|
});
|
|
@@ -169,6 +173,7 @@ function collectDependencies(asset, ast) {
|
|
|
169
173
|
priority: 'parallel',
|
|
170
174
|
bundleBehavior: sourceType === 'script' || attrs.async != null ? 'isolated' : undefined,
|
|
171
175
|
env: {
|
|
176
|
+
// @ts-expect-error TS2322
|
|
172
177
|
sourceType,
|
|
173
178
|
outputFormat: 'global',
|
|
174
179
|
loc
|
|
@@ -185,7 +190,9 @@ function collectDependencies(asset, ast) {
|
|
|
185
190
|
// because nomodule scripts won't run when modules are supported.
|
|
186
191
|
bundleBehavior: sourceType === 'script' || attrs.async != null ? 'isolated' : undefined,
|
|
187
192
|
env: {
|
|
193
|
+
// @ts-expect-error TS2322
|
|
188
194
|
sourceType,
|
|
195
|
+
// @ts-expect-error TS2322
|
|
189
196
|
outputFormat,
|
|
190
197
|
loc
|
|
191
198
|
}
|
|
@@ -204,6 +211,8 @@ function collectDependencies(asset, ast) {
|
|
|
204
211
|
if (attrs[attr][0] === '#') {
|
|
205
212
|
continue;
|
|
206
213
|
}
|
|
214
|
+
|
|
215
|
+
// @ts-expect-error TS7053
|
|
207
216
|
let elements = ATTRS[attr];
|
|
208
217
|
if (elements && elements.includes(node.tag)) {
|
|
209
218
|
// Check for empty string
|
|
@@ -215,6 +224,7 @@ function collectDependencies(asset, ast) {
|
|
|
215
224
|
});
|
|
216
225
|
}
|
|
217
226
|
let depHandler = getAttrDepHandler(attr);
|
|
227
|
+
// @ts-expect-error TS7053
|
|
218
228
|
let depOptionsHandler = OPTIONS[node.tag];
|
|
219
229
|
let depOptions = typeof depOptionsHandler === 'function' ? depOptionsHandler(attrs, asset.env) : depOptionsHandler && depOptionsHandler[attr];
|
|
220
230
|
attrs[attr] = depHandler(asset, attrs[attr], depOptions);
|
package/lib/inline.js
CHANGED
|
@@ -19,6 +19,8 @@ function _posthtml() {
|
|
|
19
19
|
return data;
|
|
20
20
|
}
|
|
21
21
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
+
// @ts-expect-error TS2724
|
|
23
|
+
|
|
22
24
|
const SCRIPT_TYPES = {
|
|
23
25
|
'application/javascript': 'js',
|
|
24
26
|
'text/javascript': 'js',
|
|
@@ -34,6 +36,7 @@ function extractInlineAssets(asset, ast) {
|
|
|
34
36
|
// Extract inline <script> and <style> tags for processing.
|
|
35
37
|
let parts = [];
|
|
36
38
|
let hasModuleScripts = false;
|
|
39
|
+
// @ts-expect-error TS2339
|
|
37
40
|
(0, _posthtml().default)().walk.call(program, node => {
|
|
38
41
|
let parcelKey = (0, _rust().hashString)(`${asset.id}:${key++}`);
|
|
39
42
|
if (node.tag === 'script' || node.tag === 'style') {
|
|
@@ -49,10 +52,14 @@ function extractInlineAssets(asset, ast) {
|
|
|
49
52
|
}
|
|
50
53
|
} else if (node.attrs && node.attrs.type != null) {
|
|
51
54
|
// Skip JSON
|
|
55
|
+
// @ts-expect-error TS7053
|
|
52
56
|
if (SCRIPT_TYPES[node.attrs.type] === false) {
|
|
53
57
|
return node;
|
|
54
58
|
}
|
|
59
|
+
|
|
60
|
+
// @ts-expect-error TS7053
|
|
55
61
|
if (SCRIPT_TYPES[node.attrs.type]) {
|
|
62
|
+
// @ts-expect-error TS7053
|
|
56
63
|
type = SCRIPT_TYPES[node.attrs.type];
|
|
57
64
|
} else {
|
|
58
65
|
type = node.attrs.type.split('/')[1];
|
|
@@ -106,6 +113,11 @@ function extractInlineAssets(asset, ast) {
|
|
|
106
113
|
if ((_node$attrs = node.attrs) !== null && _node$attrs !== void 0 && _node$attrs.type && node.tag === 'style') {
|
|
107
114
|
delete node.attrs.type;
|
|
108
115
|
}
|
|
116
|
+
let bundleBehavior = 'inline';
|
|
117
|
+
if (typeof node.attrs['data-atlaspack-isolated'] !== 'undefined') {
|
|
118
|
+
bundleBehavior = 'inlineIsolated';
|
|
119
|
+
delete node.attrs['data-atlaspack-isolated'];
|
|
120
|
+
}
|
|
109
121
|
|
|
110
122
|
// insert parcelId to allow us to retrieve node during packaging
|
|
111
123
|
node.attrs['data-parcel-key'] = parcelKey;
|
|
@@ -119,11 +131,11 @@ function extractInlineAssets(asset, ast) {
|
|
|
119
131
|
type,
|
|
120
132
|
content: value,
|
|
121
133
|
uniqueKey: parcelKey,
|
|
122
|
-
bundleBehavior
|
|
134
|
+
bundleBehavior,
|
|
135
|
+
// @ts-expect-error TS2322
|
|
123
136
|
env,
|
|
124
137
|
meta: {
|
|
125
138
|
type: 'tag',
|
|
126
|
-
// $FlowFixMe
|
|
127
139
|
node,
|
|
128
140
|
startLine: (_node$location = node.location) === null || _node$location === void 0 ? void 0 : _node$location.start.line
|
|
129
141
|
}
|
|
@@ -151,7 +163,6 @@ function extractInlineAssets(asset, ast) {
|
|
|
151
163
|
bundleBehavior: 'inline',
|
|
152
164
|
meta: {
|
|
153
165
|
type: 'attr',
|
|
154
|
-
// $FlowFixMe
|
|
155
166
|
node
|
|
156
167
|
}
|
|
157
168
|
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Transformer } from '@atlaspack/plugin';
|
|
2
|
+
import type { AST, Transformer as TransformerOpts } from '@atlaspack/types';
|
|
3
|
+
export declare function parseHTML(code: string, xmlMode: boolean): AST;
|
|
4
|
+
export declare const transformerOpts: TransformerOpts<undefined>;
|
|
5
|
+
declare const _default: Transformer<undefined>;
|
|
6
|
+
export default _default;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AST, MutableAsset, TransformerResult } from '@atlaspack/types';
|
|
2
|
+
interface ExtractInlineAssetsResult {
|
|
3
|
+
hasModuleScripts: boolean;
|
|
4
|
+
assets: Array<TransformerResult>;
|
|
5
|
+
}
|
|
6
|
+
export default function extractInlineAssets(asset: MutableAsset, ast: AST): ExtractInlineAssetsResult;
|
|
7
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,26 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/transformer-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"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
|
-
"test": "mocha"
|
|
9
|
+
"test": "mocha",
|
|
10
|
+
"build:lib": "gulp build --gulpfile ../../../gulpfile.js --cwd ."
|
|
10
11
|
},
|
|
11
12
|
"repository": {
|
|
12
13
|
"type": "git",
|
|
13
14
|
"url": "https://github.com/atlassian-labs/atlaspack.git"
|
|
14
15
|
},
|
|
15
|
-
"main": "lib/HTMLTransformer.js",
|
|
16
|
-
"source": "src/HTMLTransformer.
|
|
16
|
+
"main": "./lib/HTMLTransformer.js",
|
|
17
|
+
"source": "./src/HTMLTransformer.ts",
|
|
18
|
+
"types": "./lib/types/HTMLTransformer.d.ts",
|
|
17
19
|
"engines": {
|
|
18
20
|
"node": ">= 16.0.0"
|
|
19
21
|
},
|
|
20
22
|
"dependencies": {
|
|
21
|
-
"@atlaspack/diagnostic": "2.14.1-canary.
|
|
22
|
-
"@atlaspack/
|
|
23
|
-
"@atlaspack/
|
|
23
|
+
"@atlaspack/diagnostic": "2.14.1-canary.428+fc3adc098",
|
|
24
|
+
"@atlaspack/feature-flags": "2.14.1-canary.428+fc3adc098",
|
|
25
|
+
"@atlaspack/plugin": "2.14.5-canary.360+fc3adc098",
|
|
26
|
+
"@atlaspack/rust": "3.2.1-canary.360+fc3adc098",
|
|
24
27
|
"nullthrows": "^1.1.1",
|
|
25
28
|
"posthtml": "^0.16.5",
|
|
26
29
|
"posthtml-parser": "^0.10.1",
|
|
@@ -29,8 +32,8 @@
|
|
|
29
32
|
"srcset": "4"
|
|
30
33
|
},
|
|
31
34
|
"devDependencies": {
|
|
32
|
-
"@atlaspack/core": "2.16.2-canary.
|
|
35
|
+
"@atlaspack/core": "2.16.2-canary.360+fc3adc098"
|
|
33
36
|
},
|
|
34
37
|
"type": "commonjs",
|
|
35
|
-
"gitHead": "
|
|
36
|
-
}
|
|
38
|
+
"gitHead": "fc3adc098f583e40d6d7687412cac6dde7cbb3f3"
|
|
39
|
+
}
|