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

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.d.ts CHANGED
@@ -352,7 +352,7 @@ interface ItemComponentOpt {
352
352
  id: string;
353
353
  name: string;
354
354
  format: string;
355
- components: {
355
+ components: Partial<{
356
356
  offHand: boolean;
357
357
  damage: number;
358
358
  DestroyInCreate: boolean;
@@ -1147,7 +1147,7 @@ interface ItemComponentOpt {
1147
1147
  */
1148
1148
  value?: number;
1149
1149
  };
1150
- };
1150
+ }>;
1151
1151
  }
1152
1152
  type JSONValue<T> = T | {
1153
1153
  value: T;
package/dist/index.js CHANGED
@@ -1822,6 +1822,24 @@ async function Comp$1(ctx) {
1822
1822
  }
1823
1823
 
1824
1824
  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
+ };
1825
1843
  const eventImportIdList = [];
1826
1844
  for (const impNode of ctx.ctx.compiledCode.JSIR.BuildCache.import) {
1827
1845
  const source = impNode.source;
@@ -1829,34 +1847,45 @@ async function Comp(ctx) {
1829
1847
  if (!parsed.root && !parsed.dir.startsWith(".")) {
1830
1848
  continue;
1831
1849
  }
1832
- // path
1833
- const fPath = path.join(ctx.ctx.currentId, "../", source);
1834
- try {
1835
- // read file
1836
- const code = await fs.readFile(fPath, "utf-8");
1837
- const compiledCode = compileMCXFn(code);
1838
- // write cache
1839
- ctx.ctx.cache.set(fPath, compiledCode);
1840
- if (compiledCode.strLoc.Event.isLoad) {
1841
- for (const impItem of impNode.imported) {
1842
- let type;
1843
- if (impItem.isAll)
1844
- type = "all";
1845
- else if (impItem.import == "default")
1846
- type = "default";
1847
- else {
1848
- throw new Error("not vaild importDeclartion: Event mcx only resolve default and all import, can't use other import");
1849
- }
1850
- eventImportIdList.push({
1851
- type,
1852
- as: impItem.as
1853
- });
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;
1854
1864
  }
1865
+ lastErr = err;
1866
+ break;
1855
1867
  }
1856
1868
  }
1857
- catch (err) {
1858
- // if error: file not found, file can't write, mcx syntax error
1859
- 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}`);
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
+ }
1860
1889
  }
1861
1890
  }
1862
1891
  // only have event import