@qiankunjs/shared 0.0.1-rc.1 → 0.0.1-rc.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.map +1 -1
- package/dist/cjs/assets-transpilers/link.js +29 -46
- package/dist/cjs/assets-transpilers/link.js.map +1 -1
- package/dist/cjs/assets-transpilers/script.d.ts +3 -3
- package/dist/cjs/assets-transpilers/script.js +8 -9
- package/dist/cjs/assets-transpilers/script.js.map +1 -1
- package/dist/cjs/assets-transpilers/types.d.ts +3 -3
- package/dist/cjs/assets-transpilers/types.js +3 -3
- package/dist/cjs/assets-transpilers/types.js.map +1 -1
- package/dist/cjs/assets-transpilers/utils.d.ts +1 -0
- package/dist/cjs/assets-transpilers/utils.js +22 -0
- package/dist/cjs/assets-transpilers/utils.js.map +1 -0
- package/dist/esm/assets-transpilers/index.js.map +1 -1
- package/dist/esm/assets-transpilers/link.js +29 -46
- package/dist/esm/assets-transpilers/link.js.map +1 -1
- package/dist/esm/assets-transpilers/script.d.ts +3 -3
- package/dist/esm/assets-transpilers/script.js +8 -9
- package/dist/esm/assets-transpilers/script.js.map +1 -1
- package/dist/esm/assets-transpilers/types.d.ts +3 -3
- package/dist/esm/assets-transpilers/types.js +3 -3
- package/dist/esm/assets-transpilers/types.js.map +1 -1
- package/dist/esm/assets-transpilers/utils.d.ts +1 -0
- package/dist/esm/assets-transpilers/utils.js +13 -0
- package/dist/esm/assets-transpilers/utils.js.map +1 -0
- package/package.json +2 -2
- package/src/assets-transpilers/index.ts +3 -3
- package/src/assets-transpilers/link.ts +23 -43
- package/src/assets-transpilers/script.ts +11 -14
- package/src/assets-transpilers/types.ts +3 -3
- package/src/assets-transpilers/utils.ts +16 -0
|
@@ -1 +1 @@
|
|
|
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 transpileScript from './script';\nimport type { AssetsTranspilerOpts } from './types';\n\nexport function transpileAssets<T extends Node>(node: T, baseURI: string, opts: AssetsTranspilerOpts): T {\n const htmlElement =
|
|
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 transpileScript from './script';\nimport type { AssetsTranspilerOpts } from './types';\n\nexport function transpileAssets<T extends Node>(node: T, baseURI: string, opts: AssetsTranspilerOpts): 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 * from './types';\n"],"mappings":";;;;;;;AAIA,IAAAA,KAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,OAAA,GAAAF,sBAAA,CAAAC,OAAA;AALA;AACA;AACA;AACA;;AAKO,SAASE,eAAeA,CAAiBC,IAAO,EAAEC,OAAe,EAAEC,IAA0B,EAAK;EACvG,IAAMC,WAAW,GAAGH,IAA8B;EAClD,IAAQI,OAAO,GAAKD,WAAW,CAAvBC,OAAO;EAEf,QAAQA,OAAO;IACb,KAAK,QAAQ;MAAE;QACb,OAAO,IAAAC,eAAe,EAACF,WAAW,EAAuBF,OAAO,EAAEC,IAAI,CAAC;MACzE;IAEA,KAAK,MAAM;MAAE;QACX,OAAO,IAAAI,aAAa,EAACH,WAAW,EAAqBF,OAAO,EAAEC,IAAI,CAAC;MACrE;IAEA;MACE;EACJ;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,OAAOF,IAAI;AACb"}
|
|
@@ -9,22 +9,25 @@ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/obje
|
|
|
9
9
|
var _utils = require("../utils");
|
|
10
10
|
var _script = require("./script");
|
|
11
11
|
var _types = require("./types");
|
|
12
|
+
var _utils2 = require("./utils");
|
|
12
13
|
/**
|
|
13
14
|
* @author Kuitos
|
|
14
15
|
* @since 2023-04-26
|
|
15
16
|
*/
|
|
16
17
|
|
|
17
|
-
var
|
|
18
|
+
var preTranspileStyleSheetLink = function preTranspileStyleSheetLink(link, baseURI, opts) {
|
|
18
19
|
var sandbox = opts.sandbox,
|
|
19
20
|
moduleResolver = opts.moduleResolver;
|
|
20
|
-
var href = link.href
|
|
21
|
+
var href = link.href,
|
|
22
|
+
rel = link.rel;
|
|
21
23
|
if (sandbox) {
|
|
22
|
-
|
|
24
|
+
// filter preload links
|
|
25
|
+
if (href && rel === 'stylesheet') {
|
|
23
26
|
var linkHref = (0, _utils.getEntireUrl)(href, baseURI);
|
|
24
27
|
var matchedAssets = moduleResolver === null || moduleResolver === void 0 ? void 0 : moduleResolver(linkHref);
|
|
25
28
|
if (matchedAssets) {
|
|
26
29
|
return {
|
|
27
|
-
mode: _types.Mode.
|
|
30
|
+
mode: _types.Mode.REUSED_DEP_IN_SANDBOX,
|
|
28
31
|
result: (0, _objectSpread2.default)({
|
|
29
32
|
src: linkHref
|
|
30
33
|
}, matchedAssets)
|
|
@@ -36,26 +39,9 @@ var preTranspile = function preTranspile(link, baseURI, opts) {
|
|
|
36
39
|
mode: _types.Mode.NONE
|
|
37
40
|
};
|
|
38
41
|
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* While the assets are transpiling in sandbox, it means they will be evaluated with manual fetching,
|
|
42
|
-
* thus we need to set the attribute `as` to fetch instead of script or style to avoid preload cache missing.
|
|
43
|
-
* see https://stackoverflow.com/questions/52635660/can-link-rel-preload-be-made-to-work-with-fetch/63814972#63814972
|
|
44
|
-
*/
|
|
45
42
|
var postProcessPreloadLink = function postProcessPreloadLink(link, baseURI, opts) {
|
|
46
43
|
var as = link.as,
|
|
47
44
|
href = link.href;
|
|
48
|
-
var revokeAfterLoaded = function revokeAfterLoaded(objectURL, link) {
|
|
49
|
-
var revoke = function revoke() {
|
|
50
|
-
return URL.revokeObjectURL(objectURL);
|
|
51
|
-
};
|
|
52
|
-
link.addEventListener('load', revoke, {
|
|
53
|
-
once: true
|
|
54
|
-
});
|
|
55
|
-
link.addEventListener('error', revoke, {
|
|
56
|
-
once: true
|
|
57
|
-
});
|
|
58
|
-
};
|
|
59
45
|
switch (as) {
|
|
60
46
|
case 'script':
|
|
61
47
|
{
|
|
@@ -65,19 +51,20 @@ var postProcessPreloadLink = function postProcessPreloadLink(link, baseURI, opts
|
|
|
65
51
|
mode = _preTranspileScript.mode,
|
|
66
52
|
result = _preTranspileScript.result;
|
|
67
53
|
switch (mode) {
|
|
68
|
-
|
|
54
|
+
/**
|
|
55
|
+
* While the assets are transpiling in sandbox, it means they will be evaluated with manual fetching,
|
|
56
|
+
* thus we need to set the attribute `as` to fetch instead of script or style to avoid preload cache missing.
|
|
57
|
+
* see https://stackoverflow.com/questions/52635660/can-link-rel-preload-be-made-to-work-with-fetch/63814972#63814972
|
|
58
|
+
*/
|
|
59
|
+
case _types.Mode.REMOTE_ASSETS_IN_SANDBOX:
|
|
69
60
|
{
|
|
70
61
|
link.as = 'fetch';
|
|
71
62
|
break;
|
|
72
63
|
}
|
|
73
|
-
case _types.Mode.
|
|
64
|
+
case _types.Mode.REUSED_DEP_IN_SANDBOX:
|
|
74
65
|
{
|
|
75
66
|
var url = result.url;
|
|
76
|
-
|
|
77
|
-
type: 'text/javascript'
|
|
78
|
-
}));
|
|
79
|
-
link.href = objectURL;
|
|
80
|
-
revokeAfterLoaded(objectURL, link);
|
|
67
|
+
link.href = (0, _utils2.createReusingObjectUrl)(href, url, 'text/javascript');
|
|
81
68
|
break;
|
|
82
69
|
}
|
|
83
70
|
}
|
|
@@ -85,20 +72,17 @@ var postProcessPreloadLink = function postProcessPreloadLink(link, baseURI, opts
|
|
|
85
72
|
}
|
|
86
73
|
case 'style':
|
|
87
74
|
{
|
|
88
|
-
var
|
|
89
|
-
href: href
|
|
75
|
+
var _preTranspileStyleShe = preTranspileStyleSheetLink({
|
|
76
|
+
href: href,
|
|
77
|
+
rel: 'stylesheet'
|
|
90
78
|
}, baseURI, opts),
|
|
91
|
-
_mode =
|
|
92
|
-
_result =
|
|
79
|
+
_mode = _preTranspileStyleShe.mode,
|
|
80
|
+
_result = _preTranspileStyleShe.result;
|
|
93
81
|
switch (_mode) {
|
|
94
|
-
case _types.Mode.
|
|
82
|
+
case _types.Mode.REUSED_DEP_IN_SANDBOX:
|
|
95
83
|
{
|
|
96
84
|
var _url = _result.url;
|
|
97
|
-
|
|
98
|
-
type: 'text/css'
|
|
99
|
-
}));
|
|
100
|
-
link.href = _objectURL;
|
|
101
|
-
revokeAfterLoaded(_objectURL, link);
|
|
85
|
+
link.href = (0, _utils2.createReusingObjectUrl)(href, _url, 'text/css');
|
|
102
86
|
break;
|
|
103
87
|
}
|
|
104
88
|
}
|
|
@@ -110,22 +94,21 @@ var postProcessPreloadLink = function postProcessPreloadLink(link, baseURI, opts
|
|
|
110
94
|
};
|
|
111
95
|
function transpileLink(link, baseURI, opts) {
|
|
112
96
|
var hrefAttribute = link.getAttribute('href');
|
|
113
|
-
var
|
|
114
|
-
href: hrefAttribute || undefined
|
|
97
|
+
var _preTranspileStyleShe2 = preTranspileStyleSheetLink({
|
|
98
|
+
href: hrefAttribute || undefined,
|
|
99
|
+
rel: link.rel
|
|
115
100
|
}, baseURI, opts),
|
|
116
|
-
mode =
|
|
117
|
-
result =
|
|
101
|
+
mode = _preTranspileStyleShe2.mode,
|
|
102
|
+
result = _preTranspileStyleShe2.result;
|
|
118
103
|
switch (mode) {
|
|
119
|
-
case _types.Mode.
|
|
104
|
+
case _types.Mode.REUSED_DEP_IN_SANDBOX:
|
|
120
105
|
{
|
|
121
106
|
var src = result.src,
|
|
122
107
|
version = result.version,
|
|
123
108
|
url = result.url;
|
|
124
109
|
link.dataset.href = src;
|
|
125
110
|
link.dataset.version = version;
|
|
126
|
-
link.href =
|
|
127
|
-
type: 'text/css'
|
|
128
|
-
}));
|
|
111
|
+
link.href = (0, _utils2.createReusingObjectUrl)(src, url, 'text/css');
|
|
129
112
|
return link;
|
|
130
113
|
}
|
|
131
114
|
case _types.Mode.NONE:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_utils","require","_script","_types","
|
|
1
|
+
{"version":3,"names":["_utils","require","_script","_types","_utils2","preTranspileStyleSheetLink","link","baseURI","opts","sandbox","moduleResolver","href","rel","linkHref","getEntireUrl","matchedAssets","mode","Mode","REUSED_DEP_IN_SANDBOX","result","_objectSpread2","default","src","NONE","postProcessPreloadLink","as","_preTranspileScript","preTranspileScript","REMOTE_ASSETS_IN_SANDBOX","url","createReusingObjectUrl","_preTranspileStyleShe","transpileLink","hrefAttribute","getAttribute","_preTranspileStyleShe2","undefined","version","dataset"],"sources":["../../../src/assets-transpilers/link.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-04-26\n */\nimport type { MatchResult } from '../module-resolver';\nimport { getEntireUrl } from '../utils';\nimport { preTranspile as preTranspileScript } from './script';\nimport type { AssetsTranspilerOpts, BaseTranspilerOpts } from './types';\nimport { Mode } from './types';\nimport { createReusingObjectUrl } from './utils';\n\ntype PreTranspileResult =\n | { mode: Mode.REUSED_DEP_IN_SANDBOX; result: { src: string } & MatchResult }\n | { mode: Mode.NONE; result?: never };\nconst preTranspileStyleSheetLink = (\n link: Partial<Pick<HTMLLinkElement, 'href' | 'rel'>>,\n baseURI: string,\n opts: BaseTranspilerOpts,\n): PreTranspileResult => {\n const { sandbox, moduleResolver } = opts;\n const { href, rel } = link;\n\n if (sandbox) {\n // filter preload links\n if (href && rel === 'stylesheet') {\n const linkHref = getEntireUrl(href, baseURI);\n\n const matchedAssets = moduleResolver?.(linkHref);\n if (matchedAssets) {\n return {\n mode: Mode.REUSED_DEP_IN_SANDBOX,\n result: { src: linkHref, ...matchedAssets },\n };\n }\n }\n }\n\n return {\n mode: Mode.NONE,\n };\n};\n\nconst postProcessPreloadLink = (link: HTMLLinkElement, baseURI: string, opts: AssetsTranspilerOpts): void => {\n const { as, href } = link;\n switch (as) {\n case 'script': {\n const { mode, result } = preTranspileScript({ src: href }, baseURI, opts);\n\n switch (mode) {\n /**\n * While the assets are transpiling in sandbox, it means they will be evaluated with manual fetching,\n * thus we need to set the attribute `as` to fetch instead of script or style to avoid preload cache missing.\n * see https://stackoverflow.com/questions/52635660/can-link-rel-preload-be-made-to-work-with-fetch/63814972#63814972\n */\n case Mode.REMOTE_ASSETS_IN_SANDBOX: {\n link.as = 'fetch';\n break;\n }\n\n case Mode.REUSED_DEP_IN_SANDBOX: {\n const { url } = result;\n link.href = createReusingObjectUrl(href, url, 'text/javascript');\n\n break;\n }\n }\n\n break;\n }\n\n case 'style': {\n const { mode, result } = preTranspileStyleSheetLink({ href, rel: 'stylesheet' }, baseURI, opts);\n\n switch (mode) {\n case Mode.REUSED_DEP_IN_SANDBOX: {\n const { url } = result;\n link.href = createReusingObjectUrl(href, url, 'text/css');\n break;\n }\n }\n\n break;\n }\n\n default:\n break;\n }\n};\n\nexport default function transpileLink(\n link: HTMLLinkElement,\n baseURI: string,\n opts: AssetsTranspilerOpts,\n): HTMLLinkElement {\n const hrefAttribute = link.getAttribute('href');\n const { mode, result } = preTranspileStyleSheetLink(\n {\n href: hrefAttribute || undefined,\n rel: link.rel,\n },\n baseURI,\n opts,\n );\n\n switch (mode) {\n case Mode.REUSED_DEP_IN_SANDBOX: {\n const { src, version, url } = result;\n link.dataset.href = src;\n link.dataset.version = version;\n link.href = createReusingObjectUrl(src, url, 'text/css');\n\n return link;\n }\n\n case Mode.NONE:\n default: {\n if (hrefAttribute) {\n link.href = getEntireUrl(hrefAttribute, baseURI);\n\n if (link.rel === 'preload') {\n postProcessPreloadLink(link, baseURI, opts);\n }\n\n return link;\n }\n\n return link;\n }\n }\n}\n"],"mappings":";;;;;;;;AAKA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AAEA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,OAAA,GAAAH,OAAA;AATA;AACA;AACA;AACA;;AAWA,IAAMI,0BAA0B,GAAG,SAA7BA,0BAA0BA,CAC9BC,IAAoD,EACpDC,OAAe,EACfC,IAAwB,EACD;EACvB,IAAQC,OAAO,GAAqBD,IAAI,CAAhCC,OAAO;IAAEC,cAAc,GAAKF,IAAI,CAAvBE,cAAc;EAC/B,IAAQC,IAAI,GAAUL,IAAI,CAAlBK,IAAI;IAAEC,GAAG,GAAKN,IAAI,CAAZM,GAAG;EAEjB,IAAIH,OAAO,EAAE;IACX;IACA,IAAIE,IAAI,IAAIC,GAAG,KAAK,YAAY,EAAE;MAChC,IAAMC,QAAQ,GAAG,IAAAC,mBAAY,EAACH,IAAI,EAAEJ,OAAO,CAAC;MAE5C,IAAMQ,aAAa,GAAGL,cAAc,aAAdA,cAAc,uBAAdA,cAAc,CAAGG,QAAQ,CAAC;MAChD,IAAIE,aAAa,EAAE;QACjB,OAAO;UACLC,IAAI,EAAEC,WAAI,CAACC,qBAAqB;UAChCC,MAAM,MAAAC,cAAA,CAAAC,OAAA;YAAIC,GAAG,EAAET;UAAQ,GAAKE,aAAa;QAC3C,CAAC;MACH;IACF;EACF;EAEA,OAAO;IACLC,IAAI,EAAEC,WAAI,CAACM;EACb,CAAC;AACH,CAAC;AAED,IAAMC,sBAAsB,GAAG,SAAzBA,sBAAsBA,CAAIlB,IAAqB,EAAEC,OAAe,EAAE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OAAe,EACfC,IAA0B,EACT;EACjB,IAAMyB,aAAa,GAAG3B,IAAI,CAAC4B,YAAY,CAAC,MAAM,CAAC;EAC/C,IAAAC,sBAAA,GAAyB9B,0BAA0B,CACjD;MACEM,IAAI,EAAEsB,aAAa,IAAIG,SAAS;MAChCxB,GAAG,EAAEN,IAAI,CAACM;IACZ,CAAC,EACDL,OAAO,EACPC,IACF,CAAC;IAPOQ,IAAI,GAAAmB,sBAAA,CAAJnB,IAAI;IAAEG,MAAM,GAAAgB,sBAAA,CAANhB,MAAM;EASpB,QAAQH,IAAI;IACV,KAAKC,WAAI,CAACC,qBAAqB;MAAE;QAC/B,IAAQI,GAAG,GAAmBH,MAAM,CAA5BG,GAAG;UAAEe,OAAO,GAAUlB,MAAM,CAAvBkB,OAAO;UAAER,GAAG,GAAKV,MAAM,CAAdU,GAAG;QACzBvB,IAAI,CAACgC,OAAO,CAAC3B,IAAI,GAAGW,GAAG;QACvBhB,IAAI,CAACgC,OAAO,CAACD,OAAO,GAAGA,OAAO;QAC9B/B,IAAI,CAACK,IAAI,GAAG,IAAAmB,8BAAsB,EAACR,GAAG,EAAEO,GAAG,EAAE,UAAU,CAAC;QAExD,OAAOvB,IAAI;MACb;IAEA,KAAKW,WAAI,CAACM,IAAI;IACd;MAAS;QACP,IAAIU,aAAa,EAAE;UACjB3B,IAAI,CAACK,IAAI,GAAG,IAAAG,mBAAY,EAACmB,aAAa,EAAE1B,OAAO,CAAC;UAEhD,IAAID,IAAI,CAACM,GAAG,KAAK,SAAS,EAAE;YAC1BY,sBAAsB,CAAClB,IAAI,EAAEC,OAAO,EAAEC,IAAI,CAAC;UAC7C;UAEA,OAAOF,IAAI;QACb;QAEA,OAAOA,IAAI;MACb;EACF;AACF"}
|
|
@@ -6,17 +6,17 @@ import type { MatchResult } from '../module-resolver';
|
|
|
6
6
|
import type { AssetsTranspilerOpts } from './types';
|
|
7
7
|
import { Mode } from './types';
|
|
8
8
|
type PreTranspileResult = {
|
|
9
|
-
mode: Mode.
|
|
9
|
+
mode: Mode.REMOTE_ASSETS_IN_SANDBOX;
|
|
10
10
|
result: {
|
|
11
11
|
src: string;
|
|
12
12
|
};
|
|
13
13
|
} | {
|
|
14
|
-
mode: Mode.
|
|
14
|
+
mode: Mode.REUSED_DEP_IN_SANDBOX;
|
|
15
15
|
result: {
|
|
16
16
|
src: string;
|
|
17
17
|
} & MatchResult;
|
|
18
18
|
} | {
|
|
19
|
-
mode: Mode.
|
|
19
|
+
mode: Mode.INLINE_CODE_IN_SANDBOX;
|
|
20
20
|
result: {
|
|
21
21
|
code: string;
|
|
22
22
|
};
|
|
@@ -9,6 +9,7 @@ exports.preTranspile = void 0;
|
|
|
9
9
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
10
10
|
var _utils = require("../utils");
|
|
11
11
|
var _types = require("./types");
|
|
12
|
+
var _utils2 = require("./utils");
|
|
12
13
|
/**
|
|
13
14
|
* @author Kuitos
|
|
14
15
|
* @since 2023-03-16
|
|
@@ -39,14 +40,14 @@ var preTranspile = function preTranspile(script, baseURI, opts) {
|
|
|
39
40
|
var matchedScript = moduleResolver === null || moduleResolver === void 0 ? void 0 : moduleResolver(entireUrl);
|
|
40
41
|
if (matchedScript) {
|
|
41
42
|
return {
|
|
42
|
-
mode: _types.Mode.
|
|
43
|
+
mode: _types.Mode.REUSED_DEP_IN_SANDBOX,
|
|
43
44
|
result: (0, _objectSpread2.default)({
|
|
44
45
|
src: entireUrl
|
|
45
46
|
}, matchedScript)
|
|
46
47
|
};
|
|
47
48
|
}
|
|
48
49
|
return {
|
|
49
|
-
mode: _types.Mode.
|
|
50
|
+
mode: _types.Mode.REMOTE_ASSETS_IN_SANDBOX,
|
|
50
51
|
result: {
|
|
51
52
|
src: entireUrl
|
|
52
53
|
}
|
|
@@ -58,7 +59,7 @@ var preTranspile = function preTranspile(script, baseURI, opts) {
|
|
|
58
59
|
var code = scriptNode.textContent;
|
|
59
60
|
if (code) {
|
|
60
61
|
return {
|
|
61
|
-
mode: _types.Mode.
|
|
62
|
+
mode: _types.Mode.INLINE_CODE_IN_SANDBOX,
|
|
62
63
|
result: {
|
|
63
64
|
code: code
|
|
64
65
|
}
|
|
@@ -85,7 +86,7 @@ function transpileScript(script, baseURI, opts) {
|
|
|
85
86
|
mode = _preTranspile.mode,
|
|
86
87
|
result = _preTranspile.result;
|
|
87
88
|
switch (mode) {
|
|
88
|
-
case _types.Mode.
|
|
89
|
+
case _types.Mode.REMOTE_ASSETS_IN_SANDBOX:
|
|
89
90
|
{
|
|
90
91
|
var src = result.src;
|
|
91
92
|
|
|
@@ -113,7 +114,7 @@ function transpileScript(script, baseURI, opts) {
|
|
|
113
114
|
});
|
|
114
115
|
return script;
|
|
115
116
|
}
|
|
116
|
-
case _types.Mode.
|
|
117
|
+
case _types.Mode.INLINE_CODE_IN_SANDBOX:
|
|
117
118
|
{
|
|
118
119
|
var rawNode = opts.rawNode;
|
|
119
120
|
var scriptNode = script.textContent ? script : rawNode.childNodes[0];
|
|
@@ -123,7 +124,7 @@ function transpileScript(script, baseURI, opts) {
|
|
|
123
124
|
script.dataset.consumed = 'true';
|
|
124
125
|
return script;
|
|
125
126
|
}
|
|
126
|
-
case _types.Mode.
|
|
127
|
+
case _types.Mode.REUSED_DEP_IN_SANDBOX:
|
|
127
128
|
{
|
|
128
129
|
var url = result.url,
|
|
129
130
|
version = result.version,
|
|
@@ -137,9 +138,7 @@ function transpileScript(script, baseURI, opts) {
|
|
|
137
138
|
}
|
|
138
139
|
|
|
139
140
|
// When the script hits the dependency reuse logic, the current script is not executed, and an empty script is returned directly
|
|
140
|
-
script.src =
|
|
141
|
-
type: 'text/javascript'
|
|
142
|
-
}));
|
|
141
|
+
script.src = (0, _utils2.createReusingObjectUrl)(_src, url, 'text/javascript');
|
|
143
142
|
return script;
|
|
144
143
|
}
|
|
145
144
|
case _types.Mode.NONE:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_utils","require","_types","isValidJavaScriptType","type","handleTypes","indexOf","getCredentials","crossOrigin","undefined","preTranspile","script","baseURI","opts","sandbox","moduleResolver","src","entireUrl","getEntireUrl","matchedScript","mode","Mode","CACHE_FROM_SANDBOX","result","_objectSpread2","default","REMOTE_FROM_SANDBOX","rawNode","scriptNode","textContent","childNodes","code","INLINE_FROM_SANDBOX","NONE","exports","transpileScript","srcAttribute","getAttribute","fetch","_preTranspile","removeAttribute","dataset","syncMode","hasAttribute","priority","credentials","then","res","text","codeFactory","makeEvaluateFactory","fetchPriority","URL","createObjectURL","Blob","consumed","url","version","concat"],"sources":["../../../src/assets-transpilers/script.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-16\n */\n\nimport type { MatchResult } from '../module-resolver';\nimport { getEntireUrl } from '../utils';\nimport type { AssetsTranspilerOpts } from './types';\nimport { Mode } from './types';\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\nconst getCredentials = (crossOrigin: string | null): RequestInit['credentials'] | undefined => {\n switch (crossOrigin) {\n case 'anonymous':\n return 'same-origin';\n case 'use-credentials':\n return 'include';\n default:\n return undefined;\n }\n};\n\ntype PreTranspileResult =\n | { mode: Mode.REMOTE_FROM_SANDBOX; result: { src: string } }\n | { mode: Mode.CACHE_FROM_SANDBOX; result: { src: string } & MatchResult }\n | { mode: Mode.INLINE_FROM_SANDBOX; result: { code: string } }\n | { mode: Mode.NONE; result?: never };\n\nexport const preTranspile = (\n script: Partial<Pick<HTMLScriptElement, 'src' | 'type' | 'textContent'>>,\n baseURI: string,\n opts: AssetsTranspilerOpts,\n): PreTranspileResult => {\n const { sandbox, moduleResolver } = opts;\n\n const { src, type } = script;\n\n if (sandbox) {\n if (src) {\n const entireUrl = getEntireUrl(src, baseURI);\n const matchedScript = moduleResolver?.(entireUrl);\n if (matchedScript) {\n return {\n mode: Mode.CACHE_FROM_SANDBOX,\n result: { src: entireUrl, ...matchedScript },\n };\n }\n\n return {\n mode: Mode.REMOTE_FROM_SANDBOX,\n result: { src: entireUrl },\n };\n }\n\n if (isValidJavaScriptType(type)) {\n const rawNode = opts.rawNode as HTMLScriptElement;\n const scriptNode = script.textContent ? script : rawNode.childNodes[0];\n\n const code = scriptNode.textContent;\n if (code) {\n return {\n mode: Mode.INLINE_FROM_SANDBOX,\n result: {\n code,\n },\n };\n }\n }\n }\n\n return { mode: Mode.NONE };\n};\n\nexport default function transpileScript(\n script: HTMLScriptElement,\n baseURI: string,\n opts: AssetsTranspilerOpts,\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, fetch } = opts;\n\n const { mode, result } = preTranspile(\n {\n src: srcAttribute || undefined,\n type: script.type,\n textContent: script.textContent,\n },\n baseURI,\n opts,\n );\n\n switch (mode) {\n case Mode.REMOTE_FROM_SANDBOX: {\n const { src } = result;\n\n // We must remove script src to avoid self execution as we need to fetch the script content and transpile it\n script.removeAttribute('src');\n script.dataset.src = src;\n\n const syncMode = !script.hasAttribute('async');\n const priority: Priority = syncMode ? 'high' : 'low';\n const credentials = getCredentials(script.crossOrigin);\n\n void fetch(src, { credentials, priority })\n .then((res) => res.text())\n .then((code) => {\n const codeFactory = sandbox!.makeEvaluateFactory(code, src);\n\n // HTMLScriptElement default fetchPriority is 'auto', we should set it to 'high' to make it execute earlier while it's not async script\n if (syncMode) {\n script.fetchPriority = 'high';\n }\n\n script.src = URL.createObjectURL(new Blob([codeFactory], { type: 'text/javascript' }));\n });\n\n return script;\n }\n\n case Mode.INLINE_FROM_SANDBOX: {\n const rawNode = opts.rawNode as HTMLScriptElement;\n const scriptNode = script.textContent ? script : rawNode.childNodes[0];\n const { code } = result;\n\n scriptNode.textContent = sandbox!.makeEvaluateFactory(code, baseURI);\n // mark the script have consumed\n script.dataset.consumed = 'true';\n\n return script;\n }\n\n case Mode.CACHE_FROM_SANDBOX: {\n const { url, version, src } = result;\n\n script.dataset.src = src;\n script.dataset.version = version;\n\n const syncMode = !script.getAttribute('async');\n // HTMLScriptElement default fetchPriority is 'auto', we should set it to 'high' to make it execute earlier while it's not async script\n if (syncMode) {\n script.fetchPriority = 'high';\n }\n\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([`// ${src} is reusing the execution result of ${url}`], {\n type: 'text/javascript',\n }),\n );\n\n return script;\n }\n\n case Mode.NONE:\n default: {\n if (srcAttribute) {\n script.src = getEntireUrl(srcAttribute, baseURI);\n return script;\n }\n\n return script;\n }\n }\n\n // TODO find entry exports\n}\n"],"mappings":";;;;;;;;;AAMA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AARA;AACA;AACA;AACA;;AAOA,IAAME,qBAAqB,GAAG,SAAxBA,qBAAqBA,CAAIC,IAAa,EAAc;EACxD,IAAMC,WAAW,GAAG,CAClB,iBAAiB,EACjB,QAAQ,EACR,wBAAwB,EACxB,iBAAiB,EACjB,wBAAwB,CACzB;EACD,OAAO,CAACD,IAAI,IAAIC,WAAW,CAACC,OAAO,CAACF,IAAI,CAAC,KAAK,CAAC,CAAC;AAClD,CAAC;AAED,IAAMG,cAAc,GAAG,SAAjBA,cAAcA,CAAIC,WAA0B,EAA6C;EAC7F,QAAQA,WAAW;IACjB,KAAK,WAAW;MACd,OAAO,aAAa;IACtB,KAAK,iBAAiB;MACpB,OAAO,SAAS;IAClB;MACE,OAAOC,SAAS;EACpB;AACF,CAAC;AAQM,IAAMC,YAAY,GAAG,SAAfA,YAAYA,CACvBC,MAAwE,EACxEC,OAAe,EACfC,IAA0B,EACH;EACvB,IAAQC,OAAO,GAAqBD,IAAI,CAAhCC,OAAO;IAAEC,cAAc,GAAKF,IA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AAKU,WAAI,CAACY,IAAI;IACd;MAAS;QACP,IAAIG,YAAY,EAAE;UAChBzB,MAAM,CAACK,GAAG,GAAG,IAAAE,mBAAY,EAACkB,YAAY,EAAExB,OAAO,CAAC;UAChD,OAAOD,MAAM;QACf;QAEA,OAAOA,MAAM;MACf;EACF;;EAEA;AACF"}
|
|
1
|
+
{"version":3,"names":["_utils","require","_types","_utils2","isValidJavaScriptType","type","handleTypes","indexOf","getCredentials","crossOrigin","undefined","preTranspile","script","baseURI","opts","sandbox","moduleResolver","src","entireUrl","getEntireUrl","matchedScript","mode","Mode","REUSED_DEP_IN_SANDBOX","result","_objectSpread2","default","REMOTE_ASSETS_IN_SANDBOX","rawNode","scriptNode","textContent","childNodes","code","INLINE_CODE_IN_SANDBOX","NONE","exports","transpileScript","srcAttribute","getAttribute","fetch","_preTranspile","removeAttribute","dataset","syncMode","hasAttribute","priority","credentials","then","res","text","codeFactory","makeEvaluateFactory","fetchPriority","URL","createObjectURL","Blob","consumed","url","version","createReusingObjectUrl"],"sources":["../../../src/assets-transpilers/script.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-16\n */\n\nimport type { MatchResult } from '../module-resolver';\nimport { getEntireUrl } from '../utils';\nimport type { AssetsTranspilerOpts } from './types';\nimport { Mode } from './types';\nimport { createReusingObjectUrl } from './utils';\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\nconst getCredentials = (crossOrigin: string | null): RequestInit['credentials'] | undefined => {\n switch (crossOrigin) {\n case 'anonymous':\n return 'same-origin';\n case 'use-credentials':\n return 'include';\n default:\n return undefined;\n }\n};\n\ntype PreTranspileResult =\n | { mode: Mode.REMOTE_ASSETS_IN_SANDBOX; result: { src: string } }\n | { mode: Mode.REUSED_DEP_IN_SANDBOX; result: { src: string } & MatchResult }\n | { mode: Mode.INLINE_CODE_IN_SANDBOX; result: { code: string } }\n | { mode: Mode.NONE; result?: never };\n\nexport const preTranspile = (\n script: Partial<Pick<HTMLScriptElement, 'src' | 'type' | 'textContent'>>,\n baseURI: string,\n opts: AssetsTranspilerOpts,\n): PreTranspileResult => {\n const { sandbox, moduleResolver } = opts;\n\n const { src, type } = script;\n\n if (sandbox) {\n if (src) {\n const entireUrl = getEntireUrl(src, baseURI);\n const matchedScript = moduleResolver?.(entireUrl);\n if (matchedScript) {\n return {\n mode: Mode.REUSED_DEP_IN_SANDBOX,\n result: { src: entireUrl, ...matchedScript },\n };\n }\n\n return {\n mode: Mode.REMOTE_ASSETS_IN_SANDBOX,\n result: { src: entireUrl },\n };\n }\n\n if (isValidJavaScriptType(type)) {\n const rawNode = opts.rawNode as HTMLScriptElement;\n const scriptNode = script.textContent ? script : rawNode.childNodes[0];\n\n const code = scriptNode.textContent;\n if (code) {\n return {\n mode: Mode.INLINE_CODE_IN_SANDBOX,\n result: {\n code,\n },\n };\n }\n }\n }\n\n return { mode: Mode.NONE };\n};\n\nexport default function transpileScript(\n script: HTMLScriptElement,\n baseURI: string,\n opts: AssetsTranspilerOpts,\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, fetch } = opts;\n\n const { mode, result } = preTranspile(\n {\n src: srcAttribute || undefined,\n type: script.type,\n textContent: script.textContent,\n },\n baseURI,\n opts,\n );\n\n switch (mode) {\n case Mode.REMOTE_ASSETS_IN_SANDBOX: {\n const { src } = result;\n\n // We must remove script src to avoid self execution as we need to fetch the script content and transpile it\n script.removeAttribute('src');\n script.dataset.src = src;\n\n const syncMode = !script.hasAttribute('async');\n const priority: Priority = syncMode ? 'high' : 'low';\n const credentials = getCredentials(script.crossOrigin);\n\n void fetch(src, { credentials, priority })\n .then((res) => res.text())\n .then((code) => {\n const codeFactory = sandbox!.makeEvaluateFactory(code, src);\n\n // HTMLScriptElement default fetchPriority is 'auto', we should set it to 'high' to make it execute earlier while it's not async script\n if (syncMode) {\n script.fetchPriority = 'high';\n }\n\n script.src = URL.createObjectURL(new Blob([codeFactory], { type: 'text/javascript' }));\n });\n\n return script;\n }\n\n case Mode.INLINE_CODE_IN_SANDBOX: {\n const rawNode = opts.rawNode as HTMLScriptElement;\n const scriptNode = script.textContent ? script : rawNode.childNodes[0];\n const { code } = result;\n\n scriptNode.textContent = sandbox!.makeEvaluateFactory(code, baseURI);\n // mark the script have consumed\n script.dataset.consumed = 'true';\n\n return script;\n }\n\n case Mode.REUSED_DEP_IN_SANDBOX: {\n const { url, version, src } = result;\n\n script.dataset.src = src;\n script.dataset.version = version;\n\n const syncMode = !script.getAttribute('async');\n // HTMLScriptElement default fetchPriority is 'auto', we should set it to 'high' to make it execute earlier while it's not async script\n if (syncMode) {\n script.fetchPriority = 'high';\n }\n\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 = createReusingObjectUrl(src, url, 'text/javascript');\n\n return script;\n }\n\n case Mode.NONE:\n default: {\n if (srcAttribute) {\n script.src = getEntireUrl(srcAttribute, baseURI);\n return script;\n }\n\n return script;\n }\n }\n\n // TODO find entry exports\n}\n"],"mappings":";;;;;;;;;AAMA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AATA;AACA;AACA;AACA;;AAQA,IAAMG,qBAAqB,GAAG,SAAxBA,qBAAqBA,CAAIC,IAAa,EAAc;EACxD,IAAMC,WAAW,GAAG,CAClB,iBAAiB,EACjB,QAAQ,EACR,wBAAwB,EACxB,iBAAiB,EACjB,wBAAwB,CACzB;EACD,OAAO,CAACD,IAAI,IAAIC,WAAW,CAACC,OAAO,CAACF,IAAI,CAAC,KAAK,CAAC,CAAC;AAClD,CAAC;AAED,IAAMG,cAAc,GAAG,SAA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OAAO,CAAC;QAC9C;QACA,IAAIK,SAAQ,EAAE;UACZ/B,MAAM,CAACwC,aAAa,GAAG,MAAM;QAC/B;;QAEA;QACAxC,MAAM,CAACK,GAAG,GAAG,IAAA0C,8BAAsB,EAAC1C,IAAG,EAAEwC,GAAG,EAAE,iBAAiB,CAAC;QAEhE,OAAO7C,MAAM;MACf;IAEA,KAAKU,WAAI,CAACY,IAAI;IACd;MAAS;QACP,IAAIG,YAAY,EAAE;UAChBzB,MAAM,CAACK,GAAG,GAAG,IAAAE,mBAAY,EAACkB,YAAY,EAAExB,OAAO,CAAC;UAChD,OAAOD,MAAM;QACf;QAEA,OAAOA,MAAM;MACf;EACF;;EAEA;AACF"}
|
|
@@ -13,8 +13,8 @@ export type AssetsTranspilerOpts = BaseTranspilerOpts & {
|
|
|
13
13
|
rawNode: Node;
|
|
14
14
|
};
|
|
15
15
|
export declare enum Mode {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
REMOTE_ASSETS_IN_SANDBOX = "RAIS",
|
|
17
|
+
REUSED_DEP_IN_SANDBOX = "RDIS",
|
|
18
|
+
INLINE_CODE_IN_SANDBOX = "ICIS",
|
|
19
19
|
NONE = "NONE"
|
|
20
20
|
}
|
|
@@ -9,9 +9,9 @@ exports.Mode = void 0;
|
|
|
9
9
|
* @since 2023-08-26
|
|
10
10
|
*/
|
|
11
11
|
var Mode = /*#__PURE__*/function (Mode) {
|
|
12
|
-
Mode["
|
|
13
|
-
Mode["
|
|
14
|
-
Mode["
|
|
12
|
+
Mode["REMOTE_ASSETS_IN_SANDBOX"] = "RAIS";
|
|
13
|
+
Mode["REUSED_DEP_IN_SANDBOX"] = "RDIS";
|
|
14
|
+
Mode["INLINE_CODE_IN_SANDBOX"] = "ICIS";
|
|
15
15
|
Mode["NONE"] = "NONE";
|
|
16
16
|
return Mode;
|
|
17
17
|
}({});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Mode","exports"],"sources":["../../../src/assets-transpilers/types.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-08-26\n */\nimport type { Sandbox } from '@qiankunjs/sandbox';\nimport type { BaseLoaderOpts } from '../common';\n\nimport type { MatchResult } from '../module-resolver';\n\nexport type BaseTranspilerOpts = BaseLoaderOpts & {\n moduleResolver?: (url: string) => MatchResult | undefined;\n sandbox?: Sandbox;\n};\n\nexport type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node };\n\nexport enum Mode {\n
|
|
1
|
+
{"version":3,"names":["Mode","exports"],"sources":["../../../src/assets-transpilers/types.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-08-26\n */\nimport type { Sandbox } from '@qiankunjs/sandbox';\nimport type { BaseLoaderOpts } from '../common';\n\nimport type { MatchResult } from '../module-resolver';\n\nexport type BaseTranspilerOpts = BaseLoaderOpts & {\n moduleResolver?: (url: string) => MatchResult | undefined;\n sandbox?: Sandbox;\n};\n\nexport type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node };\n\nexport enum Mode {\n REMOTE_ASSETS_IN_SANDBOX = 'RAIS',\n REUSED_DEP_IN_SANDBOX = 'RDIS',\n INLINE_CODE_IN_SANDBOX = 'ICIS',\n NONE = 'NONE',\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AAHA,IAgBYA,IAAI,0BAAJA,IAAI;EAAJA,IAAI;EAAJA,IAAI;EAAJA,IAAI;EAAJA,IAAI;EAAA,OAAJA,IAAI;AAAA;AAAAC,OAAA,CAAAD,IAAA,GAAAA,IAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createReusingObjectUrl: ((src: string, url: string, type: 'text/javascript' | 'text/css') => string) & import("lodash").MemoizedFunction;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.createReusingObjectUrl = void 0;
|
|
8
|
+
var _memoize2 = _interopRequireDefault(require("lodash/memoize"));
|
|
9
|
+
/**
|
|
10
|
+
* @author Kuitos
|
|
11
|
+
* @since 2023-10-09
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
var createReusingObjectUrl = (0, _memoize2.default)(function (src, url, type) {
|
|
15
|
+
return URL.createObjectURL(new Blob(["/* ".concat(src, " is reusing the execution result of ").concat(url, " */")], {
|
|
16
|
+
type: type
|
|
17
|
+
}));
|
|
18
|
+
}, function (src, url, type) {
|
|
19
|
+
return "".concat(src, "#").concat(url, "#").concat(type);
|
|
20
|
+
});
|
|
21
|
+
exports.createReusingObjectUrl = createReusingObjectUrl;
|
|
22
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createReusingObjectUrl","_memoize2","default","src","url","type","URL","createObjectURL","Blob","concat","exports"],"sources":["../../../src/assets-transpilers/utils.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-10-09\n */\nimport { memoize } from 'lodash';\n\nexport const createReusingObjectUrl = memoize(\n (src: string, url: string, type: 'text/javascript' | 'text/css'): string => {\n return URL.createObjectURL(\n new Blob([`/* ${src} is reusing the execution result of ${url} */`], {\n type,\n }),\n );\n },\n (src, url, type) => `${src}#${url}#${type}`,\n);\n"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACA;;AAGO,IAAMA,sBAAsB,GAAG,IAAAC,SAAA,CAAAC,OAAA,EACpC,UAACC,GAAW,EAAEC,GAAW,EAAEC,IAAoC,EAAa;EAC1E,OAAOC,GAAG,CAACC,eAAe,CACxB,IAAIC,IAAI,CAAC,OAAAC,MAAA,CAAON,GAAG,0CAAAM,MAAA,CAAuCL,GAAG,SAAM,EAAE;IACnEC,IAAI,EAAJA;EACF,CAAC,CACH,CAAC;AACH,CAAC,EACD,UAACF,GAAG,EAAEC,GAAG,EAAEC,IAAI;EAAA,UAAAI,MAAA,CAAQN,GAAG,OAAAM,MAAA,CAAIL,GAAG,OAAAK,MAAA,CAAIJ,IAAI;AAAA,CAC3C,CAAC;AAACK,OAAA,CAAAV,sBAAA,GAAAA,sBAAA"}
|
|
@@ -1 +1 @@
|
|
|
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 transpileScript from './script';\nimport type { AssetsTranspilerOpts } from './types';\n\nexport function transpileAssets<T extends Node>(node: T, baseURI: string, opts: AssetsTranspilerOpts): T {\n const htmlElement =
|
|
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 transpileScript from './script';\nimport type { AssetsTranspilerOpts } from './types';\n\nexport function transpileAssets<T extends Node>(node: T, baseURI: string, opts: AssetsTranspilerOpts): 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 * from './types';\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA,OAAOA,aAAa;AACpB,OAAOC,eAAe;AAGtB,OAAO,SAASC,eAAeA,CAAiBC,IAAO,EAAEC,OAAe,EAAEC,IAA0B,EAAK;EACvG,IAAMC,WAAW,GAAGH,IAA8B;EAClD,IAAQI,OAAO,GAAKD,WAAW,CAAvBC,OAAO;EAEf,QAAQA,OAAO;IACb,KAAK,QAAQ;MAAE;QACb,OAAON,eAAe,CAACK,WAAW,EAAuBF,OAAO,EAAEC,IAAI,CAAC;MACzE;IAEA,KAAK,MAAM;MAAE;QACX,OAAOL,aAAa,CAACM,WAAW,EAAqBF,OAAO,EAAEC,IAAI,CAAC;MACrE;IAEA;MACE;EACJ;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,OAAOF,IAAI;AACb"}
|
|
@@ -7,17 +7,20 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
|
7
7
|
import { getEntireUrl } from "../utils";
|
|
8
8
|
import { preTranspile as preTranspileScript } from "./script";
|
|
9
9
|
import { Mode } from "./types";
|
|
10
|
-
|
|
10
|
+
import { createReusingObjectUrl } from "./utils";
|
|
11
|
+
var preTranspileStyleSheetLink = function preTranspileStyleSheetLink(link, baseURI, opts) {
|
|
11
12
|
var sandbox = opts.sandbox,
|
|
12
13
|
moduleResolver = opts.moduleResolver;
|
|
13
|
-
var href = link.href
|
|
14
|
+
var href = link.href,
|
|
15
|
+
rel = link.rel;
|
|
14
16
|
if (sandbox) {
|
|
15
|
-
|
|
17
|
+
// filter preload links
|
|
18
|
+
if (href && rel === 'stylesheet') {
|
|
16
19
|
var linkHref = getEntireUrl(href, baseURI);
|
|
17
20
|
var matchedAssets = moduleResolver === null || moduleResolver === void 0 ? void 0 : moduleResolver(linkHref);
|
|
18
21
|
if (matchedAssets) {
|
|
19
22
|
return {
|
|
20
|
-
mode: Mode.
|
|
23
|
+
mode: Mode.REUSED_DEP_IN_SANDBOX,
|
|
21
24
|
result: _objectSpread({
|
|
22
25
|
src: linkHref
|
|
23
26
|
}, matchedAssets)
|
|
@@ -29,26 +32,9 @@ var preTranspile = function preTranspile(link, baseURI, opts) {
|
|
|
29
32
|
mode: Mode.NONE
|
|
30
33
|
};
|
|
31
34
|
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* While the assets are transpiling in sandbox, it means they will be evaluated with manual fetching,
|
|
35
|
-
* thus we need to set the attribute `as` to fetch instead of script or style to avoid preload cache missing.
|
|
36
|
-
* see https://stackoverflow.com/questions/52635660/can-link-rel-preload-be-made-to-work-with-fetch/63814972#63814972
|
|
37
|
-
*/
|
|
38
35
|
var postProcessPreloadLink = function postProcessPreloadLink(link, baseURI, opts) {
|
|
39
36
|
var as = link.as,
|
|
40
37
|
href = link.href;
|
|
41
|
-
var revokeAfterLoaded = function revokeAfterLoaded(objectURL, link) {
|
|
42
|
-
var revoke = function revoke() {
|
|
43
|
-
return URL.revokeObjectURL(objectURL);
|
|
44
|
-
};
|
|
45
|
-
link.addEventListener('load', revoke, {
|
|
46
|
-
once: true
|
|
47
|
-
});
|
|
48
|
-
link.addEventListener('error', revoke, {
|
|
49
|
-
once: true
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
38
|
switch (as) {
|
|
53
39
|
case 'script':
|
|
54
40
|
{
|
|
@@ -58,19 +44,20 @@ var postProcessPreloadLink = function postProcessPreloadLink(link, baseURI, opts
|
|
|
58
44
|
mode = _preTranspileScript.mode,
|
|
59
45
|
result = _preTranspileScript.result;
|
|
60
46
|
switch (mode) {
|
|
61
|
-
|
|
47
|
+
/**
|
|
48
|
+
* While the assets are transpiling in sandbox, it means they will be evaluated with manual fetching,
|
|
49
|
+
* thus we need to set the attribute `as` to fetch instead of script or style to avoid preload cache missing.
|
|
50
|
+
* see https://stackoverflow.com/questions/52635660/can-link-rel-preload-be-made-to-work-with-fetch/63814972#63814972
|
|
51
|
+
*/
|
|
52
|
+
case Mode.REMOTE_ASSETS_IN_SANDBOX:
|
|
62
53
|
{
|
|
63
54
|
link.as = 'fetch';
|
|
64
55
|
break;
|
|
65
56
|
}
|
|
66
|
-
case Mode.
|
|
57
|
+
case Mode.REUSED_DEP_IN_SANDBOX:
|
|
67
58
|
{
|
|
68
59
|
var url = result.url;
|
|
69
|
-
|
|
70
|
-
type: 'text/javascript'
|
|
71
|
-
}));
|
|
72
|
-
link.href = objectURL;
|
|
73
|
-
revokeAfterLoaded(objectURL, link);
|
|
60
|
+
link.href = createReusingObjectUrl(href, url, 'text/javascript');
|
|
74
61
|
break;
|
|
75
62
|
}
|
|
76
63
|
}
|
|
@@ -78,20 +65,17 @@ var postProcessPreloadLink = function postProcessPreloadLink(link, baseURI, opts
|
|
|
78
65
|
}
|
|
79
66
|
case 'style':
|
|
80
67
|
{
|
|
81
|
-
var
|
|
82
|
-
href: href
|
|
68
|
+
var _preTranspileStyleShe = preTranspileStyleSheetLink({
|
|
69
|
+
href: href,
|
|
70
|
+
rel: 'stylesheet'
|
|
83
71
|
}, baseURI, opts),
|
|
84
|
-
_mode =
|
|
85
|
-
_result =
|
|
72
|
+
_mode = _preTranspileStyleShe.mode,
|
|
73
|
+
_result = _preTranspileStyleShe.result;
|
|
86
74
|
switch (_mode) {
|
|
87
|
-
case Mode.
|
|
75
|
+
case Mode.REUSED_DEP_IN_SANDBOX:
|
|
88
76
|
{
|
|
89
77
|
var _url = _result.url;
|
|
90
|
-
|
|
91
|
-
type: 'text/css'
|
|
92
|
-
}));
|
|
93
|
-
link.href = _objectURL;
|
|
94
|
-
revokeAfterLoaded(_objectURL, link);
|
|
78
|
+
link.href = createReusingObjectUrl(href, _url, 'text/css');
|
|
95
79
|
break;
|
|
96
80
|
}
|
|
97
81
|
}
|
|
@@ -103,22 +87,21 @@ var postProcessPreloadLink = function postProcessPreloadLink(link, baseURI, opts
|
|
|
103
87
|
};
|
|
104
88
|
export default function transpileLink(link, baseURI, opts) {
|
|
105
89
|
var hrefAttribute = link.getAttribute('href');
|
|
106
|
-
var
|
|
107
|
-
href: hrefAttribute || undefined
|
|
90
|
+
var _preTranspileStyleShe2 = preTranspileStyleSheetLink({
|
|
91
|
+
href: hrefAttribute || undefined,
|
|
92
|
+
rel: link.rel
|
|
108
93
|
}, baseURI, opts),
|
|
109
|
-
mode =
|
|
110
|
-
result =
|
|
94
|
+
mode = _preTranspileStyleShe2.mode,
|
|
95
|
+
result = _preTranspileStyleShe2.result;
|
|
111
96
|
switch (mode) {
|
|
112
|
-
case Mode.
|
|
97
|
+
case Mode.REUSED_DEP_IN_SANDBOX:
|
|
113
98
|
{
|
|
114
99
|
var src = result.src,
|
|
115
100
|
version = result.version,
|
|
116
101
|
url = result.url;
|
|
117
102
|
link.dataset.href = src;
|
|
118
103
|
link.dataset.version = version;
|
|
119
|
-
link.href =
|
|
120
|
-
type: 'text/css'
|
|
121
|
-
}));
|
|
104
|
+
link.href = createReusingObjectUrl(src, url, 'text/css');
|
|
122
105
|
return link;
|
|
123
106
|
}
|
|
124
107
|
case Mode.NONE:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getEntireUrl","preTranspile","preTranspileScript","Mode","link","baseURI","opts","sandbox","moduleResolver","href","linkHref","matchedAssets","mode","
|
|
1
|
+
{"version":3,"names":["getEntireUrl","preTranspile","preTranspileScript","Mode","createReusingObjectUrl","preTranspileStyleSheetLink","link","baseURI","opts","sandbox","moduleResolver","href","rel","linkHref","matchedAssets","mode","REUSED_DEP_IN_SANDBOX","result","_objectSpread","src","NONE","postProcessPreloadLink","as","_preTranspileScript","REMOTE_ASSETS_IN_SANDBOX","url","_preTranspileStyleShe","transpileLink","hrefAttribute","getAttribute","_preTranspileStyleShe2","undefined","version","dataset"],"sources":["../../../src/assets-transpilers/link.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-04-26\n */\nimport type { MatchResult } from '../module-resolver';\nimport { getEntireUrl } from '../utils';\nimport { preTranspile as preTranspileScript } from './script';\nimport type { AssetsTranspilerOpts, BaseTranspilerOpts } from './types';\nimport { Mode } from './types';\nimport { createReusingObjectUrl } from './utils';\n\ntype PreTranspileResult =\n | { mode: Mode.REUSED_DEP_IN_SANDBOX; result: { src: string } & MatchResult }\n | { mode: Mode.NONE; result?: never };\nconst preTranspileStyleSheetLink = (\n link: Partial<Pick<HTMLLinkElement, 'href' | 'rel'>>,\n baseURI: string,\n opts: BaseTranspilerOpts,\n): PreTranspileResult => {\n const { sandbox, moduleResolver } = opts;\n const { href, rel } = link;\n\n if (sandbox) {\n // filter preload links\n if (href && rel === 'stylesheet') {\n const linkHref = getEntireUrl(href, baseURI);\n\n const matchedAssets = moduleResolver?.(linkHref);\n if (matchedAssets) {\n return {\n mode: Mode.REUSED_DEP_IN_SANDBOX,\n result: { src: linkHref, ...matchedAssets },\n };\n }\n }\n }\n\n return {\n mode: Mode.NONE,\n };\n};\n\nconst postProcessPreloadLink = (link: HTMLLinkElement, baseURI: string, opts: AssetsTranspilerOpts): void => {\n const { as, href } = link;\n switch (as) {\n case 'script': {\n const { mode, result } = preTranspileScript({ src: href }, baseURI, opts);\n\n switch (mode) {\n /**\n * While the assets are transpiling in sandbox, it means they will be evaluated with manual fetching,\n * thus we need to set the attribute `as` to fetch instead of script or style to avoid preload cache missing.\n * see https://stackoverflow.com/questions/52635660/can-link-rel-preload-be-made-to-work-with-fetch/63814972#63814972\n */\n case Mode.REMOTE_ASSETS_IN_SANDBOX: {\n link.as = 'fetch';\n break;\n }\n\n case Mode.REUSED_DEP_IN_SANDBOX: {\n const { url } = result;\n link.href = createReusingObjectUrl(href, url, 'text/javascript');\n\n break;\n }\n }\n\n break;\n }\n\n case 'style': {\n const { mode, result } = preTranspileStyleSheetLink({ href, rel: 'stylesheet' }, baseURI, opts);\n\n switch (mode) {\n case Mode.REUSED_DEP_IN_SANDBOX: {\n const { url } = result;\n link.href = createReusingObjectUrl(href, url, 'text/css');\n break;\n }\n }\n\n break;\n }\n\n default:\n break;\n }\n};\n\nexport default function transpileLink(\n link: HTMLLinkElement,\n baseURI: string,\n opts: AssetsTranspilerOpts,\n): HTMLLinkElement {\n const hrefAttribute = link.getAttribute('href');\n const { mode, result } = preTranspileStyleSheetLink(\n {\n href: hrefAttribute || undefined,\n rel: link.rel,\n },\n baseURI,\n opts,\n );\n\n switch (mode) {\n case Mode.REUSED_DEP_IN_SANDBOX: {\n const { src, version, url } = result;\n link.dataset.href = src;\n link.dataset.version = version;\n link.href = createReusingObjectUrl(src, url, 'text/css');\n\n return link;\n }\n\n case Mode.NONE:\n default: {\n if (hrefAttribute) {\n link.href = getEntireUrl(hrefAttribute, baseURI);\n\n if (link.rel === 'preload') {\n postProcessPreloadLink(link, baseURI, opts);\n }\n\n return link;\n }\n\n return link;\n }\n }\n}\n"],"mappings":";AAAA;AACA;AACA;AACA;;AAEA,SAASA,YAAY;AACrB,SAASC,YAAY,IAAIC,kBAAkB;AAE3C,SAASC,IAAI;AACb,SAASC,sBAAsB;AAK/B,IAAMC,0BAA0B,GAAG,SAA7BA,0BAA0BA,CAC9BC,IAAoD,EACpDC,OAAe,EACfC,IAAwB,EACD;EACvB,IAAQC,OAAO,GAAqBD,IAAI,CAAhCC,OAAO;IAAEC,cAAc,GAAKF,IAAI,CAAvBE,cAAc;EAC/B,IAAQC,IAAI,GAAUL,IAAI,CAAlBK,IAAI;IAAEC,GAAG,GAAKN,IAAI,CAAZM,GAAG;EAEjB,IAAIH,OAAO,EAAE;IACX;IACA,IAAIE,IAAI,IAAIC,GAAG,KAAK,YAAY,EAAE;MAChC,IAAMC,QAAQ,GAAGb,YAAY,CAACW,IAAI,EAAEJ,OAAO,CAAC;MAE5C,IAAMO,aAAa,GAAGJ,cAAc,aAAdA,cAAc,uBAAdA,cAAc,CAAGG,QAAQ,CAAC;MAChD,IAAIC,aAAa,EAAE;QACjB,OAAO;UACLC,IAAI,EAAEZ,IAAI,CAACa,qBAAqB;UAChCC,MAAM,EAAAC,aAAA;YAAIC,GAAG,EAAEN;UAAQ,GAAKC,aAAa;QAC3C,CAAC;MACH;IACF;EACF;EAEA,OAAO;IACLC,IAAI,EAAEZ,IAAI,CAACiB;EACb,CAAC;AACH,CAAC;AAED,IAAMC,sBAAsB,GAAG,SAAzBA,sBAAsBA,CAAIf,IAAqB,E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OAAe,EACfC,IAA0B,EACT;EACjB,IAAMoB,aAAa,GAAGtB,IAAI,CAACuB,YAAY,CAAC,MAAM,CAAC;EAC/C,IAAAC,sBAAA,GAAyBzB,0BAA0B,CACjD;MACEM,IAAI,EAAEiB,aAAa,IAAIG,SAAS;MAChCnB,GAAG,EAAEN,IAAI,CAACM;IACZ,CAAC,EACDL,OAAO,EACPC,IACF,CAAC;IAPOO,IAAI,GAAAe,sBAAA,CAAJf,IAAI;IAAEE,MAAM,GAAAa,sBAAA,CAANb,MAAM;EASpB,QAAQF,IAAI;IACV,KAAKZ,IAAI,CAACa,qBAAqB;MAAE;QAC/B,IAAQG,GAAG,GAAmBF,MAAM,CAA5BE,GAAG;UAAEa,OAAO,GAAUf,MAAM,CAAvBe,OAAO;UAAEP,GAAG,GAAKR,MAAM,CAAdQ,GAAG;QACzBnB,IAAI,CAAC2B,OAAO,CAACtB,IAAI,GAAGQ,GAAG;QACvBb,IAAI,CAAC2B,OAAO,CAACD,OAAO,GAAGA,OAAO;QAC9B1B,IAAI,CAACK,IAAI,GAAGP,sBAAsB,CAACe,GAAG,EAAEM,GAAG,EAAE,UAAU,CAAC;QAExD,OAAOnB,IAAI;MACb;IAEA,KAAKH,IAAI,CAACiB,IAAI;IACd;MAAS;QACP,IAAIQ,aAAa,EAAE;UACjBtB,IAAI,CAACK,IAAI,GAAGX,YAAY,CAAC4B,aAAa,EAAErB,OAAO,CAAC;UAEhD,IAAID,IAAI,CAACM,GAAG,KAAK,SAAS,EAAE;YAC1BS,sBAAsB,CAACf,IAAI,EAAEC,OAAO,EAAEC,IAAI,CAAC;UAC7C;UAEA,OAAOF,IAAI;QACb;QAEA,OAAOA,IAAI;MACb;EACF;AACF"}
|
|
@@ -6,17 +6,17 @@ import type { MatchResult } from '../module-resolver';
|
|
|
6
6
|
import type { AssetsTranspilerOpts } from './types';
|
|
7
7
|
import { Mode } from './types';
|
|
8
8
|
type PreTranspileResult = {
|
|
9
|
-
mode: Mode.
|
|
9
|
+
mode: Mode.REMOTE_ASSETS_IN_SANDBOX;
|
|
10
10
|
result: {
|
|
11
11
|
src: string;
|
|
12
12
|
};
|
|
13
13
|
} | {
|
|
14
|
-
mode: Mode.
|
|
14
|
+
mode: Mode.REUSED_DEP_IN_SANDBOX;
|
|
15
15
|
result: {
|
|
16
16
|
src: string;
|
|
17
17
|
} & MatchResult;
|
|
18
18
|
} | {
|
|
19
|
-
mode: Mode.
|
|
19
|
+
mode: Mode.INLINE_CODE_IN_SANDBOX;
|
|
20
20
|
result: {
|
|
21
21
|
code: string;
|
|
22
22
|
};
|
|
@@ -6,6 +6,7 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
|
6
6
|
|
|
7
7
|
import { getEntireUrl } from "../utils";
|
|
8
8
|
import { Mode } from "./types";
|
|
9
|
+
import { createReusingObjectUrl } from "./utils";
|
|
9
10
|
var isValidJavaScriptType = function isValidJavaScriptType(type) {
|
|
10
11
|
var handleTypes = ['text/javascript', 'module', 'application/javascript', 'text/ecmascript', 'application/ecmascript'];
|
|
11
12
|
return !type || handleTypes.indexOf(type) !== -1;
|
|
@@ -31,14 +32,14 @@ export var preTranspile = function preTranspile(script, baseURI, opts) {
|
|
|
31
32
|
var matchedScript = moduleResolver === null || moduleResolver === void 0 ? void 0 : moduleResolver(entireUrl);
|
|
32
33
|
if (matchedScript) {
|
|
33
34
|
return {
|
|
34
|
-
mode: Mode.
|
|
35
|
+
mode: Mode.REUSED_DEP_IN_SANDBOX,
|
|
35
36
|
result: _objectSpread({
|
|
36
37
|
src: entireUrl
|
|
37
38
|
}, matchedScript)
|
|
38
39
|
};
|
|
39
40
|
}
|
|
40
41
|
return {
|
|
41
|
-
mode: Mode.
|
|
42
|
+
mode: Mode.REMOTE_ASSETS_IN_SANDBOX,
|
|
42
43
|
result: {
|
|
43
44
|
src: entireUrl
|
|
44
45
|
}
|
|
@@ -50,7 +51,7 @@ export var preTranspile = function preTranspile(script, baseURI, opts) {
|
|
|
50
51
|
var code = scriptNode.textContent;
|
|
51
52
|
if (code) {
|
|
52
53
|
return {
|
|
53
|
-
mode: Mode.
|
|
54
|
+
mode: Mode.INLINE_CODE_IN_SANDBOX,
|
|
54
55
|
result: {
|
|
55
56
|
code: code
|
|
56
57
|
}
|
|
@@ -76,7 +77,7 @@ export default function transpileScript(script, baseURI, opts) {
|
|
|
76
77
|
mode = _preTranspile.mode,
|
|
77
78
|
result = _preTranspile.result;
|
|
78
79
|
switch (mode) {
|
|
79
|
-
case Mode.
|
|
80
|
+
case Mode.REMOTE_ASSETS_IN_SANDBOX:
|
|
80
81
|
{
|
|
81
82
|
var src = result.src;
|
|
82
83
|
|
|
@@ -104,7 +105,7 @@ export default function transpileScript(script, baseURI, opts) {
|
|
|
104
105
|
});
|
|
105
106
|
return script;
|
|
106
107
|
}
|
|
107
|
-
case Mode.
|
|
108
|
+
case Mode.INLINE_CODE_IN_SANDBOX:
|
|
108
109
|
{
|
|
109
110
|
var rawNode = opts.rawNode;
|
|
110
111
|
var scriptNode = script.textContent ? script : rawNode.childNodes[0];
|
|
@@ -114,7 +115,7 @@ export default function transpileScript(script, baseURI, opts) {
|
|
|
114
115
|
script.dataset.consumed = 'true';
|
|
115
116
|
return script;
|
|
116
117
|
}
|
|
117
|
-
case Mode.
|
|
118
|
+
case Mode.REUSED_DEP_IN_SANDBOX:
|
|
118
119
|
{
|
|
119
120
|
var url = result.url,
|
|
120
121
|
version = result.version,
|
|
@@ -128,9 +129,7 @@ export default function transpileScript(script, baseURI, opts) {
|
|
|
128
129
|
}
|
|
129
130
|
|
|
130
131
|
// When the script hits the dependency reuse logic, the current script is not executed, and an empty script is returned directly
|
|
131
|
-
script.src =
|
|
132
|
-
type: 'text/javascript'
|
|
133
|
-
}));
|
|
132
|
+
script.src = createReusingObjectUrl(_src, url, 'text/javascript');
|
|
134
133
|
return script;
|
|
135
134
|
}
|
|
136
135
|
case Mode.NONE:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getEntireUrl","Mode","isValidJavaScriptType","type","handleTypes","indexOf","getCredentials","crossOrigin","undefined","preTranspile","script","baseURI","opts","sandbox","moduleResolver","src","entireUrl","matchedScript","mode","
|
|
1
|
+
{"version":3,"names":["getEntireUrl","Mode","createReusingObjectUrl","isValidJavaScriptType","type","handleTypes","indexOf","getCredentials","crossOrigin","undefined","preTranspile","script","baseURI","opts","sandbox","moduleResolver","src","entireUrl","matchedScript","mode","REUSED_DEP_IN_SANDBOX","result","_objectSpread","REMOTE_ASSETS_IN_SANDBOX","rawNode","scriptNode","textContent","childNodes","code","INLINE_CODE_IN_SANDBOX","NONE","transpileScript","srcAttribute","getAttribute","fetch","_preTranspile","removeAttribute","dataset","syncMode","hasAttribute","priority","credentials","then","res","text","codeFactory","makeEvaluateFactory","fetchPriority","URL","createObjectURL","Blob","consumed","url","version"],"sources":["../../../src/assets-transpilers/script.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-03-16\n */\n\nimport type { MatchResult } from '../module-resolver';\nimport { getEntireUrl } from '../utils';\nimport type { AssetsTranspilerOpts } from './types';\nimport { Mode } from './types';\nimport { createReusingObjectUrl } from './utils';\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\nconst getCredentials = (crossOrigin: string | null): RequestInit['credentials'] | undefined => {\n switch (crossOrigin) {\n case 'anonymous':\n return 'same-origin';\n case 'use-credentials':\n return 'include';\n default:\n return undefined;\n }\n};\n\ntype PreTranspileResult =\n | { mode: Mode.REMOTE_ASSETS_IN_SANDBOX; result: { src: string } }\n | { mode: Mode.REUSED_DEP_IN_SANDBOX; result: { src: string } & MatchResult }\n | { mode: Mode.INLINE_CODE_IN_SANDBOX; result: { code: string } }\n | { mode: Mode.NONE; result?: never };\n\nexport const preTranspile = (\n script: Partial<Pick<HTMLScriptElement, 'src' | 'type' | 'textContent'>>,\n baseURI: string,\n opts: AssetsTranspilerOpts,\n): PreTranspileResult => {\n const { sandbox, moduleResolver } = opts;\n\n const { src, type } = script;\n\n if (sandbox) {\n if (src) {\n const entireUrl = getEntireUrl(src, baseURI);\n const matchedScript = moduleResolver?.(entireUrl);\n if (matchedScript) {\n return {\n mode: Mode.REUSED_DEP_IN_SANDBOX,\n result: { src: entireUrl, ...matchedScript },\n };\n }\n\n return {\n mode: Mode.REMOTE_ASSETS_IN_SANDBOX,\n result: { src: entireUrl },\n };\n }\n\n if (isValidJavaScriptType(type)) {\n const rawNode = opts.rawNode as HTMLScriptElement;\n const scriptNode = script.textContent ? script : rawNode.childNodes[0];\n\n const code = scriptNode.textContent;\n if (code) {\n return {\n mode: Mode.INLINE_CODE_IN_SANDBOX,\n result: {\n code,\n },\n };\n }\n }\n }\n\n return { mode: Mode.NONE };\n};\n\nexport default function transpileScript(\n script: HTMLScriptElement,\n baseURI: string,\n opts: AssetsTranspilerOpts,\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, fetch } = opts;\n\n const { mode, result } = preTranspile(\n {\n src: srcAttribute || undefined,\n type: script.type,\n textContent: script.textContent,\n },\n baseURI,\n opts,\n );\n\n switch (mode) {\n case Mode.REMOTE_ASSETS_IN_SANDBOX: {\n const { src } = result;\n\n // We must remove script src to avoid self execution as we need to fetch the script content and transpile it\n script.removeAttribute('src');\n script.dataset.src = src;\n\n const syncMode = !script.hasAttribute('async');\n const priority: Priority = syncMode ? 'high' : 'low';\n const credentials = getCredentials(script.crossOrigin);\n\n void fetch(src, { credentials, priority })\n .then((res) => res.text())\n .then((code) => {\n const codeFactory = sandbox!.makeEvaluateFactory(code, src);\n\n // HTMLScriptElement default fetchPriority is 'auto', we should set it to 'high' to make it execute earlier while it's not async script\n if (syncMode) {\n script.fetchPriority = 'high';\n }\n\n script.src = URL.createObjectURL(new Blob([codeFactory], { type: 'text/javascript' }));\n });\n\n return script;\n }\n\n case Mode.INLINE_CODE_IN_SANDBOX: {\n const rawNode = opts.rawNode as HTMLScriptElement;\n const scriptNode = script.textContent ? script : rawNode.childNodes[0];\n const { code } = result;\n\n scriptNode.textContent = sandbox!.makeEvaluateFactory(code, baseURI);\n // mark the script have consumed\n script.dataset.consumed = 'true';\n\n return script;\n }\n\n case Mode.REUSED_DEP_IN_SANDBOX: {\n const { url, version, src } = result;\n\n script.dataset.src = src;\n script.dataset.version = version;\n\n const syncMode = !script.getAttribute('async');\n // HTMLScriptElement default fetchPriority is 'auto', we should set it to 'high' to make it execute earlier while it's not async script\n if (syncMode) {\n script.fetchPriority = 'high';\n }\n\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 = createReusingObjectUrl(src, url, 'text/javascript');\n\n return script;\n }\n\n case Mode.NONE:\n default: {\n if (srcAttribute) {\n script.src = getEntireUrl(srcAttribute, baseURI);\n return script;\n }\n\n return script;\n }\n }\n\n // TODO find entry exports\n}\n"],"mappings":";AAAA;AACA;AACA;AACA;;AAGA,SAASA,YAAY;AAErB,SAASC,IAAI;AACb,SAASC,sBAAsB;AAE/B,IAAMC,qBAAqB,GAAG,SAAxBA,qBAAqBA,CAAIC,IAAa,EAAc;EACxD,IAAMC,WAAW,GAAG,CAClB,iBAAiB,EACjB,QAAQ,EACR,wBAAwB,EACxB,iBAAiB,EACjB,wBAAwB,CACzB;EACD,OAAO,CAACD,IAAI,IAAIC,WAAW,CAACC,OAAO,CAACF,IAAI,CAAC,KAAK,CAAC,CAAC;AAClD,CAAC;AAED,IAAMG,cAAc,GAAG,SAAjBA,cAAcA,CAAIC,WAA0B,EAA6C;EAC7F,QAAQA,WAAW;IACjB,KAAK,WAAW;MACd,OAAO,aAAa;IACtB,KAAK,iBAAiB;MACpB,OAAO,SAAS;IAClB;MACE,OAAOC,SAAS;EACpB;AACF,C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AAKV,IAAI,CAAC6B,IAAI;IACd;MAAS;QACP,IAAIE,YAAY,EAAE;UAChBrB,MAAM,CAACK,GAAG,GAAGhB,YAAY,CAACgC,YAAY,EAAEpB,OAAO,CAAC;UAChD,OAAOD,MAAM;QACf;QAEA,OAAOA,MAAM;MACf;EACF;;EAEA;AACF"}
|
|
@@ -13,8 +13,8 @@ export type AssetsTranspilerOpts = BaseTranspilerOpts & {
|
|
|
13
13
|
rawNode: Node;
|
|
14
14
|
};
|
|
15
15
|
export declare enum Mode {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
REMOTE_ASSETS_IN_SANDBOX = "RAIS",
|
|
17
|
+
REUSED_DEP_IN_SANDBOX = "RDIS",
|
|
18
|
+
INLINE_CODE_IN_SANDBOX = "ICIS",
|
|
19
19
|
NONE = "NONE"
|
|
20
20
|
}
|
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
export var Mode = /*#__PURE__*/function (Mode) {
|
|
7
|
-
Mode["
|
|
8
|
-
Mode["
|
|
9
|
-
Mode["
|
|
7
|
+
Mode["REMOTE_ASSETS_IN_SANDBOX"] = "RAIS";
|
|
8
|
+
Mode["REUSED_DEP_IN_SANDBOX"] = "RDIS";
|
|
9
|
+
Mode["INLINE_CODE_IN_SANDBOX"] = "ICIS";
|
|
10
10
|
Mode["NONE"] = "NONE";
|
|
11
11
|
return Mode;
|
|
12
12
|
}({});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Mode"],"sources":["../../../src/assets-transpilers/types.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-08-26\n */\nimport type { Sandbox } from '@qiankunjs/sandbox';\nimport type { BaseLoaderOpts } from '../common';\n\nimport type { MatchResult } from '../module-resolver';\n\nexport type BaseTranspilerOpts = BaseLoaderOpts & {\n moduleResolver?: (url: string) => MatchResult | undefined;\n sandbox?: Sandbox;\n};\n\nexport type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node };\n\nexport enum Mode {\n
|
|
1
|
+
{"version":3,"names":["Mode"],"sources":["../../../src/assets-transpilers/types.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-08-26\n */\nimport type { Sandbox } from '@qiankunjs/sandbox';\nimport type { BaseLoaderOpts } from '../common';\n\nimport type { MatchResult } from '../module-resolver';\n\nexport type BaseTranspilerOpts = BaseLoaderOpts & {\n moduleResolver?: (url: string) => MatchResult | undefined;\n sandbox?: Sandbox;\n};\n\nexport type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node };\n\nexport enum Mode {\n REMOTE_ASSETS_IN_SANDBOX = 'RAIS',\n REUSED_DEP_IN_SANDBOX = 'RDIS',\n INLINE_CODE_IN_SANDBOX = 'ICIS',\n NONE = 'NONE',\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;;AAaA,WAAYA,IAAI,0BAAJA,IAAI;EAAJA,IAAI;EAAJA,IAAI;EAAJA,IAAI;EAAJA,IAAI;EAAA,OAAJA,IAAI;AAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createReusingObjectUrl: ((src: string, url: string, type: 'text/javascript' | 'text/css') => string) & import("lodash").MemoizedFunction;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import _memoize from "lodash/memoize";
|
|
2
|
+
/**
|
|
3
|
+
* @author Kuitos
|
|
4
|
+
* @since 2023-10-09
|
|
5
|
+
*/
|
|
6
|
+
export var createReusingObjectUrl = _memoize(function (src, url, type) {
|
|
7
|
+
return URL.createObjectURL(new Blob(["/* ".concat(src, " is reusing the execution result of ").concat(url, " */")], {
|
|
8
|
+
type: type
|
|
9
|
+
}));
|
|
10
|
+
}, function (src, url, type) {
|
|
11
|
+
return "".concat(src, "#").concat(url, "#").concat(type);
|
|
12
|
+
});
|
|
13
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createReusingObjectUrl","_memoize","src","url","type","URL","createObjectURL","Blob","concat"],"sources":["../../../src/assets-transpilers/utils.ts"],"sourcesContent":["/**\n * @author Kuitos\n * @since 2023-10-09\n */\nimport { memoize } from 'lodash';\n\nexport const createReusingObjectUrl = memoize(\n (src: string, url: string, type: 'text/javascript' | 'text/css'): string => {\n return URL.createObjectURL(\n new Blob([`/* ${src} is reusing the execution result of ${url} */`], {\n type,\n }),\n );\n },\n (src, url, type) => `${src}#${url}#${type}`,\n);\n"],"mappings":";AAAA;AACA;AACA;AACA;AAGA,OAAO,IAAMA,sBAAsB,GAAGC,QAAA,CACpC,UAACC,GAAW,EAAEC,GAAW,EAAEC,IAAoC,EAAa;EAC1E,OAAOC,GAAG,CAACC,eAAe,CACxB,IAAIC,IAAI,CAAC,OAAAC,MAAA,CAAON,GAAG,0CAAAM,MAAA,CAAuCL,GAAG,SAAM,EAAE;IACnEC,IAAI,EAAJA;EACF,CAAC,CACH,CAAC;AACH,CAAC,EACD,UAACF,GAAG,EAAEC,GAAG,EAAEC,IAAI;EAAA,UAAAI,MAAA,CAAQN,GAAG,OAAAM,MAAA,CAAIL,GAAG,OAAAK,MAAA,CAAIJ,IAAI;AAAA,CAC3C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qiankunjs/shared",
|
|
3
|
-
"version": "0.0.1-rc.
|
|
3
|
+
"version": "0.0.1-rc.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-rc.
|
|
15
|
+
"@qiankunjs/sandbox": "^0.0.1-rc.3"
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"dist",
|
|
@@ -7,16 +7,16 @@ import transpileScript from './script';
|
|
|
7
7
|
import type { AssetsTranspilerOpts } from './types';
|
|
8
8
|
|
|
9
9
|
export function transpileAssets<T extends Node>(node: T, baseURI: string, opts: AssetsTranspilerOpts): T {
|
|
10
|
-
const htmlElement =
|
|
10
|
+
const htmlElement = node as unknown as HTMLElement;
|
|
11
11
|
const { tagName } = htmlElement;
|
|
12
12
|
|
|
13
13
|
switch (tagName) {
|
|
14
14
|
case 'SCRIPT': {
|
|
15
|
-
return
|
|
15
|
+
return transpileScript(htmlElement as HTMLScriptElement, baseURI, opts) as unknown as T;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
case 'LINK': {
|
|
19
|
-
return
|
|
19
|
+
return transpileLink(htmlElement as HTMLLinkElement, baseURI, opts) as unknown as T;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
default:
|
|
@@ -7,26 +7,28 @@ import { getEntireUrl } from '../utils';
|
|
|
7
7
|
import { preTranspile as preTranspileScript } from './script';
|
|
8
8
|
import type { AssetsTranspilerOpts, BaseTranspilerOpts } from './types';
|
|
9
9
|
import { Mode } from './types';
|
|
10
|
+
import { createReusingObjectUrl } from './utils';
|
|
10
11
|
|
|
11
12
|
type PreTranspileResult =
|
|
12
|
-
| { mode: Mode.
|
|
13
|
+
| { mode: Mode.REUSED_DEP_IN_SANDBOX; result: { src: string } & MatchResult }
|
|
13
14
|
| { mode: Mode.NONE; result?: never };
|
|
14
|
-
const
|
|
15
|
-
link: Partial<Pick<HTMLLinkElement, 'href'>>,
|
|
15
|
+
const preTranspileStyleSheetLink = (
|
|
16
|
+
link: Partial<Pick<HTMLLinkElement, 'href' | 'rel'>>,
|
|
16
17
|
baseURI: string,
|
|
17
18
|
opts: BaseTranspilerOpts,
|
|
18
19
|
): PreTranspileResult => {
|
|
19
20
|
const { sandbox, moduleResolver } = opts;
|
|
20
|
-
const { href } = link;
|
|
21
|
+
const { href, rel } = link;
|
|
21
22
|
|
|
22
23
|
if (sandbox) {
|
|
23
|
-
|
|
24
|
+
// filter preload links
|
|
25
|
+
if (href && rel === 'stylesheet') {
|
|
24
26
|
const linkHref = getEntireUrl(href, baseURI);
|
|
25
27
|
|
|
26
28
|
const matchedAssets = moduleResolver?.(linkHref);
|
|
27
29
|
if (matchedAssets) {
|
|
28
30
|
return {
|
|
29
|
-
mode: Mode.
|
|
31
|
+
mode: Mode.REUSED_DEP_IN_SANDBOX,
|
|
30
32
|
result: { src: linkHref, ...matchedAssets },
|
|
31
33
|
};
|
|
32
34
|
}
|
|
@@ -38,39 +40,26 @@ const preTranspile = (
|
|
|
38
40
|
};
|
|
39
41
|
};
|
|
40
42
|
|
|
41
|
-
/**
|
|
42
|
-
* While the assets are transpiling in sandbox, it means they will be evaluated with manual fetching,
|
|
43
|
-
* thus we need to set the attribute `as` to fetch instead of script or style to avoid preload cache missing.
|
|
44
|
-
* see https://stackoverflow.com/questions/52635660/can-link-rel-preload-be-made-to-work-with-fetch/63814972#63814972
|
|
45
|
-
*/
|
|
46
43
|
const postProcessPreloadLink = (link: HTMLLinkElement, baseURI: string, opts: AssetsTranspilerOpts): void => {
|
|
47
44
|
const { as, href } = link;
|
|
48
|
-
|
|
49
|
-
const revokeAfterLoaded = (objectURL: string, link: HTMLLinkElement) => {
|
|
50
|
-
const revoke = () => URL.revokeObjectURL(objectURL);
|
|
51
|
-
link.addEventListener('load', revoke, { once: true });
|
|
52
|
-
link.addEventListener('error', revoke, { once: true });
|
|
53
|
-
};
|
|
54
|
-
|
|
55
45
|
switch (as) {
|
|
56
46
|
case 'script': {
|
|
57
47
|
const { mode, result } = preTranspileScript({ src: href }, baseURI, opts);
|
|
58
48
|
|
|
59
49
|
switch (mode) {
|
|
60
|
-
|
|
50
|
+
/**
|
|
51
|
+
* While the assets are transpiling in sandbox, it means they will be evaluated with manual fetching,
|
|
52
|
+
* thus we need to set the attribute `as` to fetch instead of script or style to avoid preload cache missing.
|
|
53
|
+
* see https://stackoverflow.com/questions/52635660/can-link-rel-preload-be-made-to-work-with-fetch/63814972#63814972
|
|
54
|
+
*/
|
|
55
|
+
case Mode.REMOTE_ASSETS_IN_SANDBOX: {
|
|
61
56
|
link.as = 'fetch';
|
|
62
57
|
break;
|
|
63
58
|
}
|
|
64
59
|
|
|
65
|
-
case Mode.
|
|
60
|
+
case Mode.REUSED_DEP_IN_SANDBOX: {
|
|
66
61
|
const { url } = result;
|
|
67
|
-
|
|
68
|
-
new Blob([`// ${href} is reusing the execution result of ${url}`], {
|
|
69
|
-
type: 'text/javascript',
|
|
70
|
-
}),
|
|
71
|
-
);
|
|
72
|
-
link.href = objectURL;
|
|
73
|
-
revokeAfterLoaded(objectURL, link);
|
|
62
|
+
link.href = createReusingObjectUrl(href, url, 'text/javascript');
|
|
74
63
|
|
|
75
64
|
break;
|
|
76
65
|
}
|
|
@@ -80,18 +69,12 @@ const postProcessPreloadLink = (link: HTMLLinkElement, baseURI: string, opts: As
|
|
|
80
69
|
}
|
|
81
70
|
|
|
82
71
|
case 'style': {
|
|
83
|
-
const { mode, result } =
|
|
72
|
+
const { mode, result } = preTranspileStyleSheetLink({ href, rel: 'stylesheet' }, baseURI, opts);
|
|
84
73
|
|
|
85
74
|
switch (mode) {
|
|
86
|
-
case Mode.
|
|
75
|
+
case Mode.REUSED_DEP_IN_SANDBOX: {
|
|
87
76
|
const { url } = result;
|
|
88
|
-
|
|
89
|
-
new Blob([`// ${href} is reusing the execution result of ${url}`], {
|
|
90
|
-
type: 'text/css',
|
|
91
|
-
}),
|
|
92
|
-
);
|
|
93
|
-
link.href = objectURL;
|
|
94
|
-
revokeAfterLoaded(objectURL, link);
|
|
77
|
+
link.href = createReusingObjectUrl(href, url, 'text/css');
|
|
95
78
|
break;
|
|
96
79
|
}
|
|
97
80
|
}
|
|
@@ -110,24 +93,21 @@ export default function transpileLink(
|
|
|
110
93
|
opts: AssetsTranspilerOpts,
|
|
111
94
|
): HTMLLinkElement {
|
|
112
95
|
const hrefAttribute = link.getAttribute('href');
|
|
113
|
-
const { mode, result } =
|
|
96
|
+
const { mode, result } = preTranspileStyleSheetLink(
|
|
114
97
|
{
|
|
115
98
|
href: hrefAttribute || undefined,
|
|
99
|
+
rel: link.rel,
|
|
116
100
|
},
|
|
117
101
|
baseURI,
|
|
118
102
|
opts,
|
|
119
103
|
);
|
|
120
104
|
|
|
121
105
|
switch (mode) {
|
|
122
|
-
case Mode.
|
|
106
|
+
case Mode.REUSED_DEP_IN_SANDBOX: {
|
|
123
107
|
const { src, version, url } = result;
|
|
124
108
|
link.dataset.href = src;
|
|
125
109
|
link.dataset.version = version;
|
|
126
|
-
link.href =
|
|
127
|
-
new Blob([`// ${src} is reusing the execution result of ${url}`], {
|
|
128
|
-
type: 'text/css',
|
|
129
|
-
}),
|
|
130
|
-
);
|
|
110
|
+
link.href = createReusingObjectUrl(src, url, 'text/css');
|
|
131
111
|
|
|
132
112
|
return link;
|
|
133
113
|
}
|
|
@@ -7,6 +7,7 @@ import type { MatchResult } from '../module-resolver';
|
|
|
7
7
|
import { getEntireUrl } from '../utils';
|
|
8
8
|
import type { AssetsTranspilerOpts } from './types';
|
|
9
9
|
import { Mode } from './types';
|
|
10
|
+
import { createReusingObjectUrl } from './utils';
|
|
10
11
|
|
|
11
12
|
const isValidJavaScriptType = (type?: string): boolean => {
|
|
12
13
|
const handleTypes = [
|
|
@@ -31,9 +32,9 @@ const getCredentials = (crossOrigin: string | null): RequestInit['credentials']
|
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
type PreTranspileResult =
|
|
34
|
-
| { mode: Mode.
|
|
35
|
-
| { mode: Mode.
|
|
36
|
-
| { mode: Mode.
|
|
35
|
+
| { mode: Mode.REMOTE_ASSETS_IN_SANDBOX; result: { src: string } }
|
|
36
|
+
| { mode: Mode.REUSED_DEP_IN_SANDBOX; result: { src: string } & MatchResult }
|
|
37
|
+
| { mode: Mode.INLINE_CODE_IN_SANDBOX; result: { code: string } }
|
|
37
38
|
| { mode: Mode.NONE; result?: never };
|
|
38
39
|
|
|
39
40
|
export const preTranspile = (
|
|
@@ -51,13 +52,13 @@ export const preTranspile = (
|
|
|
51
52
|
const matchedScript = moduleResolver?.(entireUrl);
|
|
52
53
|
if (matchedScript) {
|
|
53
54
|
return {
|
|
54
|
-
mode: Mode.
|
|
55
|
+
mode: Mode.REUSED_DEP_IN_SANDBOX,
|
|
55
56
|
result: { src: entireUrl, ...matchedScript },
|
|
56
57
|
};
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
return {
|
|
60
|
-
mode: Mode.
|
|
61
|
+
mode: Mode.REMOTE_ASSETS_IN_SANDBOX,
|
|
61
62
|
result: { src: entireUrl },
|
|
62
63
|
};
|
|
63
64
|
}
|
|
@@ -69,7 +70,7 @@ export const preTranspile = (
|
|
|
69
70
|
const code = scriptNode.textContent;
|
|
70
71
|
if (code) {
|
|
71
72
|
return {
|
|
72
|
-
mode: Mode.
|
|
73
|
+
mode: Mode.INLINE_CODE_IN_SANDBOX,
|
|
73
74
|
result: {
|
|
74
75
|
code,
|
|
75
76
|
},
|
|
@@ -102,7 +103,7 @@ export default function transpileScript(
|
|
|
102
103
|
);
|
|
103
104
|
|
|
104
105
|
switch (mode) {
|
|
105
|
-
case Mode.
|
|
106
|
+
case Mode.REMOTE_ASSETS_IN_SANDBOX: {
|
|
106
107
|
const { src } = result;
|
|
107
108
|
|
|
108
109
|
// We must remove script src to avoid self execution as we need to fetch the script content and transpile it
|
|
@@ -129,7 +130,7 @@ export default function transpileScript(
|
|
|
129
130
|
return script;
|
|
130
131
|
}
|
|
131
132
|
|
|
132
|
-
case Mode.
|
|
133
|
+
case Mode.INLINE_CODE_IN_SANDBOX: {
|
|
133
134
|
const rawNode = opts.rawNode as HTMLScriptElement;
|
|
134
135
|
const scriptNode = script.textContent ? script : rawNode.childNodes[0];
|
|
135
136
|
const { code } = result;
|
|
@@ -141,7 +142,7 @@ export default function transpileScript(
|
|
|
141
142
|
return script;
|
|
142
143
|
}
|
|
143
144
|
|
|
144
|
-
case Mode.
|
|
145
|
+
case Mode.REUSED_DEP_IN_SANDBOX: {
|
|
145
146
|
const { url, version, src } = result;
|
|
146
147
|
|
|
147
148
|
script.dataset.src = src;
|
|
@@ -154,11 +155,7 @@ export default function transpileScript(
|
|
|
154
155
|
}
|
|
155
156
|
|
|
156
157
|
// When the script hits the dependency reuse logic, the current script is not executed, and an empty script is returned directly
|
|
157
|
-
script.src =
|
|
158
|
-
new Blob([`// ${src} is reusing the execution result of ${url}`], {
|
|
159
|
-
type: 'text/javascript',
|
|
160
|
-
}),
|
|
161
|
-
);
|
|
158
|
+
script.src = createReusingObjectUrl(src, url, 'text/javascript');
|
|
162
159
|
|
|
163
160
|
return script;
|
|
164
161
|
}
|
|
@@ -15,8 +15,8 @@ export type BaseTranspilerOpts = BaseLoaderOpts & {
|
|
|
15
15
|
export type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node };
|
|
16
16
|
|
|
17
17
|
export enum Mode {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
REMOTE_ASSETS_IN_SANDBOX = 'RAIS',
|
|
19
|
+
REUSED_DEP_IN_SANDBOX = 'RDIS',
|
|
20
|
+
INLINE_CODE_IN_SANDBOX = 'ICIS',
|
|
21
21
|
NONE = 'NONE',
|
|
22
22
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Kuitos
|
|
3
|
+
* @since 2023-10-09
|
|
4
|
+
*/
|
|
5
|
+
import { memoize } from 'lodash';
|
|
6
|
+
|
|
7
|
+
export const createReusingObjectUrl = memoize(
|
|
8
|
+
(src: string, url: string, type: 'text/javascript' | 'text/css'): string => {
|
|
9
|
+
return URL.createObjectURL(
|
|
10
|
+
new Blob([`/* ${src} is reusing the execution result of ${url} */`], {
|
|
11
|
+
type,
|
|
12
|
+
}),
|
|
13
|
+
);
|
|
14
|
+
},
|
|
15
|
+
(src, url, type) => `${src}#${url}#${type}`,
|
|
16
|
+
);
|