@mbler/mcx-core 0.0.7-alpha.r2 → 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.js +54 -25
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
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
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
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
|