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

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,14 +11022,78 @@ 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 parts = id.split("/");
11094
+ const pkgName = parts[0];
11095
+ const subPath = parts.slice(1).join("/");
11096
+ const d = path.join(opt.moduleDir, pkgName);
11031
11097
  let pkgJson;
11032
11098
  try {
11033
11099
  pkgJson = JSON.parse(await fs.readFile(path.join(d, "package.json"), "utf-8"));
@@ -11040,12 +11106,20 @@ function mcxPlugn(opt, output) {
11040
11106
  throw new Error(`[mcx resolveId]\: invalid package.json for '${id}'`);
11041
11107
  }
11042
11108
  }
11109
+ if (subPath) {
11110
+ const fromExports = await resolvePackageExports(d, subPath, pkgJson);
11111
+ if (fromExports)
11112
+ return fromExports;
11113
+ const fromDist = await tryResolvePath(path.join(d, "./dist", subPath));
11114
+ if (fromDist)
11115
+ return fromDist;
11116
+ const fromRoot = await tryResolvePath(path.join(d, subPath));
11117
+ if (fromRoot)
11118
+ return fromRoot;
11119
+ return null;
11120
+ }
11043
11121
  return path.join(d, pkgJson.main);
11044
11122
  }
11045
- else if (imp) {
11046
- return path.join(path.dirname(imp), id);
11047
- }
11048
- return null;
11049
11123
  },
11050
11124
  transform: async function (code, id) {
11051
11125
  const magic = new MagicString(code);