@vesk/compiler 0.2.50-canary.3 → 0.2.50-canary.4

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.
@@ -74,6 +74,31 @@ export declare function resolveImportPath(spec: string, fromDir: string): string
74
74
  * `node_modules`) share one cache entry and one module instance.
75
75
  */
76
76
  export declare function resolveSsrModule(specifier: string, fromDir: string): string | null;
77
+ /**
78
+ * Resolve a subpath through a package's `exports` map.
79
+ *
80
+ * This is what makes a Vesk app work inside an existing pnpm/npm/yarn
81
+ * workspace. A modern workspace package declares `exports` and often NO `main`
82
+ * (or points `main` at a CJS build while the app imports ESM), and subpath
83
+ * imports (`@acme/ui/button`) resolve through the map rather than through the
84
+ * directory layout. Without this, such a package simply does not resolve, and
85
+ * the app has to vendor it.
86
+ *
87
+ * Conditions are tried in import order: `import`, `module`, `node`, `default`.
88
+ * A pattern (`./dist/*.js`) is matched with the single `*` capture.
89
+ */
90
+ export declare function resolveExports(exportsField: unknown, subpath: string): string | null;
91
+ /**
92
+ * Resolve a bare specifier through a package's `module` field (the ESM entry
93
+ * in a package that also has a CJS `main`).
94
+ */
95
+ export declare function resolveViaPackageModuleField(specifier: string, fromDir: string): string | null;
96
+ /**
97
+ * Resolve a bare specifier through its package's `exports` map, walking
98
+ * node_modules upwards. Returns null when the package has no map, does not
99
+ * export the subpath, or cannot be read.
100
+ */
101
+ export declare function resolveViaPackageExports(specifier: string, fromDir: string): string | null;
77
102
  /** Rewrites an ESM program body (imports/exports removed) into CJS statements. */
