@esmx/core 3.0.0-rc.11 → 3.0.0-rc.13

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/cli/cli.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import module from "node:module";
2
2
  import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
- import { COMMAND, Esmx } from "../esmx";
4
+ import { COMMAND, Esmx } from "../esmx.mjs";
5
5
  async function getSrcOptions() {
6
6
  return import(path.resolve(process.cwd(), "./src/entry.node.ts")).then(
7
7
  (m) => m.default
@@ -738,7 +738,7 @@ export class Esmx {
738
738
  if (this._importmapHash === null) {
739
739
  let wrote = false;
740
740
  const code = `(() => {
741
- const base = document.currentScript.getAttribute('data-base');
741
+ const base = document.currentScript.getAttribute("data-base");
742
742
  const importmap = ${serialize(importmap, { isJSON: true })};
743
743
  if (importmap.imports && base) {
744
744
  const imports = importmap.imports;
@@ -746,10 +746,10 @@ if (importmap.imports && base) {
746
746
  imports[k] = base + v;
747
747
  });
748
748
  }
749
- document.head.appendChild(Object.assign(document.createElement('script'), {
750
- type: 'importmap',
751
- innerHTML: JSON.stringify(importmap)
752
- }));
749
+ const script = document.createElement("script");
750
+ script.type = "importmap";
751
+ script.innerHTML = JSON.stringify(importmap);
752
+ document.head.appendChild(script);
753
753
  })();`;
754
754
  const hash = contentHash(code);
755
755
  filepath = this.resolvePath(
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  export {
2
2
  Esmx
3
- } from "./esmx";
3
+ } from "./esmx.mjs";
4
4
  export {
5
5
  PathType,
6
6
  parseModuleConfig
@@ -23,4 +23,6 @@ export type ImportPreloadInfo = SpecifierMap;
23
23
  * - `Promise<{ [specifier: string]: ImportPreloadPathString }>` 模块名和文件路径的映射对象
24
24
  * - `null` specifier 不存在
25
25
  */
26
- export declare function getImportPreloadInfo(specifier: string, importMap: ImportMap, moduleConfig: ParsedModuleConfig): Promise<SpecifierMap | null>;
26
+ export declare function getImportPreloadInfo(specifier: string, importMap: ImportMap, moduleConfig: ParsedModuleConfig): Promise<{
27
+ [k: string]: string;
28
+ } | null>;
@@ -15,29 +15,28 @@ export async function getImportPreloadInfo(specifier, importMap, moduleConfig) {
15
15
  if (!importInfo || !(specifier in importInfo)) {
16
16
  return null;
17
17
  }
18
- const ans = {};
19
- const needHandles = [[specifier]];
18
+ const ans = {
19
+ // 入口文件也放入预加载列表
20
+ [specifier]: importInfo[specifier]
21
+ };
22
+ const needHandles = [specifier];
20
23
  while (needHandles.length) {
21
- const needHandle = [];
22
- for (const specifier2 of needHandles.shift()) {
23
- let filepath = importInfo[specifier2];
24
- const splitRes = filepath.split("/");
25
- if (splitRes[0] === "") splitRes.shift();
26
- const name = splitRes.shift() + "";
27
- const link = moduleConfig.links.find((item) => item.name === name);
28
- if (!link) {
29
- continue;
30
- }
31
- filepath = path.join(link.root, "client", ...splitRes);
32
- const imports = await getImportsFromJsFile(filepath);
33
- imports.forEach((specifier3) => {
34
- if (specifier3 in importInfo && !ans[specifier3]) {
35
- ans[specifier3] = importInfo[specifier3];
36
- needHandle.push(specifier3);
37
- }
38
- });
24
+ const specifier2 = needHandles.shift();
25
+ let filepath = importInfo[specifier2];
26
+ const splitRes = filepath.split("/");
27
+ if (splitRes[0] === "") splitRes.shift();
28
+ const name = splitRes.shift() + "";
29
+ const link = moduleConfig.links.find((item) => item.name === name);
30
+ if (!link) {
31
+ continue;
32
+ }
33
+ filepath = path.join(link.root, "client", ...splitRes);
34
+ const imports = await getImportsFromJsFile(filepath);
35
+ for (const specifier3 of imports) {
36
+ if (!(specifier3 in importInfo) || specifier3 in ans) continue;
37
+ ans[specifier3] = importInfo[specifier3];
38
+ needHandles.push(specifier3);
39
39
  }
40
- needHandle.length && needHandles.push(needHandle);
41
40
  }
42
- return ans;
41
+ return Object.fromEntries(Object.entries(ans).reverse());
43
42
  }
package/package.json CHANGED
@@ -59,17 +59,18 @@
59
59
  "build": "unbuild"
60
60
  },
61
61
  "dependencies": {
62
- "@esmx/import": "3.0.0-rc.11",
62
+ "@esmx/import": "3.0.0-rc.13",
63
63
  "@types/serialize-javascript": "^5.0.4",
64
64
  "es-module-lexer": "^1.6.0",
65
65
  "find": "^0.3.0",
66
- "send": "^1.1.0",
66
+ "send": "^1.2.0",
67
67
  "serialize-javascript": "^6.0.2",
68
68
  "write": "^2.0.0"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@biomejs/biome": "1.9.4",
72
- "@esmx/lint": "3.0.0-rc.11",
72
+ "@esmx/lint": "3.0.0-rc.13",
73
+ "@gez/lint": "3.0.0-rc.9",
73
74
  "@types/find": "^0.2.4",
74
75
  "@types/node": "22.13.10",
75
76
  "@types/send": "^0.17.4",
@@ -80,7 +81,7 @@
80
81
  "unbuild": "2.0.0",
81
82
  "vitest": "3.0.8"
82
83
  },
83
- "version": "3.0.0-rc.11",
84
+ "version": "3.0.0-rc.13",
84
85
  "type": "module",
85
86
  "private": false,
86
87
  "exports": {
@@ -103,5 +104,5 @@
103
104
  "template",
104
105
  "public"
105
106
  ],
106
- "gitHead": "0920e0485284528eb642a7078f4cb48b013352a5"
107
+ "gitHead": "801082f89364db5e1137431e3cc1121f689fa7ca"
107
108
  }
@@ -915,7 +915,7 @@ export class Esmx {
915
915
  if (this._importmapHash === null) {
916
916
  let wrote = false;
917
917
  const code = `(() => {
918
- const base = document.currentScript.getAttribute('data-base');
918
+ const base = document.currentScript.getAttribute("data-base");
919
919
  const importmap = ${serialize(importmap, { isJSON: true })};
920
920
  if (importmap.imports && base) {
921
921
  const imports = importmap.imports;
@@ -923,10 +923,10 @@ if (importmap.imports && base) {
923
923
  imports[k] = base + v;
924
924
  });
925
925
  }
926
- document.head.appendChild(Object.assign(document.createElement('script'), {
927
- type: 'importmap',
928
- innerHTML: JSON.stringify(importmap)
929
- }));
926
+ const script = document.createElement("script");
927
+ script.type = "importmap";
928
+ script.innerHTML = JSON.stringify(importmap);
929
+ document.head.appendChild(script);
930
930
  })();`;
931
931
  const hash = contentHash(code);
932
932
  filepath = this.resolvePath(
@@ -53,33 +53,34 @@ export async function getImportPreloadInfo(
53
53
  return null;
54
54
  }
55
55
 
56
- const ans: ImportPreloadInfo = {};
56
+ const ans: ImportPreloadInfo = {
57
+ // 入口文件也放入预加载列表
58
+ [specifier]: importInfo[specifier]
59
+ };
57
60
 
58
- const needHandles: string[][] = [[specifier]];
59
61
  // 词法分析是耗时操作,因此处理的文件越少越快,换句话说就是深度越浅越快,因此这里使用广度优先搜索
62
+ const needHandles: string[] = [specifier];
60
63
  while (needHandles.length) {
61
- const needHandle: string[] = [];
62
- for (const specifier of needHandles.shift()!) {
63
- let filepath = importInfo[specifier];
64
- const splitRes = filepath.split('/');
65
- if (splitRes[0] === '') splitRes.shift();
66
- const name = splitRes.shift() + '';
67
- const link = moduleConfig.links.find((item) => item.name === name);
68
- if (!link) {
69
- continue;
70
- }
71
- filepath = path.join(link.root, 'client', ...splitRes);
72
- const imports = await getImportsFromJsFile(filepath);
73
- imports.forEach((specifier) => {
74
- // 如果模块名在 importMap 中存在,且没处理过
75
- if (specifier in importInfo && !ans[specifier]) {
76
- ans[specifier] = importInfo[specifier];
77
- needHandle.push(specifier);
78
- }
79
- });
64
+ const specifier = needHandles.shift()!;
65
+ let filepath = importInfo[specifier];
66
+ const splitRes = filepath.split('/');
67
+ if (splitRes[0] === '') splitRes.shift();
68
+ // 这里默认路径的第一个目录是软包名称
69
+ const name = splitRes.shift() + '';
70
+ const link = moduleConfig.links.find((item) => item.name === name);
71
+ if (!link) {
72
+ continue;
73
+ }
74
+ filepath = path.join(link.root, 'client', ...splitRes);
75
+ const imports = await getImportsFromJsFile(filepath);
76
+ for (const specifier of imports) {
77
+ // 如果模块名在 importMap 中不存在,或已经处理过
78
+ if (!(specifier in importInfo) || specifier in ans) continue;
79
+ ans[specifier] = importInfo[specifier];
80
+ needHandles.push(specifier);
80
81
  }
81
- needHandle.length && needHandles.push(needHandle);
82
82
  }
83
83
 
84
- return ans;
84
+ // 倒序,理论上倒序后浏览器解析可能会更快
85
+ return Object.fromEntries(Object.entries(ans).reverse());
85
86
  }
File without changes