@qiankunjs/shared 0.0.1-alpha.2 → 0.0.1-alpha.3
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/dist/cjs/assets-transpilers/index.js +16 -6
- package/dist/cjs/assets-transpilers/index.js.map +1 -1
- package/dist/cjs/assets-transpilers/link.js +3 -2
- package/dist/cjs/assets-transpilers/link.js.map +1 -1
- package/dist/cjs/assets-transpilers/script.d.ts +2 -1
- package/dist/cjs/assets-transpilers/script.js +29 -18
- package/dist/cjs/assets-transpilers/script.js.map +1 -1
- package/dist/esm/assets-transpilers/index.js +16 -6
- package/dist/esm/assets-transpilers/index.js.map +1 -1
- package/dist/esm/assets-transpilers/link.js +3 -2
- package/dist/esm/assets-transpilers/link.js.map +1 -1
- package/dist/esm/assets-transpilers/script.d.ts +2 -1
- package/dist/esm/assets-transpilers/script.js +29 -18
- package/dist/esm/assets-transpilers/script.js.map +1 -1
- package/package.json +2 -2
- package/src/assets-transpilers/index.ts +15 -5
- package/src/assets-transpilers/link.ts +4 -2
- package/src/assets-transpilers/script.ts +38 -18
|
@@ -13,22 +13,32 @@ var _script = _interopRequireDefault(require("./script"));
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
function transpileAssets(node, baseURI, opts) {
|
|
16
|
-
var
|
|
17
|
-
|
|
16
|
+
var htmlElement = node;
|
|
17
|
+
var tagName = htmlElement.tagName;
|
|
18
18
|
switch (tagName) {
|
|
19
19
|
case 'SCRIPT':
|
|
20
20
|
{
|
|
21
|
-
(0, _script.default)(
|
|
22
|
-
break;
|
|
21
|
+
return (0, _script.default)(htmlElement, baseURI, opts);
|
|
23
22
|
}
|
|
24
23
|
case 'LINK':
|
|
25
24
|
{
|
|
26
|
-
(0, _link.default)(
|
|
27
|
-
break;
|
|
25
|
+
return (0, _link.default)(htmlElement, baseURI, opts);
|
|
28
26
|
}
|
|
29
27
|
default:
|
|
30
28
|
break;
|
|
31
29
|
}
|
|
30
|
+
|
|
31
|
+
// TODO need to deal with the text scenario of script
|
|
32
|
+
// const { nodeType } = node;
|
|
33
|
+
// switch (nodeType) {
|
|
34
|
+
// case Node.TEXT_NODE: {
|
|
35
|
+
// if (node.parentNode?.nodeName === 'script') {
|
|
36
|
+
// break;
|
|
37
|
+
// }
|
|
38
|
+
// }
|
|
39
|
+
//
|
|
40
|
+
// }
|
|
41
|
+
// }
|
|
32
42
|
return node;
|
|
33
43
|
}
|
|
34
44
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_link","_interopRequireDefault","require","_script","transpileAssets","node","baseURI","opts","
|
|
1
|
+
{"version":3,"names":["_link","_interopRequireDefault","require","_script","transpileAssets","node","baseURI","opts","htmlElement","tagName","transpileScript","transpileLink"],"sources":["../../../src/assets-transpilers/index.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-14\n */\nimport transpileLink from './link';\nimport type { TransformerOpts } from './script';\nimport transpileScript from './script';\n\nexport function transpileAssets<T extends Node>(node: T, baseURI: string, opts: TransformerOpts): T {\n const htmlElement = (node as unknown) as HTMLElement;\n const { tagName } = htmlElement;\n\n switch (tagName) {\n case 'SCRIPT': {\n return (transpileScript(htmlElement as HTMLScriptElement, baseURI, opts) as unknown) as T;\n }\n\n case 'LINK': {\n return (transpileLink(htmlElement as HTMLLinkElement, baseURI, opts) as unknown) as T;\n }\n\n default:\n break;\n }\n\n // TODO need to deal with the text scenario of script\n // const { nodeType } = node;\n // switch (nodeType) {\n // case Node.TEXT_NODE: {\n // if (node.parentNode?.nodeName === 'script') {\n // break;\n // }\n // }\n //\n // }\n // }\n return node;\n}\n\nexport type { TransformerOpts };\n"],"mappings":";;;;;;;AAIA,IAAAA,KAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC,OAAA,GAAAF,sBAAA,CAAAC,OAAA;AANA;AACA;AACA;AACA;;AAKO,SAASE,eAAeA,CAAiBC,IAAO,EAAEC,OAAe,EAAEC,IAAqB,EAAK;EAClG,IAAMC,WAAW,GAAIH,IAA+B;EACpD,IAAQI,OAAO,GAAKD,WAAW,CAAvBC,OAAO;EAEf,QAAQA,OAAO;IACb,KAAK,QAAQ;MAAE;QACb,OAAQ,IAAAC,eAAe,EAACF,WAAW,EAAuBF,OAAO,EAAEC,IAAI,CAAC;MAC1E;IAEA,KAAK,MAAM;MAAE;QACX,OAAQ,IAAAI,aAAa,EAACH,WAAW,EAAqBF,OAAO,EAAEC,IAAI,CAAC;MACtE;IAEA;MACE;EACJ;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,OAAOF,IAAI;AACb"}
|
|
@@ -24,9 +24,10 @@ function transpileLink(link, baseURI, opts) {
|
|
|
24
24
|
link.href = URL.createObjectURL(new Blob(["// ".concat(linkHref, " has reused the execution result of ").concat(url)], {
|
|
25
25
|
type: 'application/javascript'
|
|
26
26
|
}));
|
|
27
|
-
|
|
28
|
-
link.href = (0, _utils.getEntireUrl)(hrefAttribute, baseURI);
|
|
27
|
+
return link;
|
|
29
28
|
}
|
|
29
|
+
link.href = (0, _utils.getEntireUrl)(hrefAttribute, baseURI);
|
|
30
|
+
return link;
|
|
30
31
|
}
|
|
31
32
|
return link;
|
|
32
33
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_utils","require","transpileLink","link","baseURI","opts","moduleResolver","hrefAttribute","getAttribute","linkHref","getEntireUrl","matchedAssets","url","version","dataset","href","URL","createObjectURL","Blob","concat","type"],"sources":["../../../src/assets-transpilers/link.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-04-26\n */\nimport { getEntireUrl } from '../utils';\nimport type { TransformerOpts } from './script';\n\nexport default function transpileLink(link: HTMLLinkElement, baseURI: string, opts: TransformerOpts): HTMLLinkElement {\n const { moduleResolver } = opts;\n\n const hrefAttribute = link.getAttribute('href');\n if (hrefAttribute) {\n const linkHref = getEntireUrl(hrefAttribute, baseURI);\n\n const matchedAssets = moduleResolver?.(hrefAttribute);\n if (matchedAssets) {\n const { url, version } = matchedAssets;\n link.dataset.href = linkHref;\n link.dataset.version = version;\n link.href = URL.createObjectURL(\n new Blob([`// ${linkHref} has reused the execution result of ${url}`], {\n type: 'application/javascript',\n }),\n );\n }
|
|
1
|
+
{"version":3,"names":["_utils","require","transpileLink","link","baseURI","opts","moduleResolver","hrefAttribute","getAttribute","linkHref","getEntireUrl","matchedAssets","url","version","dataset","href","URL","createObjectURL","Blob","concat","type"],"sources":["../../../src/assets-transpilers/link.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-04-26\n */\nimport { getEntireUrl } from '../utils';\nimport type { TransformerOpts } from './script';\n\nexport default function transpileLink(link: HTMLLinkElement, baseURI: string, opts: TransformerOpts): HTMLLinkElement {\n const { moduleResolver } = opts;\n\n const hrefAttribute = link.getAttribute('href');\n if (hrefAttribute) {\n const linkHref = getEntireUrl(hrefAttribute, baseURI);\n\n const matchedAssets = moduleResolver?.(hrefAttribute);\n if (matchedAssets) {\n const { url, version } = matchedAssets;\n link.dataset.href = linkHref;\n link.dataset.version = version;\n link.href = URL.createObjectURL(\n new Blob([`// ${linkHref} has reused the execution result of ${url}`], {\n type: 'application/javascript',\n }),\n );\n return link;\n }\n\n link.href = getEntireUrl(hrefAttribute, baseURI);\n return link;\n }\n\n return link;\n}\n"],"mappings":";;;;;;AAIA,IAAAA,MAAA,GAAAC,OAAA;AAJA;AACA;AACA;AACA;;AAIe,SAASC,aAAaA,CAACC,IAAqB,EAAEC,OAAe,EAAEC,IAAqB,EAAmB;EACpH,IAAQC,cAAc,GAAKD,IAAI,CAAvBC,cAAc;EAEtB,IAAMC,aAAa,GAAGJ,IAAI,CAACK,YAAY,CAAC,MAAM,CAAC;EAC/C,IAAID,aAAa,EAAE;IACjB,IAAME,QAAQ,GAAG,IAAAC,mBAAY,EAACH,aAAa,EAAEH,OAAO,CAAC;IAErD,IAAMO,aAAa,GAAGL,cAAc,aAAdA,cAAc,uBAAdA,cAAc,CAAGC,aAAa,CAAC;IACrD,IAAII,aAAa,EAAE;MACjB,IAAQC,GAAG,GAAcD,aAAa,CAA9BC,GAAG;QAAEC,OAAO,GAAKF,aAAa,CAAzBE,OAAO;MACpBV,IAAI,CAACW,OAAO,CAACC,IAAI,GAAGN,QAAQ;MAC5BN,IAAI,CAACW,OAAO,CAACD,OAAO,GAAGA,OAAO;MAC9BV,IAAI,CAACY,IAAI,GAAGC,GAAG,CAACC,eAAe,CAC7B,IAAIC,IAAI,CAAC,OAAAC,MAAA,CAAOV,QAAQ,0CAAAU,MAAA,CAAuCP,GAAG,EAAG,EAAE;QACrEQ,IAAI,EAAE;MACR,CAAC,CACH,CAAC;MACD,OAAOjB,IAAI;IACb;IAEAA,IAAI,CAACY,IAAI,GAAG,IAAAL,mBAAY,EAACH,aAAa,EAAEH,OAAO,CAAC;IAChD,OAAOD,IAAI;EACb;EAEA,OAAOA,IAAI;AACb"}
|
|
@@ -8,5 +8,6 @@ export type TransformerOpts = {
|
|
|
8
8
|
fetch: typeof window.fetch;
|
|
9
9
|
sandbox?: Sandbox;
|
|
10
10
|
moduleResolver?: (url: string) => MatchResult | undefined;
|
|
11
|
+
rawNode: Node;
|
|
11
12
|
};
|
|
12
|
-
export default function transpileScript(script: HTMLScriptElement, baseURI: string, opts: TransformerOpts):
|
|
13
|
+
export default function transpileScript(script: HTMLScriptElement, baseURI: string, opts: TransformerOpts): HTMLScriptElement;
|
|
@@ -24,38 +24,49 @@ function transpileScript(script, baseURI, opts) {
|
|
|
24
24
|
if (srcAttribute) {
|
|
25
25
|
script.removeAttribute('src');
|
|
26
26
|
var scriptSrc = (0, _utils.getEntireUrl)(srcAttribute, baseURI);
|
|
27
|
+
|
|
28
|
+
// try to resolve the script from module resolver
|
|
27
29
|
var matchedScript = moduleResolver === null || moduleResolver === void 0 ? void 0 : moduleResolver(srcAttribute);
|
|
28
30
|
if (matchedScript) {
|
|
29
31
|
var _url = matchedScript.url,
|
|
30
32
|
version = matchedScript.version;
|
|
31
33
|
script.dataset.src = scriptSrc;
|
|
32
34
|
script.dataset.version = version;
|
|
35
|
+
// When the script hits the dependency reuse logic, the current script is not executed, and an empty script is returned directly
|
|
33
36
|
script.src = URL.createObjectURL(new Blob(["// ".concat(srcAttribute, " has reused the execution result of ").concat(_url)], {
|
|
34
37
|
type: 'application/javascript'
|
|
35
38
|
}));
|
|
36
|
-
|
|
37
|
-
script.dataset.src = scriptSrc;
|
|
38
|
-
var _fetch = opts.fetch;
|
|
39
|
-
void _fetch(scriptSrc).then(function (res) {
|
|
40
|
-
return res.text();
|
|
41
|
-
}).then(function (code) {
|
|
42
|
-
var codeFactory = sandbox.makeEvaluateFactory(code, scriptSrc);
|
|
43
|
-
script.src = URL.createObjectURL(new Blob([codeFactory], {
|
|
44
|
-
type: 'application/javascript'
|
|
45
|
-
}));
|
|
46
|
-
});
|
|
39
|
+
return script;
|
|
47
40
|
}
|
|
48
|
-
|
|
49
|
-
var
|
|
41
|
+
script.dataset.src = scriptSrc;
|
|
42
|
+
var _fetch = opts.fetch;
|
|
43
|
+
void _fetch(scriptSrc).then(function (res) {
|
|
44
|
+
return res.text();
|
|
45
|
+
}).then(function (code) {
|
|
46
|
+
var codeFactory = sandbox.makeEvaluateFactory(code, scriptSrc);
|
|
47
|
+
script.src = URL.createObjectURL(new Blob([codeFactory], {
|
|
48
|
+
type: 'application/javascript'
|
|
49
|
+
}));
|
|
50
|
+
});
|
|
51
|
+
return script;
|
|
52
|
+
}
|
|
53
|
+
if (isValidJavaScriptType(script.type)) {
|
|
54
|
+
var rawNode = opts.rawNode;
|
|
55
|
+
var scriptNode = script.textContent ? script : rawNode.childNodes[0];
|
|
56
|
+
var code = scriptNode.textContent;
|
|
50
57
|
if (code) {
|
|
51
|
-
|
|
58
|
+
scriptNode.textContent = sandbox.makeEvaluateFactory(code, baseURI);
|
|
59
|
+
// mark the script have consumed
|
|
60
|
+
script.dataset.consumed = 'true';
|
|
61
|
+
return script;
|
|
52
62
|
}
|
|
53
63
|
}
|
|
54
|
-
} else {
|
|
55
|
-
if (srcAttribute) {
|
|
56
|
-
script.src = (0, _utils.getEntireUrl)(srcAttribute, baseURI);
|
|
57
|
-
}
|
|
58
64
|
}
|
|
65
|
+
if (srcAttribute) {
|
|
66
|
+
script.src = (0, _utils.getEntireUrl)(srcAttribute, baseURI);
|
|
67
|
+
return script;
|
|
68
|
+
}
|
|
69
|
+
return script;
|
|
59
70
|
|
|
60
71
|
// TODO find entry exports
|
|
61
72
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_utils","require","isValidJavaScriptType","type","handleTypes","indexOf","transpileScript","script","baseURI","opts","srcAttribute","getAttribute","sandbox","moduleResolver","removeAttribute","scriptSrc","getEntireUrl","matchedScript","url","version","dataset","src","URL","createObjectURL","Blob","concat","fetch","then","res","text","code","codeFactory","makeEvaluateFactory","textContent"],"sources":["../../../src/assets-transpilers/script.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-16\n */\n\nimport type { Sandbox } from '@qiankunjs/sandbox';\nimport type { MatchResult } from '../common';\nimport { getEntireUrl } from '../utils';\n\nexport type TransformerOpts = {\n fetch: typeof window.fetch;\n sandbox?: Sandbox;\n moduleResolver?: (url: string) => MatchResult | undefined;\n};\n\nconst isValidJavaScriptType = (type?: string): boolean => {\n const handleTypes = [\n 'text/javascript',\n 'module',\n 'application/javascript',\n 'text/ecmascript',\n 'application/ecmascript',\n ];\n return !type || handleTypes.indexOf(type) !== -1;\n};\n\nexport default function transpileScript(script: HTMLScriptElement
|
|
1
|
+
{"version":3,"names":["_utils","require","isValidJavaScriptType","type","handleTypes","indexOf","transpileScript","script","baseURI","opts","srcAttribute","getAttribute","sandbox","moduleResolver","removeAttribute","scriptSrc","getEntireUrl","matchedScript","url","version","dataset","src","URL","createObjectURL","Blob","concat","fetch","then","res","text","code","codeFactory","makeEvaluateFactory","rawNode","scriptNode","textContent","childNodes","consumed"],"sources":["../../../src/assets-transpilers/script.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-16\n */\n\nimport type { Sandbox } from '@qiankunjs/sandbox';\nimport type { MatchResult } from '../common';\nimport { getEntireUrl } from '../utils';\n\nexport type TransformerOpts = {\n fetch: typeof window.fetch;\n sandbox?: Sandbox;\n moduleResolver?: (url: string) => MatchResult | undefined;\n rawNode: Node;\n};\n\nconst isValidJavaScriptType = (type?: string): boolean => {\n const handleTypes = [\n 'text/javascript',\n 'module',\n 'application/javascript',\n 'text/ecmascript',\n 'application/ecmascript',\n ];\n return !type || handleTypes.indexOf(type) !== -1;\n};\n\nexport default function transpileScript(\n script: HTMLScriptElement,\n baseURI: string,\n opts: TransformerOpts,\n): HTMLScriptElement {\n // Can't use script.src directly, because it will be resolved to absolute path by browser with Node.baseURI\n // Such as <script src=\"./foo.js\"></script> will be resolved to http://localhost:8000/foo.js while read script.src\n const srcAttribute = script.getAttribute('src');\n const { sandbox, moduleResolver } = opts;\n\n if (sandbox) {\n if (srcAttribute) {\n script.removeAttribute('src');\n\n const scriptSrc = getEntireUrl(srcAttribute, baseURI);\n\n // try to resolve the script from module resolver\n const matchedScript = moduleResolver?.(srcAttribute);\n if (matchedScript) {\n const { url, version } = matchedScript;\n script.dataset.src = scriptSrc;\n script.dataset.version = version;\n // When the script hits the dependency reuse logic, the current script is not executed, and an empty script is returned directly\n script.src = URL.createObjectURL(\n new Blob([`// ${srcAttribute} has reused the execution result of ${url}`], {\n type: 'application/javascript',\n }),\n );\n return script;\n }\n\n script.dataset.src = scriptSrc;\n\n const { fetch } = opts;\n void fetch(scriptSrc)\n .then((res) => res.text())\n .then((code) => {\n const codeFactory = sandbox.makeEvaluateFactory(code, scriptSrc);\n script.src = URL.createObjectURL(new Blob([codeFactory], { type: 'application/javascript' }));\n });\n return script;\n }\n\n if (isValidJavaScriptType(script.type)) {\n const rawNode = opts.rawNode as HTMLScriptElement;\n const scriptNode = script.textContent ? script : rawNode.childNodes[0];\n const code = scriptNode.textContent;\n if (code) {\n scriptNode.textContent = sandbox.makeEvaluateFactory(code, baseURI);\n // mark the script have consumed\n script.dataset.consumed = 'true';\n return script;\n }\n }\n }\n\n if (srcAttribute) {\n script.src = getEntireUrl(srcAttribute, baseURI);\n return script;\n }\n\n return script;\n\n // TODO find entry exports\n}\n"],"mappings":"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"}
|
|
@@ -5,22 +5,32 @@
|
|
|
5
5
|
import transpileLink from "./link";
|
|
6
6
|
import transpileScript from "./script";
|
|
7
7
|
export function transpileAssets(node, baseURI, opts) {
|
|
8
|
-
var
|
|
9
|
-
|
|
8
|
+
var htmlElement = node;
|
|
9
|
+
var tagName = htmlElement.tagName;
|
|
10
10
|
switch (tagName) {
|
|
11
11
|
case 'SCRIPT':
|
|
12
12
|
{
|
|
13
|
-
transpileScript(
|
|
14
|
-
break;
|
|
13
|
+
return transpileScript(htmlElement, baseURI, opts);
|
|
15
14
|
}
|
|
16
15
|
case 'LINK':
|
|
17
16
|
{
|
|
18
|
-
transpileLink(
|
|
19
|
-
break;
|
|
17
|
+
return transpileLink(htmlElement, baseURI, opts);
|
|
20
18
|
}
|
|
21
19
|
default:
|
|
22
20
|
break;
|
|
23
21
|
}
|
|
22
|
+
|
|
23
|
+
// TODO need to deal with the text scenario of script
|
|
24
|
+
// const { nodeType } = node;
|
|
25
|
+
// switch (nodeType) {
|
|
26
|
+
// case Node.TEXT_NODE: {
|
|
27
|
+
// if (node.parentNode?.nodeName === 'script') {
|
|
28
|
+
// break;
|
|
29
|
+
// }
|
|
30
|
+
// }
|
|
31
|
+
//
|
|
32
|
+
// }
|
|
33
|
+
// }
|
|
24
34
|
return node;
|
|
25
35
|
}
|
|
26
36
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["transpileLink","transpileScript","transpileAssets","node","baseURI","opts","
|
|
1
|
+
{"version":3,"names":["transpileLink","transpileScript","transpileAssets","node","baseURI","opts","htmlElement","tagName"],"sources":["../../../src/assets-transpilers/index.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-14\n */\nimport transpileLink from './link';\nimport type { TransformerOpts } from './script';\nimport transpileScript from './script';\n\nexport function transpileAssets<T extends Node>(node: T, baseURI: string, opts: TransformerOpts): T {\n const htmlElement = (node as unknown) as HTMLElement;\n const { tagName } = htmlElement;\n\n switch (tagName) {\n case 'SCRIPT': {\n return (transpileScript(htmlElement as HTMLScriptElement, baseURI, opts) as unknown) as T;\n }\n\n case 'LINK': {\n return (transpileLink(htmlElement as HTMLLinkElement, baseURI, opts) as unknown) as T;\n }\n\n default:\n break;\n }\n\n // TODO need to deal with the text scenario of script\n // const { nodeType } = node;\n // switch (nodeType) {\n // case Node.TEXT_NODE: {\n // if (node.parentNode?.nodeName === 'script') {\n // break;\n // }\n // }\n //\n // }\n // }\n return node;\n}\n\nexport type { TransformerOpts };\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA,OAAOA,aAAa;AAEpB,OAAOC,eAAe;AAEtB,OAAO,SAASC,eAAeA,CAAiBC,IAAO,EAAEC,OAAe,EAAEC,IAAqB,EAAK;EAClG,IAAMC,WAAW,GAAIH,IAA+B;EACpD,IAAQI,OAAO,GAAKD,WAAW,CAAvBC,OAAO;EAEf,QAAQA,OAAO;IACb,KAAK,QAAQ;MAAE;QACb,OAAQN,eAAe,CAACK,WAAW,EAAuBF,OAAO,EAAEC,IAAI,CAAC;MAC1E;IAEA,KAAK,MAAM;MAAE;QACX,OAAQL,aAAa,CAACM,WAAW,EAAqBF,OAAO,EAAEC,IAAI,CAAC;MACtE;IAEA;MACE;EACJ;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,OAAOF,IAAI;AACb"}
|
|
@@ -17,9 +17,10 @@ export default function transpileLink(link, baseURI, opts) {
|
|
|
17
17
|
link.href = URL.createObjectURL(new Blob(["// ".concat(linkHref, " has reused the execution result of ").concat(url)], {
|
|
18
18
|
type: 'application/javascript'
|
|
19
19
|
}));
|
|
20
|
-
|
|
21
|
-
link.href = getEntireUrl(hrefAttribute, baseURI);
|
|
20
|
+
return link;
|
|
22
21
|
}
|
|
22
|
+
link.href = getEntireUrl(hrefAttribute, baseURI);
|
|
23
|
+
return link;
|
|
23
24
|
}
|
|
24
25
|
return link;
|
|
25
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getEntireUrl","transpileLink","link","baseURI","opts","moduleResolver","hrefAttribute","getAttribute","linkHref","matchedAssets","url","version","dataset","href","URL","createObjectURL","Blob","concat","type"],"sources":["../../../src/assets-transpilers/link.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-04-26\n */\nimport { getEntireUrl } from '../utils';\nimport type { TransformerOpts } from './script';\n\nexport default function transpileLink(link: HTMLLinkElement, baseURI: string, opts: TransformerOpts): HTMLLinkElement {\n const { moduleResolver } = opts;\n\n const hrefAttribute = link.getAttribute('href');\n if (hrefAttribute) {\n const linkHref = getEntireUrl(hrefAttribute, baseURI);\n\n const matchedAssets = moduleResolver?.(hrefAttribute);\n if (matchedAssets) {\n const { url, version } = matchedAssets;\n link.dataset.href = linkHref;\n link.dataset.version = version;\n link.href = URL.createObjectURL(\n new Blob([`// ${linkHref} has reused the execution result of ${url}`], {\n type: 'application/javascript',\n }),\n );\n }
|
|
1
|
+
{"version":3,"names":["getEntireUrl","transpileLink","link","baseURI","opts","moduleResolver","hrefAttribute","getAttribute","linkHref","matchedAssets","url","version","dataset","href","URL","createObjectURL","Blob","concat","type"],"sources":["../../../src/assets-transpilers/link.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-04-26\n */\nimport { getEntireUrl } from '../utils';\nimport type { TransformerOpts } from './script';\n\nexport default function transpileLink(link: HTMLLinkElement, baseURI: string, opts: TransformerOpts): HTMLLinkElement {\n const { moduleResolver } = opts;\n\n const hrefAttribute = link.getAttribute('href');\n if (hrefAttribute) {\n const linkHref = getEntireUrl(hrefAttribute, baseURI);\n\n const matchedAssets = moduleResolver?.(hrefAttribute);\n if (matchedAssets) {\n const { url, version } = matchedAssets;\n link.dataset.href = linkHref;\n link.dataset.version = version;\n link.href = URL.createObjectURL(\n new Blob([`// ${linkHref} has reused the execution result of ${url}`], {\n type: 'application/javascript',\n }),\n );\n return link;\n }\n\n link.href = getEntireUrl(hrefAttribute, baseURI);\n return link;\n }\n\n return link;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA,SAASA,YAAY;AAGrB,eAAe,SAASC,aAAaA,CAACC,IAAqB,EAAEC,OAAe,EAAEC,IAAqB,EAAmB;EACpH,IAAQC,cAAc,GAAKD,IAAI,CAAvBC,cAAc;EAEtB,IAAMC,aAAa,GAAGJ,IAAI,CAACK,YAAY,CAAC,MAAM,CAAC;EAC/C,IAAID,aAAa,EAAE;IACjB,IAAME,QAAQ,GAAGR,YAAY,CAACM,aAAa,EAAEH,OAAO,CAAC;IAErD,IAAMM,aAAa,GAAGJ,cAAc,aAAdA,cAAc,uBAAdA,cAAc,CAAGC,aAAa,CAAC;IACrD,IAAIG,aAAa,EAAE;MACjB,IAAQC,GAAG,GAAcD,aAAa,CAA9BC,GAAG;QAAEC,OAAO,GAAKF,aAAa,CAAzBE,OAAO;MACpBT,IAAI,CAACU,OAAO,CAACC,IAAI,GAAGL,QAAQ;MAC5BN,IAAI,CAACU,OAAO,CAACD,OAAO,GAAGA,OAAO;MAC9BT,IAAI,CAACW,IAAI,GAAGC,GAAG,CAACC,eAAe,CAC7B,IAAIC,IAAI,CAAC,OAAAC,MAAA,CAAOT,QAAQ,0CAAAS,MAAA,CAAuCP,GAAG,EAAG,EAAE;QACrEQ,IAAI,EAAE;MACR,CAAC,CACH,CAAC;MACD,OAAOhB,IAAI;IACb;IAEAA,IAAI,CAACW,IAAI,GAAGb,YAAY,CAACM,aAAa,EAAEH,OAAO,CAAC;IAChD,OAAOD,IAAI;EACb;EAEA,OAAOA,IAAI;AACb"}
|
|
@@ -8,5 +8,6 @@ export type TransformerOpts = {
|
|
|
8
8
|
fetch: typeof window.fetch;
|
|
9
9
|
sandbox?: Sandbox;
|
|
10
10
|
moduleResolver?: (url: string) => MatchResult | undefined;
|
|
11
|
+
rawNode: Node;
|
|
11
12
|
};
|
|
12
|
-
export default function transpileScript(script: HTMLScriptElement, baseURI: string, opts: TransformerOpts):
|
|
13
|
+
export default function transpileScript(script: HTMLScriptElement, baseURI: string, opts: TransformerOpts): HTMLScriptElement;
|
|
@@ -18,38 +18,49 @@ export default function transpileScript(script, baseURI, opts) {
|
|
|
18
18
|
if (srcAttribute) {
|
|
19
19
|
script.removeAttribute('src');
|
|
20
20
|
var scriptSrc = getEntireUrl(srcAttribute, baseURI);
|
|
21
|
+
|
|
22
|
+
// try to resolve the script from module resolver
|
|
21
23
|
var matchedScript = moduleResolver === null || moduleResolver === void 0 ? void 0 : moduleResolver(srcAttribute);
|
|
22
24
|
if (matchedScript) {
|
|
23
25
|
var _url = matchedScript.url,
|
|
24
26
|
version = matchedScript.version;
|
|
25
27
|
script.dataset.src = scriptSrc;
|
|
26
28
|
script.dataset.version = version;
|
|
29
|
+
// When the script hits the dependency reuse logic, the current script is not executed, and an empty script is returned directly
|
|
27
30
|
script.src = URL.createObjectURL(new Blob(["// ".concat(srcAttribute, " has reused the execution result of ").concat(_url)], {
|
|
28
31
|
type: 'application/javascript'
|
|
29
32
|
}));
|
|
30
|
-
|
|
31
|
-
script.dataset.src = scriptSrc;
|
|
32
|
-
var _fetch = opts.fetch;
|
|
33
|
-
void _fetch(scriptSrc).then(function (res) {
|
|
34
|
-
return res.text();
|
|
35
|
-
}).then(function (code) {
|
|
36
|
-
var codeFactory = sandbox.makeEvaluateFactory(code, scriptSrc);
|
|
37
|
-
script.src = URL.createObjectURL(new Blob([codeFactory], {
|
|
38
|
-
type: 'application/javascript'
|
|
39
|
-
}));
|
|
40
|
-
});
|
|
33
|
+
return script;
|
|
41
34
|
}
|
|
42
|
-
|
|
43
|
-
var
|
|
35
|
+
script.dataset.src = scriptSrc;
|
|
36
|
+
var _fetch = opts.fetch;
|
|
37
|
+
void _fetch(scriptSrc).then(function (res) {
|
|
38
|
+
return res.text();
|
|
39
|
+
}).then(function (code) {
|
|
40
|
+
var codeFactory = sandbox.makeEvaluateFactory(code, scriptSrc);
|
|
41
|
+
script.src = URL.createObjectURL(new Blob([codeFactory], {
|
|
42
|
+
type: 'application/javascript'
|
|
43
|
+
}));
|
|
44
|
+
});
|
|
45
|
+
return script;
|
|
46
|
+
}
|
|
47
|
+
if (isValidJavaScriptType(script.type)) {
|
|
48
|
+
var rawNode = opts.rawNode;
|
|
49
|
+
var scriptNode = script.textContent ? script : rawNode.childNodes[0];
|
|
50
|
+
var code = scriptNode.textContent;
|
|
44
51
|
if (code) {
|
|
45
|
-
|
|
52
|
+
scriptNode.textContent = sandbox.makeEvaluateFactory(code, baseURI);
|
|
53
|
+
// mark the script have consumed
|
|
54
|
+
script.dataset.consumed = 'true';
|
|
55
|
+
return script;
|
|
46
56
|
}
|
|
47
57
|
}
|
|
48
|
-
} else {
|
|
49
|
-
if (srcAttribute) {
|
|
50
|
-
script.src = getEntireUrl(srcAttribute, baseURI);
|
|
51
|
-
}
|
|
52
58
|
}
|
|
59
|
+
if (srcAttribute) {
|
|
60
|
+
script.src = getEntireUrl(srcAttribute, baseURI);
|
|
61
|
+
return script;
|
|
62
|
+
}
|
|
63
|
+
return script;
|
|
53
64
|
|
|
54
65
|
// TODO find entry exports
|
|
55
66
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getEntireUrl","isValidJavaScriptType","type","handleTypes","indexOf","transpileScript","script","baseURI","opts","srcAttribute","getAttribute","sandbox","moduleResolver","removeAttribute","scriptSrc","matchedScript","url","version","dataset","src","URL","createObjectURL","Blob","concat","fetch","then","res","text","code","codeFactory","makeEvaluateFactory","textContent"],"sources":["../../../src/assets-transpilers/script.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-16\n */\n\nimport type { Sandbox } from '@qiankunjs/sandbox';\nimport type { MatchResult } from '../common';\nimport { getEntireUrl } from '../utils';\n\nexport type TransformerOpts = {\n fetch: typeof window.fetch;\n sandbox?: Sandbox;\n moduleResolver?: (url: string) => MatchResult | undefined;\n};\n\nconst isValidJavaScriptType = (type?: string): boolean => {\n const handleTypes = [\n 'text/javascript',\n 'module',\n 'application/javascript',\n 'text/ecmascript',\n 'application/ecmascript',\n ];\n return !type || handleTypes.indexOf(type) !== -1;\n};\n\nexport default function transpileScript(script: HTMLScriptElement
|
|
1
|
+
{"version":3,"names":["getEntireUrl","isValidJavaScriptType","type","handleTypes","indexOf","transpileScript","script","baseURI","opts","srcAttribute","getAttribute","sandbox","moduleResolver","removeAttribute","scriptSrc","matchedScript","url","version","dataset","src","URL","createObjectURL","Blob","concat","fetch","then","res","text","code","codeFactory","makeEvaluateFactory","rawNode","scriptNode","textContent","childNodes","consumed"],"sources":["../../../src/assets-transpilers/script.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-16\n */\n\nimport type { Sandbox } from '@qiankunjs/sandbox';\nimport type { MatchResult } from '../common';\nimport { getEntireUrl } from '../utils';\n\nexport type TransformerOpts = {\n fetch: typeof window.fetch;\n sandbox?: Sandbox;\n moduleResolver?: (url: string) => MatchResult | undefined;\n rawNode: Node;\n};\n\nconst isValidJavaScriptType = (type?: string): boolean => {\n const handleTypes = [\n 'text/javascript',\n 'module',\n 'application/javascript',\n 'text/ecmascript',\n 'application/ecmascript',\n ];\n return !type || handleTypes.indexOf(type) !== -1;\n};\n\nexport default function transpileScript(\n script: HTMLScriptElement,\n baseURI: string,\n opts: TransformerOpts,\n): HTMLScriptElement {\n // Can't use script.src directly, because it will be resolved to absolute path by browser with Node.baseURI\n // Such as <script src=\"./foo.js\"></script> will be resolved to http://localhost:8000/foo.js while read script.src\n const srcAttribute = script.getAttribute('src');\n const { sandbox, moduleResolver } = opts;\n\n if (sandbox) {\n if (srcAttribute) {\n script.removeAttribute('src');\n\n const scriptSrc = getEntireUrl(srcAttribute, baseURI);\n\n // try to resolve the script from module resolver\n const matchedScript = moduleResolver?.(srcAttribute);\n if (matchedScript) {\n const { url, version } = matchedScript;\n script.dataset.src = scriptSrc;\n script.dataset.version = version;\n // When the script hits the dependency reuse logic, the current script is not executed, and an empty script is returned directly\n script.src = URL.createObjectURL(\n new Blob([`// ${srcAttribute} has reused the execution result of ${url}`], {\n type: 'application/javascript',\n }),\n );\n return script;\n }\n\n script.dataset.src = scriptSrc;\n\n const { fetch } = opts;\n void fetch(scriptSrc)\n .then((res) => res.text())\n .then((code) => {\n const codeFactory = sandbox.makeEvaluateFactory(code, scriptSrc);\n script.src = URL.createObjectURL(new Blob([codeFactory], { type: 'application/javascript' }));\n });\n return script;\n }\n\n if (isValidJavaScriptType(script.type)) {\n const rawNode = opts.rawNode as HTMLScriptElement;\n const scriptNode = script.textContent ? script : rawNode.childNodes[0];\n const code = scriptNode.textContent;\n if (code) {\n scriptNode.textContent = sandbox.makeEvaluateFactory(code, baseURI);\n // mark the script have consumed\n script.dataset.consumed = 'true';\n return script;\n }\n }\n }\n\n if (srcAttribute) {\n script.src = getEntireUrl(srcAttribute, baseURI);\n return script;\n }\n\n return script;\n\n // TODO find entry exports\n}\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qiankunjs/shared",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"@babel/runtime": "^7.10.5",
|
|
13
13
|
"lodash": "^4.17.11",
|
|
14
14
|
"semver": "^7.5.3",
|
|
15
|
-
"@qiankunjs/sandbox": "^0.0.1-alpha.
|
|
15
|
+
"@qiankunjs/sandbox": "^0.0.1-alpha.3"
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"dist",
|
|
@@ -7,23 +7,33 @@ import type { TransformerOpts } from './script';
|
|
|
7
7
|
import transpileScript from './script';
|
|
8
8
|
|
|
9
9
|
export function transpileAssets<T extends Node>(node: T, baseURI: string, opts: TransformerOpts): T {
|
|
10
|
-
const
|
|
10
|
+
const htmlElement = (node as unknown) as HTMLElement;
|
|
11
|
+
const { tagName } = htmlElement;
|
|
11
12
|
|
|
12
13
|
switch (tagName) {
|
|
13
14
|
case 'SCRIPT': {
|
|
14
|
-
transpileScript(
|
|
15
|
-
break;
|
|
15
|
+
return (transpileScript(htmlElement as HTMLScriptElement, baseURI, opts) as unknown) as T;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
case 'LINK': {
|
|
19
|
-
transpileLink(
|
|
20
|
-
break;
|
|
19
|
+
return (transpileLink(htmlElement as HTMLLinkElement, baseURI, opts) as unknown) as T;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
default:
|
|
24
23
|
break;
|
|
25
24
|
}
|
|
26
25
|
|
|
26
|
+
// TODO need to deal with the text scenario of script
|
|
27
|
+
// const { nodeType } = node;
|
|
28
|
+
// switch (nodeType) {
|
|
29
|
+
// case Node.TEXT_NODE: {
|
|
30
|
+
// if (node.parentNode?.nodeName === 'script') {
|
|
31
|
+
// break;
|
|
32
|
+
// }
|
|
33
|
+
// }
|
|
34
|
+
//
|
|
35
|
+
// }
|
|
36
|
+
// }
|
|
27
37
|
return node;
|
|
28
38
|
}
|
|
29
39
|
|
|
@@ -22,9 +22,11 @@ export default function transpileLink(link: HTMLLinkElement, baseURI: string, op
|
|
|
22
22
|
type: 'application/javascript',
|
|
23
23
|
}),
|
|
24
24
|
);
|
|
25
|
-
|
|
26
|
-
link.href = getEntireUrl(hrefAttribute, baseURI);
|
|
25
|
+
return link;
|
|
27
26
|
}
|
|
27
|
+
|
|
28
|
+
link.href = getEntireUrl(hrefAttribute, baseURI);
|
|
29
|
+
return link;
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
return link;
|
|
@@ -11,6 +11,7 @@ export type TransformerOpts = {
|
|
|
11
11
|
fetch: typeof window.fetch;
|
|
12
12
|
sandbox?: Sandbox;
|
|
13
13
|
moduleResolver?: (url: string) => MatchResult | undefined;
|
|
14
|
+
rawNode: Node;
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
const isValidJavaScriptType = (type?: string): boolean => {
|
|
@@ -24,49 +25,68 @@ const isValidJavaScriptType = (type?: string): boolean => {
|
|
|
24
25
|
return !type || handleTypes.indexOf(type) !== -1;
|
|
25
26
|
};
|
|
26
27
|
|
|
27
|
-
export default function transpileScript(
|
|
28
|
+
export default function transpileScript(
|
|
29
|
+
script: HTMLScriptElement,
|
|
30
|
+
baseURI: string,
|
|
31
|
+
opts: TransformerOpts,
|
|
32
|
+
): HTMLScriptElement {
|
|
28
33
|
// Can't use script.src directly, because it will be resolved to absolute path by browser with Node.baseURI
|
|
29
34
|
// Such as <script src="./foo.js"></script> will be resolved to http://localhost:8000/foo.js while read script.src
|
|
30
35
|
const srcAttribute = script.getAttribute('src');
|
|
31
36
|
const { sandbox, moduleResolver } = opts;
|
|
37
|
+
|
|
32
38
|
if (sandbox) {
|
|
33
39
|
if (srcAttribute) {
|
|
34
40
|
script.removeAttribute('src');
|
|
35
41
|
|
|
36
42
|
const scriptSrc = getEntireUrl(srcAttribute, baseURI);
|
|
37
43
|
|
|
44
|
+
// try to resolve the script from module resolver
|
|
38
45
|
const matchedScript = moduleResolver?.(srcAttribute);
|
|
39
46
|
if (matchedScript) {
|
|
40
47
|
const { url, version } = matchedScript;
|
|
41
48
|
script.dataset.src = scriptSrc;
|
|
42
49
|
script.dataset.version = version;
|
|
50
|
+
// When the script hits the dependency reuse logic, the current script is not executed, and an empty script is returned directly
|
|
43
51
|
script.src = URL.createObjectURL(
|
|
44
52
|
new Blob([`// ${srcAttribute} has reused the execution result of ${url}`], {
|
|
45
53
|
type: 'application/javascript',
|
|
46
54
|
}),
|
|
47
55
|
);
|
|
48
|
-
|
|
49
|
-
script.dataset.src = scriptSrc;
|
|
50
|
-
|
|
51
|
-
const { fetch } = opts;
|
|
52
|
-
void fetch(scriptSrc)
|
|
53
|
-
.then((res) => res.text())
|
|
54
|
-
.then((code) => {
|
|
55
|
-
const codeFactory = sandbox.makeEvaluateFactory(code, scriptSrc);
|
|
56
|
-
script.src = URL.createObjectURL(new Blob([codeFactory], { type: 'application/javascript' }));
|
|
57
|
-
});
|
|
56
|
+
return script;
|
|
58
57
|
}
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
|
|
59
|
+
script.dataset.src = scriptSrc;
|
|
60
|
+
|
|
61
|
+
const { fetch } = opts;
|
|
62
|
+
void fetch(scriptSrc)
|
|
63
|
+
.then((res) => res.text())
|
|
64
|
+
.then((code) => {
|
|
65
|
+
const codeFactory = sandbox.makeEvaluateFactory(code, scriptSrc);
|
|
66
|
+
script.src = URL.createObjectURL(new Blob([codeFactory], { type: 'application/javascript' }));
|
|
67
|
+
});
|
|
68
|
+
return script;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (isValidJavaScriptType(script.type)) {
|
|
72
|
+
const rawNode = opts.rawNode as HTMLScriptElement;
|
|
73
|
+
const scriptNode = script.textContent ? script : rawNode.childNodes[0];
|
|
74
|
+
const code = scriptNode.textContent;
|
|
61
75
|
if (code) {
|
|
62
|
-
|
|
76
|
+
scriptNode.textContent = sandbox.makeEvaluateFactory(code, baseURI);
|
|
77
|
+
// mark the script have consumed
|
|
78
|
+
script.dataset.consumed = 'true';
|
|
79
|
+
return script;
|
|
63
80
|
}
|
|
64
81
|
}
|
|
65
|
-
} else {
|
|
66
|
-
if (srcAttribute) {
|
|
67
|
-
script.src = getEntireUrl(srcAttribute, baseURI);
|
|
68
|
-
}
|
|
69
82
|
}
|
|
70
83
|
|
|
84
|
+
if (srcAttribute) {
|
|
85
|
+
script.src = getEntireUrl(srcAttribute, baseURI);
|
|
86
|
+
return script;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return script;
|
|
90
|
+
|
|
71
91
|
// TODO find entry exports
|
|
72
92
|
}
|