@mbler/mcx-core 0.0.8-rc.1 → 0.0.8-rc.3

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
@@ -1684,7 +1684,9 @@ async function generateEventConfig(eventTag, ctx, impBody) {
1684
1684
  extend.push(t__namespace.identifier(id));
1685
1685
  }
1686
1686
  }
1687
- data.push(t__namespace.objectProperty(t__namespace.identifier(name), t__namespace.stringLiteral(handlerName)));
1687
+ else {
1688
+ data.push(t__namespace.objectProperty(t__namespace.identifier(name), t__namespace.stringLiteral(handlerName)));
1689
+ }
1688
1690
  }
1689
1691
  argm.properties.push(t__namespace.objectProperty(t__namespace.identifier("data"), t__namespace.objectExpression(data)), t__namespace.objectProperty(t__namespace.identifier("extends"), t__namespace.arrayExpression(extend)));
1690
1692
  return argm;
@@ -11020,32 +11022,105 @@ function mcxPlugn(opt, output) {
11020
11022
  errors: []
11021
11023
  };
11022
11024
  }
11025
+ const resolveExtensions = ["", ".ts", ".mts", ".cts", ".js", ".mjs", ".cjs"];
11026
+ const indexExtensions = resolveExtensions.map(ext => "/index" + ext);
11027
+ async function tryResolvePath(filePath) {
11028
+ for (const idxExt of indexExtensions) {
11029
+ try {
11030
+ const fullPath = filePath + idxExt;
11031
+ await fs.readFile(fullPath, "utf-8");
11032
+ return fullPath;
11033
+ }
11034
+ catch { }
11035
+ }
11036
+ for (const ext of resolveExtensions) {
11037
+ try {
11038
+ const fullPath = filePath + ext;
11039
+ await fs.readFile(fullPath, "utf-8");
11040
+ return fullPath;
11041
+ }
11042
+ catch { }
11043
+ }
11044
+ return null;
11045
+ }
11046
+ async function resolvePackageExports(pkgDir, subPath, pkgJson) {
11047
+ const exports$1 = pkgJson.exports;
11048
+ if (exports$1) {
11049
+ const subImport = subPath.startsWith("./") ? subPath : `./${subPath}`;
11050
+ if (typeof exports$1 === "object" && exports$1 !== null) {
11051
+ if (exports$1[subImport]) {
11052
+ const target = exports$1[subImport];
11053
+ if (typeof target === "string") {
11054
+ return path.join(pkgDir, target);
11055
+ }
11056
+ else if (typeof target === "object" && target !== null) {
11057
+ if (target.import) {
11058
+ return path.join(pkgDir, target.import);
11059
+ }
11060
+ return path.join(pkgDir, target.default || Object.values(target)[0]);
11061
+ }
11062
+ }
11063
+ if (subImport.endsWith("/") || subImport.endsWith("/*")) {
11064
+ const dirMapping = subImport.slice(0, -1);
11065
+ for (const [key, value] of Object.entries(exports$1)) {
11066
+ if (key.startsWith(dirMapping) && key !== dirMapping) {
11067
+ const target = value;
11068
+ return path.join(pkgDir, target);
11069
+ }
11070
+ }
11071
+ }
11072
+ }
11073
+ else if (typeof exports$1 === "string") {
11074
+ return path.join(pkgDir, exports$1);
11075
+ }
11076
+ }
11077
+ return null;
11078
+ }
11023
11079
  return {
11024
11080
  name: "mbler-mcx-core",
11025
11081
  async resolveId(id, imp) {
11026
11082
  const i = path.parse(id);
11027
- // if is not a file path
11028
- if (!i.root && !i.dir.startsWith(".")) {
11029
- // read module package.json
11030
- const d = path.join(opt.moduleDir, id);
11083
+ if (i.dir.startsWith(".") || i.root) {
11084
+ if (imp) {
11085
+ const baseDir = path.dirname(imp);
11086
+ const resolved = await tryResolvePath(path.join(baseDir, id));
11087
+ if (resolved)
11088
+ return resolved;
11089
+ }
11090
+ return null;
11091
+ }
11092
+ else {
11093
+ const isScopedPackage = id.startsWith("@");
11094
+ const parts = id.split("/");
11095
+ const pkgName = isScopedPackage ? `${parts[0]}/${parts[1]}` : parts[0];
11096
+ const subPath = isScopedPackage ? parts.slice(2).join("/") : parts.slice(1).join("/");
11097
+ const d = path.join(opt.moduleDir, pkgName);
11031
11098
  let pkgJson;
11032
11099
  try {
11033
11100
  pkgJson = JSON.parse(await fs.readFile(path.join(d, "package.json"), "utf-8"));
11034
11101
  }
11035
11102
  catch (err) {
11036
- if (err.code === "ENOENT") {
11037
- throw new Error(`[mcx resolveId]\: package.json not found for '${id}'`);
11103
+ if (!err.code || err.code === "ENOENT") {
11104
+ throw new Error(`[mcx resolveId]: package.json not found for '${id}' at '${d}'`);
11038
11105
  }
11039
11106
  else {
11040
- throw new Error(`[mcx resolveId]\: invalid package.json for '${id}'`);
11041
- }
11107
+ throw new Error(`[mcx resolveId]: invalid package.json for '${id}': ${err.message}`);
11108
+ }
11109
+ }
11110
+ if (subPath) {
11111
+ const fromExports = await resolvePackageExports(d, subPath, pkgJson);
11112
+ if (fromExports)
11113
+ return fromExports;
11114
+ const fromDist = await tryResolvePath(path.join(d, "./dist", subPath));
11115
+ if (fromDist)
11116
+ return fromDist;
11117
+ const fromRoot = await tryResolvePath(path.join(d, subPath));
11118
+ if (fromRoot)
11119
+ return fromRoot;
11120
+ return null;
11042
11121
  }
11043
11122
  return path.join(d, pkgJson.main);
11044
11123
  }
11045
- else if (imp) {
11046
- return path.join(path.dirname(imp), id);
11047
- }
11048
- return null;
11049
11124
  },
11050
11125
  transform: async function (code, id) {
11051
11126
  const magic = new MagicString(code);