78
103
  export declare function esmToCjs(body: Array<{
79
104
  type: string;
@@ -1 +1 @@
1
- {"version":3,"file":"module-imports.d.ts","sourceRoot":"","sources":["../src/module-imports.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AA2ElD,gFAAgF;AAChF,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAM7D;AAED,gEAAgE;AAChE,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AAE,CAAC;CAC9B,CAAC;AAEF;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,eAAe,GAAG,MAAM,CA8DxF;AAID;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,MAAM,EAAE,EACpB,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,SAAS,EAAE,eAAe,GACzB,iBAAiB,EAAE,CAiBrB"}
@@ -900,6 +900,21 @@ export function resolveSsrModule(specifier, fromDir) {
900
900
  const aliased = resolveAliasModule(specifier, fromDir);
901
901
  if (aliased)
902
902
  return toRealPath(aliased);
903
+ // Bare specifier. A package's `exports` map is consulted BEFORE Node's own
904
+ // resolver: `createRequire.resolve` applies the CJS conditions, so it picks
905
+ // a `.cjs` build for a package that also ships ESM — and fails outright on
906
+ // an `import`-only package. Vesk emits ESM, so the `import` condition is
907
+ // the correct one. Node's resolver stays as the fallback (builtins, exotic
908
+ // conditions), and the node_modules walk after that.
909
+ const viaExports = resolveViaPackageExports(specifier, fromDir);
910
+ if (viaExports)
911
+ return viaExports;
912
+ // Same reasoning for the legacy fields: a package declaring both `module`
913
+ // and `main` wants the ESM build (Vesk emits ESM), while `createRequire`
914
+ // would take `main` and hand us the CJS one.
915
+ const viaModuleField = resolveViaPackageModuleField(specifier, fromDir);
916
+ if (viaModuleField)
917
+ return viaModuleField;
903
918
  // Bare specifier — prefer the native resolver (exports map, conditions,
904
919
  // builtins, symlinks), then fall back to a node_modules walk-up.
905
920
  const native = nativeResolve(specifier, fromDir);
@@ -983,6 +998,145 @@ function statOrNull(p) {
983
998
  return null;
984
999
  }
985
1000
  }
1001
+ /**
1002
+ * Resolve a subpath through a package's `exports` map.
1003
+ *
1004
+ * This is what makes a Vesk app work inside an existing pnpm/npm/yarn
1005
+ * workspace. A modern workspace package declares `exports` and often NO `main`
1006
+ * (or points `main` at a CJS build while the app imports ESM), and subpath
1007
+ * imports (`@acme/ui/button`) resolve through the map rather than through the
1008
+ * directory layout. Without this, such a package simply does not resolve, and
1009
+ * the app has to vendor it.
1010
+ *
1011
+ * Conditions are tried in import order: `import`, `module`, `node`, `default`.
1012
+ * A pattern (`./dist/*.js`) is matched with the single `*` capture.
1013
+ */
1014
+ export function resolveExports(exportsField, subpath) {
1015
+ const conditions = ['import', 'module', 'node', 'default'];
1016
+ const target = (value) => {
1017
+ if (typeof value === 'string')
1018
+ return value;
1019
+ if (!value || typeof value !== 'object')
1020
+ return null;
1021
+ for (const cond of conditions) {
1022
+ if (cond in value) {
1023
+ const got = target(value[cond]);
1024
+ if (got)
1025
+ return got;
1026
+ }
1027
+ }
1028
+ return null;
1029
+ };
1030
+ if (typeof exportsField === 'string')
1031
+ return subpath === '.' ? exportsField : null;
1032
+ if (!exportsField || typeof exportsField !== 'object')
1033
+ return null;
1034
+ const map = exportsField;
1035
+ if (subpath in map) {
1036
+ const direct = target(map[subpath]);
1037
+ if (direct)
1038
+ return direct;
1039
+ return null;
1040
+ }
1041
+ // Longest-prefix match for `./*` patterns, as Node does.
1042
+ let best = null;
1043
+ for (const key of Object.keys(map)) {
1044
+ const star = key.indexOf('*');
1045
+ if (star === -1)
1046
+ continue;
1047
+ const prefix = key.slice(0, star);
1048
+ const suffix = key.slice(star + 1);
1049
+ if (!subpath.startsWith(prefix) || !subpath.endsWith(suffix))
1050
+ continue;
1051
+ if (subpath.length < prefix.length + suffix.length)
1052
+ continue;
1053
+ if (best === null || prefix.length > best.key.length) {
1054
+ best = { key: prefix, star: subpath.slice(prefix.length, subpath.length - suffix.length) };
1055
+ }
1056
+ }
1057
+ if (!best)
1058
+ return null;
1059
+ const pattern = target(map[Object.keys(map).find((k) => k.startsWith(best.key + '*') && k.endsWith(best.key.slice(0, -1)) + '*')] ?? map[`${best.key}*`]);
1060
+ if (pattern === null)
1061
+ return null;
1062
+ return pattern.split('*').join(best.star);
1063
+ }
1064
+ /**
1065
+ * Resolve a bare specifier through a package's `module` field (the ESM entry
1066
+ * in a package that also has a CJS `main`).
1067
+ */
1068
+ export function resolveViaPackageModuleField(specifier, fromDir) {
1069
+ const firstSlash = specifier.indexOf('/');
1070
+ const splitAt = specifier.startsWith('@') ? specifier.indexOf('/', firstSlash + 1) : firstSlash;
1071
+ const pkgName = splitAt > 0 ? specifier.slice(0, splitAt) : specifier;
1072
+ if (splitAt > 0)
1073
+ return null; // a subpath import needs the exports map
1074
+ let dir = fromDir;
1075
+ for (let depth = 0; depth < 64; depth++) {
1076
+ const pkgPath = join(dir, 'node_modules', pkgName, 'package.json');
1077
+ if (statOrNull(pkgPath) !== null) {
1078
+ try {
1079
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
1080
+ if (typeof pkg.module === 'string' && pkg.module.length > 0) {
1081
+ const found = probeFile(resolve(join(dir, 'node_modules', pkgName), pkg.module));
1082
+ if (found)
1083
+ return toRealPath(found);
1084
+ }
1085
+ }
1086
+ catch {
1087
+ // malformed package.json — fall through
1088
+ }
1089
+ return null;
1090
+ }
1091
+ const parent = dirname(dir);
1092
+ if (parent === dir)
1093
+ break;
1094
+ dir = parent;
1095
+ }
1096
+ return null;
1097
+ }
1098
+ /**
1099
+ * Resolve a bare specifier through its package's `exports` map, walking
1100
+ * node_modules upwards. Returns null when the package has no map, does not
1101
+ * export the subpath, or cannot be read.
1102
+ */
1103
+ export function resolveViaPackageExports(specifier, fromDir) {
1104
+ // A scoped name (`@acme/ui`) has its slash INSIDE the package name — taking
1105
+ // the first slash as the subpath separator turns it into package `@acme` +
1106
+ // subpath `./ui`, which no `exports` map matches, and the lookup silently
1107
+ // falls through to Node's resolver.
1108
+ const firstSlash = specifier.indexOf('/');
1109
+ const splitAt = specifier.startsWith('@') ? specifier.indexOf('/', firstSlash + 1) : firstSlash;
1110
+ const pkgName = splitAt > 0 ? specifier.slice(0, splitAt) : specifier;
1111
+ const subpath = splitAt > 0 ? '.' + specifier.slice(splitAt) : '.';
1112
+ let dir = fromDir;
1113
+ for (let depth = 0; depth < 64; depth++) {
1114
+ const pkgDir = join(dir, 'node_modules', pkgName);
1115
+ const pkgPath = join(pkgDir, 'package.json');
1116
+ if (statOrNull(pkgPath) !== null) {
1117
+ try {
1118
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
1119
+ if (pkg.exports !== undefined) {
1120
+ const target = resolveExports(pkg.exports, subpath);
1121
+ if (target) {
1122
+ const found = probeFile(resolve(pkgDir, target));
1123
+ if (found)
1124
+ return toRealPath(found);
1125
+ }
1126
+ }
1127
+ }
1128
+ catch {
1129
+ // malformed package.json — fall through to the resolvers below
1130
+ }
1131
+ return null;
1132
+ }
1133
+ const parent = dirname(dir);
1134
+ if (parent === dir)
1135
+ break;
1136
+ dir = parent;
1137
+ }
1138
+ return null;
1139
+ }
986
1140
  function probeFile(base) {
987
1141
  const st = statOrNull(base);
988
1142
  if (st && st.isFile())
@@ -993,10 +1147,24 @@ function probeFile(base) {
993
1147
  if (pkgSt && pkgSt.isFile()) {
994
1148
  try {
995
1149
  const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
996
- if (typeof pkg.main === 'string' && pkg.main.length > 0) {
997
- const viaMain = probeFile(resolve(base, pkg.main));
998
- if (viaMain)
999
- return viaMain;
1150
+ // `exports` first: it is what the package author intends and what a
1151
+ // workspace package usually ships. `main`/`module` are the legacy
1152
+ // fallbacks.
1153
+ if (pkg.exports !== undefined) {
1154
+ const viaExports = resolveExports(pkg.exports, '.');
1155
+ if (viaExports) {
1156
+ const found = probeFile(resolve(base, viaExports));
1157
+ if (found)
1158
+ return found;
1159
+ }
1160
+ }
1161
+ for (const field of ['module', 'main']) {
1162
+ const value = pkg[field];
1163
+ if (typeof value === 'string' && value.length > 0) {
1164
+ const found = probeFile(resolve(base, value));
1165
+ if (found)
1166
+ return found;
1167
+ }
1000
1168
  }
1001
1169
  }
1002
1170
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vesk/compiler",
3
- "version": "0.2.50-canary.3",
3
+ "version": "0.2.50-canary.4",
4
4
  "type": "module",
5
5
  "description": "Vesk compiler — lexer, parser, semantic analysis, IR, codegen",
6
6
  "main": "./dist/index.js",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@sveltejs/acorn-typescript": "^1.0.0",
23
- "@vesk/types": "^0.2.50-canary.3",
23
+ "@vesk/types": "^0.2.50-canary.4",
24
24
  "acorn": "^8.15.0",
25
25
  "esrap": "^2.3.0",
26
26
  "is-reference": "^3.0.0",