@c4a/extract-ts 0.5.38 → 0.5.39-beta.1

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.
Files changed (2) hide show
  1. package/index.js +157 -49
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -13497,8 +13497,21 @@ var TEXT_EXTENSIONS = collectExtensions((definition) => definition.cas.encoding
13497
13497
  var UPLOAD_MAX_FILE_SIZE = 5 * 1024 * 1024;
13498
13498
  // src/pathUtils.ts
13499
13499
  import { posix } from "node:path";
13500
- var SOURCE_EXTENSIONS = [".ts", ".tsx"];
13500
+ var SOURCE_EXTENSIONS = [".ts", ".tsx", ".mts", ".cts"];
13501
13501
  var BUILD_EXTENSIONS = [".js", ".jsx", ".mjs", ".cjs"];
13502
+ var DECLARATION_EXTENSIONS = [".d.ts", ".d.mts", ".d.cts"];
13503
+ var KNOWN_CODE_SUFFIXES = [
13504
+ ...DECLARATION_EXTENSIONS,
13505
+ ".esm.js",
13506
+ ".cjs.js",
13507
+ ".umd.js",
13508
+ ".amd.js",
13509
+ ".iife.js",
13510
+ ".system.js",
13511
+ ".min.js",
13512
+ ...SOURCE_EXTENSIONS,
13513
+ ...BUILD_EXTENSIONS
13514
+ ];
13502
13515
  var BUILD_OUTPUT_ROOTS = new Set(["dist", "lib", "build", "output", "out"]);
13503
13516
  var BUILD_FORMAT_DIRS = new Set([
13504
13517
  "es",
@@ -13522,8 +13535,15 @@ var normalizeRelativePath = (value) => {
13522
13535
  const normalized = posix.normalize(value.replace(/\\/g, "/"));
13523
13536
  return normalized.replace(/^\.\//, "");
13524
13537
  };
13538
+ var stripResourceQuery = (value) => value.split(/[?#]/u)[0] ?? value;
13539
+ var hasUnsupportedExplicitExtension = (value) => {
13540
+ const basename = posix.basename(value);
13541
+ if (!basename.includes("."))
13542
+ return false;
13543
+ return !KNOWN_CODE_SUFFIXES.some((extension) => value.endsWith(extension));
13544
+ };
13525
13545
  var removeKnownExtension = (value) => {
13526
- for (const extension of [...SOURCE_EXTENSIONS, ...BUILD_EXTENSIONS]) {
13546
+ for (const extension of KNOWN_CODE_SUFFIXES) {
13527
13547
  if (value.endsWith(extension)) {
13528
13548
  return value.slice(0, -extension.length);
13529
13549
  }
@@ -13547,14 +13567,14 @@ var buildToSourcePaths = (withoutExtension) => {
13547
13567
  results.push(`src/${fullRest}`);
13548
13568
  return unique(results);
13549
13569
  };
13550
- var createCandidatePaths = (value) => {
13551
- const normalized = normalizeRelativePath(value);
13570
+ var createCandidatePaths = (value, options = {}) => {
13571
+ const normalized = normalizeRelativePath(stripResourceQuery(value));
13572
+ if (hasUnsupportedExplicitExtension(normalized))
13573
+ return [];
13552
13574
  const withoutExtension = removeKnownExtension(normalized);
13553
- const bases = unique([
13554
- normalized,
13555
- withoutExtension,
13556
- ...buildToSourcePaths(withoutExtension)
13557
- ]);
13575
+ const mappedSourcePaths = buildToSourcePaths(withoutExtension);
13576
+ const directFirst = SOURCE_EXTENSIONS.some((extension) => normalized.endsWith(extension));
13577
+ const bases = directFirst ? unique([normalized, withoutExtension, ...mappedSourcePaths]) : unique([...mappedSourcePaths, normalized, withoutExtension]);
13558
13578
  const candidates = new Set;
13559
13579
  for (const base of bases) {
13560
13580
  if (SOURCE_EXTENSIONS.some((ext) => base.endsWith(ext))) {
@@ -13564,19 +13584,22 @@ var createCandidatePaths = (value) => {
13564
13584
  candidates.add(`${base}${extension}`);
13565
13585
  candidates.add(posix.join(base, `index${extension}`));
13566
13586
  }
13567
- if (normalized.endsWith(".js")) {
13568
- candidates.add(normalized.slice(0, -3) + ".ts");
13569
- candidates.add(normalized.slice(0, -3) + ".tsx");
13587
+ if (BUILD_EXTENSIONS.some((extension) => normalized.endsWith(extension))) {
13588
+ for (const extension of SOURCE_EXTENSIONS) {
13589
+ candidates.add(removeKnownExtension(normalized) + extension);
13590
+ }
13570
13591
  }
13571
13592
  }
13572
- for (const extension of SOURCE_EXTENSIONS) {
13573
- candidates.add(`src/index${extension}`);
13593
+ if (options.allowIndexFallback ?? true) {
13594
+ for (const extension of SOURCE_EXTENSIONS) {
13595
+ candidates.add(`src/index${extension}`);
13596
+ }
13574
13597
  }
13575
13598
  return [...candidates].map(normalizeRelativePath);
13576
13599
  };
13577
13600
  var isRelativeModuleSpecifier = (value) => value.startsWith("./") || value.startsWith("../");
13578
- var resolveEntrySourcePath = async (packageDir, targetPath, fs2) => {
13579
- for (const candidate of createCandidatePaths(targetPath)) {
13601
+ var resolveEntrySourcePath = async (packageDir, targetPath, fs2, options = {}) => {
13602
+ for (const candidate of createCandidatePaths(targetPath, options)) {
13580
13603
  const fullPath = packageDir ? posix.join(packageDir, candidate) : candidate;
13581
13604
  if (await fs2.exists(fullPath)) {
13582
13605
  return fullPath;
@@ -13598,40 +13621,58 @@ var resolveImportSourcePath = async (fromFile, specifier, fs2) => {
13598
13621
  };
13599
13622
 
13600
13623
  // src/entryDetector.ts
13624
+ var CONDITION_PRIORITY = [
13625
+ "types",
13626
+ "typings",
13627
+ "import",
13628
+ "require",
13629
+ "module",
13630
+ "browser",
13631
+ "node",
13632
+ "default",
13633
+ "main"
13634
+ ];
13601
13635
  var asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : {};
13602
13636
  var asStringArray = (value) => Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
13603
13637
  var readString = (value) => typeof value === "string" && value.trim() ? value : null;
13604
- var resolveConditionalTarget = (value, mainFallback) => {
13638
+ var unique2 = (items) => [...new Set(items)];
13639
+ var resolveConditionalTargets = (value, mainFallback) => {
13605
13640
  const direct = readString(value);
13606
13641
  if (direct)
13607
- return direct;
13642
+ return [direct];
13643
+ if (Array.isArray(value)) {
13644
+ return unique2(value.flatMap((item) => resolveConditionalTargets(item, null)));
13645
+ }
13608
13646
  const record = asRecord(value);
13609
- for (const key of ["import", "default", "main"]) {
13610
- const candidate = readString(record[key]);
13611
- if (candidate)
13612
- return candidate;
13647
+ const targets = [];
13648
+ for (const key of CONDITION_PRIORITY) {
13649
+ if (!(key in record))
13650
+ continue;
13651
+ targets.push(...resolveConditionalTargets(record[key], null));
13613
13652
  }
13614
- return mainFallback;
13653
+ if (mainFallback)
13654
+ targets.push(mainFallback);
13655
+ return unique2(targets);
13615
13656
  };
13616
13657
  var collectExportTargets = (pkg) => {
13617
13658
  const exportsField = pkg.exports;
13618
13659
  const mainFallback = readString(pkg.main);
13619
13660
  if (!exportsField) {
13620
- return mainFallback ? [{ subpath: ".", targetPath: mainFallback, type: "library" }] : [];
13661
+ return mainFallback ? [{ subpath: ".", targetPaths: [mainFallback], type: "library" }] : [];
13621
13662
  }
13622
13663
  const exportsRecord = asRecord(exportsField);
13623
13664
  const exportKeys = Object.keys(exportsRecord);
13624
13665
  const hasSubpathMap = exportKeys.some((key) => key.startsWith("."));
13625
- const direct = !hasSubpathMap ? resolveConditionalTarget(exportsField, mainFallback) : null;
13626
- if (direct) {
13627
- return [{ subpath: ".", targetPath: direct, type: "library" }];
13666
+ const direct = !hasSubpathMap ? resolveConditionalTargets(exportsField, mainFallback) : [];
13667
+ if (direct.length > 0) {
13668
+ return [{ subpath: ".", targetPaths: direct, type: "library" }];
13628
13669
  }
13629
13670
  const targets = [];
13630
13671
  for (const [subpath, rawValue] of Object.entries(exportsRecord)) {
13631
- const targetPath = resolveConditionalTarget(rawValue, mainFallback);
13632
- if (!targetPath)
13672
+ const targetPaths = resolveConditionalTargets(rawValue, null);
13673
+ if (targetPaths.length === 0)
13633
13674
  continue;
13634
- targets.push({ subpath, targetPath, type: "library" });
13675
+ targets.push({ subpath, targetPaths, type: "library" });
13635
13676
  }
13636
13677
  return targets;
13637
13678
  };
@@ -13639,11 +13680,11 @@ var collectBinTargets = (pkg) => {
13639
13680
  const binField = pkg.bin;
13640
13681
  const direct = readString(binField);
13641
13682
  if (direct) {
13642
- return [{ subpath: "./bin", targetPath: direct, type: "cli" }];
13683
+ return [{ subpath: "./bin", targetPaths: [direct], type: "cli" }];
13643
13684
  }
13644
13685
  return Object.entries(asRecord(binField)).flatMap(([name, value]) => {
13645
13686
  const targetPath = readString(value);
13646
- return targetPath ? [{ subpath: `./bin/${name}`, targetPath, type: "cli" }] : [];
13687
+ return targetPath ? [{ subpath: `./bin/${name}`, targetPaths: [targetPath], type: "cli" }] : [];
13647
13688
  });
13648
13689
  };
13649
13690
  var detectPackageKind = (pkg) => {
@@ -13667,7 +13708,11 @@ var expandWorkspacePattern = async (rootDir, pattern, fs2) => {
13667
13708
  return [`${rootDir}/${pattern}`.replace(/^\.\//, "")];
13668
13709
  };
13669
13710
  var detectSubPackages = async (rootDir, pkg, fs2) => {
13670
- const workspaces = asStringArray(pkg.workspaces);
13711
+ const workspaceConfig = asRecord(pkg.workspaces);
13712
+ const workspaces = [
13713
+ ...asStringArray(pkg.workspaces),
13714
+ ...asStringArray(workspaceConfig.packages)
13715
+ ];
13671
13716
  if (workspaces.length === 0)
13672
13717
  return;
13673
13718
  const subPackages = [];
@@ -13697,14 +13742,18 @@ var detectEntries = async (manifest, fs2) => {
13697
13742
  const entryTargets = [...collectExportTargets(pkg), ...collectBinTargets(pkg)];
13698
13743
  const entries = [];
13699
13744
  for (const entryTarget of entryTargets) {
13700
- const resolvedPath = await resolveEntrySourcePath(packageDir, entryTarget.targetPath, fs2);
13701
- if (!resolvedPath)
13702
- continue;
13703
- entries.push({
13704
- path: resolvedPath,
13705
- subpath: entryTarget.subpath,
13706
- type: entryTarget.type
13707
- });
13745
+ const allowIndexFallback = entryTarget.subpath === ".";
13746
+ for (const targetPath of entryTarget.targetPaths) {
13747
+ const resolvedPath = await resolveEntrySourcePath(packageDir, targetPath, fs2, { allowIndexFallback });
13748
+ if (!resolvedPath)
13749
+ continue;
13750
+ entries.push({
13751
+ path: resolvedPath,
13752
+ subpath: entryTarget.subpath,
13753
+ type: entryTarget.type
13754
+ });
13755
+ break;
13756
+ }
13708
13757
  }
13709
13758
  const subPackages = await detectSubPackages(packageDir || ".", pkg, fs2);
13710
13759
  return {
@@ -14080,6 +14129,43 @@ var parseExportSpecifier = (node2) => {
14080
14129
  const exportedName = identifiers[1] ?? localName;
14081
14130
  return { localName, exportedName };
14082
14131
  };
14132
+ var parseImportSpecifier = (node2) => {
14133
+ const identifiers = node2.namedChildren.filter((child) => child.type === "identifier" || child.type === "type_identifier").map((child) => child.text);
14134
+ if (identifiers.length === 0)
14135
+ return null;
14136
+ const importedName = identifiers[0];
14137
+ const localName = identifiers[1] ?? importedName;
14138
+ return { importedName, localName };
14139
+ };
14140
+ var collectImportBindings = (root) => {
14141
+ const bindings = new Map;
14142
+ for (const node2 of root.namedChildren) {
14143
+ if (node2.type !== "import_statement")
14144
+ continue;
14145
+ const stringNode = node2.namedChildren.find((child) => child.type === "string");
14146
+ if (!stringNode)
14147
+ continue;
14148
+ const source2 = stripQuotes(stringNode.text);
14149
+ if (!isRelativeModuleSpecifier(source2))
14150
+ continue;
14151
+ const clause = node2.namedChildren.find((child) => child.type === "import_clause");
14152
+ for (const part of clause?.namedChildren ?? []) {
14153
+ if (part.type === "identifier") {
14154
+ bindings.set(part.text, { source: source2, importedName: "default" });
14155
+ continue;
14156
+ }
14157
+ if (part.type !== "named_imports")
14158
+ continue;
14159
+ for (const specifier of part.namedChildren.filter((child) => child.type === "import_specifier")) {
14160
+ const parsed = parseImportSpecifier(specifier);
14161
+ if (parsed) {
14162
+ bindings.set(parsed.localName, { source: source2, importedName: parsed.importedName });
14163
+ }
14164
+ }
14165
+ }
14166
+ }
14167
+ return bindings;
14168
+ };
14083
14169
  var uniqueExports = (exportsList) => {
14084
14170
  const seen = new Set;
14085
14171
  return exportsList.filter((item) => {
@@ -14090,6 +14176,20 @@ var uniqueExports = (exportsList) => {
14090
14176
  return true;
14091
14177
  });
14092
14178
  };
14179
+ var traceImportedBinding = async (filePath, binding, exportedName, fs2, state) => {
14180
+ const targetPath = await resolveImportSourcePath(filePath, binding.source, fs2);
14181
+ if (!targetPath)
14182
+ return [];
14183
+ const traced = await traceFile(targetPath, fs2, state);
14184
+ const match = traced.find((item) => item.exportedName === binding.importedName || binding.importedName === "default" && item.exportedName === item.localName);
14185
+ if (!match)
14186
+ return [];
14187
+ return [{
14188
+ exportedName,
14189
+ localName: match.localName,
14190
+ declarationFile: match.declarationFile
14191
+ }];
14192
+ };
14093
14193
  var traceFile = async (filePath, fs2, state) => {
14094
14194
  const cached = state.cache.get(filePath);
14095
14195
  if (cached)
@@ -14105,6 +14205,7 @@ var traceFile = async (filePath, fs2, state) => {
14105
14205
  return [];
14106
14206
  const root = tree.rootNode;
14107
14207
  const localDeclarations = collectLocalDeclarations(root);
14208
+ const importBindings = collectImportBindings(root);
14108
14209
  const exportsList = [];
14109
14210
  for (const node2 of root.namedChildren) {
14110
14211
  if (node2.type !== "export_statement")
@@ -14141,13 +14242,20 @@ var traceFile = async (filePath, fs2, state) => {
14141
14242
  if (!stringNode) {
14142
14243
  for (const specifier of exportClause.namedChildren.filter((child) => child.type === "export_specifier")) {
14143
14244
  const parsed = parseExportSpecifier(specifier);
14144
- if (!parsed || !localDeclarations.has(parsed.localName))
14245
+ if (!parsed)
14145
14246
  continue;
14146
- exportsList.push({
14147
- exportedName: parsed.exportedName,
14148
- localName: parsed.localName,
14149
- declarationFile: filePath
14150
- });
14247
+ if (localDeclarations.has(parsed.localName)) {
14248
+ exportsList.push({
14249
+ exportedName: parsed.exportedName,
14250
+ localName: parsed.localName,
14251
+ declarationFile: filePath
14252
+ });
14253
+ continue;
14254
+ }
14255
+ const importBinding = importBindings.get(parsed.localName);
14256
+ if (importBinding) {
14257
+ exportsList.push(...await traceImportedBinding(filePath, importBinding, parsed.exportedName, fs2, state));
14258
+ }
14151
14259
  }
14152
14260
  continue;
14153
14261
  }
@@ -14539,7 +14647,7 @@ var analyzeDeclaration = (node2, filePath, declarations, importBindings, relatio
14539
14647
  });
14540
14648
  }
14541
14649
  };
14542
- var collectImportBindings = (root, relations) => {
14650
+ var collectImportBindings2 = (root, relations) => {
14543
14651
  const bindings = new Map;
14544
14652
  for (const node2 of root.namedChildren.filter((child) => child.type === "import_statement")) {
14545
14653
  const specifierNode = node2.namedChildren.find((child) => child.type === "string");
@@ -14598,7 +14706,7 @@ var analyzeFile = async (filePath, fs2) => {
14598
14706
  }
14599
14707
  const root = tree.rootNode;
14600
14708
  const relations = [];
14601
- const importBindings = collectImportBindings(root, relations);
14709
+ const importBindings = collectImportBindings2(root, relations);
14602
14710
  const declarations = new Map;
14603
14711
  for (const relation of relations) {
14604
14712
  if (relation.from === "") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@c4a/extract-ts",
3
- "version": "0.5.38",
3
+ "version": "0.5.39-beta.1",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "web-tree-sitter": "^0.20.8"