ts-swc-transform 2.6.17 → 2.6.19
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 +52 -6
- package/dist/cjs/lib/rewriteExtensions.js.map +1 -1
- package/dist/cjs/toPath.js +2 -4
- package/dist/cjs/toPath.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 +48 -6
- package/dist/esm/lib/rewriteExtensions.js.map +1 -1
- package/dist/esm/toPath.js +1 -1
- package/dist/esm/toPath.js.map +1 -1
- package/dist/esm/workers/transformTypes.js +1 -1
- package/package.json +3 -3
- 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,8 +17,17 @@ _export(exports, {
|
|
|
17
17
|
},
|
|
18
18
|
get rewriteExtensions () {
|
|
19
19
|
return rewriteExtensions;
|
|
20
|
+
},
|
|
21
|
+
get rewriteExtensionsCJS () {
|
|
22
|
+
return rewriteExtensionsCJS;
|
|
20
23
|
}
|
|
21
24
|
});
|
|
25
|
+
var _startswith = /*#__PURE__*/ _interop_require_default(require("starts-with"));
|
|
26
|
+
function _interop_require_default(obj) {
|
|
27
|
+
return obj && obj.__esModule ? obj : {
|
|
28
|
+
default: obj
|
|
29
|
+
};
|
|
30
|
+
}
|
|
22
31
|
var extensions = {
|
|
23
32
|
'.ts': '.js',
|
|
24
33
|
'.tsx': '.js',
|
|
@@ -29,13 +38,50 @@ function replaceExtension(ext) {
|
|
|
29
38
|
var replace = extensions[ext];
|
|
30
39
|
return replace === undefined ? ext : replace;
|
|
31
40
|
}
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
41
|
+
// Helper to check if a path is relative (starts with ./ or ../)
|
|
42
|
+
function isRelativePath(path) {
|
|
43
|
+
return (0, _startswith.default)(path, './') || (0, _startswith.default)(path, '../');
|
|
44
|
+
}
|
|
35
45
|
function rewriteExtensions(content) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
var result = content;
|
|
47
|
+
// Pattern 1: Import/Export statements (with optional 'type' keyword)
|
|
48
|
+
// Matches: import { X } from './path.js'
|
|
49
|
+
// import type { X } from './path.js'
|
|
50
|
+
// export * from './path.js'
|
|
51
|
+
// export type * from './path.js'
|
|
52
|
+
result = result.replace(/\b(import|export)(\s+type)?(?:\s+[^'"]*?\s+from\s+|\s+)['"]([^'"]+)\.(tsx?|mts|cts)['"]/g, function(match, keyword, typeKeyword, path, ext) {
|
|
53
|
+
if (!isRelativePath(path)) return match;
|
|
54
|
+
var newExt = replaceExtension(".".concat(ext));
|
|
55
|
+
return match.replace(".".concat(ext, '"'), "".concat(newExt, '"')).replace(".".concat(ext, "'"), "".concat(newExt, "'")).replace(".".concat(ext, "`"), "".concat(newExt, "`"));
|
|
56
|
+
});
|
|
57
|
+
// Pattern 2: Dynamic import types
|
|
58
|
+
// Matches: typeof import('./path.js')
|
|
59
|
+
result = result.replace(/\bimport\s*\(\s*['"]([^'"]+)\.(tsx?|mts|cts)['"]\s*\)/g, function(match, path, ext) {
|
|
60
|
+
if (!isRelativePath(path)) return match;
|
|
61
|
+
var newExt = replaceExtension(".".concat(ext));
|
|
62
|
+
return match.replace(".".concat(ext, '"'), "".concat(newExt, '"')).replace(".".concat(ext, "'"), "".concat(newExt, "'")).replace(".".concat(ext, "`"), "".concat(newExt, "`"));
|
|
63
|
+
});
|
|
64
|
+
// Pattern 3: Triple-slash path references
|
|
65
|
+
// Matches: /// <reference path="./file.js" />
|
|
66
|
+
// /// <reference path="./file.d.js" />
|
|
67
|
+
result = result.replace(/\/\/\/\s*<reference\s+path\s*=\s*['"]([^'"]+)\.(d\.ts|tsx?|mts|cts)['"]\s*\/>/g, function(match, path, ext) {
|
|
68
|
+
if (!isRelativePath(path)) return match;
|
|
69
|
+
// Special case: .d.ts → .d.js
|
|
70
|
+
var newExt = ext === 'd.ts' ? '.d.js' : replaceExtension(".".concat(ext));
|
|
71
|
+
return match.replace(".".concat(ext, '"'), "".concat(newExt, '"')).replace(".".concat(ext, "'"), "".concat(newExt, "'")).replace(".".concat(ext, "`"), "".concat(newExt, "`"));
|
|
72
|
+
});
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
function rewriteExtensionsCJS(content) {
|
|
76
|
+
// Start with all ESM patterns
|
|
77
|
+
var result = rewriteExtensions(content);
|
|
78
|
+
// Pattern 4: CommonJS require() statements
|
|
79
|
+
// Matches: require('./path.js')
|
|
80
|
+
result = result.replace(/\brequire\s*\(\s*['"]([^'"]+)\.(tsx?|mts|cts)['"]\s*\)/g, function(match, path, ext) {
|
|
81
|
+
if (!isRelativePath(path)) return match;
|
|
82
|
+
var newExt = replaceExtension(".".concat(ext));
|
|
83
|
+
return match.replace(".".concat(ext, '"'), "".concat(newExt, '"')).replace(".".concat(ext, "'"), "".concat(newExt, "'")).replace(".".concat(ext, "`"), "".concat(newExt, "`"));
|
|
39
84
|
});
|
|
85
|
+
return result;
|
|
40
86
|
}
|
|
41
87
|
/* 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":["
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/rewriteExtensions.ts"],"sourcesContent":["import startsWith from 'starts-with';\n\nexport 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 startsWith(path, './') || startsWith(path, '../');\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":"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"}
|
package/dist/cjs/toPath.js
CHANGED
|
@@ -16,6 +16,7 @@ var _url = /*#__PURE__*/ _interop_require_default(require("url"));
|
|
|
16
16
|
var _constantsts = require("./constants.js");
|
|
17
17
|
var _importmetaresolvets = /*#__PURE__*/ _interop_require_default(require("./lib/import-meta-resolve.js"));
|
|
18
18
|
var _urlFileUrlts = /*#__PURE__*/ _interop_require_wildcard(require("./lib/urlFileUrl.js"));
|
|
19
|
+
var _startswith = /*#__PURE__*/ _interop_require_default(require("starts-with"));
|
|
19
20
|
function _interop_require_default(obj) {
|
|
20
21
|
return obj && obj.__esModule ? obj : {
|
|
21
22
|
default: obj
|
|
@@ -67,15 +68,12 @@ var resolveSync = ((_resolve_default = _resolve.default) !== null && _resolve_de
|
|
|
67
68
|
var useCJS = !_module.default.createRequire;
|
|
68
69
|
var fileURLToPath = _url.default.fileURLToPath || _urlFileUrlts.fileURLToPath;
|
|
69
70
|
var pathToFileURL = _url.default.pathToFileURL || _urlFileUrlts.pathToFileURL;
|
|
70
|
-
var startsWith = function(string, check) {
|
|
71
|
-
return string.lastIndexOf(check, 0) === 0;
|
|
72
|
-
};
|
|
73
71
|
function getParentPath(context) {
|
|
74
72
|
if (context.parentPath) return _path.default.dirname(context.parentPath);
|
|
75
73
|
return context.parentURL ? _path.default.dirname(toPath(context.parentURL)) : process.cwd();
|
|
76
74
|
}
|
|
77
75
|
function toPath(specifier, context) {
|
|
78
|
-
if (
|
|
76
|
+
if ((0, _startswith.default)(specifier, 'file:')) return fileURLToPath(specifier);
|
|
79
77
|
if ((0, _isabsolute.default)(specifier)) return specifier;
|
|
80
78
|
if (specifier[0] === '.') {
|
|
81
79
|
var parentPath = context ? getParentPath(context) : process.cwd();
|
package/dist/cjs/toPath.js.map
CHANGED
|
@@ -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';\nimport { moduleRegEx } from './constants.ts';\nimport importMetaResolve from './lib/import-meta-resolve.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;\nconst pathToFileURL = url.pathToFileURL || urlPolyfills.pathToFileURL;\
|
|
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';\nimport { moduleRegEx } from './constants.ts';\nimport importMetaResolve from './lib/import-meta-resolve.ts';\nimport * as urlPolyfills from './lib/urlFileUrl.ts';\nimport type { Context } from './types.ts';\nimport startsWith from 'starts-with';\n\nconst resolveSync = (resolve.default ?? resolve).sync;\n\nconst useCJS = !module.createRequire;\nconst fileURLToPath = url.fileURLToPath || urlPolyfills.fileURLToPath;\nconst pathToFileURL = url.pathToFileURL || urlPolyfills.pathToFileURL;\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 (startsWith(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 entryURL = importMetaResolve(specifier, pathToFileURL(parentPath));\n if (entryURL) return fileURLToPath(entryURL);\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":["toPath","resolve","resolveSync","default","sync","useCJS","module","createRequire","fileURLToPath","url","urlPolyfills","pathToFileURL","getParentPath","context","parentPath","path","dirname","parentURL","process","cwd","specifier","startsWith","isAbsolute","join","moduleRegEx","test","entryURL","importMetaResolve","_","entryPath","basedir","extensions"],"mappings":";;;;+BAsBA;;;eAAwBA;;;iEAtBD;6DACJ;2DACF;+DACQ;0DACT;2BACY;0EACE;oEACA;iEAEP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAEFC;AAArB,IAAMC,cAAc,AAACD,CAAAA,CAAAA,mBAAAA,SAAQE,OAAO,cAAfF,8BAAAA,mBAAmBA,QAAM,EAAGG,IAAI;AAErD,IAAMC,SAAS,CAACC,eAAM,CAACC,aAAa;AACpC,IAAMC,gBAAgBC,YAAG,CAACD,aAAa,IAAIE,cAAaF,aAAa;AACrE,IAAMG,gBAAgBF,YAAG,CAACE,aAAa,IAAID,cAAaC,aAAa;AAErE,SAASC,cAAcC,OAAgB;IACrC,IAAIA,QAAQC,UAAU,EAAE,OAAOC,aAAI,CAACC,OAAO,CAACH,QAAQC,UAAU;IAC9D,OAAOD,QAAQI,SAAS,GAAGF,aAAI,CAACC,OAAO,CAAChB,OAAOa,QAAQI,SAAS,KAAKC,QAAQC,GAAG;AAClF;AAEe,SAASnB,OAAOoB,SAAiB,EAAEP,OAAiB;IACjE,IAAIQ,IAAAA,mBAAU,EAACD,WAAW,UAAU,OAAOZ,cAAcY;IACzD,IAAIE,IAAAA,mBAAU,EAACF,YAAY,OAAOA;IAClC,IAAIA,SAAS,CAAC,EAAE,KAAK,KAAK;QACxB,IAAMN,aAAaD,UAAUD,cAAcC,WAAWK,QAAQC,GAAG;QACjE,OAAOJ,aAAI,CAACQ,IAAI,CAACT,YAAYM;IAC/B;IACA,IAAII,wBAAW,CAACC,IAAI,CAACL,YAAY;QAC/B,IAAMN,cAAaD,UAAUD,cAAcC,WAAWK,QAAQC,GAAG;QACjE,IAAI,CAACd,QAAQ;YACX,IAAI;gBACF,IAAMqB,WAAWC,IAAAA,4BAAiB,EAACP,WAAWT,cAAcG;gBAC5D,IAAIY,UAAU,OAAOlB,cAAckB;YACrC,EAAE,OAAOE,GAAG;YACV,0CAA0C,GAC5C;QACF;QACA,IAAMC,YAAY3B,YAAYkB,WAAW;YACvCU,SAAShB;YACTiB,YAAY;gBAAC;gBAAO;gBAAS;gBAAS;aAAO;QAC/C;QACA,IAAIF,WAAW,OAAOA;IACxB;IAEA,OAAOT;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"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import startsWith from 'starts-with';
|
|
1
2
|
export const extensions = {
|
|
2
3
|
'.ts': '.js',
|
|
3
4
|
'.tsx': '.js',
|
|
@@ -8,12 +9,53 @@ export function replaceExtension(ext) {
|
|
|
8
9
|
const replace = extensions[ext];
|
|
9
10
|
return replace === undefined ? ext : replace;
|
|
10
11
|
}
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
// Helper to check if a path is relative (starts with ./ or ../)
|
|
13
|
+
function isRelativePath(path) {
|
|
14
|
+
return startsWith(path, './') || startsWith(path, '../');
|
|
15
|
+
}
|
|
16
|
+
// Multi-pattern transformer for TypeScript extension rewriting
|
|
17
|
+
// See: https://github.com/microsoft/TypeScript/issues/61037
|
|
18
|
+
// TODO: Remove when TypeScript natively supports rewriteRelativeImportExtensions in .d.ts files
|
|
14
19
|
export function rewriteExtensions(content) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
let result = content;
|
|
21
|
+
// Pattern 1: Import/Export statements (with optional 'type' keyword)
|
|
22
|
+
// Matches: import { X } from './path.js'
|
|
23
|
+
// import type { X } from './path.js'
|
|
24
|
+
// export * from './path.js'
|
|
25
|
+
// export type * from './path.js'
|
|
26
|
+
result = result.replace(/\b(import|export)(\s+type)?(?:\s+[^'"]*?\s+from\s+|\s+)['"]([^'"]+)\.(tsx?|mts|cts)['"]/g, (match, keyword, typeKeyword, path, ext)=>{
|
|
27
|
+
if (!isRelativePath(path)) return match;
|
|
28
|
+
const newExt = replaceExtension(`.${ext}`);
|
|
29
|
+
return match.replace(`.${ext}"`, `${newExt}"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\``, `${newExt}\``);
|
|
30
|
+
});
|
|
31
|
+
// Pattern 2: Dynamic import types
|
|
32
|
+
// Matches: typeof import('./path.js')
|
|
33
|
+
result = result.replace(/\bimport\s*\(\s*['"]([^'"]+)\.(tsx?|mts|cts)['"]\s*\)/g, (match, path, ext)=>{
|
|
34
|
+
if (!isRelativePath(path)) return match;
|
|
35
|
+
const newExt = replaceExtension(`.${ext}`);
|
|
36
|
+
return match.replace(`.${ext}"`, `${newExt}"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\``, `${newExt}\``);
|
|
37
|
+
});
|
|
38
|
+
// Pattern 3: Triple-slash path references
|
|
39
|
+
// Matches: /// <reference path="./file.js" />
|
|
40
|
+
// /// <reference path="./file.d.js" />
|
|
41
|
+
result = result.replace(/\/\/\/\s*<reference\s+path\s*=\s*['"]([^'"]+)\.(d\.ts|tsx?|mts|cts)['"]\s*\/>/g, (match, path, ext)=>{
|
|
42
|
+
if (!isRelativePath(path)) return match;
|
|
43
|
+
// Special case: .d.ts → .d.js
|
|
44
|
+
const newExt = ext === 'd.ts' ? '.d.js' : replaceExtension(`.${ext}`);
|
|
45
|
+
return match.replace(`.${ext}"`, `${newExt}"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\``, `${newExt}\``);
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
// CJS-specific version that also handles require() statements
|
|
50
|
+
export function rewriteExtensionsCJS(content) {
|
|
51
|
+
// Start with all ESM patterns
|
|
52
|
+
let result = rewriteExtensions(content);
|
|
53
|
+
// Pattern 4: CommonJS require() statements
|
|
54
|
+
// Matches: require('./path.ts')
|
|
55
|
+
result = result.replace(/\brequire\s*\(\s*['"]([^'"]+)\.(tsx?|mts|cts)['"]\s*\)/g, (match, path, ext)=>{
|
|
56
|
+
if (!isRelativePath(path)) return match;
|
|
57
|
+
const newExt = replaceExtension(`.${ext}`);
|
|
58
|
+
return match.replace(`.${ext}"`, `${newExt}"`).replace(`.${ext}'`, `${newExt}'`).replace(`.${ext}\``, `${newExt}\``);
|
|
18
59
|
});
|
|
60
|
+
return result;
|
|
19
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/rewriteExtensions.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/rewriteExtensions.ts"],"sourcesContent":["import startsWith from 'starts-with';\n\nexport 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 startsWith(path, './') || startsWith(path, '../');\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":["startsWith","extensions","replaceExtension","ext","replace","undefined","isRelativePath","path","rewriteExtensions","content","result","match","keyword","typeKeyword","newExt","rewriteExtensionsCJS"],"mappings":"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"}
|
package/dist/esm/toPath.js
CHANGED
|
@@ -6,12 +6,12 @@ import url from 'url';
|
|
|
6
6
|
import { moduleRegEx } from './constants.js';
|
|
7
7
|
import importMetaResolve from './lib/import-meta-resolve.js';
|
|
8
8
|
import * as urlPolyfills from './lib/urlFileUrl.js';
|
|
9
|
+
import startsWith from 'starts-with';
|
|
9
10
|
var _resolve_default;
|
|
10
11
|
const resolveSync = ((_resolve_default = resolve.default) !== null && _resolve_default !== void 0 ? _resolve_default : resolve).sync;
|
|
11
12
|
const useCJS = !module.createRequire;
|
|
12
13
|
const fileURLToPath = url.fileURLToPath || urlPolyfills.fileURLToPath;
|
|
13
14
|
const pathToFileURL = url.pathToFileURL || urlPolyfills.pathToFileURL;
|
|
14
|
-
const startsWith = (string, check)=>string.lastIndexOf(check, 0) === 0;
|
|
15
15
|
function getParentPath(context) {
|
|
16
16
|
if (context.parentPath) return path.dirname(context.parentPath);
|
|
17
17
|
return context.parentURL ? path.dirname(toPath(context.parentURL)) : process.cwd();
|
package/dist/esm/toPath.js.map
CHANGED
|
@@ -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';\nimport { moduleRegEx } from './constants.ts';\nimport importMetaResolve from './lib/import-meta-resolve.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;\nconst pathToFileURL = url.pathToFileURL || urlPolyfills.pathToFileURL;\
|
|
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';\nimport { moduleRegEx } from './constants.ts';\nimport importMetaResolve from './lib/import-meta-resolve.ts';\nimport * as urlPolyfills from './lib/urlFileUrl.ts';\nimport type { Context } from './types.ts';\nimport startsWith from 'starts-with';\n\nconst resolveSync = (resolve.default ?? resolve).sync;\n\nconst useCJS = !module.createRequire;\nconst fileURLToPath = url.fileURLToPath || urlPolyfills.fileURLToPath;\nconst pathToFileURL = url.pathToFileURL || urlPolyfills.pathToFileURL;\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 (startsWith(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 entryURL = importMetaResolve(specifier, pathToFileURL(parentPath));\n if (entryURL) return fileURLToPath(entryURL);\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","moduleRegEx","importMetaResolve","urlPolyfills","startsWith","resolveSync","default","sync","useCJS","createRequire","fileURLToPath","pathToFileURL","getParentPath","context","parentPath","dirname","parentURL","toPath","process","cwd","specifier","join","test","entryURL","_","entryPath","basedir","extensions"],"mappings":"AAAA,OAAOA,gBAAgB,cAAc;AACrC,OAAOC,YAAY,SAAS;AAC5B,OAAOC,UAAU,OAAO;AACxB,YAAYC,aAAa,UAAU;AACnC,OAAOC,SAAS,MAAM;AACtB,SAASC,WAAW,QAAQ,iBAAiB;AAC7C,OAAOC,uBAAuB,+BAA+B;AAC7D,YAAYC,kBAAkB,sBAAsB;AAEpD,OAAOC,gBAAgB,cAAc;IAEhBL;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,IAAIP,aAAaO,aAAa;AACrE,MAAMC,gBAAgBX,IAAIW,aAAa,IAAIR,aAAaQ,aAAa;AAErE,SAASC,cAAcC,OAAgB;IACrC,IAAIA,QAAQC,UAAU,EAAE,OAAOhB,KAAKiB,OAAO,CAACF,QAAQC,UAAU;IAC9D,OAAOD,QAAQG,SAAS,GAAGlB,KAAKiB,OAAO,CAACE,OAAOJ,QAAQG,SAAS,KAAKE,QAAQC,GAAG;AAClF;AAEA,eAAe,SAASF,OAAOG,SAAiB,EAAEP,OAAiB;IACjE,IAAIT,WAAWgB,WAAW,UAAU,OAAOV,cAAcU;IACzD,IAAIxB,WAAWwB,YAAY,OAAOA;IAClC,IAAIA,SAAS,CAAC,EAAE,KAAK,KAAK;QACxB,MAAMN,aAAaD,UAAUD,cAAcC,WAAWK,QAAQC,GAAG;QACjE,OAAOrB,KAAKuB,IAAI,CAACP,YAAYM;IAC/B;IACA,IAAInB,YAAYqB,IAAI,CAACF,YAAY;QAC/B,MAAMN,aAAaD,UAAUD,cAAcC,WAAWK,QAAQC,GAAG;QACjE,IAAI,CAACX,QAAQ;YACX,IAAI;gBACF,MAAMe,WAAWrB,kBAAkBkB,WAAWT,cAAcG;gBAC5D,IAAIS,UAAU,OAAOb,cAAca;YACrC,EAAE,OAAOC,GAAG;YACV,0CAA0C,GAC5C;QACF;QACA,MAAMC,YAAYpB,YAAYe,WAAW;YACvCM,SAASZ;YACTa,YAAY;gBAAC;gBAAO;gBAAS;gBAAS;aAAO;QAC/C;QACA,IAAIF,WAAW,OAAOA;IACxB;IAEA,OAAOL;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.19",
|
|
4
4
|
"description": "Typescript transformers for swc. Supports Node >= 0.8",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"matcher",
|
|
@@ -29,10 +29,9 @@
|
|
|
29
29
|
"./package.json": "./package.json"
|
|
30
30
|
},
|
|
31
31
|
"main": "dist/cjs/index.js",
|
|
32
|
-
"types": "dist/cjs/index.d.
|
|
32
|
+
"types": "dist/cjs/index.d.ts",
|
|
33
33
|
"files": [
|
|
34
34
|
"dist",
|
|
35
|
-
"scripts",
|
|
36
35
|
"assets"
|
|
37
36
|
],
|
|
38
37
|
"scripts": {
|
|
@@ -57,6 +56,7 @@
|
|
|
57
56
|
"read-tsconfig-sync": "*",
|
|
58
57
|
"resolve": "*",
|
|
59
58
|
"rimraf2": "*",
|
|
59
|
+
"starts-with": "^1.0.2",
|
|
60
60
|
"test-match": "*",
|
|
61
61
|
"ts-node": "*",
|
|
62
62
|
"typescript": "*"
|
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';
|