@wordpress/interactivity-router 2.32.0 → 2.32.1-next.ff1cebbba.0
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 +2 -0
- package/build/assets/dynamic-importmap/fetch.js +32 -43
- package/build/assets/dynamic-importmap/fetch.js.map +7 -1
- package/build/assets/dynamic-importmap/index.js +43 -73
- package/build/assets/dynamic-importmap/index.js.map +7 -1
- package/build/assets/dynamic-importmap/loader.js +139 -172
- package/build/assets/dynamic-importmap/loader.js.map +7 -1
- package/build/assets/dynamic-importmap/resolver.js +99 -112
- package/build/assets/dynamic-importmap/resolver.js.map +7 -1
- package/build/assets/script-modules.js +46 -58
- package/build/assets/script-modules.js.map +7 -1
- package/build/assets/scs.js +30 -39
- package/build/assets/scs.js.map +7 -1
- package/build/assets/styles.js +81 -171
- package/build/assets/styles.js.map +7 -1
- package/build/full-page.js +43 -36
- package/build/full-page.js.map +7 -1
- package/build/index.js +139 -265
- package/build/index.js.map +7 -1
- package/build-module/assets/dynamic-importmap/fetch.js +11 -39
- package/build-module/assets/dynamic-importmap/fetch.js.map +7 -1
- package/build-module/assets/dynamic-importmap/index.js +18 -52
- package/build-module/assets/dynamic-importmap/index.js.map +7 -1
- package/build-module/assets/dynamic-importmap/loader.js +101 -165
- package/build-module/assets/dynamic-importmap/loader.js.map +7 -1
- package/build-module/assets/dynamic-importmap/resolver.js +78 -108
- package/build-module/assets/dynamic-importmap/resolver.js.map +7 -1
- package/build-module/assets/script-modules.js +25 -49
- package/build-module/assets/script-modules.js.map +7 -1
- package/build-module/assets/scs.js +9 -35
- package/build-module/assets/scs.js.map +7 -1
- package/build-module/assets/styles.js +58 -163
- package/build-module/assets/styles.js.map +7 -1
- package/build-module/full-page.js +21 -32
- package/build-module/full-page.js.map +7 -1
- package/build-module/index.js +108 -257
- package/build-module/index.js.map +7 -1
- package/build-types/index.d.ts.map +1 -1
- package/package.json +12 -4
- package/src/index.ts +2 -3
- package/tsconfig.main.tsbuildinfo +1 -1
|
@@ -1,22 +1,6 @@
|
|
|
1
|
-
/* wp:polyfill */
|
|
2
|
-
/**
|
|
3
|
-
* This code is derived from the following projects:
|
|
4
|
-
*
|
|
5
|
-
* 1. dynamic-importmap (https://github.com/keller-mark/dynamic-importmap)
|
|
6
|
-
* 2. es-module-shims (https://github.com/guybedford/es-module-shims)
|
|
7
|
-
*
|
|
8
|
-
* The original implementation was created by Guy Bedford in es-module-shims,
|
|
9
|
-
* then adapted by Mark Keller in dynamic-importmap, and further modified
|
|
10
|
-
* for use in this project.
|
|
11
|
-
*
|
|
12
|
-
* Both projects are licensed under the MIT license.
|
|
13
|
-
*
|
|
14
|
-
* MIT License: https://opensource.org/licenses/MIT
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
1
|
const backslashRegEx = /\\/g;
|
|
18
2
|
function isURL(url) {
|
|
19
|
-
if (url.indexOf(
|
|
3
|
+
if (url.indexOf(":") === -1) {
|
|
20
4
|
return false;
|
|
21
5
|
}
|
|
22
6
|
try {
|
|
@@ -27,89 +11,73 @@ function isURL(url) {
|
|
|
27
11
|
}
|
|
28
12
|
}
|
|
29
13
|
function resolveIfNotPlainOrUrl(relUrl, parentUrl) {
|
|
30
|
-
const hIdx = parentUrl.indexOf(
|
|
31
|
-
qIdx = parentUrl.indexOf('?');
|
|
14
|
+
const hIdx = parentUrl.indexOf("#"), qIdx = parentUrl.indexOf("?");
|
|
32
15
|
if (hIdx + qIdx > -2) {
|
|
33
|
-
parentUrl = parentUrl.slice(
|
|
34
|
-
|
|
35
|
-
|
|
16
|
+
parentUrl = parentUrl.slice(
|
|
17
|
+
0,
|
|
18
|
+
// eslint-disable-next-line no-nested-ternary
|
|
19
|
+
hIdx === -1 ? qIdx : qIdx === -1 || qIdx > hIdx ? hIdx : qIdx
|
|
20
|
+
);
|
|
36
21
|
}
|
|
37
|
-
if (relUrl.indexOf(
|
|
38
|
-
relUrl = relUrl.replace(backslashRegEx,
|
|
22
|
+
if (relUrl.indexOf("\\") !== -1) {
|
|
23
|
+
relUrl = relUrl.replace(backslashRegEx, "/");
|
|
39
24
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// Disabled, but these cases will give inconsistent results for deep backtracking
|
|
48
|
-
//if (parentUrl[parentProtocol.length] !== '/')
|
|
49
|
-
// throw new Error('Cannot resolve');
|
|
50
|
-
// read pathname from parent URL
|
|
51
|
-
// pathname taken to be part after leading "/"
|
|
25
|
+
if (relUrl[0] === "/" && relUrl[1] === "/") {
|
|
26
|
+
return parentUrl.slice(0, parentUrl.indexOf(":") + 1) + relUrl;
|
|
27
|
+
} else if (relUrl[0] === "." && (relUrl[1] === "/" || relUrl[1] === "." && (relUrl[2] === "/" || relUrl.length === 2 && (relUrl += "/")) || relUrl.length === 1 && (relUrl += "/")) || relUrl[0] === "/") {
|
|
28
|
+
const parentProtocol = parentUrl.slice(
|
|
29
|
+
0,
|
|
30
|
+
parentUrl.indexOf(":") + 1
|
|
31
|
+
);
|
|
52
32
|
let pathname;
|
|
53
|
-
if (parentUrl[parentProtocol.length + 1] ===
|
|
54
|
-
|
|
55
|
-
if (parentProtocol !== 'file:') {
|
|
33
|
+
if (parentUrl[parentProtocol.length + 1] === "/") {
|
|
34
|
+
if (parentProtocol !== "file:") {
|
|
56
35
|
pathname = parentUrl.slice(parentProtocol.length + 2);
|
|
57
|
-
pathname = pathname.slice(pathname.indexOf(
|
|
36
|
+
pathname = pathname.slice(pathname.indexOf("/") + 1);
|
|
58
37
|
} else {
|
|
59
38
|
pathname = parentUrl.slice(8);
|
|
60
39
|
}
|
|
61
40
|
} else {
|
|
62
|
-
|
|
63
|
-
|
|
41
|
+
pathname = parentUrl.slice(
|
|
42
|
+
parentProtocol.length + (parentUrl[parentProtocol.length] === "/")
|
|
43
|
+
);
|
|
64
44
|
}
|
|
65
|
-
if (relUrl[0] ===
|
|
45
|
+
if (relUrl[0] === "/") {
|
|
66
46
|
return parentUrl.slice(0, parentUrl.length - pathname.length - 1) + relUrl;
|
|
67
47
|
}
|
|
68
|
-
|
|
69
|
-
// join together and split for removal of .. and . segments
|
|
70
|
-
// looping the string instead of anything fancy for perf reasons
|
|
71
|
-
// '../../../../../z' resolved to 'x/y' is just 'z'
|
|
72
|
-
const segmented = pathname.slice(0, pathname.lastIndexOf('/') + 1) + relUrl;
|
|
48
|
+
const segmented = pathname.slice(0, pathname.lastIndexOf("/") + 1) + relUrl;
|
|
73
49
|
const output = [];
|
|
74
50
|
let segmentIndex = -1;
|
|
75
51
|
for (let i = 0; i < segmented.length; i++) {
|
|
76
|
-
// busy reading a segment - only terminate on '/'
|
|
77
52
|
if (segmentIndex !== -1) {
|
|
78
|
-
if (segmented[i] ===
|
|
53
|
+
if (segmented[i] === "/") {
|
|
79
54
|
output.push(segmented.slice(segmentIndex, i + 1));
|
|
80
55
|
segmentIndex = -1;
|
|
81
56
|
}
|
|
82
57
|
continue;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
else if (segmented[i] === '.') {
|
|
86
|
-
// ../ segment
|
|
87
|
-
if (segmented[i + 1] === '.' && (segmented[i + 2] === '/' || i + 2 === segmented.length)) {
|
|
58
|
+
} else if (segmented[i] === ".") {
|
|
59
|
+
if (segmented[i + 1] === "." && (segmented[i + 2] === "/" || i + 2 === segmented.length)) {
|
|
88
60
|
output.pop();
|
|
89
61
|
i += 2;
|
|
90
62
|
continue;
|
|
91
|
-
}
|
|
92
|
-
// ./ segment
|
|
93
|
-
else if (segmented[i + 1] === '/' || i + 1 === segmented.length) {
|
|
63
|
+
} else if (segmented[i + 1] === "/" || i + 1 === segmented.length) {
|
|
94
64
|
i += 1;
|
|
95
65
|
continue;
|
|
96
66
|
}
|
|
97
67
|
}
|
|
98
|
-
|
|
99
|
-
while (segmented[i] === '/') {
|
|
68
|
+
while (segmented[i] === "/") {
|
|
100
69
|
i++;
|
|
101
70
|
}
|
|
102
71
|
segmentIndex = i;
|
|
103
72
|
}
|
|
104
|
-
// finish reading out the last segment
|
|
105
73
|
if (segmentIndex !== -1) {
|
|
106
74
|
output.push(segmented.slice(segmentIndex));
|
|
107
75
|
}
|
|
108
|
-
return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join(
|
|
76
|
+
return parentUrl.slice(0, parentUrl.length - pathname.length) + output.join("");
|
|
109
77
|
}
|
|
110
78
|
}
|
|
111
79
|
function resolveUrl(relUrl, parentUrl) {
|
|
112
|
-
return resolveIfNotPlainOrUrl(relUrl, parentUrl) || (isURL(relUrl) ? relUrl : resolveIfNotPlainOrUrl(
|
|
80
|
+
return resolveIfNotPlainOrUrl(relUrl, parentUrl) || (isURL(relUrl) ? relUrl : resolveIfNotPlainOrUrl("./" + relUrl, parentUrl));
|
|
113
81
|
}
|
|
114
82
|
function getMatch(path, matchObj) {
|
|
115
83
|
if (matchObj[path]) {
|
|
@@ -121,7 +89,7 @@ function getMatch(path, matchObj) {
|
|
|
121
89
|
if (segment in matchObj) {
|
|
122
90
|
return segment;
|
|
123
91
|
}
|
|
124
|
-
} while ((sepIndex = path.lastIndexOf(
|
|
92
|
+
} while ((sepIndex = path.lastIndexOf("/", sepIndex - 1)) !== -1);
|
|
125
93
|
}
|
|
126
94
|
function applyPackages(id, packages) {
|
|
127
95
|
const pkgName = getMatch(id, packages);
|
|
@@ -133,81 +101,83 @@ function applyPackages(id, packages) {
|
|
|
133
101
|
return pkg + id.slice(pkgName.length);
|
|
134
102
|
}
|
|
135
103
|
}
|
|
136
|
-
function resolveImportMap(
|
|
137
|
-
let scopeUrl = parentUrl && getMatch(parentUrl,
|
|
104
|
+
function resolveImportMap(importMap2, resolvedOrPlain, parentUrl) {
|
|
105
|
+
let scopeUrl = parentUrl && getMatch(parentUrl, importMap2.scopes);
|
|
138
106
|
while (scopeUrl) {
|
|
139
|
-
const packageResolution = applyPackages(
|
|
107
|
+
const packageResolution = applyPackages(
|
|
108
|
+
resolvedOrPlain,
|
|
109
|
+
importMap2.scopes[scopeUrl]
|
|
110
|
+
);
|
|
140
111
|
if (packageResolution) {
|
|
141
112
|
return packageResolution;
|
|
142
113
|
}
|
|
143
|
-
scopeUrl = getMatch(
|
|
114
|
+
scopeUrl = getMatch(
|
|
115
|
+
scopeUrl.slice(0, scopeUrl.lastIndexOf("/")),
|
|
116
|
+
importMap2.scopes
|
|
117
|
+
);
|
|
144
118
|
}
|
|
145
|
-
return applyPackages(resolvedOrPlain,
|
|
119
|
+
return applyPackages(resolvedOrPlain, importMap2.imports) || resolvedOrPlain.indexOf(":") !== -1 && resolvedOrPlain;
|
|
146
120
|
}
|
|
147
|
-
function resolveAndComposePackages(packages, outPackages,
|
|
121
|
+
function resolveAndComposePackages(packages, outPackages, baseUrl2, parentMap) {
|
|
148
122
|
for (const p in packages) {
|
|
149
|
-
const resolvedLhs = resolveIfNotPlainOrUrl(p,
|
|
123
|
+
const resolvedLhs = resolveIfNotPlainOrUrl(p, baseUrl2) || p;
|
|
150
124
|
const target = packages[p];
|
|
151
|
-
if (typeof target !==
|
|
125
|
+
if (typeof target !== "string") {
|
|
152
126
|
continue;
|
|
153
127
|
}
|
|
154
|
-
const mapped = resolveImportMap(
|
|
128
|
+
const mapped = resolveImportMap(
|
|
129
|
+
parentMap,
|
|
130
|
+
resolveIfNotPlainOrUrl(target, baseUrl2) || target,
|
|
131
|
+
baseUrl2
|
|
132
|
+
);
|
|
155
133
|
if (mapped) {
|
|
156
134
|
outPackages[resolvedLhs] = mapped;
|
|
157
135
|
continue;
|
|
158
136
|
}
|
|
159
|
-
// console.warn(
|
|
160
|
-
// `Mapping "${ p }" -> "${ packages[ p ] }" does not resolve`
|
|
161
|
-
// );
|
|
162
137
|
}
|
|
163
138
|
}
|
|
164
|
-
function resolveAndComposeImportMap(json,
|
|
139
|
+
function resolveAndComposeImportMap(json, baseUrl2, parentMap) {
|
|
165
140
|
const outMap = {
|
|
166
141
|
imports: Object.assign({}, parentMap.imports),
|
|
167
142
|
scopes: Object.assign({}, parentMap.scopes)
|
|
168
143
|
};
|
|
169
144
|
if (json.imports) {
|
|
170
|
-
resolveAndComposePackages(
|
|
145
|
+
resolveAndComposePackages(
|
|
146
|
+
json.imports,
|
|
147
|
+
outMap.imports,
|
|
148
|
+
baseUrl2,
|
|
149
|
+
parentMap
|
|
150
|
+
);
|
|
171
151
|
}
|
|
172
152
|
if (json.scopes) {
|
|
173
153
|
for (const s in json.scopes) {
|
|
174
|
-
const resolvedScope = resolveUrl(s,
|
|
175
|
-
resolveAndComposePackages(
|
|
154
|
+
const resolvedScope = resolveUrl(s, baseUrl2);
|
|
155
|
+
resolveAndComposePackages(
|
|
156
|
+
json.scopes[s],
|
|
157
|
+
outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}),
|
|
158
|
+
baseUrl2,
|
|
159
|
+
parentMap
|
|
160
|
+
);
|
|
176
161
|
}
|
|
177
162
|
}
|
|
178
163
|
return outMap;
|
|
179
164
|
}
|
|
180
|
-
let importMap = {
|
|
181
|
-
imports: {},
|
|
182
|
-
scopes: {}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
// TODO: check if this baseURI should change per document, and so
|
|
186
|
-
// it need to be passed as a parameter to methods like `resolve`.
|
|
165
|
+
let importMap = { imports: {}, scopes: {} };
|
|
187
166
|
const baseUrl = document.baseURI;
|
|
188
167
|
const pageBaseUrl = baseUrl;
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
* @param importMapIn.scopes Scopes declaration.
|
|
196
|
-
*/
|
|
197
|
-
export function addImportMap(importMapIn) {
|
|
198
|
-
importMap = resolveAndComposeImportMap(importMapIn, pageBaseUrl, importMap);
|
|
168
|
+
function addImportMap(importMapIn) {
|
|
169
|
+
importMap = resolveAndComposeImportMap(
|
|
170
|
+
importMapIn,
|
|
171
|
+
pageBaseUrl,
|
|
172
|
+
importMap
|
|
173
|
+
);
|
|
199
174
|
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Resolves the URL of the passed module ID against the current internal
|
|
203
|
-
* dynamic import map.
|
|
204
|
-
*
|
|
205
|
-
* @param id Module ID.
|
|
206
|
-
* @param parentUrl Parent URL, in case the module ID is relative.
|
|
207
|
-
* @return Resolved module URL.
|
|
208
|
-
*/
|
|
209
|
-
export function resolve(id, parentUrl) {
|
|
175
|
+
function resolve(id, parentUrl) {
|
|
210
176
|
const urlResolved = resolveIfNotPlainOrUrl(id, parentUrl);
|
|
211
177
|
return resolveImportMap(importMap, urlResolved || id, parentUrl) || id;
|
|
212
178
|
}
|
|
213
|
-
|
|
179
|
+
export {
|
|
180
|
+
addImportMap,
|
|
181
|
+
resolve
|
|
182
|
+
};
|
|
183
|
+
//# sourceMappingURL=resolver.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"names":["backslashRegEx","isURL","url","indexOf","URL","_","resolveIfNotPlainOrUrl","relUrl","parentUrl","hIdx","qIdx","slice","replace","length","parentProtocol","pathname","segmented","lastIndexOf","output","segmentIndex","i","push","pop","join","resolveUrl","getMatch","path","matchObj","sepIndex","segment","applyPackages","id","packages","pkgName","pkg","resolveImportMap","importMap","resolvedOrPlain","scopeUrl","scopes","packageResolution","imports","resolveAndComposePackages","outPackages","baseUrl","parentMap","p","resolvedLhs","target","mapped","resolveAndComposeImportMap","json","outMap","Object","assign","s","resolvedScope","document","baseURI","pageBaseUrl","addImportMap","importMapIn","resolve","urlResolved"],"sources":["@wordpress/interactivity-router/src/assets/dynamic-importmap/resolver.ts"],"sourcesContent":["/**\n * This code is derived from the following projects:\n *\n * 1. dynamic-importmap (https://github.com/keller-mark/dynamic-importmap)\n * 2. es-module-shims (https://github.com/guybedford/es-module-shims)\n *\n * The original implementation was created by Guy Bedford in es-module-shims,\n * then adapted by Mark Keller in dynamic-importmap, and further modified\n * for use in this project.\n *\n * Both projects are licensed under the MIT license.\n *\n * MIT License: https://opensource.org/licenses/MIT\n */\n\nconst backslashRegEx = /\\\\/g;\n\nfunction isURL( url: string ) {\n\tif ( url.indexOf( ':' ) === -1 ) {\n\t\treturn false;\n\t}\n\ttry {\n\t\tnew URL( url );\n\t\treturn true;\n\t} catch ( _ ) {\n\t\treturn false;\n\t}\n}\n\nfunction resolveIfNotPlainOrUrl( relUrl, parentUrl ) {\n\tconst hIdx = parentUrl.indexOf( '#' ),\n\t\tqIdx = parentUrl.indexOf( '?' );\n\tif ( hIdx + qIdx > -2 ) {\n\t\tparentUrl = parentUrl.slice(\n\t\t\t0,\n\t\t\t// eslint-disable-next-line no-nested-ternary\n\t\t\thIdx === -1 ? qIdx : qIdx === -1 || qIdx > hIdx ? hIdx : qIdx\n\t\t);\n\t}\n\tif ( relUrl.indexOf( '\\\\' ) !== -1 ) {\n\t\trelUrl = relUrl.replace( backslashRegEx, '/' );\n\t}\n\t// protocol-relative\n\tif ( relUrl[ 0 ] === '/' && relUrl[ 1 ] === '/' ) {\n\t\treturn parentUrl.slice( 0, parentUrl.indexOf( ':' ) + 1 ) + relUrl;\n\t}\n\t// relative-url\n\telse if (\n\t\t( relUrl[ 0 ] === '.' &&\n\t\t\t( relUrl[ 1 ] === '/' ||\n\t\t\t\t( relUrl[ 1 ] === '.' &&\n\t\t\t\t\t( relUrl[ 2 ] === '/' ||\n\t\t\t\t\t\t( relUrl.length === 2 && ( relUrl += '/' ) ) ) ) ||\n\t\t\t\t( relUrl.length === 1 && ( relUrl += '/' ) ) ) ) ||\n\t\trelUrl[ 0 ] === '/'\n\t) {\n\t\tconst parentProtocol = parentUrl.slice(\n\t\t\t0,\n\t\t\tparentUrl.indexOf( ':' ) + 1\n\t\t);\n\t\t// Disabled, but these cases will give inconsistent results for deep backtracking\n\t\t//if (parentUrl[parentProtocol.length] !== '/')\n\t\t// throw new Error('Cannot resolve');\n\t\t// read pathname from parent URL\n\t\t// pathname taken to be part after leading \"/\"\n\t\tlet pathname;\n\t\tif ( parentUrl[ parentProtocol.length + 1 ] === '/' ) {\n\t\t\t// resolving to a :// so we need to read out the auth and host\n\t\t\tif ( parentProtocol !== 'file:' ) {\n\t\t\t\tpathname = parentUrl.slice( parentProtocol.length + 2 );\n\t\t\t\tpathname = pathname.slice( pathname.indexOf( '/' ) + 1 );\n\t\t\t} else {\n\t\t\t\tpathname = parentUrl.slice( 8 );\n\t\t\t}\n\t\t} else {\n\t\t\t// resolving to :/ so pathname is the /... part\n\t\t\tpathname = parentUrl.slice(\n\t\t\t\tparentProtocol.length +\n\t\t\t\t\t( parentUrl[ parentProtocol.length ] === '/' )\n\t\t\t);\n\t\t}\n\n\t\tif ( relUrl[ 0 ] === '/' ) {\n\t\t\treturn (\n\t\t\t\tparentUrl.slice( 0, parentUrl.length - pathname.length - 1 ) +\n\t\t\t\trelUrl\n\t\t\t);\n\t\t}\n\n\t\t// join together and split for removal of .. and . segments\n\t\t// looping the string instead of anything fancy for perf reasons\n\t\t// '../../../../../z' resolved to 'x/y' is just 'z'\n\t\tconst segmented =\n\t\t\tpathname.slice( 0, pathname.lastIndexOf( '/' ) + 1 ) + relUrl;\n\n\t\tconst output = [];\n\t\tlet segmentIndex = -1;\n\t\tfor ( let i = 0; i < segmented.length; i++ ) {\n\t\t\t// busy reading a segment - only terminate on '/'\n\t\t\tif ( segmentIndex !== -1 ) {\n\t\t\t\tif ( segmented[ i ] === '/' ) {\n\t\t\t\t\toutput.push( segmented.slice( segmentIndex, i + 1 ) );\n\t\t\t\t\tsegmentIndex = -1;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// new segment - check if it is relative\n\t\t\telse if ( segmented[ i ] === '.' ) {\n\t\t\t\t// ../ segment\n\t\t\t\tif (\n\t\t\t\t\tsegmented[ i + 1 ] === '.' &&\n\t\t\t\t\t( segmented[ i + 2 ] === '/' || i + 2 === segmented.length )\n\t\t\t\t) {\n\t\t\t\t\toutput.pop();\n\t\t\t\t\ti += 2;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// ./ segment\n\t\t\t\telse if (\n\t\t\t\t\tsegmented[ i + 1 ] === '/' ||\n\t\t\t\t\ti + 1 === segmented.length\n\t\t\t\t) {\n\t\t\t\t\ti += 1;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// it is the start of a new segment\n\t\t\twhile ( segmented[ i ] === '/' ) {\n\t\t\t\ti++;\n\t\t\t}\n\t\t\tsegmentIndex = i;\n\t\t}\n\t\t// finish reading out the last segment\n\t\tif ( segmentIndex !== -1 ) {\n\t\t\toutput.push( segmented.slice( segmentIndex ) );\n\t\t}\n\t\treturn (\n\t\t\tparentUrl.slice( 0, parentUrl.length - pathname.length ) +\n\t\t\toutput.join( '' )\n\t\t);\n\t}\n}\n\nfunction resolveUrl( relUrl, parentUrl ) {\n\treturn (\n\t\tresolveIfNotPlainOrUrl( relUrl, parentUrl ) ||\n\t\t( isURL( relUrl )\n\t\t\t? relUrl\n\t\t\t: resolveIfNotPlainOrUrl( './' + relUrl, parentUrl ) )\n\t);\n}\n\nfunction getMatch( path, matchObj ) {\n\tif ( matchObj[ path ] ) {\n\t\treturn path;\n\t}\n\tlet sepIndex = path.length;\n\tdo {\n\t\tconst segment = path.slice( 0, sepIndex + 1 );\n\t\tif ( segment in matchObj ) {\n\t\t\treturn segment;\n\t\t}\n\t} while ( ( sepIndex = path.lastIndexOf( '/', sepIndex - 1 ) ) !== -1 );\n}\n\nfunction applyPackages( id, packages ) {\n\tconst pkgName = getMatch( id, packages );\n\tif ( pkgName ) {\n\t\tconst pkg = packages[ pkgName ];\n\t\tif ( pkg === null ) {\n\t\t\treturn;\n\t\t}\n\t\treturn pkg + id.slice( pkgName.length );\n\t}\n}\n\nfunction resolveImportMap( importMap, resolvedOrPlain, parentUrl ) {\n\tlet scopeUrl = parentUrl && getMatch( parentUrl, importMap.scopes );\n\twhile ( scopeUrl ) {\n\t\tconst packageResolution = applyPackages(\n\t\t\tresolvedOrPlain,\n\t\t\timportMap.scopes[ scopeUrl ]\n\t\t);\n\t\tif ( packageResolution ) {\n\t\t\treturn packageResolution;\n\t\t}\n\t\tscopeUrl = getMatch(\n\t\t\tscopeUrl.slice( 0, scopeUrl.lastIndexOf( '/' ) ),\n\t\t\timportMap.scopes\n\t\t);\n\t}\n\treturn (\n\t\tapplyPackages( resolvedOrPlain, importMap.imports ) ||\n\t\t( resolvedOrPlain.indexOf( ':' ) !== -1 && resolvedOrPlain )\n\t);\n}\n\nfunction resolveAndComposePackages(\n\tpackages,\n\toutPackages,\n\tbaseUrl,\n\tparentMap\n) {\n\tfor ( const p in packages ) {\n\t\tconst resolvedLhs = resolveIfNotPlainOrUrl( p, baseUrl ) || p;\n\t\tconst target = packages[ p ];\n\t\tif ( typeof target !== 'string' ) {\n\t\t\tcontinue;\n\t\t}\n\t\tconst mapped = resolveImportMap(\n\t\t\tparentMap,\n\t\t\tresolveIfNotPlainOrUrl( target, baseUrl ) || target,\n\t\t\tbaseUrl\n\t\t);\n\t\tif ( mapped ) {\n\t\t\toutPackages[ resolvedLhs ] = mapped;\n\t\t\tcontinue;\n\t\t}\n\t\t// console.warn(\n\t\t// \t`Mapping \"${ p }\" -> \"${ packages[ p ] }\" does not resolve`\n\t\t// );\n\t}\n}\n\nfunction resolveAndComposeImportMap( json, baseUrl, parentMap ) {\n\tconst outMap = {\n\t\timports: Object.assign( {}, parentMap.imports ),\n\t\tscopes: Object.assign( {}, parentMap.scopes ),\n\t};\n\n\tif ( json.imports ) {\n\t\tresolveAndComposePackages(\n\t\t\tjson.imports,\n\t\t\toutMap.imports,\n\t\t\tbaseUrl,\n\t\t\tparentMap\n\t\t);\n\t}\n\n\tif ( json.scopes ) {\n\t\tfor ( const s in json.scopes ) {\n\t\t\tconst resolvedScope = resolveUrl( s, baseUrl );\n\t\t\tresolveAndComposePackages(\n\t\t\t\tjson.scopes[ s ],\n\t\t\t\toutMap.scopes[ resolvedScope ] ||\n\t\t\t\t\t( outMap.scopes[ resolvedScope ] = {} ),\n\t\t\t\tbaseUrl,\n\t\t\t\tparentMap\n\t\t\t);\n\t\t}\n\t}\n\n\treturn outMap;\n}\n\nlet importMap = { imports: {}, scopes: {} };\n\n// TODO: check if this baseURI should change per document, and so\n// it need to be passed as a parameter to methods like `resolve`.\nconst baseUrl = document.baseURI;\nconst pageBaseUrl = baseUrl;\n\n/**\n * Extends the internal dynamic import map with the passed one.\n *\n * @param importMapIn Import map.\n * @param importMapIn.imports Imports declaration.\n * @param importMapIn.scopes Scopes declaration.\n */\nexport function addImportMap( importMapIn: {\n\timports?: Record< string, string >;\n\tscopes?: Record< string, any >;\n} ) {\n\timportMap = resolveAndComposeImportMap(\n\t\timportMapIn,\n\t\tpageBaseUrl,\n\t\timportMap\n\t);\n}\n\n/**\n * Resolves the URL of the passed module ID against the current internal\n * dynamic import map.\n *\n * @param id Module ID.\n * @param parentUrl Parent URL, in case the module ID is relative.\n * @return Resolved module URL.\n */\nexport function resolve( id: string, parentUrl: string ): string {\n\tconst urlResolved = resolveIfNotPlainOrUrl( id, parentUrl );\n\treturn resolveImportMap( importMap, urlResolved || id, parentUrl ) || id;\n}\n"],"mappings":";AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,MAAMA,cAAc,GAAG,KAAK;AAE5B,SAASC,KAAKA,CAAEC,GAAW,EAAG;EAC7B,IAAKA,GAAG,CAACC,OAAO,CAAE,GAAI,CAAC,KAAK,CAAC,CAAC,EAAG;IAChC,OAAO,KAAK;EACb;EACA,IAAI;IACH,IAAIC,GAAG,CAAEF,GAAI,CAAC;IACd,OAAO,IAAI;EACZ,CAAC,CAAC,OAAQG,CAAC,EAAG;IACb,OAAO,KAAK;EACb;AACD;AAEA,SAASC,sBAAsBA,CAAEC,MAAM,EAAEC,SAAS,EAAG;EACpD,MAAMC,IAAI,GAAGD,SAAS,CAACL,OAAO,CAAE,GAAI,CAAC;IACpCO,IAAI,GAAGF,SAAS,CAACL,OAAO,CAAE,GAAI,CAAC;EAChC,IAA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,CAAC,EAAEX,OAAQ,CAAC;MAC9CF,yBAAyB,CACxBS,IAAI,CAACZ,MAAM,CAAEgB,CAAC,CAAE,EAChBH,MAAM,CAACb,MAAM,CAAEiB,aAAa,CAAE,KAC3BJ,MAAM,CAACb,MAAM,CAAEiB,aAAa,CAAE,GAAG,CAAC,CAAC,CAAE,EACxCZ,OAAO,EACPC,SACD,CAAC;IACF;EACD;EAEA,OAAOO,MAAM;AACd;AAEA,IAAIhB,SAAS,GAAG;EAAEK,OAAO,EAAE,CAAC,CAAC;EAAEF,MAAM,EAAE,CAAC;AAAE,CAAC;;AAE3C;AACA;AACA,MAAMK,OAAO,GAAGa,QAAQ,CAACC,OAAO;AAChC,MAAMC,WAAW,GAAGf,OAAO;;AAE3B;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASgB,YAAYA,CAAEC,WAG7B,EAAG;EACHzB,SAAS,GAAGc,0BAA0B,CACrCW,WAAW,EACXF,WAAW,EACXvB,SACD,CAAC;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAAS0B,OAAOA,CAAE/B,EAAU,EAAEvB,SAAiB,EAAW;EAChE,MAAMuD,WAAW,GAAGzD,sBAAsB,CAAEyB,EAAE,EAAEvB,SAAU,CAAC;EAC3D,OAAO2B,gBAAgB,CAAEC,SAAS,EAAE2B,WAAW,IAAIhC,EAAE,EAAEvB,SAAU,CAAC,IAAIuB,EAAE;AACzE","ignoreList":[]}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/assets/dynamic-importmap/resolver.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * This code is derived from the following projects:\n *\n * 1. dynamic-importmap (https://github.com/keller-mark/dynamic-importmap)\n * 2. es-module-shims (https://github.com/guybedford/es-module-shims)\n *\n * The original implementation was created by Guy Bedford in es-module-shims,\n * then adapted by Mark Keller in dynamic-importmap, and further modified\n * for use in this project.\n *\n * Both projects are licensed under the MIT license.\n *\n * MIT License: https://opensource.org/licenses/MIT\n */\n\nconst backslashRegEx = /\\\\/g;\n\nfunction isURL( url: string ) {\n\tif ( url.indexOf( ':' ) === -1 ) {\n\t\treturn false;\n\t}\n\ttry {\n\t\tnew URL( url );\n\t\treturn true;\n\t} catch ( _ ) {\n\t\treturn false;\n\t}\n}\n\nfunction resolveIfNotPlainOrUrl( relUrl, parentUrl ) {\n\tconst hIdx = parentUrl.indexOf( '#' ),\n\t\tqIdx = parentUrl.indexOf( '?' );\n\tif ( hIdx + qIdx > -2 ) {\n\t\tparentUrl = parentUrl.slice(\n\t\t\t0,\n\t\t\t// eslint-disable-next-line no-nested-ternary\n\t\t\thIdx === -1 ? qIdx : qIdx === -1 || qIdx > hIdx ? hIdx : qIdx\n\t\t);\n\t}\n\tif ( relUrl.indexOf( '\\\\' ) !== -1 ) {\n\t\trelUrl = relUrl.replace( backslashRegEx, '/' );\n\t}\n\t// protocol-relative\n\tif ( relUrl[ 0 ] === '/' && relUrl[ 1 ] === '/' ) {\n\t\treturn parentUrl.slice( 0, parentUrl.indexOf( ':' ) + 1 ) + relUrl;\n\t}\n\t// relative-url\n\telse if (\n\t\t( relUrl[ 0 ] === '.' &&\n\t\t\t( relUrl[ 1 ] === '/' ||\n\t\t\t\t( relUrl[ 1 ] === '.' &&\n\t\t\t\t\t( relUrl[ 2 ] === '/' ||\n\t\t\t\t\t\t( relUrl.length === 2 && ( relUrl += '/' ) ) ) ) ||\n\t\t\t\t( relUrl.length === 1 && ( relUrl += '/' ) ) ) ) ||\n\t\trelUrl[ 0 ] === '/'\n\t) {\n\t\tconst parentProtocol = parentUrl.slice(\n\t\t\t0,\n\t\t\tparentUrl.indexOf( ':' ) + 1\n\t\t);\n\t\t// Disabled, but these cases will give inconsistent results for deep backtracking\n\t\t//if (parentUrl[parentProtocol.length] !== '/')\n\t\t// throw new Error('Cannot resolve');\n\t\t// read pathname from parent URL\n\t\t// pathname taken to be part after leading \"/\"\n\t\tlet pathname;\n\t\tif ( parentUrl[ parentProtocol.length + 1 ] === '/' ) {\n\t\t\t// resolving to a :// so we need to read out the auth and host\n\t\t\tif ( parentProtocol !== 'file:' ) {\n\t\t\t\tpathname = parentUrl.slice( parentProtocol.length + 2 );\n\t\t\t\tpathname = pathname.slice( pathname.indexOf( '/' ) + 1 );\n\t\t\t} else {\n\t\t\t\tpathname = parentUrl.slice( 8 );\n\t\t\t}\n\t\t} else {\n\t\t\t// resolving to :/ so pathname is the /... part\n\t\t\tpathname = parentUrl.slice(\n\t\t\t\tparentProtocol.length +\n\t\t\t\t\t( parentUrl[ parentProtocol.length ] === '/' )\n\t\t\t);\n\t\t}\n\n\t\tif ( relUrl[ 0 ] === '/' ) {\n\t\t\treturn (\n\t\t\t\tparentUrl.slice( 0, parentUrl.length - pathname.length - 1 ) +\n\t\t\t\trelUrl\n\t\t\t);\n\t\t}\n\n\t\t// join together and split for removal of .. and . segments\n\t\t// looping the string instead of anything fancy for perf reasons\n\t\t// '../../../../../z' resolved to 'x/y' is just 'z'\n\t\tconst segmented =\n\t\t\tpathname.slice( 0, pathname.lastIndexOf( '/' ) + 1 ) + relUrl;\n\n\t\tconst output = [];\n\t\tlet segmentIndex = -1;\n\t\tfor ( let i = 0; i < segmented.length; i++ ) {\n\t\t\t// busy reading a segment - only terminate on '/'\n\t\t\tif ( segmentIndex !== -1 ) {\n\t\t\t\tif ( segmented[ i ] === '/' ) {\n\t\t\t\t\toutput.push( segmented.slice( segmentIndex, i + 1 ) );\n\t\t\t\t\tsegmentIndex = -1;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// new segment - check if it is relative\n\t\t\telse if ( segmented[ i ] === '.' ) {\n\t\t\t\t// ../ segment\n\t\t\t\tif (\n\t\t\t\t\tsegmented[ i + 1 ] === '.' &&\n\t\t\t\t\t( segmented[ i + 2 ] === '/' || i + 2 === segmented.length )\n\t\t\t\t) {\n\t\t\t\t\toutput.pop();\n\t\t\t\t\ti += 2;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// ./ segment\n\t\t\t\telse if (\n\t\t\t\t\tsegmented[ i + 1 ] === '/' ||\n\t\t\t\t\ti + 1 === segmented.length\n\t\t\t\t) {\n\t\t\t\t\ti += 1;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// it is the start of a new segment\n\t\t\twhile ( segmented[ i ] === '/' ) {\n\t\t\t\ti++;\n\t\t\t}\n\t\t\tsegmentIndex = i;\n\t\t}\n\t\t// finish reading out the last segment\n\t\tif ( segmentIndex !== -1 ) {\n\t\t\toutput.push( segmented.slice( segmentIndex ) );\n\t\t}\n\t\treturn (\n\t\t\tparentUrl.slice( 0, parentUrl.length - pathname.length ) +\n\t\t\toutput.join( '' )\n\t\t);\n\t}\n}\n\nfunction resolveUrl( relUrl, parentUrl ) {\n\treturn (\n\t\tresolveIfNotPlainOrUrl( relUrl, parentUrl ) ||\n\t\t( isURL( relUrl )\n\t\t\t? relUrl\n\t\t\t: resolveIfNotPlainOrUrl( './' + relUrl, parentUrl ) )\n\t);\n}\n\nfunction getMatch( path, matchObj ) {\n\tif ( matchObj[ path ] ) {\n\t\treturn path;\n\t}\n\tlet sepIndex = path.length;\n\tdo {\n\t\tconst segment = path.slice( 0, sepIndex + 1 );\n\t\tif ( segment in matchObj ) {\n\t\t\treturn segment;\n\t\t}\n\t} while ( ( sepIndex = path.lastIndexOf( '/', sepIndex - 1 ) ) !== -1 );\n}\n\nfunction applyPackages( id, packages ) {\n\tconst pkgName = getMatch( id, packages );\n\tif ( pkgName ) {\n\t\tconst pkg = packages[ pkgName ];\n\t\tif ( pkg === null ) {\n\t\t\treturn;\n\t\t}\n\t\treturn pkg + id.slice( pkgName.length );\n\t}\n}\n\nfunction resolveImportMap( importMap, resolvedOrPlain, parentUrl ) {\n\tlet scopeUrl = parentUrl && getMatch( parentUrl, importMap.scopes );\n\twhile ( scopeUrl ) {\n\t\tconst packageResolution = applyPackages(\n\t\t\tresolvedOrPlain,\n\t\t\timportMap.scopes[ scopeUrl ]\n\t\t);\n\t\tif ( packageResolution ) {\n\t\t\treturn packageResolution;\n\t\t}\n\t\tscopeUrl = getMatch(\n\t\t\tscopeUrl.slice( 0, scopeUrl.lastIndexOf( '/' ) ),\n\t\t\timportMap.scopes\n\t\t);\n\t}\n\treturn (\n\t\tapplyPackages( resolvedOrPlain, importMap.imports ) ||\n\t\t( resolvedOrPlain.indexOf( ':' ) !== -1 && resolvedOrPlain )\n\t);\n}\n\nfunction resolveAndComposePackages(\n\tpackages,\n\toutPackages,\n\tbaseUrl,\n\tparentMap\n) {\n\tfor ( const p in packages ) {\n\t\tconst resolvedLhs = resolveIfNotPlainOrUrl( p, baseUrl ) || p;\n\t\tconst target = packages[ p ];\n\t\tif ( typeof target !== 'string' ) {\n\t\t\tcontinue;\n\t\t}\n\t\tconst mapped = resolveImportMap(\n\t\t\tparentMap,\n\t\t\tresolveIfNotPlainOrUrl( target, baseUrl ) || target,\n\t\t\tbaseUrl\n\t\t);\n\t\tif ( mapped ) {\n\t\t\toutPackages[ resolvedLhs ] = mapped;\n\t\t\tcontinue;\n\t\t}\n\t\t// console.warn(\n\t\t// \t`Mapping \"${ p }\" -> \"${ packages[ p ] }\" does not resolve`\n\t\t// );\n\t}\n}\n\nfunction resolveAndComposeImportMap( json, baseUrl, parentMap ) {\n\tconst outMap = {\n\t\timports: Object.assign( {}, parentMap.imports ),\n\t\tscopes: Object.assign( {}, parentMap.scopes ),\n\t};\n\n\tif ( json.imports ) {\n\t\tresolveAndComposePackages(\n\t\t\tjson.imports,\n\t\t\toutMap.imports,\n\t\t\tbaseUrl,\n\t\t\tparentMap\n\t\t);\n\t}\n\n\tif ( json.scopes ) {\n\t\tfor ( const s in json.scopes ) {\n\t\t\tconst resolvedScope = resolveUrl( s, baseUrl );\n\t\t\tresolveAndComposePackages(\n\t\t\t\tjson.scopes[ s ],\n\t\t\t\toutMap.scopes[ resolvedScope ] ||\n\t\t\t\t\t( outMap.scopes[ resolvedScope ] = {} ),\n\t\t\t\tbaseUrl,\n\t\t\t\tparentMap\n\t\t\t);\n\t\t}\n\t}\n\n\treturn outMap;\n}\n\nlet importMap = { imports: {}, scopes: {} };\n\n// TODO: check if this baseURI should change per document, and so\n// it need to be passed as a parameter to methods like `resolve`.\nconst baseUrl = document.baseURI;\nconst pageBaseUrl = baseUrl;\n\n/**\n * Extends the internal dynamic import map with the passed one.\n *\n * @param importMapIn Import map.\n * @param importMapIn.imports Imports declaration.\n * @param importMapIn.scopes Scopes declaration.\n */\nexport function addImportMap( importMapIn: {\n\timports?: Record< string, string >;\n\tscopes?: Record< string, any >;\n} ) {\n\timportMap = resolveAndComposeImportMap(\n\t\timportMapIn,\n\t\tpageBaseUrl,\n\t\timportMap\n\t);\n}\n\n/**\n * Resolves the URL of the passed module ID against the current internal\n * dynamic import map.\n *\n * @param id Module ID.\n * @param parentUrl Parent URL, in case the module ID is relative.\n * @return Resolved module URL.\n */\nexport function resolve( id: string, parentUrl: string ): string {\n\tconst urlResolved = resolveIfNotPlainOrUrl( id, parentUrl );\n\treturn resolveImportMap( importMap, urlResolved || id, parentUrl ) || id;\n}\n"],
|
|
5
|
+
"mappings": "AAeA,MAAM,iBAAiB;AAEvB,SAAS,MAAO,KAAc;AAC7B,MAAK,IAAI,QAAS,GAAI,MAAM,IAAK;AAChC,WAAO;AAAA,EACR;AACA,MAAI;AACH,QAAI,IAAK,GAAI;AACb,WAAO;AAAA,EACR,SAAU,GAAI;AACb,WAAO;AAAA,EACR;AACD;AAEA,SAAS,uBAAwB,QAAQ,WAAY;AACpD,QAAM,OAAO,UAAU,QAAS,GAAI,GACnC,OAAO,UAAU,QAAS,GAAI;AAC/B,MAAK,OAAO,OAAO,IAAK;AACvB,gBAAY,UAAU;AAAA,MACrB;AAAA;AAAA,MAEA,SAAS,KAAK,OAAO,SAAS,MAAM,OAAO,OAAO,OAAO;AAAA,IAC1D;AAAA,EACD;AACA,MAAK,OAAO,QAAS,IAAK,MAAM,IA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AAA,QACA;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AACR;AAEA,IAAI,YAAY,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAC,EAAE;AAI1C,MAAM,UAAU,SAAS;AACzB,MAAM,cAAc;AASb,SAAS,aAAc,aAG1B;AACH,cAAY;AAAA,IACX;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;AAUO,SAAS,QAAS,IAAY,WAA4B;AAChE,QAAM,cAAc,uBAAwB,IAAI,SAAU;AAC1D,SAAO,iBAAkB,WAAW,eAAe,IAAI,SAAU,KAAK;AACvE;",
|
|
6
|
+
"names": ["importMap", "baseUrl"]
|
|
7
|
+
}
|
|
@@ -1,55 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* processed internally.
|
|
9
|
-
*/
|
|
10
|
-
const resolvedScriptModules = new Set();
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Marks the specified module as natively resolved.
|
|
14
|
-
* @param url Script module URL.
|
|
15
|
-
*/
|
|
16
|
-
export const markScriptModuleAsResolved = url => {
|
|
1
|
+
import {
|
|
2
|
+
initialImportMap,
|
|
3
|
+
importPreloadedModule,
|
|
4
|
+
preloadWithMap
|
|
5
|
+
} from "./dynamic-importmap";
|
|
6
|
+
const resolvedScriptModules = /* @__PURE__ */ new Set();
|
|
7
|
+
const markScriptModuleAsResolved = (url) => {
|
|
17
8
|
resolvedScriptModules.add(url);
|
|
18
9
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* @param doc Document containing the modules to preload.
|
|
25
|
-
* @return Array of promises that resolve to a `ScriptModuleLoad` instance.
|
|
26
|
-
*/
|
|
27
|
-
export const preloadScriptModules = doc => {
|
|
28
|
-
// Extract the import map from the document.
|
|
29
|
-
const importMapElement = doc.querySelector('script#wp-importmap[type=importmap]');
|
|
30
|
-
const importMap = importMapElement ? JSON.parse(importMapElement.text) : {
|
|
31
|
-
imports: {},
|
|
32
|
-
scopes: {}
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// Remove imports also in the initial page's import map.
|
|
36
|
-
// Those should be handled natively.
|
|
10
|
+
const preloadScriptModules = (doc) => {
|
|
11
|
+
const importMapElement = doc.querySelector(
|
|
12
|
+
"script#wp-importmap[type=importmap]"
|
|
13
|
+
);
|
|
14
|
+
const importMap = importMapElement ? JSON.parse(importMapElement.text) : { imports: {}, scopes: {} };
|
|
37
15
|
for (const key in initialImportMap.imports) {
|
|
38
16
|
delete importMap.imports[key];
|
|
39
17
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return moduleUrls.filter(url => !resolvedScriptModules.has(url)).map(url => preloadWithMap(url, importMap));
|
|
18
|
+
const moduleUrls = [
|
|
19
|
+
...doc.querySelectorAll(
|
|
20
|
+
"script[type=module][src]"
|
|
21
|
+
)
|
|
22
|
+
].map((s) => s.src);
|
|
23
|
+
return moduleUrls.filter((url) => !resolvedScriptModules.has(url)).map((url) => preloadWithMap(url, importMap));
|
|
46
24
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
export const importScriptModules = modules => Promise.all(modules.map(m => importPreloadedModule(m)));
|
|
55
|
-
//# sourceMappingURL=script-modules.js.map
|
|
25
|
+
const importScriptModules = (modules) => Promise.all(modules.map((m) => importPreloadedModule(m)));
|
|
26
|
+
export {
|
|
27
|
+
importScriptModules,
|
|
28
|
+
markScriptModuleAsResolved,
|
|
29
|
+
preloadScriptModules
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=script-modules.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/assets/script-modules.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Internal dependencies\n */\nimport {\n\tinitialImportMap,\n\timportPreloadedModule,\n\tpreloadWithMap,\n\ttype ModuleLoad,\n} from './dynamic-importmap';\n\n/**\n * IDs of modules that should be resolved by the browser rather than\n * processed internally.\n */\nconst resolvedScriptModules = new Set< string >();\n\n/**\n * Marks the specified module as natively resolved.\n * @param url Script module URL.\n */\nexport const markScriptModuleAsResolved = ( url: string ) => {\n\tresolvedScriptModules.add( url );\n};\n\n/**\n * Resolves and fetches modules present in the passed document, using the\n * document's import map to resolve them.\n *\n * @param doc Document containing the modules to preload.\n * @return Array of promises that resolve to a `ScriptModuleLoad` instance.\n */\nexport const preloadScriptModules = ( doc: Document ) => {\n\t// Extract the import map from the document.\n\tconst importMapElement = doc.querySelector< HTMLScriptElement >(\n\t\t'script#wp-importmap[type=importmap]'\n\t);\n\tconst importMap = importMapElement\n\t\t? JSON.parse( importMapElement.text )\n\t\t: { imports: {}, scopes: {} };\n\n\t// Remove imports also in the initial page's import map.\n\t// Those should be handled natively.\n\tfor ( const key in initialImportMap.imports ) {\n\t\tdelete importMap.imports[ key ];\n\t}\n\n\t// Get the URL of all modules contained in the document.\n\tconst moduleUrls = [\n\t\t...doc.querySelectorAll< HTMLScriptElement >(\n\t\t\t'script[type=module][src]'\n\t\t),\n\t].map( ( s ) => s.src );\n\n\t// Resolve and fetch those not resolved natively.\n\treturn moduleUrls\n\t\t.filter( ( url ) => ! resolvedScriptModules.has( url ) )\n\t\t.map( ( url ) => preloadWithMap( url, importMap ) );\n};\n\n/**\n * Imports modules respresented by the passed `ScriptModuleLoad` instances.\n *\n * @param modules Array of `MoudleLoad` instances.\n * @return Promise that resolves once all modules are imported.\n */\nexport const importScriptModules = ( modules: ScriptModuleLoad[] ) =>\n\tPromise.all( modules.map( ( m ) => importPreloadedModule( m ) ) );\n\nexport type ScriptModuleLoad = ModuleLoad;\n"],
|
|
5
|
+
"mappings": "AAGA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OAEM;AAMP,MAAM,wBAAwB,oBAAI,IAAc;AAMzC,MAAM,6BAA6B,CAAE,QAAiB;AAC5D,wBAAsB,IAAK,GAAI;AAChC;AASO,MAAM,uBAAuB,CAAE,QAAmB;AAExD,QAAM,mBAAmB,IAAI;AAAA,IAC5B;AAAA,EACD;AACA,QAAM,YAAY,mBACf,KAAK,MAAO,iBAAiB,IAAK,IAClC,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAC,EAAE;AAI7B,aAAY,OAAO,iBAAiB,SAAU;AAC7C,WAAO,UAAU,QAAS,GAAI;AAAA,EAC/B;AAGA,QAAM,aAAa;AAAA,IAClB,GAAG,IAAI;AAAA,MACN;AAAA,IACD;AAAA,EACD,EAAE,IAAK,CAAE,MAAO,EAAE,GAAI;AAGtB,SAAO,WACL,OAAQ,CAAE,QAAS,CAAE,sBAAsB,IAAK,GAAI,CAAE,EACtD,IAAK,CAAE,QAAS,eAAgB,KAAK,SAAU,CAAE;AACpD;AAQO,MAAM,sBAAsB,CAAE,YACpC,QAAQ,IAAK,QAAQ,IAAK,CAAE,MAAO,sBAAuB,CAAE,CAAE,CAAE;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,50 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
* Calculates the Shortest Common Supersequence (SCS) of two sequences.
|
|
3
|
-
*
|
|
4
|
-
* A supersequence is a sequence that contains both input sequences as subsequences.
|
|
5
|
-
* The shortest common supersequence is the shortest possible such sequence.
|
|
6
|
-
*
|
|
7
|
-
* This implementation uses dynamic programming with a time complexity of O(mn)
|
|
8
|
-
* and space complexity of O(mn), where m and n are the lengths of sequences X and Y.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* const seq1 = [1, 3, 5];
|
|
13
|
-
* const seq2 = [2, 3, 4];
|
|
14
|
-
* const scs = shortestCommonSupersequence(seq1, seq2); // [1, 2, 3, 4, 5]
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @param X The first sequence.
|
|
18
|
-
* @param Y The second sequence.
|
|
19
|
-
* @param isEqual Optional equality function to compare elements.
|
|
20
|
-
* Defaults to strict equality (===).
|
|
21
|
-
* @return The shortest common supersequence of X and Y.
|
|
22
|
-
*/
|
|
23
|
-
export function shortestCommonSupersequence(X, Y, isEqual = (a, b) => a === b) {
|
|
1
|
+
function shortestCommonSupersequence(X, Y, isEqual = (a, b) => a === b) {
|
|
24
2
|
const m = X.length;
|
|
25
3
|
const n = Y.length;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}, () => Array(n + 1).fill(null));
|
|
31
|
-
|
|
32
|
-
// Base cases: one of the sequences is empty.
|
|
4
|
+
const dp = Array.from(
|
|
5
|
+
{ length: m + 1 },
|
|
6
|
+
() => Array(n + 1).fill(null)
|
|
7
|
+
);
|
|
33
8
|
for (let i = 0; i <= m; i++) {
|
|
34
9
|
dp[i][0] = X.slice(0, i);
|
|
35
10
|
}
|
|
36
11
|
for (let j = 0; j <= n; j++) {
|
|
37
12
|
dp[0][j] = Y.slice(0, j);
|
|
38
13
|
}
|
|
39
|
-
|
|
40
|
-
// Fill in the dp table.
|
|
41
14
|
for (let i = 1; i <= m; i++) {
|
|
42
15
|
for (let j = 1; j <= n; j++) {
|
|
43
16
|
if (isEqual(X[i - 1], Y[j - 1])) {
|
|
44
|
-
// When X[i-1] equals Y[j-1], use the reference from X.
|
|
45
17
|
dp[i][j] = dp[i - 1][j - 1].concat(X[i - 1]);
|
|
46
18
|
} else {
|
|
47
|
-
// Choose the shorter option between appending X[i-1] or Y[j-1].
|
|
48
19
|
const option1 = dp[i - 1][j].concat(X[i - 1]);
|
|
49
20
|
const option2 = dp[i][j - 1].concat(Y[j - 1]);
|
|
50
21
|
dp[i][j] = option1.length <= option2.length ? option1 : option2;
|
|
@@ -53,4 +24,7 @@ export function shortestCommonSupersequence(X, Y, isEqual = (a, b) => a === b) {
|
|
|
53
24
|
}
|
|
54
25
|
return dp[m][n];
|
|
55
26
|
}
|
|
56
|
-
|
|
27
|
+
export {
|
|
28
|
+
shortestCommonSupersequence
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=scs.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/assets/scs.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Calculates the Shortest Common Supersequence (SCS) of two sequences.\n *\n * A supersequence is a sequence that contains both input sequences as subsequences.\n * The shortest common supersequence is the shortest possible such sequence.\n *\n * This implementation uses dynamic programming with a time complexity of O(mn)\n * and space complexity of O(mn), where m and n are the lengths of sequences X and Y.\n *\n * @example\n * ```ts\n * const seq1 = [1, 3, 5];\n * const seq2 = [2, 3, 4];\n * const scs = shortestCommonSupersequence(seq1, seq2); // [1, 2, 3, 4, 5]\n * ```\n *\n * @param X The first sequence.\n * @param Y The second sequence.\n * @param isEqual Optional equality function to compare elements.\n * Defaults to strict equality (===).\n * @return The shortest common supersequence of X and Y.\n */\nexport function shortestCommonSupersequence< E = unknown >(\n\tX: E[],\n\tY: E[],\n\tisEqual = ( a: E, b: E ) => a === b\n) {\n\tconst m = X.length;\n\tconst n = Y.length;\n\n\t// Create a 2D dp table where dp[i][j] is the SCS for X[0..i-1] and Y[0..j-1].\n\tconst dp: E[][][] = Array.from( { length: m + 1 }, () =>\n\t\tArray( n + 1 ).fill( null )\n\t);\n\n\t// Base cases: one of the sequences is empty.\n\tfor ( let i = 0; i <= m; i++ ) {\n\t\tdp[ i ][ 0 ] = X.slice( 0, i );\n\t}\n\tfor ( let j = 0; j <= n; j++ ) {\n\t\tdp[ 0 ][ j ] = Y.slice( 0, j );\n\t}\n\n\t// Fill in the dp table.\n\tfor ( let i = 1; i <= m; i++ ) {\n\t\tfor ( let j = 1; j <= n; j++ ) {\n\t\t\tif ( isEqual( X[ i - 1 ], Y[ j - 1 ] ) ) {\n\t\t\t\t// When X[i-1] equals Y[j-1], use the reference from X.\n\t\t\t\tdp[ i ][ j ] = dp[ i - 1 ][ j - 1 ].concat( X[ i - 1 ] );\n\t\t\t} else {\n\t\t\t\t// Choose the shorter option between appending X[i-1] or Y[j-1].\n\t\t\t\tconst option1 = dp[ i - 1 ][ j ].concat( X[ i - 1 ] );\n\t\t\t\tconst option2 = dp[ i ][ j - 1 ].concat( Y[ j - 1 ] );\n\t\t\t\tdp[ i ][ j ] =\n\t\t\t\t\toption1.length <= option2.length ? option1 : option2;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn dp[ m ][ n ];\n}\n"],
|
|
5
|
+
"mappings": "AAsBO,SAAS,4BACf,GACA,GACA,UAAU,CAAE,GAAM,MAAU,MAAM,GACjC;AACD,QAAM,IAAI,EAAE;AACZ,QAAM,IAAI,EAAE;AAGZ,QAAM,KAAc,MAAM;AAAA,IAAM,EAAE,QAAQ,IAAI,EAAE;AAAA,IAAG,MAClD,MAAO,IAAI,CAAE,EAAE,KAAM,IAAK;AAAA,EAC3B;AAGA,WAAU,IAAI,GAAG,KAAK,GAAG,KAAM;AAC9B,OAAI,CAAE,EAAG,CAAE,IAAI,EAAE,MAAO,GAAG,CAAE;AAAA,EAC9B;AACA,WAAU,IAAI,GAAG,KAAK,GAAG,KAAM;AAC9B,OAAI,CAAE,EAAG,CAAE,IAAI,EAAE,MAAO,GAAG,CAAE;AAAA,EAC9B;AAGA,WAAU,IAAI,GAAG,KAAK,GAAG,KAAM;AAC9B,aAAU,IAAI,GAAG,KAAK,GAAG,KAAM;AAC9B,UAAK,QAAS,EAAG,IAAI,CAAE,GAAG,EAAG,IAAI,CAAE,CAAE,GAAI;AAExC,WAAI,CAAE,EAAG,CAAE,IAAI,GAAI,IAAI,CAAE,EAAG,IAAI,CAAE,EAAE,OAAQ,EAAG,IAAI,CAAE,CAAE;AAAA,MACxD,OAAO;AAEN,cAAM,UAAU,GAAI,IAAI,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