@qiankunjs/shared 0.0.1-rc.0 → 0.0.1-rc.2

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.
Files changed (37) hide show
  1. package/dist/cjs/assets-transpilers/index.js.map +1 -1
  2. package/dist/cjs/assets-transpilers/link.js +29 -46
  3. package/dist/cjs/assets-transpilers/link.js.map +1 -1
  4. package/dist/cjs/assets-transpilers/script.d.ts +3 -3
  5. package/dist/cjs/assets-transpilers/script.js +8 -9
  6. package/dist/cjs/assets-transpilers/script.js.map +1 -1
  7. package/dist/cjs/assets-transpilers/types.d.ts +3 -3
  8. package/dist/cjs/assets-transpilers/types.js +3 -3
  9. package/dist/cjs/assets-transpilers/types.js.map +1 -1
  10. package/dist/cjs/assets-transpilers/utils.d.ts +1 -0
  11. package/dist/cjs/assets-transpilers/utils.js +22 -0
  12. package/dist/cjs/assets-transpilers/utils.js.map +1 -0
  13. package/dist/cjs/module-resolver/index.d.ts +1 -1
  14. package/dist/cjs/module-resolver/index.js +10 -10
  15. package/dist/cjs/module-resolver/index.js.map +1 -1
  16. package/dist/esm/assets-transpilers/index.js.map +1 -1
  17. package/dist/esm/assets-transpilers/link.js +29 -46
  18. package/dist/esm/assets-transpilers/link.js.map +1 -1
  19. package/dist/esm/assets-transpilers/script.d.ts +3 -3
  20. package/dist/esm/assets-transpilers/script.js +8 -9
  21. package/dist/esm/assets-transpilers/script.js.map +1 -1
  22. package/dist/esm/assets-transpilers/types.d.ts +3 -3
  23. package/dist/esm/assets-transpilers/types.js +3 -3
  24. package/dist/esm/assets-transpilers/types.js.map +1 -1
  25. package/dist/esm/assets-transpilers/utils.d.ts +1 -0
  26. package/dist/esm/assets-transpilers/utils.js +13 -0
  27. package/dist/esm/assets-transpilers/utils.js.map +1 -0
  28. package/dist/esm/module-resolver/index.d.ts +1 -1
  29. package/dist/esm/module-resolver/index.js +10 -10
  30. package/dist/esm/module-resolver/index.js.map +1 -1
  31. package/package.json +2 -2
  32. package/src/assets-transpilers/index.ts +3 -3
  33. package/src/assets-transpilers/link.ts +23 -43
  34. package/src/assets-transpilers/script.ts +11 -14
  35. package/src/assets-transpilers/types.ts +3 -3
  36. package/src/assets-transpilers/utils.ts +16 -0
  37. package/src/module-resolver/index.ts +9 -9
@@ -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.CACHE_FROM_SANDBOX; result: { src: string } & MatchResult }
13
+ | { mode: Mode.REUSED_DEP_IN_SANDBOX; result: { src: string } & MatchResult }
13
14
  | { mode: Mode.NONE; result?: never };
14
- const preTranspile = (
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
- if (href) {
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.CACHE_FROM_SANDBOX,
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
- case Mode.REMOTE_FROM_SANDBOX: {
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.CACHE_FROM_SANDBOX: {
60
+ case Mode.REUSED_DEP_IN_SANDBOX: {
66
61
  const { url } = result;
67
- const objectURL = URL.createObjectURL(
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 } = preTranspile({ href }, baseURI, opts);
72
+ const { mode, result } = preTranspileStyleSheetLink({ href, rel: 'stylesheet' }, baseURI, opts);
84
73
 
85
74
  switch (mode) {
86
- case Mode.CACHE_FROM_SANDBOX: {
75
+ case Mode.REUSED_DEP_IN_SANDBOX: {
87
76
  const { url } = result;
88
- const objectURL = URL.createObjectURL(
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 } = preTranspile(
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.CACHE_FROM_SANDBOX: {
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 = URL.createObjectURL(
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.REMOTE_FROM_SANDBOX; result: { src: string } }
35
- | { mode: Mode.CACHE_FROM_SANDBOX; result: { src: string } & MatchResult }
36
- | { mode: Mode.INLINE_FROM_SANDBOX; result: { code: string } }
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.CACHE_FROM_SANDBOX,
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.REMOTE_FROM_SANDBOX,
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.INLINE_FROM_SANDBOX,
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.REMOTE_FROM_SANDBOX: {
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.INLINE_FROM_SANDBOX: {
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.CACHE_FROM_SANDBOX: {
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 = URL.createObjectURL(
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
- REMOTE_FROM_SANDBOX = 'REMOTE_FROM_SANDBOX',
19
- CACHE_FROM_SANDBOX = 'CACHE_IN_SANDBOX',
20
- INLINE_FROM_SANDBOX = 'INLINE_FROM_SANDBOX',
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
+ );
@@ -3,7 +3,7 @@ import type { MatchResult } from './types';
3
3
 
4
4
  declare global {
5
5
  interface HTMLElement {
6
- __cached_deps__?: string[];
6
+ __matched_deps__?: string[];
7
7
  }
8
8
  }
9
9
 
@@ -42,15 +42,15 @@ export function moduleResolver(
42
42
 
43
43
  if (mainAppDependencyMapString) {
44
44
  const mainAppDependencyMap = JSON.parse(mainAppDependencyMapString) as DependencyMap;
45
- const cachedDeps = (microAppContainer.__cached_deps__ ??= []);
45
+ const matchedDeps = (microAppContainer.__matched_deps__ ??= []);
46
46
  const matchedDep = findDependency(
47
47
  microAppDependency,
48
48
  normalizeDependencies(mainAppDependencyMap.dependencies),
49
- cachedDeps,
49
+ matchedDeps,
50
50
  );
51
51
 
52
52
  if (matchedDep) {
53
- cachedDeps.push(matchedDep.name);
53
+ matchedDeps.push(matchedDep.name);
54
54
  return matchedDep;
55
55
  }
56
56
  }
@@ -63,14 +63,14 @@ export function moduleResolver(
63
63
  function findDependency(
64
64
  dependency: NormalizedDependency,
65
65
  mainAppDependencies: NormalizedDependency[],
66
- cachedDependencies: string[],
66
+ matchedDependencies: string[],
67
67
  ): MatchResult | undefined {
68
68
  const matched = mainAppDependencies.find(
69
- (cachedDependency) =>
70
- cachedDependency.name === dependency.name &&
71
- satisfies(cachedDependency.version, dependency.range) &&
69
+ (mainAppDependency) =>
70
+ mainAppDependency.name === dependency.name &&
71
+ satisfies(mainAppDependency.version, dependency.range) &&
72
72
  // peer dependencies must be cached before
73
- dependency.peerDeps?.every((peerDep) => cachedDependencies.indexOf(peerDep) !== -1),
73
+ (dependency.peerDeps || []).every((peerDep) => matchedDependencies.indexOf(peerDep) !== -1),
74
74
  );
75
75
 
76
76
  if (matched) {