@mbler/mcx-core 0.0.7-alpha.r3 → 0.0.7

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/index.js CHANGED
@@ -670,8 +670,8 @@ class Utils {
670
670
  // first verify ir.raw
671
671
  if (ir?.raw && Utils.CheckImportNode(ir?.raw, ir))
672
672
  return ir.raw;
673
- let result = [];
674
- for (let ImportIt of ir.imported) {
673
+ const result = [];
674
+ for (const ImportIt of ir.imported) {
675
675
  if (!ImportIt)
676
676
  continue;
677
677
  if (ImportIt.isAll) {
@@ -690,7 +690,7 @@ class Utils {
690
690
  }
691
691
  static ImportToCache(node) {
692
692
  const result = [];
693
- for (let item of node.specifiers) {
693
+ for (const item of node.specifiers) {
694
694
  const thisName = item.local.name;
695
695
  if (item.type == "ImportNamespaceSpecifier") {
696
696
  result.push({
@@ -855,7 +855,7 @@ class CompileJS {
855
855
  }
856
856
  writeBuildCache() {
857
857
  const currenySource = [];
858
- let build = [];
858
+ const build = [];
859
859
  for (const [as, data] of Object.entries(this.indexTemp)) {
860
860
  if (!this.writeImportKeys.includes(as))
861
861
  continue;
@@ -1497,7 +1497,7 @@ class McxUtlis {
1497
1497
  try {
1498
1498
  text = JSON.parse(buffer.toString()); // Buffer -> string -> object
1499
1499
  }
1500
- catch (parseErr) {
1500
+ catch {
1501
1501
  // JSON 解析失败时返回空对象
1502
1502
  text = {};
1503
1503
  }
@@ -1508,7 +1508,8 @@ class McxUtlis {
1508
1508
  }
1509
1509
  return text;
1510
1510
  }
1511
- catch (err) {
1511
+ catch {
1512
+ // err 变量不再使用,直接忽略
1512
1513
  // 如果不是最后一次尝试,则等待后重试
1513
1514
  if (attempt < opts.maxRetries - 1) {
1514
1515
  await McxUtlis.sleep(opts.delay);
@@ -1822,24 +1823,6 @@ async function Comp$1(ctx) {
1822
1823
  }
1823
1824
 
1824
1825
  async function Comp(ctx) {
1825
- const resolveImportCandidates = (importerId, source) => {
1826
- const baseDir = path.dirname(importerId);
1827
- const sourcePath = path.resolve(baseDir, source);
1828
- const parsed = path.parse(sourcePath);
1829
- const mcxExt = ".mcx";
1830
- const candidates = new Set();
1831
- // compileMCXFn only accepts mcx source; normalize all import styles to .mcx candidates.
1832
- if (parsed.ext === mcxExt) {
1833
- candidates.add(sourcePath);
1834
- }
1835
- else {
1836
- // e.g. ./foo or ./foo.js -> ./foo.mcx
1837
- candidates.add(path.join(parsed.dir, `${parsed.name}${mcxExt}`));
1838
- }
1839
- // e.g. ./foo -> ./foo/index.mcx
1840
- candidates.add(path.join(sourcePath, `index${mcxExt}`));
1841
- return [...candidates];
1842
- };
1843
1826
  const eventImportIdList = [];
1844
1827
  for (const impNode of ctx.ctx.compiledCode.JSIR.BuildCache.import) {
1845
1828
  const source = impNode.source;
@@ -1847,45 +1830,34 @@ async function Comp(ctx) {
1847
1830
  if (!parsed.root && !parsed.dir.startsWith(".")) {
1848
1831
  continue;
1849
1832
  }
1850
- const candidates = resolveImportCandidates(ctx.ctx.currentId, source);
1851
- let resolvedPath = null;
1852
- let compileResult = null;
1853
- let lastErr = null;
1854
- for (const candidate of candidates) {
1855
- try {
1856
- const code = await fs.readFile(candidate, "utf-8");
1857
- compileResult = compileMCXFn(code);
1858
- resolvedPath = candidate;
1859
- break;
1860
- }
1861
- catch (err) {
1862
- if (err?.code === "ENOENT") {
1863
- continue;
1833
+ // path
1834
+ const fPath = path.join(ctx.ctx.currentId, "../", source);
1835
+ try {
1836
+ // read file
1837
+ const code = await fs.readFile(fPath, "utf-8");
1838
+ const compiledCode = compileMCXFn(code);
1839
+ // write cache
1840
+ ctx.ctx.cache.set(fPath, compiledCode);
1841
+ if (compiledCode.strLoc.Event.isLoad) {
1842
+ for (const impItem of impNode.imported) {
1843
+ let type;
1844
+ if (impItem.isAll)
1845
+ type = "all";
1846
+ else if (impItem.import == "default")
1847
+ type = "default";
1848
+ else {
1849
+ throw new Error("not vaild importDeclartion: Event mcx only resolve default and all import, can't use other import");
1850
+ }
1851
+ eventImportIdList.push({
1852
+ type,
1853
+ as: impItem.as
1854
+ });
1864
1855
  }
1865
- lastErr = err;
1866
- break;
1867
1856
  }
1868
1857
  }
1869
- if (!resolvedPath || !compileResult) {
1870
- ctx.ctx.rollupContext.warn(`[extract import]: can't resolve file from ${source} by ${ctx.ctx.currentId}\n- candidates: ${candidates.join(", ")}\n- err: ${(lastErr instanceof Error) ? lastErr.stack : lastErr ?? "not found"}`);
1871
- continue;
1872
- }
1873
- ctx.ctx.cache.set(resolvedPath, compileResult);
1874
- if (compileResult.strLoc.Event.isLoad) {
1875
- for (const impItem of impNode.imported) {
1876
- let type;
1877
- if (impItem.isAll)
1878
- type = "all";
1879
- else if (impItem.import == "default")
1880
- type = "default";
1881
- else {
1882
- throw new Error("not vaild importDeclartion: Event mcx only resolve default and all import, can't use other import");
1883
- }
1884
- eventImportIdList.push({
1885
- type,
1886
- as: impItem.as
1887
- });
1888
- }
1858
+ catch (err) {
1859
+ // if error: file not found, file can't write, mcx syntax error
1860
+ ctx.ctx.rollupContext.warn(`[extract import]: can't resolve file ${fPath} and import by ${ctx.ctx.currentId}\n- err: ${(err instanceof Error) ? err.stack : err}`);
1889
1861
  }
1890
1862
  }
1891
1863
  // only have event import
@@ -2055,7 +2027,7 @@ function transformESMToCJS(code, pluginContext, hook) {
2055
2027
  * 将 import IR 转换为 require 声明
2056
2028
  */
2057
2029
  function transformImportIRtoRequire(importIR) {
2058
- let define = [
2030
+ const define = [
2059
2031
  t__namespace.variableDeclarator(t__namespace.identifier('__import_default'), t__namespace.functionExpression(null, [t__namespace.identifier('obj')], t__namespace.blockStatement([
2060
2032
  t__namespace.returnStatement(t__namespace.conditionalExpression(t__namespace.memberExpression(t__namespace.identifier('obj'), t__namespace.identifier('__esModule')), t__namespace.memberExpression(t__namespace.identifier('obj'), t__namespace.identifier('default')), t__namespace.identifier('obj'))),
2061
2033
  ]))),