ts-swc-transform 2.6.17 → 2.6.18
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/cjs/lib/patchCJS.js +1 -1
- package/dist/cjs/lib/patchCJS.js.map +1 -1
- package/dist/cjs/lib/rewriteExtensions.d.ts +1 -0
- package/dist/cjs/lib/rewriteExtensions.js +46 -6
- package/dist/cjs/lib/rewriteExtensions.js.map +1 -1
- package/dist/cjs/workers/transformTypes.js +1 -1
- package/dist/esm/lib/patchCJS.js +2 -2
- package/dist/esm/lib/patchCJS.js.map +1 -1
- package/dist/esm/lib/rewriteExtensions.d.ts +1 -0
- package/dist/esm/lib/rewriteExtensions.js +47 -6
- package/dist/esm/lib/rewriteExtensions.js.map +1 -1
- package/dist/esm/workers/transformTypes.js +1 -1
- package/package.json +1 -2
- package/dist/cjs/constants.d.cts +0 -3
- package/dist/cjs/createMatcher.d.cts +0 -2
- package/dist/cjs/index.d.cts +0 -8
- package/dist/cjs/lib/import-meta-resolve.d.cts +0 -1
- package/dist/cjs/lib/parseSpecifiers.d.cts +0 -5
- package/dist/cjs/lib/patchCJS.d.cts +0 -5
- package/dist/cjs/lib/patchESM.d.cts +0 -4
- package/dist/cjs/lib/prepareSWCOptions.d.cts +0 -7
- package/dist/cjs/lib/rewriteExtensions.d.cts +0 -8
- package/dist/cjs/lib/transformFile.d.cts +0 -3
- package/dist/cjs/lib/urlFileUrl.d.cts +0 -3
- package/dist/cjs/resolveFileSync.d.cts +0 -2
- package/dist/cjs/toPath.d.cts +0 -2
- package/dist/cjs/transformDirectory.d.cts +0 -2
- package/dist/cjs/transformSync.d.cts +0 -3
- package/dist/cjs/transformTypes.d.cts +0 -2
- package/dist/cjs/types.d.cts +0 -16
- package/dist/cjs/workers/transformDirectory.d.cts +0 -2
- package/dist/cjs/workers/transformSync.d.cts +0 -3
- package/dist/cjs/workers/transformTypes.d.cts +0 -2
package/dist/cjs/lib/patchCJS.js
CHANGED
|
@@ -26,7 +26,7 @@ function _interop_require_default(obj) {
|
|
|
26
26
|
var interop = "/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }";
|
|
27
27
|
function patchCJS(entry, output, options) {
|
|
28
28
|
var rewrite = (options.tsconfig.config.compilerOptions || {}).rewriteRelativeImportExtensions;
|
|
29
|
-
if (rewrite) output.code = (0, _rewriteExtensionsts.
|
|
29
|
+
if (rewrite) output.code = (0, _rewriteExtensionsts.rewriteExtensionsCJS)(output.code);
|
|
30
30
|
output.code += interop;
|
|
31
31
|
return (0, _rewriteExtensionsts.replaceExtension)(_path.default.extname(entry.basename));
|
|
32
32
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/patchCJS.ts"],"sourcesContent":["import path from 'path';\nimport { replaceExtension,
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/patchCJS.ts"],"sourcesContent":["import path from 'path';\nimport { replaceExtension, rewriteExtensionsCJS } from './rewriteExtensions.ts';\n\n// https://github.com/vercel/next.js/blob/20b63e13ab2631d6043277895d373aa31a1b327c/packages/next/taskfile-swc.js#L118-L125\nexport const interop = \"/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }\";\n\nimport type { Output } from '@swc/core';\nimport type { Entry } from 'fs-iterator';\nimport type { CompilerOptions } from 'typescript';\nimport type { ConfigOptions } from '../types.ts';\n\ninterface InternalCompilerOptions extends CompilerOptions {\n rewriteRelativeImportExtensions?: boolean;\n}\n\nexport default function patchCJS(entry: Entry, output: Output, options: ConfigOptions): string {\n const rewrite = ((options.tsconfig.config.compilerOptions || {}) as unknown as InternalCompilerOptions).rewriteRelativeImportExtensions;\n if (rewrite) output.code = rewriteExtensionsCJS(output.code);\n output.code += interop;\n\n return replaceExtension(path.extname(entry.basename));\n}\n"],"names":["patchCJS","interop","entry","output","options","rewrite","tsconfig","config","compilerOptions","rewriteRelativeImportExtensions","code","rewriteExtensionsCJS","replaceExtension","path","extname","basename"],"mappings":";;;;;;;;;;;QAeA;eAAwBA;;QAXXC;eAAAA;;;2DAJI;mCACsC;;;;;;AAGhD,IAAMA,UAAU;AAWR,SAASD,SAASE,KAAY,EAAEC,MAAc,EAAEC,OAAsB;IACnF,IAAMC,UAAU,AAAED,CAAAA,QAAQE,QAAQ,CAACC,MAAM,CAACC,eAAe,IAAI,CAAC,CAAA,EAA0CC,+BAA+B;IACvI,IAAIJ,SAASF,OAAOO,IAAI,GAAGC,IAAAA,yCAAoB,EAACR,OAAOO,IAAI;IAC3DP,OAAOO,IAAI,IAAIT;IAEf,OAAOW,IAAAA,qCAAgB,EAACC,aAAI,CAACC,OAAO,CAACZ,MAAMa,QAAQ;AACrD"}
|
|
@@ -17,6 +17,9 @@ _export(exports, {
|
|
|
17
17
|
},
|
|
18
18
|
get rewriteExtensions () {
|
|
19
19
|
return rewriteExtensions;
|
|
20
|
+
},
|
|
21
|
+
get rewriteExtensionsCJS () {
|
|
22
|
+
return rewriteExtensionsCJS;
|
|
20
23
|
}
|
|
21
24
|
});
|
|
22
25
|
var extensions = {
|
|
@@ -29,13 +32,50 @@ function replaceExtension(ext) {
|
|
|
29
32
|
var replace = extensions[ext];
|
|
30
33
|
return replace === undefined ? ext : replace;
|
|
31
34
|
}
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
// Helper to check if a path is relative (starts with ./ or ../)
|
|
36
|
+
function isRelativePath(path) {
|
|
37
|
+
return path.startsWith('./') || path.startsWith('../');
|
|
38
|
+
}
|
|
35
39
|
function rewriteExtensions(content) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
var result = content;
|
|
41
|
+
// Pattern 1: Import/Export statements (with optional 'type' keyword)
|
|
42
|
+
// Matches: import { X } from './path.ts'
|
|
43
|
+
// import type { X } from './path.ts'
|
|
44
|
+
// export * from './path.ts'
|
|
45
|
+
// export type * from './path.ts'
|
|
46
|
+
result = result.replace(/\b(import|export)(\s+type)?(?:\s+[^'"]*?\s+from\s+|\s+)['"]([^'"]+)\.(tsx?|mts|cts)['"]/g, function(match, keyword, typeKeyword, path, ext) {
|
|
47
|
+
if (!isRelativePath(path)) return match;
|
|
48
|
+
var newExt = replaceExtension(".".concat(ext));
|
|
49
|
+
return match.replace(".".concat(ext, '"'), "".concat(newExt, '"')).replace(".".concat(ext, "'"), "".concat(newExt, "'")).replace(".".concat(ext, "`"), "".concat(newExt, "`"));
|
|
50
|
+
});
|
|
51
|
+
// Pattern 2: Dynamic import types
|
|
52
|
+
// Matches: typeof import('./path.ts')
|
|
53
|
+
result = result.replace(/\bimport\s*\(\s*['"]([^'"]+)\.(tsx?|mts|cts)['"]\s*\)/g, function(match, path, ext) {
|
|
54
|
+
if (!isRelativePath(path)) return match;
|
|
55
|
+
var newExt = replaceExtension(".".concat(ext));
|
|
56
|
+
return match.replace(".".concat(ext, '"'), "".concat(newExt, '"')).replace(".".concat(ext, "'"), "".concat(newExt, "'")).replace(".".concat(ext, "`"), "".concat(newExt, "`"));
|
|
57
|
+
});
|
|
58
|
+
// Pattern 3: Triple-slash path references
|
|
59
|
+
// Matches: /// <reference path="./file.ts" />
|
|
60
|
+
// /// <reference path="./file.d.ts" />
|
|
61
|
+
result = result.replace(/\/\/\/\s*<reference\s+path\s*=\s*['"]([^'"]+)\.(d\.ts|tsx?|mts|cts)['"]\s*\/>/g, function(match, path, ext) {
|
|
62
|
+
if (!isRelativePath(path)) return match;
|
|
63
|
+
// Special case: .d.ts → .d.js
|
|
64
|
+
var newExt = ext === 'd.ts' ? '.d.js' : replaceExtension(".".concat(ext));
|
|
65
|
+
return match.replace(".".concat(ext, '"'), "".concat(newExt, '"')).replace(".".concat(ext, "'"), "".concat(newExt, "'")).replace(".".concat(ext, "`"), "".concat(newExt, "`"));
|
|
66
|
+
});
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
function rewriteExtensionsCJS(content) {
|
|
70
|
+
// Start with all ESM patterns
|
|
71
|
+
var result = rewriteExtensions(content);
|
|
72
|
+
// Pattern 4: CommonJS require() statements
|
|
73
|
+
// Matches: require('./path.js')
|
|
74
|
+
result = result.replace(/\brequire\s*\(\s*['"]([^'"]+)\.(tsx?|mts|cts)['"]\s*\)/g, function(match, path, ext) {
|
|
75
|
+
if (!isRelativePath(path)) return match;
|
|
76
|
+
var newExt = replaceExtension(".".concat(ext));
|
|
77
|
+
return match.replace(".".concat(ext, '"'), "".concat(newExt, '"')).replace(".".concat(ext, "'"), "".concat(newExt, "'")).replace(".".concat(ext, "`"), "".concat(newExt, "`"));
|
|
39
78
|
});
|
|
79
|
+
return result;
|
|
40
80
|
}
|
|
41
81
|
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/rewriteExtensions.ts"],"sourcesContent":["export const extensions = {\n '.ts': '.js',\n '.tsx': '.js',\n '.mts': '.mjs',\n '.cts': '.cjs',\n};\n\nexport function replaceExtension(ext: string): string {\n const replace = extensions[ext];\n return replace === undefined ? ext : replace;\n}\n\n//
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/rewriteExtensions.ts"],"sourcesContent":["export const extensions = {\n '.ts': '.js',\n '.tsx': '.js',\n '.mts': '.mjs',\n '.cts': '.cjs',\n};\n\nexport function replaceExtension(ext: string): string {\n const replace = extensions[ext];\n return replace === undefined ? ext : replace;\n}\n\n// Helper to check if a path is relative (starts with ./ or ../)\nfunction isRelativePath(path: string): boolean {\n return path.startsWith('./') || path.startsWith('../');\n}\n\n// Multi-pattern transformer for TypeScript extension rewriting\n// See: https://github.com/microsoft/TypeScript/issues/61037\n// TODO: Remove when TypeScript natively supports rewriteRelativeImportExtensions in .d.ts files\n\nexport function rewriteExtensions(content: string): string {\n let result = content;\n\n // Pattern 1: Import/Export statements (with optional 'type' keyword)\n // Matches: import { X } from './path.ts'\n // import type { X } from './path.ts'\n // export * from './path.ts'\n // export type * from './path.ts'\n result = result.replace(\n /\\b(import|export)(\\s+type)?(?:\\s+[^'\"]*?\\s+from\\s+|\\s+)['\"]([^'\"]+)\\.(tsx?|mts|cts)['\"]/g,\n (match, keyword, typeKeyword, path, ext) => {\n if (!isRelativePath(path)) return match;\n const newExt = replaceExtension(`.${ext}`);\n return match.replace(`.${ext}\"`, `${newExt}\"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\\``, `${newExt}\\``);\n }\n );\n\n // Pattern 2: Dynamic import types\n // Matches: typeof import('./path.ts')\n result = result.replace(\n /\\bimport\\s*\\(\\s*['\"]([^'\"]+)\\.(tsx?|mts|cts)['\"]\\s*\\)/g,\n (match, path, ext) => {\n if (!isRelativePath(path)) return match;\n const newExt = replaceExtension(`.${ext}`);\n return match.replace(`.${ext}\"`, `${newExt}\"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\\``, `${newExt}\\``);\n }\n );\n\n // Pattern 3: Triple-slash path references\n // Matches: /// <reference path=\"./file.ts\" />\n // /// <reference path=\"./file.d.ts\" />\n result = result.replace(\n /\\/\\/\\/\\s*<reference\\s+path\\s*=\\s*['\"]([^'\"]+)\\.(d\\.ts|tsx?|mts|cts)['\"]\\s*\\/>/g,\n (match, path, ext) => {\n if (!isRelativePath(path)) return match;\n // Special case: .d.ts → .d.js\n const newExt = ext === 'd.ts' ? '.d.js' : replaceExtension(`.${ext}`);\n return match.replace(`.${ext}\"`, `${newExt}\"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\\``, `${newExt}\\``);\n }\n );\n\n return result;\n}\n\n// CJS-specific version that also handles require() statements\nexport function rewriteExtensionsCJS(content: string): string {\n // Start with all ESM patterns\n let result = rewriteExtensions(content);\n\n // Pattern 4: CommonJS require() statements\n // Matches: require('./path.ts')\n result = result.replace(\n /\\brequire\\s*\\(\\s*['\"]([^'\"]+)\\.(tsx?|mts|cts)['\"]\\s*\\)/g,\n (match, path, ext) => {\n if (!isRelativePath(path)) return match;\n const newExt = replaceExtension(`.${ext}`);\n return match.replace(`.${ext}\"`, `${newExt}\"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\\``, `${newExt}\\``);\n }\n );\n\n return result;\n}\n"],"names":["extensions","replaceExtension","rewriteExtensions","rewriteExtensionsCJS","ext","replace","undefined","isRelativePath","path","startsWith","content","result","match","keyword","typeKeyword","newExt"],"mappings":";;;;;;;;;;;QAAaA;eAAAA;;QAOGC;eAAAA;;QAcAC;eAAAA;;QA6CAC;eAAAA;;;AAlET,IAAMH,aAAa;IACxB,OAAO;IACP,QAAQ;IACR,QAAQ;IACR,QAAQ;AACV;AAEO,SAASC,iBAAiBG,GAAW;IAC1C,IAAMC,UAAUL,UAAU,CAACI,IAAI;IAC/B,OAAOC,YAAYC,YAAYF,MAAMC;AACvC;AAEA,gEAAgE;AAChE,SAASE,eAAeC,IAAY;IAClC,OAAOA,KAAKC,UAAU,CAAC,SAASD,KAAKC,UAAU,CAAC;AAClD;AAMO,SAASP,kBAAkBQ,OAAe;IAC/C,IAAIC,SAASD;IAEb,qEAAqE;IACrE,yCAAyC;IACzC,8CAA8C;IAC9C,qCAAqC;IACrC,0CAA0C;IAC1CC,SAASA,OAAON,OAAO,CACrB,4FACA,SAACO,OAAOC,SAASC,aAAaN,MAAMJ;QAClC,IAAI,CAACG,eAAeC,OAAO,OAAOI;QAClC,IAAMG,SAASd,iBAAiB,AAAC,IAAO,OAAJG;QACpC,OAAOQ,MAAMP,OA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;IAC3C,gCAAgC;IAChCC,SAASA,OAAON,OAAO,CACrB,2DACA,SAACO,OAAOJ,MAAMJ;QACZ,IAAI,CAACG,eAAeC,OAAO,OAAOI;QAClC,IAAMG,SAASd,iBAAiB,AAAC,IAAO,OAAJG;QACpC,OAAOQ,MAAMP,OAAO,CAAC,AAAC,IAAO,OAAJD,KAAI,MAAI,AAAC,GAAS,OAAPW,QAAO,MAAIV,OAAO,CAAC,AAAC,IAAO,OAAJD,KAAI,MAAI,AAAC,GAAS,OAAPW,QAAO,MAAIV,OAAO,CAAC,AAAC,IAAO,OAAJD,KAAI,MAAK,AAAC,GAAS,OAAPW,QAAO;IAClH;IAGF,OAAOJ;AACT"}
|
|
@@ -123,7 +123,7 @@ function transformTypesWorker(src, dest, options, callback) {
|
|
|
123
123
|
// TODO: remove patch for https://github.com/microsoft/TypeScript/issues/61037
|
|
124
124
|
if (res.emittedFiles && compilerOptions.options.rewriteRelativeImportExtensions) {
|
|
125
125
|
res.emittedFiles.forEach(function(file) {
|
|
126
|
-
if (file.endsWith('.d.
|
|
126
|
+
if (file.endsWith('.d.ts') || file.endsWith('.d.cts') || file.endsWith('.d.mts')) {
|
|
127
127
|
try {
|
|
128
128
|
var content = _fs.default.readFileSync(file, 'utf8');
|
|
129
129
|
var updated = (0, _rewriteExtensionsts.rewriteExtensions)(content);
|
package/dist/esm/lib/patchCJS.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { replaceExtension,
|
|
2
|
+
import { replaceExtension, rewriteExtensionsCJS } from './rewriteExtensions.js';
|
|
3
3
|
// https://github.com/vercel/next.js/blob/20b63e13ab2631d6043277895d373aa31a1b327c/packages/next/taskfile-swc.js#L118-L125
|
|
4
4
|
export const interop = "/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }";
|
|
5
5
|
export default function patchCJS(entry, output, options) {
|
|
6
6
|
const rewrite = (options.tsconfig.config.compilerOptions || {}).rewriteRelativeImportExtensions;
|
|
7
|
-
if (rewrite) output.code =
|
|
7
|
+
if (rewrite) output.code = rewriteExtensionsCJS(output.code);
|
|
8
8
|
output.code += interop;
|
|
9
9
|
return replaceExtension(path.extname(entry.basename));
|
|
10
10
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/patchCJS.ts"],"sourcesContent":["import path from 'path';\nimport { replaceExtension,
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/patchCJS.ts"],"sourcesContent":["import path from 'path';\nimport { replaceExtension, rewriteExtensionsCJS } from './rewriteExtensions.ts';\n\n// https://github.com/vercel/next.js/blob/20b63e13ab2631d6043277895d373aa31a1b327c/packages/next/taskfile-swc.js#L118-L125\nexport const interop = \"/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }\";\n\nimport type { Output } from '@swc/core';\nimport type { Entry } from 'fs-iterator';\nimport type { CompilerOptions } from 'typescript';\nimport type { ConfigOptions } from '../types.ts';\n\ninterface InternalCompilerOptions extends CompilerOptions {\n rewriteRelativeImportExtensions?: boolean;\n}\n\nexport default function patchCJS(entry: Entry, output: Output, options: ConfigOptions): string {\n const rewrite = ((options.tsconfig.config.compilerOptions || {}) as unknown as InternalCompilerOptions).rewriteRelativeImportExtensions;\n if (rewrite) output.code = rewriteExtensionsCJS(output.code);\n output.code += interop;\n\n return replaceExtension(path.extname(entry.basename));\n}\n"],"names":["path","replaceExtension","rewriteExtensionsCJS","interop","patchCJS","entry","output","options","rewrite","tsconfig","config","compilerOptions","rewriteRelativeImportExtensions","code","extname","basename"],"mappings":"AAAA,OAAOA,UAAU,OAAO;AACxB,SAASC,gBAAgB,EAAEC,oBAAoB,QAAQ,yBAAyB;AAEhF,0HAA0H;AAC1H,OAAO,MAAMC,UAAU,oQAAoQ;AAW3R,eAAe,SAASC,SAASC,KAAY,EAAEC,MAAc,EAAEC,OAAsB;IACnF,MAAMC,UAAU,AAAED,CAAAA,QAAQE,QAAQ,CAACC,MAAM,CAACC,eAAe,IAAI,CAAC,CAAA,EAA0CC,+BAA+B;IACvI,IAAIJ,SAASF,OAAOO,IAAI,GAAGX,qBAAqBI,OAAOO,IAAI;IAC3DP,OAAOO,IAAI,IAAIV;IAEf,OAAOF,iBAAiBD,KAAKc,OAAO,CAACT,MAAMU,QAAQ;AACrD"}
|
|
@@ -8,12 +8,53 @@ export function replaceExtension(ext) {
|
|
|
8
8
|
const replace = extensions[ext];
|
|
9
9
|
return replace === undefined ? ext : replace;
|
|
10
10
|
}
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
// Helper to check if a path is relative (starts with ./ or ../)
|
|
12
|
+
function isRelativePath(path) {
|
|
13
|
+
return path.startsWith('./') || path.startsWith('../');
|
|
14
|
+
}
|
|
15
|
+
// Multi-pattern transformer for TypeScript extension rewriting
|
|
16
|
+
// See: https://github.com/microsoft/TypeScript/issues/61037
|
|
17
|
+
// TODO: Remove when TypeScript natively supports rewriteRelativeImportExtensions in .d.ts files
|
|
14
18
|
export function rewriteExtensions(content) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
let result = content;
|
|
20
|
+
// Pattern 1: Import/Export statements (with optional 'type' keyword)
|
|
21
|
+
// Matches: import { X } from './path.js'
|
|
22
|
+
// import type { X } from './path.js'
|
|
23
|
+
// export * from './path.js'
|
|
24
|
+
// export type * from './path.js'
|
|
25
|
+
result = result.replace(/\b(import|export)(\s+type)?(?:\s+[^'"]*?\s+from\s+|\s+)['"]([^'"]+)\.(tsx?|mts|cts)['"]/g, (match, keyword, typeKeyword, path, ext)=>{
|
|
26
|
+
if (!isRelativePath(path)) return match;
|
|
27
|
+
const newExt = replaceExtension(`.${ext}`);
|
|
28
|
+
return match.replace(`.${ext}"`, `${newExt}"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\``, `${newExt}\``);
|
|
29
|
+
});
|
|
30
|
+
// Pattern 2: Dynamic import types
|
|
31
|
+
// Matches: typeof import('./path.ts')
|
|
32
|
+
result = result.replace(/\bimport\s*\(\s*['"]([^'"]+)\.(tsx?|mts|cts)['"]\s*\)/g, (match, path, ext)=>{
|
|
33
|
+
if (!isRelativePath(path)) return match;
|
|
34
|
+
const newExt = replaceExtension(`.${ext}`);
|
|
35
|
+
return match.replace(`.${ext}"`, `${newExt}"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\``, `${newExt}\``);
|
|
36
|
+
});
|
|
37
|
+
// Pattern 3: Triple-slash path references
|
|
38
|
+
// Matches: /// <reference path="./file.ts" />
|
|
39
|
+
// /// <reference path="./file.d.ts" />
|
|
40
|
+
result = result.replace(/\/\/\/\s*<reference\s+path\s*=\s*['"]([^'"]+)\.(d\.ts|tsx?|mts|cts)['"]\s*\/>/g, (match, path, ext)=>{
|
|
41
|
+
if (!isRelativePath(path)) return match;
|
|
42
|
+
// Special case: .d.ts → .d.js
|
|
43
|
+
const newExt = ext === 'd.ts' ? '.d.js' : replaceExtension(`.${ext}`);
|
|
44
|
+
return match.replace(`.${ext}"`, `${newExt}"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\``, `${newExt}\``);
|
|
45
|
+
});
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
// CJS-specific version that also handles require() statements
|
|
49
|
+
export function rewriteExtensionsCJS(content) {
|
|
50
|
+
// Start with all ESM patterns
|
|
51
|
+
let result = rewriteExtensions(content);
|
|
52
|
+
// Pattern 4: CommonJS require() statements
|
|
53
|
+
// Matches: require('./path.ts')
|
|
54
|
+
result = result.replace(/\brequire\s*\(\s*['"]([^'"]+)\.(tsx?|mts|cts)['"]\s*\)/g, (match, path, ext)=>{
|
|
55
|
+
if (!isRelativePath(path)) return match;
|
|
56
|
+
const newExt = replaceExtension(`.${ext}`);
|
|
57
|
+
return match.replace(`.${ext}"`, `${newExt}"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\``, `${newExt}\``);
|
|
18
58
|
});
|
|
59
|
+
return result;
|
|
19
60
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/rewriteExtensions.ts"],"sourcesContent":["export const extensions = {\n '.ts': '.js',\n '.tsx': '.js',\n '.mts': '.mjs',\n '.cts': '.cjs',\n};\n\nexport function replaceExtension(ext: string): string {\n const replace = extensions[ext];\n return replace === undefined ? ext : replace;\n}\n\n//
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/rewriteExtensions.ts"],"sourcesContent":["export const extensions = {\n '.ts': '.js',\n '.tsx': '.js',\n '.mts': '.mjs',\n '.cts': '.cjs',\n};\n\nexport function replaceExtension(ext: string): string {\n const replace = extensions[ext];\n return replace === undefined ? ext : replace;\n}\n\n// Helper to check if a path is relative (starts with ./ or ../)\nfunction isRelativePath(path: string): boolean {\n return path.startsWith('./') || path.startsWith('../');\n}\n\n// Multi-pattern transformer for TypeScript extension rewriting\n// See: https://github.com/microsoft/TypeScript/issues/61037\n// TODO: Remove when TypeScript natively supports rewriteRelativeImportExtensions in .d.ts files\n\nexport function rewriteExtensions(content: string): string {\n let result = content;\n\n // Pattern 1: Import/Export statements (with optional 'type' keyword)\n // Matches: import { X } from './path.ts'\n // import type { X } from './path.ts'\n // export * from './path.ts'\n // export type * from './path.ts'\n result = result.replace(\n /\\b(import|export)(\\s+type)?(?:\\s+[^'\"]*?\\s+from\\s+|\\s+)['\"]([^'\"]+)\\.(tsx?|mts|cts)['\"]/g,\n (match, keyword, typeKeyword, path, ext) => {\n if (!isRelativePath(path)) return match;\n const newExt = replaceExtension(`.${ext}`);\n return match.replace(`.${ext}\"`, `${newExt}\"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\\``, `${newExt}\\``);\n }\n );\n\n // Pattern 2: Dynamic import types\n // Matches: typeof import('./path.ts')\n result = result.replace(\n /\\bimport\\s*\\(\\s*['\"]([^'\"]+)\\.(tsx?|mts|cts)['\"]\\s*\\)/g,\n (match, path, ext) => {\n if (!isRelativePath(path)) return match;\n const newExt = replaceExtension(`.${ext}`);\n return match.replace(`.${ext}\"`, `${newExt}\"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\\``, `${newExt}\\``);\n }\n );\n\n // Pattern 3: Triple-slash path references\n // Matches: /// <reference path=\"./file.ts\" />\n // /// <reference path=\"./file.d.ts\" />\n result = result.replace(\n /\\/\\/\\/\\s*<reference\\s+path\\s*=\\s*['\"]([^'\"]+)\\.(d\\.ts|tsx?|mts|cts)['\"]\\s*\\/>/g,\n (match, path, ext) => {\n if (!isRelativePath(path)) return match;\n // Special case: .d.ts → .d.js\n const newExt = ext === 'd.ts' ? '.d.js' : replaceExtension(`.${ext}`);\n return match.replace(`.${ext}\"`, `${newExt}\"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\\``, `${newExt}\\``);\n }\n );\n\n return result;\n}\n\n// CJS-specific version that also handles require() statements\nexport function rewriteExtensionsCJS(content: string): string {\n // Start with all ESM patterns\n let result = rewriteExtensions(content);\n\n // Pattern 4: CommonJS require() statements\n // Matches: require('./path.ts')\n result = result.replace(\n /\\brequire\\s*\\(\\s*['\"]([^'\"]+)\\.(tsx?|mts|cts)['\"]\\s*\\)/g,\n (match, path, ext) => {\n if (!isRelativePath(path)) return match;\n const newExt = replaceExtension(`.${ext}`);\n return match.replace(`.${ext}\"`, `${newExt}\"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\\``, `${newExt}\\``);\n }\n );\n\n return result;\n}\n"],"names":["extensions","replaceExtension","ext","replace","undefined","isRelativePath","path","startsWith","rewriteExt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CAAC,EAAED,IAAI,EAAE,CAAC,EAAE,GAAGY,OAAO,EAAE,CAAC;IACrH;IAGF,OAAOJ;AACT"}
|
|
@@ -55,7 +55,7 @@ export default function transformTypesWorker(src, dest, options, callback) {
|
|
|
55
55
|
// TODO: remove patch for https://github.com/microsoft/TypeScript/issues/61037
|
|
56
56
|
if (res.emittedFiles && compilerOptions.options.rewriteRelativeImportExtensions) {
|
|
57
57
|
res.emittedFiles.forEach((file)=>{
|
|
58
|
-
if (file.endsWith('.d.
|
|
58
|
+
if (file.endsWith('.d.ts') || file.endsWith('.d.cts') || file.endsWith('.d.mts')) {
|
|
59
59
|
try {
|
|
60
60
|
const content = fs.readFileSync(file, 'utf8');
|
|
61
61
|
const updated = rewriteExtensions(content);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-swc-transform",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.18",
|
|
4
4
|
"description": "Typescript transformers for swc. Supports Node >= 0.8",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"matcher",
|
|
@@ -32,7 +32,6 @@
|
|
|
32
32
|
"types": "dist/cjs/index.d.cts",
|
|
33
33
|
"files": [
|
|
34
34
|
"dist",
|
|
35
|
-
"scripts",
|
|
36
35
|
"assets"
|
|
37
36
|
],
|
|
38
37
|
"scripts": {
|
package/dist/cjs/constants.d.cts
DELETED
package/dist/cjs/index.d.cts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export * as constants from './constants.js';
|
|
2
|
-
export { default as createMatcher } from './createMatcher.js';
|
|
3
|
-
export { default as resolveFileSync } from './resolveFileSync.js';
|
|
4
|
-
export { default as toPath } from './toPath.js';
|
|
5
|
-
export { default as transformDirectory } from './transformDirectory.js';
|
|
6
|
-
export { default as transformSync } from './transformSync.js';
|
|
7
|
-
export { default as transformTypes } from './transformTypes.js';
|
|
8
|
-
export type * from './types.js';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function importMetaResolve(specifier: string, url: URL): string;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare const interop = "/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }";
|
|
2
|
-
import type { Output } from '@swc/core';
|
|
3
|
-
import type { Entry } from 'fs-iterator';
|
|
4
|
-
import type { ConfigOptions } from '../types.js';
|
|
5
|
-
export default function patchCJS(entry: Entry, output: Output, options: ConfigOptions): string;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { Entry } from 'fs-iterator';
|
|
2
|
-
import type { ConfigOptions, TargetType, TransformFileCallback } from '../types.js';
|
|
3
|
-
export default function transformFile(entry: Entry, dest: string, type: TargetType, options: ConfigOptions, callback: TransformFileCallback): undefined;
|
package/dist/cjs/toPath.d.cts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import type { ConfigOptions, TargetType, TransformDirectoryCallback } from './types.js';
|
|
2
|
-
export default function transformDirectory(src: string, dest: string, type: TargetType, options?: ConfigOptions | TransformDirectoryCallback, callback?: TransformDirectoryCallback): undefined | Promise<string[]>;
|
package/dist/cjs/types.d.cts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { TSConfig } from 'read-tsconfig-sync';
|
|
2
|
-
export interface Context {
|
|
3
|
-
parentURL?: string;
|
|
4
|
-
parentPath?: string;
|
|
5
|
-
}
|
|
6
|
-
export type { TSConfig, TSConfigSchema } from 'read-tsconfig-sync';
|
|
7
|
-
export interface ConfigOptions {
|
|
8
|
-
tsconfig?: TSConfig;
|
|
9
|
-
cwd?: string;
|
|
10
|
-
sourceMaps?: boolean;
|
|
11
|
-
}
|
|
12
|
-
export type TransformDirectoryCallback = (err?: Error, filePaths?: string[]) => void;
|
|
13
|
-
export type TransformFileCallback = (err?: Error, destFilePath?: string) => void;
|
|
14
|
-
export type TransformTypesCallback = (err?: Error, filePaths?: string[]) => void;
|
|
15
|
-
export type Matcher = (filePath: string) => boolean;
|
|
16
|
-
export type TargetType = 'cjs' | 'esm';
|