ts-swc-transform 2.8.0 → 2.8.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.
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Unified ESM resolver
3
+ *
4
+ * Handles both:
5
+ * - Package exports: import x from 'lodash' → uses exports field
6
+ * - Subpath imports: import x from '#internal' → uses imports field
7
+ *
8
+ * Uses resolve.exports.resolve() which automatically detects the specifier type.
9
+ * Only loaded on Node >= 12.2 where module.createRequire exists.
10
+ */ import fs from 'fs';
11
+ import Module from 'module';
12
+ import path from 'path';
13
+ let _resolveExports = null;
14
+ function getResolveExports() {
15
+ if (_resolveExports === null) {
16
+ try {
17
+ const _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;
18
+ _resolveExports = _require('resolve.exports');
19
+ } catch (_) {
20
+ _resolveExports = null;
21
+ }
22
+ }
23
+ return _resolveExports;
24
+ }
25
+ /**
26
+ * Parse a specifier into package name and subpath
27
+ * "lodash" → { pkgName: "lodash", subpath: "." }
28
+ * "lodash/get" → { pkgName: "lodash", subpath: "./get" }
29
+ * "@scope/pkg" → { pkgName: "@scope/pkg", subpath: "." }
30
+ * "@scope/pkg/foo" → { pkgName: "@scope/pkg", subpath: "./foo" }
31
+ */ function parseSpecifier(specifier) {
32
+ const parts = specifier.split('/');
33
+ const pkgName = specifier[0] === '@' ? parts.slice(0, 2).join('/') : parts[0];
34
+ const remainder = specifier.slice(pkgName.length);
35
+ const subpath = remainder ? `.${remainder}` : '.';
36
+ return {
37
+ pkgName,
38
+ subpath
39
+ };
40
+ }
41
+ /**
42
+ * Find package.json in node_modules for external packages
43
+ */ function findPackageInNodeModules(pkgName, basedir) {
44
+ let dir = basedir;
45
+ const root = path.parse(dir).root;
46
+ while(dir !== root){
47
+ const pkgDir = path.join(dir, 'node_modules', pkgName);
48
+ const pkgJsonPath = path.join(pkgDir, 'package.json');
49
+ try {
50
+ if (fs.existsSync(pkgJsonPath)) {
51
+ const content = fs.readFileSync(pkgJsonPath, 'utf8');
52
+ return {
53
+ dir: pkgDir,
54
+ json: JSON.parse(content)
55
+ };
56
+ }
57
+ } catch (_) {
58
+ // Ignore filesystem errors
59
+ }
60
+ const parent = path.dirname(dir);
61
+ if (parent === dir) break;
62
+ dir = parent;
63
+ }
64
+ return null;
65
+ }
66
+ /**
67
+ * Find the containing package.json by walking up from a file path
68
+ * Used for # subpath imports which are scoped to the containing package
69
+ */ function findContainingPackage(filePath) {
70
+ let dir = path.dirname(filePath);
71
+ const root = path.parse(dir).root;
72
+ while(dir !== root){
73
+ const pkgJsonPath = path.join(dir, 'package.json');
74
+ try {
75
+ if (fs.existsSync(pkgJsonPath)) {
76
+ const content = fs.readFileSync(pkgJsonPath, 'utf8');
77
+ return {
78
+ dir,
79
+ json: JSON.parse(content)
80
+ };
81
+ }
82
+ } catch (_) {
83
+ // Ignore filesystem errors
84
+ }
85
+ const parent = path.dirname(dir);
86
+ if (parent === dir) break;
87
+ dir = parent;
88
+ }
89
+ return null;
90
+ }
91
+ /**
92
+ * Resolve an ESM specifier to an absolute file path
93
+ *
94
+ * @param specifier - The import specifier (e.g., 'lodash', 'lodash/get', '#internal')
95
+ * @param parentPath - The file path of the importing module
96
+ * @param conditions - Export conditions (defaults to ['node', 'import'])
97
+ * @returns The resolved absolute file path, or null if not resolvable
98
+ */ export default function resolveESM(specifier, parentPath, conditions = [
99
+ 'node',
100
+ 'import'
101
+ ]) {
102
+ const resolveExportsMod = getResolveExports();
103
+ if (!resolveExportsMod) {
104
+ return null;
105
+ }
106
+ const { resolve: resolveFn, legacy } = resolveExportsMod;
107
+ // Determine how to find the package.json based on specifier type
108
+ let pkg;
109
+ let subpath;
110
+ if (specifier.startsWith('#')) {
111
+ // Subpath import - find containing package
112
+ pkg = findContainingPackage(parentPath);
113
+ subpath = specifier; // resolve.exports expects the full #specifier
114
+ } else {
115
+ // External package - find in node_modules
116
+ const { pkgName, subpath: parsedSubpath } = parseSpecifier(specifier);
117
+ pkg = findPackageInNodeModules(pkgName, parentPath);
118
+ subpath = parsedSubpath;
119
+ }
120
+ if (!pkg) {
121
+ return null;
122
+ }
123
+ // Use resolve.exports.resolve() which handles both exports and imports
124
+ try {
125
+ const resolved = resolveFn(pkg.json, subpath, {
126
+ conditions
127
+ });
128
+ if (resolved === null || resolved === void 0 ? void 0 : resolved[0]) {
129
+ return path.join(pkg.dir, resolved[0]);
130
+ }
131
+ } catch (_) {
132
+ // Resolution failed, try legacy
133
+ }
134
+ // Try legacy main/module fields for non-# imports
135
+ if (!specifier.startsWith('#')) {
136
+ try {
137
+ const legacyMain = legacy(pkg.json);
138
+ if (legacyMain) {
139
+ let mainPath;
140
+ if (typeof legacyMain === 'string') {
141
+ mainPath = legacyMain;
142
+ } else if (Array.isArray(legacyMain)) {
143
+ mainPath = legacyMain[0];
144
+ }
145
+ if (mainPath) {
146
+ return path.join(pkg.dir, mainPath);
147
+ }
148
+ }
149
+ } catch (_) {
150
+ // Legacy parsing failed
151
+ }
152
+ // Last resort: try index.js
153
+ const indexPath = path.join(pkg.dir, 'index.js');
154
+ try {
155
+ if (fs.existsSync(indexPath)) {
156
+ return indexPath;
157
+ }
158
+ } catch (_) {
159
+ // Ignore
160
+ }
161
+ }
162
+ return null;
163
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/resolveESM.ts"],"sourcesContent":["/**\n * Unified ESM resolver\n *\n * Handles both:\n * - Package exports: import x from 'lodash' → uses exports field\n * - Subpath imports: import x from '#internal' → uses imports field\n *\n * Uses resolve.exports.resolve() which automatically detects the specifier type.\n * Only loaded on Node >= 12.2 where module.createRequire exists.\n */\n\nimport fs from 'fs';\nimport Module from 'module';\nimport path from 'path';\n\n// Lazy-load resolve.exports\ntype ResolveExportsModule = typeof import('resolve.exports');\nlet _resolveExports: ResolveExportsModule | null = null;\n\nfunction getResolveExports(): ResolveExportsModule | null {\n if (_resolveExports === null) {\n try {\n const _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n _resolveExports = _require('resolve.exports') as ResolveExportsModule;\n } catch (_) {\n _resolveExports = null;\n }\n }\n return _resolveExports;\n}\n\n/**\n * Parse a specifier into package name and subpath\n * \"lodash\" → { pkgName: \"lodash\", subpath: \".\" }\n * \"lodash/get\" → { pkgName: \"lodash\", subpath: \"./get\" }\n * \"@scope/pkg\" → { pkgName: \"@scope/pkg\", subpath: \".\" }\n * \"@scope/pkg/foo\" → { pkgName: \"@scope/pkg\", subpath: \"./foo\" }\n */\nfunction parseSpecifier(specifier: string): { pkgName: string; subpath: string } {\n const parts = specifier.split('/');\n const pkgName = specifier[0] === '@' ? parts.slice(0, 2).join('/') : parts[0];\n const remainder = specifier.slice(pkgName.length);\n const subpath = remainder ? `.${remainder}` : '.';\n return { pkgName, subpath };\n}\n\n/**\n * Find package.json in node_modules for external packages\n */\nfunction findPackageInNodeModules(pkgName: string, basedir: string): { dir: string; json: Record<string, unknown> } | null {\n let dir = basedir;\n const root = path.parse(dir).root;\n\n while (dir !== root) {\n const pkgDir = path.join(dir, 'node_modules', pkgName);\n const pkgJsonPath = path.join(pkgDir, 'package.json');\n try {\n if (fs.existsSync(pkgJsonPath)) {\n const content = fs.readFileSync(pkgJsonPath, 'utf8');\n return { dir: pkgDir, json: JSON.parse(content) };\n }\n } catch (_) {\n // Ignore filesystem errors\n }\n const parent = path.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n\n return null;\n}\n\n/**\n * Find the containing package.json by walking up from a file path\n * Used for # subpath imports which are scoped to the containing package\n */\nfunction findContainingPackage(filePath: string): { dir: string; json: Record<string, unknown> } | null {\n let dir = path.dirname(filePath);\n const root = path.parse(dir).root;\n\n while (dir !== root) {\n const pkgJsonPath = path.join(dir, 'package.json');\n try {\n if (fs.existsSync(pkgJsonPath)) {\n const content = fs.readFileSync(pkgJsonPath, 'utf8');\n return { dir, json: JSON.parse(content) };\n }\n } catch (_) {\n // Ignore filesystem errors\n }\n const parent = path.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n\n return null;\n}\n\n/**\n * Resolve an ESM specifier to an absolute file path\n *\n * @param specifier - The import specifier (e.g., 'lodash', 'lodash/get', '#internal')\n * @param parentPath - The file path of the importing module\n * @param conditions - Export conditions (defaults to ['node', 'import'])\n * @returns The resolved absolute file path, or null if not resolvable\n */\nexport default function resolveESM(specifier: string, parentPath: string, conditions: string[] = ['node', 'import']): string | null {\n const resolveExportsMod = getResolveExports();\n if (!resolveExportsMod) {\n return null;\n }\n\n const { resolve: resolveFn, legacy } = resolveExportsMod;\n\n // Determine how to find the package.json based on specifier type\n let pkg: { dir: string; json: Record<string, unknown> } | null;\n let subpath: string;\n\n if (specifier.startsWith('#')) {\n // Subpath import - find containing package\n pkg = findContainingPackage(parentPath);\n subpath = specifier; // resolve.exports expects the full #specifier\n } else {\n // External package - find in node_modules\n const { pkgName, subpath: parsedSubpath } = parseSpecifier(specifier);\n pkg = findPackageInNodeModules(pkgName, parentPath);\n subpath = parsedSubpath;\n }\n\n if (!pkg) {\n return null;\n }\n\n // Use resolve.exports.resolve() which handles both exports and imports\n try {\n const resolved = resolveFn(pkg.json, subpath, { conditions });\n if (resolved?.[0]) {\n return path.join(pkg.dir, resolved[0]);\n }\n } catch (_) {\n // Resolution failed, try legacy\n }\n\n // Try legacy main/module fields for non-# imports\n if (!specifier.startsWith('#')) {\n try {\n const legacyMain = legacy(pkg.json);\n if (legacyMain) {\n let mainPath: string | undefined;\n if (typeof legacyMain === 'string') {\n mainPath = legacyMain;\n } else if (Array.isArray(legacyMain)) {\n mainPath = legacyMain[0];\n }\n if (mainPath) {\n return path.join(pkg.dir, mainPath);\n }\n }\n } catch (_) {\n // Legacy parsing failed\n }\n\n // Last resort: try index.js\n const indexPath = path.join(pkg.dir, 'index.js');\n try {\n if (fs.existsSync(indexPath)) {\n return indexPath;\n }\n } catch (_) {\n // Ignore\n }\n }\n\n return null;\n}\n"],"names":["fs","Module","path","_resolveExports","getResolveExports","_require","require","createRequire","url","_","parseSpecifier","specifier","parts","split","pkgName","slice","join","remainder","length","subpath","findPackageInNodeModules","basedir","dir","root","parse","pkgDir","pkgJsonPath","existsSync","content","readFileSync","json","JSON","parent","dirname","findContainingPackage","filePath","resolveESM","parentPath","conditions","resolveExportsMod","resolve","resolveFn","legacy","pkg","startsWith","parsedSubpath","resolved","legacyMain","mainPath","Array","isArray","indexPath"],"mappings":"AAAA;;;;;;;;;CASC,GAED,OAAOA,QAAQ,KAAK;AACpB,OAAOC,YAAY,SAAS;AAC5B,OAAOC,UAAU,OAAO;AAIxB,IAAIC,kBAA+C;AAEnD,SAASC;IACP,IAAID,oBAAoB,MAAM;QAC5B,IAAI;YACF,MAAME,WAAW,OAAOC,YAAY,cAAcL,OAAOM,aAAa,CAAC,YAAYC,GAAG,IAAIF;YAC1FH,kBAAkBE,SAAS;QAC7B,EAAE,OAAOI,GAAG;YACVN,kBAAkB;QACpB;IACF;IACA,OAAOA;AACT;AAEA;;;;;;CAMC,GACD,SAASO,eAAeC,SAAiB;IACvC,MAAMC,QAAQD,UAAUE,KAAK,CAAC;IAC9B,MAAMC,UAAUH,SAAS,CAAC,EAAE,KAAK,MAAMC,MAAMG,KAAK,CAAC,GAAG,GAAGC,IAAI,CAAC,OAAOJ,KAAK,CAAC,EAAE;IAC7E,MAAMK,YAAYN,UAAUI,KAAK,CAACD,QAAQI,MAAM;IAChD,MAAMC,UAAUF,YAAY,CAAC,CAAC,EAAEA,WAAW,GAAG;IAC9C,OAAO;QAAEH;QAASK;IAAQ;AAC5B;AAEA;;CAEC,GACD,SAASC,yBAAyBN,OAAe,EAAEO,OAAe;IAChE,IAAIC,MAAMD;IACV,MAAME,OAAOrB,KAAKsB,KAAK,CAACF,KAAKC,IAAI;IAEjC,MAAOD,QAAQC,KAAM;QACnB,MAAME,SAASvB,KAAKc,IAAI,CAACM,KAAK,gBAAgBR;QAC9C,MAAMY,cAAcxB,KAAKc,IAAI,CAACS,QAAQ;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BlB,UAAUR,WAAW,8CAA8C;IACrE,OAAO;QACL,0CAA0C;QAC1C,MAAM,EAAEG,OAAO,EAAEK,SAAS0B,aAAa,EAAE,GAAGnC,eAAeC;QAC3DgC,MAAMvB,yBAAyBN,SAASuB;QACxClB,UAAU0B;IACZ;IAEA,IAAI,CAACF,KAAK;QACR,OAAO;IACT;IAEA,uEAAuE;IACvE,IAAI;QACF,MAAMG,WAAWL,UAAUE,IAAIb,IAAI,EAAEX,SAAS;YAAEmB;QAAW;QAC3D,IAAIQ,qBAAAA,+BAAAA,QAAU,CAAC,EAAE,EAAE;YACjB,OAAO5C,KAAKc,IAAI,CAAC2B,IAAIrB,GAAG,EAAEwB,QAAQ,CAAC,EAAE;QACvC;IACF,EAAE,OAAOrC,GAAG;IACV,gCAAgC;IAClC;IAEA,kDAAkD;IAClD,IAAI,CAACE,UAAUiC,UAAU,CAAC,MAAM;QAC9B,IAAI;YACF,MAAMG,aAAaL,OAAOC,IAAIb,IAAI;YAClC,IAAIiB,YAAY;gBACd,IAAIC;gBACJ,IAAI,OAAOD,eAAe,UAAU;oBAClCC,WAAWD;gBACb,OAAO,IAAIE,MAAMC,OAAO,CAACH,aAAa;oBACpCC,WAAWD,UAAU,CAAC,EAAE;gBAC1B;gBACA,IAAIC,UAAU;oBACZ,OAAO9C,KAAKc,IAAI,CAAC2B,IAAIrB,GAAG,EAAE0B;gBAC5B;YACF;QACF,EAAE,OAAOvC,GAAG;QACV,wBAAwB;QAC1B;QAEA,4BAA4B;QAC5B,MAAM0C,YAAYjD,KAAKc,IAAI,CAAC2B,IAAIrB,GAAG,EAAE;QACrC,IAAI;YACF,IAAItB,GAAG2B,UAAU,CAACwB,YAAY;gBAC5B,OAAOA;YACT;QACF,EAAE,OAAO1C,GAAG;QACV,SAAS;QACX;IACF;IAEA,OAAO;AACT"}
@@ -1,46 +1,51 @@
1
1
  import isAbsolute from 'is-absolute';
2
2
  import module from 'module';
3
3
  import path from 'path';
4
- import * as resolve from 'resolve';
5
4
  import url from 'url';
6
5
  import { stringStartsWith } from './compat.js';
7
6
  import { moduleRegEx } from './constants.js';
8
- import resolveWithExports from './lib/resolve-with-exports.js';
7
+ import resolveCJS from './lib/resolveCJS.js';
8
+ import resolveESM from './lib/resolveESM.js';
9
9
  import * as urlPolyfills from './lib/urlFileUrl.js';
10
- var _resolve_default;
11
- const resolveSync = ((_resolve_default = resolve.default) !== null && _resolve_default !== void 0 ? _resolve_default : resolve).sync;
12
10
  const useCJS = !module.createRequire;
13
11
  const fileURLToPath = url.fileURLToPath || urlPolyfills.fileURLToPath;
14
12
  function getParentPath(context) {
15
13
  if (context.parentPath) return path.dirname(context.parentPath);
16
14
  return context.parentURL ? path.dirname(toPath(context.parentURL)) : process.cwd();
17
15
  }
16
+ function getParentFilePath(context) {
17
+ if (context === null || context === void 0 ? void 0 : context.parentPath) return context.parentPath;
18
+ if (context === null || context === void 0 ? void 0 : context.parentURL) return fileURLToPath(context.parentURL);
19
+ return path.join(process.cwd(), 'index.js');
20
+ }
18
21
  export default function toPath(specifier, context) {
22
+ // Handle file:// URLs
19
23
  if (stringStartsWith(specifier, 'file:')) return fileURLToPath(specifier);
24
+ // Handle absolute paths
20
25
  if (isAbsolute(specifier)) return specifier;
26
+ // Handle relative paths
21
27
  if (specifier[0] === '.') {
22
28
  const parentPath = context ? getParentPath(context) : process.cwd();
23
29
  return path.join(parentPath, specifier);
24
30
  }
25
- if (moduleRegEx.test(specifier)) {
26
- const parentPath = context ? getParentPath(context) : process.cwd();
27
- if (!useCJS) {
28
- try {
29
- const entryPath = resolveWithExports(specifier, parentPath);
30
- if (entryPath) return entryPath;
31
- } catch (_) {
32
- /* it may fail due to commonjs edge cases */ }
31
+ // Handle module specifiers (bare specifiers and # imports)
32
+ // moduleRegEx matches: bare specifiers like 'lodash', '@scope/pkg'
33
+ // specifier[0] === '#' matches: subpath imports like '#internal'
34
+ if (moduleRegEx.test(specifier) || specifier[0] === '#') {
35
+ const parentFilePath = getParentFilePath(context);
36
+ const parentDir = path.dirname(parentFilePath);
37
+ if (useCJS) {
38
+ // CJS: use resolve package (does not support # imports)
39
+ if (specifier[0] === '#') {
40
+ throw new Error(`Cannot find module '${specifier}' from '${parentDir}' (subpath imports not supported in CJS mode)`);
41
+ }
42
+ return resolveCJS(specifier, parentDir);
33
43
  }
34
- const entryPath = resolveSync(specifier, {
35
- basedir: parentPath,
36
- extensions: [
37
- '.js',
38
- '.json',
39
- '.node',
40
- '.mjs'
41
- ]
42
- });
44
+ // ESM: use unified resolver that handles both exports and imports
45
+ const entryPath = resolveESM(specifier, parentFilePath);
43
46
  if (entryPath) return entryPath;
47
+ // If ESM resolver failed, throw meaningful error
48
+ throw new Error(`Cannot find module '${specifier}' from '${parentDir}'`);
44
49
  }
45
50
  return specifier;
46
51
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/toPath.ts"],"sourcesContent":["import isAbsolute from 'is-absolute';\nimport module from 'module';\nimport path from 'path';\nimport * as resolve from 'resolve';\nimport url from 'url';\n\nimport { stringStartsWith } from './compat.ts';\nimport { moduleRegEx } from './constants.ts';\nimport resolveWithExports from './lib/resolve-with-exports.ts';\nimport * as urlPolyfills from './lib/urlFileUrl.ts';\nimport type { Context } from './types.ts';\n\nconst resolveSync = (resolve.default ?? resolve).sync;\n\nconst useCJS = !module.createRequire;\nconst fileURLToPath = url.fileURLToPath || urlPolyfills.fileURLToPath;\n\nfunction getParentPath(context: Context): string {\n if (context.parentPath) return path.dirname(context.parentPath);\n return context.parentURL ? path.dirname(toPath(context.parentURL)) : process.cwd();\n}\n\nexport default function toPath(specifier: string, context?: Context): string {\n if (stringStartsWith(specifier, 'file:')) return fileURLToPath(specifier);\n if (isAbsolute(specifier)) return specifier;\n if (specifier[0] === '.') {\n const parentPath = context ? getParentPath(context) : process.cwd();\n return path.join(parentPath, specifier);\n }\n if (moduleRegEx.test(specifier)) {\n const parentPath = context ? getParentPath(context) : process.cwd();\n if (!useCJS) {\n try {\n const entryPath = resolveWithExports(specifier, parentPath);\n if (entryPath) return entryPath;\n } catch (_) {\n /* it may fail due to commonjs edge cases */\n }\n }\n const entryPath = resolveSync(specifier, {\n basedir: parentPath,\n extensions: ['.js', '.json', '.node', '.mjs'],\n });\n if (entryPath) return entryPath;\n }\n\n return specifier;\n}\n"],"names":["isAbsolute","module","path","resolve","url","stringStartsWith","moduleRegEx","resolveWithExports","urlPolyfills","resolveSync","default","sync","useCJS","createRequire","fileURLToPath","getParentPath","context","parentPath","dirname","parentURL","toPath","process","cwd","specifier","join","test","entryPath","_","basedir","extensions"],"mappings":"AAAA,OAAOA,gBAAgB,cAAc;AACrC,OAAOC,YAAY,SAAS;AAC5B,OAAOC,UAAU,OAAO;AACxB,YAAYC,aAAa,UAAU;AACnC,OAAOC,SAAS,MAAM;AAEtB,SAASC,gBAAgB,QAAQ,cAAc;AAC/C,SAASC,WAAW,QAAQ,iBAAiB;AAC7C,OAAOC,wBAAwB,gCAAgC;AAC/D,YAAYC,kBAAkB,sBAAsB;IAG/BL;AAArB,MAAMM,cAAc,AAACN,CAAAA,CAAAA,mBAAAA,QAAQO,OAAO,cAAfP,8BAAAA,mBAAmBA,OAAM,EAAGQ,IAAI;AAErD,MAAMC,SAAS,CAACX,OAAOY,aAAa;AACpC,MAAMC,gBAAgBV,IAAIU,aAAa,IAAIN,aAAaM,aAAa;AAErE,SAASC,cAAcC,OAAgB;IACrC,IAAIA,QAAQC,UAAU,EAAE,OAAOf,KAAKgB,OAAO,CAACF,QAAQC,UAAU;IAC9D,OAAOD,QAAQG,SAAS,GAAGjB,KAAKgB,OAAO,CAACE,OAAOJ,QAAQG,SAAS,KAAKE,QAAQC,GAAG;AAClF;AAEA,eAAe,SAASF,OAAOG,SAAiB,EAAEP,OAAiB;IACjE,IAAIX,iBAAiBkB,WAAW,UAAU,OAAOT,cAAcS;IAC/D,IAAIvB,WAAWuB,YAAY,OAAOA;IAClC,IAAIA,SAAS,CAAC,EAAE,KAAK,KAAK;QACxB,MAAMN,aAAaD,UAAUD,cAAcC,WAAWK,QAAQC,GAAG;QACjE,OAAOpB,KAAKsB,IAAI,CAACP,YAAYM;IAC/B;IACA,IAAIjB,YAAYmB,IAAI,CAACF,YAAY;QAC/B,MAAMN,aAAaD,UAAUD,cAAcC,WAAWK,QAAQC,GAAG;QACjE,IAAI,CAACV,QAAQ;YACX,IAAI;gBACF,MAAMc,YAAYnB,mBAAmBgB,WAAWN;gBAChD,IAAIS,WAAW,OAAOA;YACxB,EAAE,OAAOC,GAAG;YACV,0CAA0C,GAC5C;QACF;QACA,MAAMD,YAAYjB,YAAYc,WAAW;YACvCK,SAASX;YACTY,YAAY;gBAAC;gBAAO;gBAAS;gBAAS;aAAO;QAC/C;QACA,IAAIH,WAAW,OAAOA;IACxB;IAEA,OAAOH;AACT"}
1
+ {"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/toPath.ts"],"sourcesContent":["import isAbsolute from 'is-absolute';\nimport module from 'module';\nimport path from 'path';\nimport url from 'url';\n\nimport { stringStartsWith } from './compat.ts';\nimport { moduleRegEx } from './constants.ts';\nimport resolveCJS from './lib/resolveCJS.ts';\nimport resolveESM from './lib/resolveESM.ts';\nimport * as urlPolyfills from './lib/urlFileUrl.ts';\nimport type { Context } from './types.ts';\n\nconst useCJS = !module.createRequire;\nconst fileURLToPath = url.fileURLToPath || urlPolyfills.fileURLToPath;\n\nfunction getParentPath(context: Context): string {\n if (context.parentPath) return path.dirname(context.parentPath);\n return context.parentURL ? path.dirname(toPath(context.parentURL)) : process.cwd();\n}\n\nfunction getParentFilePath(context?: Context): string {\n if (context?.parentPath) return context.parentPath;\n if (context?.parentURL) return fileURLToPath(context.parentURL);\n return path.join(process.cwd(), 'index.js');\n}\n\nexport default function toPath(specifier: string, context?: Context): string {\n // Handle file:// URLs\n if (stringStartsWith(specifier, 'file:')) return fileURLToPath(specifier);\n\n // Handle absolute paths\n if (isAbsolute(specifier)) return specifier;\n\n // Handle relative paths\n if (specifier[0] === '.') {\n const parentPath = context ? getParentPath(context) : process.cwd();\n return path.join(parentPath, specifier);\n }\n\n // Handle module specifiers (bare specifiers and # imports)\n // moduleRegEx matches: bare specifiers like 'lodash', '@scope/pkg'\n // specifier[0] === '#' matches: subpath imports like '#internal'\n if (moduleRegEx.test(specifier) || specifier[0] === '#') {\n const parentFilePath = getParentFilePath(context);\n const parentDir = path.dirname(parentFilePath);\n\n if (useCJS) {\n // CJS: use resolve package (does not support # imports)\n if (specifier[0] === '#') {\n throw new Error(`Cannot find module '${specifier}' from '${parentDir}' (subpath imports not supported in CJS mode)`);\n }\n return resolveCJS(specifier, parentDir);\n }\n // ESM: use unified resolver that handles both exports and imports\n const entryPath = resolveESM(specifier, parentFilePath);\n if (entryPath) return entryPath;\n\n // If ESM resolver failed, throw meaningful error\n throw new Error(`Cannot find module '${specifier}' from '${parentDir}'`);\n }\n\n return specifier;\n}\n"],"names":["isAbsolute","module","path","url","stringStartsWith","moduleRegEx","resolveCJS","resolveESM","urlPolyfills","useCJS","createRequire","fileURLToPath","getParentPath","context","parentPath","dirname","parentURL","toPath","process","cwd","getParentFilePath","join","specifier","test","parentFilePath","parentDir","Error","entryPath"],"mappings":"AAAA,OAAOA,gBAAgB,cAAc;AACrC,OAAOC,YAAY,SAAS;AAC5B,OAAOC,UAAU,OAAO;AACxB,OAAOC,SAAS,MAAM;AAEtB,SAASC,gBAAgB,QAAQ,cAAc;AAC/C,SAASC,WAAW,QAAQ,iBAAiB;AAC7C,OAAOC,gBAAgB,sBAAsB;AAC7C,OAAOC,gBAAgB,sBAAsB;AAC7C,YAAYC,kBAAkB,sBAAsB;AAGpD,MAAMC,SAAS,CAACR,OAAOS,aAAa;AACpC,MAAMC,gBAAgBR,IAAIQ,aAAa,IAAIH,aAAaG,aAAa;AAErE,SAASC,cAAcC,OAAgB;IACrC,IAAIA,QAAQC,UAAU,EAAE,OAAOZ,KAAKa,OAAO,CAACF,QAAQC,UAAU;IAC9D,OA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IAEA,OAAOH;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-swc-transform",
3
- "version": "2.8.0",
3
+ "version": "2.8.2",
4
4
  "description": "Typescript transformers for swc. Supports Node >= 0.8",
5
5
  "keywords": [
6
6
  "matcher",
@@ -67,6 +67,8 @@
67
67
  "cr": "^0.1.0",
68
68
  "cross-spawn-cb": "^2.0.0",
69
69
  "fs-remove-compat": "^1.0.0",
70
+ "mock-exports-only-pkg": "file:test/data/mock-exports-only-pkg",
71
+ "mock-subpath-imports-pkg": "file:test/data/mock-subpath-imports-pkg",
70
72
  "node-version-use": "*",
71
73
  "pinkie-promise": "*",
72
74
  "react": "^19.2.3",