ts-swc-transform 2.8.1 → 2.9.0
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/prepareSWCOptions.js +2 -1
- package/dist/cjs/lib/prepareSWCOptions.js.map +1 -1
- package/dist/cjs/lib/resolveCJS.d.cts +17 -0
- package/dist/cjs/lib/resolveCJS.d.ts +17 -0
- package/dist/cjs/lib/resolveCJS.js +80 -0
- package/dist/cjs/lib/resolveCJS.js.map +1 -0
- package/dist/cjs/lib/resolveESM.d.cts +19 -0
- package/dist/cjs/lib/resolveESM.d.ts +19 -0
- package/dist/cjs/lib/resolveESM.js +180 -0
- package/dist/cjs/lib/resolveESM.js.map +1 -0
- package/dist/cjs/lib/transformFile.d.cts +1 -1
- package/dist/cjs/lib/transformFile.d.ts +1 -1
- package/dist/cjs/lib/transformFile.js.map +1 -1
- package/dist/cjs/toPath.js +27 -22
- package/dist/cjs/toPath.js.map +1 -1
- package/dist/cjs/transformDirectory.d.cts +4 -1
- package/dist/cjs/transformDirectory.d.ts +4 -1
- package/dist/cjs/transformDirectory.js.map +1 -1
- package/dist/cjs/transformTypes.d.cts +4 -1
- package/dist/cjs/transformTypes.d.ts +4 -1
- package/dist/cjs/transformTypes.js.map +1 -1
- package/dist/cjs/workers/transformDirectory.d.cts +1 -1
- package/dist/cjs/workers/transformDirectory.d.ts +1 -1
- package/dist/cjs/workers/transformDirectory.js +1 -4
- package/dist/cjs/workers/transformDirectory.js.map +1 -1
- package/dist/cjs/workers/transformTypes.d.cts +1 -1
- package/dist/cjs/workers/transformTypes.d.ts +1 -1
- package/dist/cjs/workers/transformTypes.js +2 -8
- package/dist/cjs/workers/transformTypes.js.map +1 -1
- package/dist/esm/lib/prepareSWCOptions.js +3 -2
- package/dist/esm/lib/prepareSWCOptions.js.map +1 -1
- package/dist/esm/lib/resolveCJS.d.ts +17 -0
- package/dist/esm/lib/resolveCJS.js +28 -0
- package/dist/esm/lib/resolveCJS.js.map +1 -0
- package/dist/esm/lib/resolveESM.d.ts +19 -0
- package/dist/esm/lib/resolveESM.js +163 -0
- package/dist/esm/lib/resolveESM.js.map +1 -0
- package/dist/esm/lib/transformFile.d.ts +1 -1
- package/dist/esm/lib/transformFile.js.map +1 -1
- package/dist/esm/toPath.js +26 -21
- package/dist/esm/toPath.js.map +1 -1
- package/dist/esm/transformDirectory.d.ts +4 -1
- package/dist/esm/transformDirectory.js.map +1 -1
- package/dist/esm/transformTypes.d.ts +4 -1
- package/dist/esm/transformTypes.js.map +1 -1
- package/dist/esm/workers/transformDirectory.d.ts +1 -1
- package/dist/esm/workers/transformDirectory.js +1 -4
- package/dist/esm/workers/transformDirectory.js.map +1 -1
- package/dist/esm/workers/transformTypes.d.ts +1 -1
- package/dist/esm/workers/transformTypes.js +2 -8
- package/dist/esm/workers/transformTypes.js.map +1 -1
- package/package.json +4 -3
- package/dist/cjs/lib/resolve-with-exports.d.cts +0 -16
- package/dist/cjs/lib/resolve-with-exports.d.ts +0 -16
- package/dist/cjs/lib/resolve-with-exports.js +0 -157
- package/dist/cjs/lib/resolve-with-exports.js.map +0 -1
- package/dist/esm/lib/resolve-with-exports.d.ts +0 -16
- package/dist/esm/lib/resolve-with-exports.js +0 -140
- package/dist/esm/lib/resolve-with-exports.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/transformTypes.ts"],"sourcesContent":["import path from 'path';\nimport loadConfigSync from 'read-tsconfig-sync';\nimport url from 'url';\n\nconst major = +process.versions.node.split('.')[0];\nconst version = major < 14 ? 'stable' : 'local';\nconst __dirname = path.dirname(typeof __filename === 'undefined' ? url.fileURLToPath(import.meta.url) : __filename);\nconst workerPath = path.join(__dirname, '..', 'cjs', 'workers', 'transformTypes.js');\n\nimport Module from 'module';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\nfunction dispatch(version, src, dest, options, callback) {\n if (version === 'local') return _require(workerPath)(src, dest, options, callback);\n try {\n callback(null, _require('node-version-call')({ version, callbacks: true }, workerPath, src, dest, options));\n } catch (err) {\n callback(err);\n }\n}\n\nimport type { ConfigOptions, TransformTypesCallback } from './types.ts';\nexport default function transformTypes(src: string, dest: string, options?: ConfigOptions | TransformTypesCallback, callback?: TransformTypesCallback):
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/transformTypes.ts"],"sourcesContent":["import path from 'path';\nimport loadConfigSync from 'read-tsconfig-sync';\nimport url from 'url';\n\nconst major = +process.versions.node.split('.')[0];\nconst version = major < 14 ? 'stable' : 'local';\nconst __dirname = path.dirname(typeof __filename === 'undefined' ? url.fileURLToPath(import.meta.url) : __filename);\nconst workerPath = path.join(__dirname, '..', 'cjs', 'workers', 'transformTypes.js');\n\nimport Module from 'module';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\nfunction dispatch(version, src, dest, options, callback) {\n if (version === 'local') return _require(workerPath)(src, dest, options, callback);\n try {\n callback(null, _require('node-version-call')({ version, callbacks: true }, workerPath, src, dest, options));\n } catch (err) {\n callback(err);\n }\n}\n\nimport type { ConfigOptions, TransformTypesCallback } from './types.ts';\n\nexport default function transformTypes(src: string, dest: string, callback: TransformTypesCallback): void;\nexport default function transformTypes(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback): void;\nexport default function transformTypes(src: string, dest: string): Promise<string[]>;\nexport default function transformTypes(src: string, dest: string, options: ConfigOptions): Promise<string[]>;\nexport default function transformTypes(src: string, dest: string, options?: ConfigOptions | TransformTypesCallback, callback?: TransformTypesCallback): void | Promise<string[]> {\n try {\n if (typeof src !== 'string') throw new Error('transformTypes: unexpected source');\n if (typeof dest !== 'string') throw new Error('transformTypes: unexpected destination directory');\n\n if (typeof options === 'function') {\n callback = options as TransformTypesCallback;\n options = null;\n }\n options = (options || {}) as ConfigOptions;\n const tsconfig = options.tsconfig ? options.tsconfig : loadConfigSync(src);\n options = { tsconfig, ...options };\n\n if (typeof callback === 'function') return dispatch(version, src, dest, options, callback);\n return new Promise((resolve, reject) => dispatch(version, src, dest, options, (err, result) => (err ? reject(err) : resolve(result))));\n } catch (err) {\n if (callback) callback(err);\n else return Promise.reject(err);\n }\n}\n"],"names":["transformTypes","major","process","versions","node","split","version","__dirname","path","dirname","__filename","url","fileURLToPath","workerPath","join","_require","require","Module","createRequire","dispatch","src","dest","options","callback","callbacks","err","Error","tsconfig","loadConfigSync","Promise","resolve","reject","result"],"mappings":";;;;+BA4BA;;;eAAwBA;;;2DA5BP;uEACU;0DACX;6DAOG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AALnB,IAAMC,QAAQ,CAACC,QAAQC,QAAQ,CAACC,IAAI,CAACC,KAAK,CAAC,IAAI,CAAC,EAAE;AAClD,IAAMC,UAAUL,QAAQ,KAAK,WAAW;AACxC,IAAMM,YAAYC,aAAI,CAACC,OAAO,CAAC,OAAOC,eAAe,cAAcC,YAAG,CAACC,aAAa,CAAC,uDAAmBF;AACxG,IAAMG,aAAaL,aAAI,CAACM,IAAI,CAACP,WAAW,MAAM,OAAO,WAAW;AAIhE,IAAMQ,WAAW,OAAOC,YAAY,cAAcC,eAAM,CAACC,aAAa,CAAC,uDAAmBF;AAE1F,SAASG,SAASb,OAAO,EAAEc,GAAG,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IACrD,IAAIjB,YAAY,SAAS,OAAOS,SAASF,YAAYO,KAAKC,MAAMC,SAASC;IACzE,IAAI;QACFA,SAAS,MAAMR,SAAS,qBAAqB;YAAET,SAAAA;YAASkB,WAAW;QAAK,GAAGX,YAAYO,KAAKC,MAAMC;IACpG,EAAE,OAAOG,KAAK;QACZF,SAASE;IACX;AACF;AAQe,SAASzB,eAAeoB,GAAW,EAAEC,IAAY,EAAEC,OAAgD,EAAEC,QAAiC;IACnJ,IAAI;QACF,IAAI,OAAOH,QAAQ,UAAU,MAAM,IAAIM,MAAM;QAC7C,IAAI,OAAOL,SAAS,UAAU,MAAM,IAAIK,MAAM;QAE9C,IAAI,OAAOJ,YAAY,YAAY;YACjCC,WAAWD;YACXA,UAAU;QACZ;QACAA,UAAWA,WAAW,CAAC;QACvB,IAAMK,WAAWL,QAAQK,QAAQ,GAAGL,QAAQK,QAAQ,GAAGC,IAAAA,yBAAc,EAACR;QACtEE,UAAU;YAAEK,UAAAA;WAAaL;QAEzB,IAAI,OAAOC,aAAa,YAAY,OAAOJ,SAASb,SAASc,KAAKC,MAAMC,SAASC;QACjF,OAAO,IAAIM,QAAQ,SAACC,SAASC;mBAAWZ,SAASb,SAASc,KAAKC,MAAMC,SAAS,SAACG,KAAKO;uBAAYP,MAAMM,OAAON,OAAOK,QAAQE;;;IAC9H,EAAE,OAAOP,KAAK;QACZ,IAAIF,UAAUA,SAASE;aAClB,OAAOI,QAAQE,MAAM,CAACN;IAC7B;AACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { ConfigOptions, TargetType, TransformDirectoryCallback } from '../types.js';
|
|
2
|
-
export default function transformDirectoryWorker(src: string, dest: string, type: TargetType, options: ConfigOptions, callback: TransformDirectoryCallback):
|
|
2
|
+
export default function transformDirectoryWorker(src: string, dest: string, type: TargetType, options: ConfigOptions, callback: TransformDirectoryCallback): void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { ConfigOptions, TargetType, TransformDirectoryCallback } from '../types.js';
|
|
2
|
-
export default function transformDirectoryWorker(src: string, dest: string, type: TargetType, options: ConfigOptions, callback: TransformDirectoryCallback):
|
|
2
|
+
export default function transformDirectoryWorker(src: string, dest: string, type: TargetType, options: ConfigOptions, callback: TransformDirectoryCallback): void;
|
|
@@ -85,10 +85,7 @@ function transformDirectoryWorker(src, dest, type, options, callback) {
|
|
|
85
85
|
if (ext && _constantsts.extensions.indexOf(ext) < 0) return;
|
|
86
86
|
entries.push(entry);
|
|
87
87
|
}, function(err) {
|
|
88
|
-
if (err)
|
|
89
|
-
callback(err);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
88
|
+
if (err) return callback(err);
|
|
92
89
|
var results = [];
|
|
93
90
|
options = _object_spread_props(_object_spread({}, options), {
|
|
94
91
|
tsconfig: tsconfig
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformDirectory.ts"],"sourcesContent":["import Iterator, { type Entry } from 'fs-iterator';\nimport path from 'path';\nimport Queue from 'queue-cb';\n\nimport { extensions, typeFileRegEx } from '../constants.ts';\nimport createMatcher from '../createMatcher.ts';\nimport transformFile from '../lib/transformFile.ts';\n\nimport type { ConfigOptions, TargetType, TransformDirectoryCallback } from '../types.ts';\n\nexport default function transformDirectoryWorker(src: string, dest: string, type: TargetType, options: ConfigOptions, callback: TransformDirectoryCallback)
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformDirectory.ts"],"sourcesContent":["import Iterator, { type Entry } from 'fs-iterator';\nimport path from 'path';\nimport Queue from 'queue-cb';\n\nimport { extensions, typeFileRegEx } from '../constants.ts';\nimport createMatcher from '../createMatcher.ts';\nimport transformFile from '../lib/transformFile.ts';\n\nimport type { ConfigOptions, TargetType, TransformDirectoryCallback } from '../types.ts';\n\nexport default function transformDirectoryWorker(src: string, dest: string, type: TargetType, options: ConfigOptions, callback: TransformDirectoryCallback) {\n const tsconfig = options.tsconfig;\n const matcher = createMatcher(tsconfig);\n\n const entries: Entry[] = [];\n const iterator = new Iterator(src);\n iterator.forEach(\n (entry: Entry): void => {\n if (!entry.stats.isFile()) return;\n if (entry.basename[0] === '.') return;\n if (typeFileRegEx.test(entry.basename)) return;\n if (!matcher(entry.fullPath)) return;\n const ext = path.extname(entry.basename);\n if (ext && extensions.indexOf(ext) < 0) return;\n entries.push(entry);\n },\n (err) => {\n if (err) return callback(err);\n const results = [];\n options = { ...options, tsconfig };\n\n const queue = new Queue();\n entries.forEach((entry: Entry) => {\n queue.defer((cb) =>\n transformFile(entry, dest, type, options, (err, outPath) => {\n if (err) return cb(err);\n results.push(path.normalize(outPath));\n if (options.sourceMaps) results.push(`${path.normalize(outPath)}.map`);\n cb();\n })\n );\n });\n queue.await((err) => (err ? callback(err) : callback(null, results)));\n }\n );\n}\n"],"names":["transformDirectoryWorker","src","dest","type","options","callback","tsconfig","matcher","createMatcher","entries","iterator","Iterator","forEach","entry","stats","isFile","basename","typeFileRegEx","test","fullPath","ext","path","extname","extensions","indexOf","push","err","results","queue","Queue","defer","cb","transformFile","outPath","normalize","sourceMaps","await"],"mappings":";;;;+BAUA;;;eAAwBA;;;iEAVa;2DACpB;8DACC;2BAEwB;sEAChB;sEACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIX,SAASA,yBAAyBC,GAAW,EAAEC,IAAY,EAAEC,IAAgB,EAAEC,OAAsB,EAAEC,QAAoC;IACxJ,IAAMC,WAAWF,QAAQE,QAAQ;IACjC,IAAMC,UAAUC,IAAAA,wBAAa,EAACF;IAE9B,IAAMG,UAAmB,EAAE;IAC3B,IAAMC,WAAW,IAAIC,mBAAQ,CAACV;IAC9BS,SAASE,OAAO,CACd,SAACC;QACC,IAAI,CAACA,MAAMC,KAAK,CAACC,MAAM,IAAI;QAC3B,IAAIF,MAAMG,QAAQ,CAAC,EAAE,KAAK,KAAK;QAC/B,IAAIC,0BAAa,CAACC,IAAI,CAACL,MAAMG,QAAQ,GAAG;QACxC,IAAI,CAACT,QAAQM,MAAMM,QAAQ,GAAG;QAC9B,IAAMC,MAAMC,aAAI,CAACC,OAAO,CAACT,MAAMG,QAAQ;QACvC,IAAII,OAAOG,uBAAU,CAACC,OAAO,CAACJ,OAAO,GAAG;QACxCX,QAAQgB,IAAI,CAACZ;IACf,GACA,SAACa;QACC,IAAIA,KAAK,OAAOrB,SAASqB;QACzB,IAAMC,UAAU,EAAE;QAClBvB,UAAU,wCAAKA;YAASE,UAAAA;;QAExB,IAAMsB,QAAQ,IAAIC,gBAAK;QACvBpB,QAAQG,OAAO,CAAC,SAACC;YACfe,MAAME,KAAK,CAAC,SAACC;uBACXC,IAAAA,wBAAa,EAACnB,OAAOX,MAAMC,MAAMC,SAAS,SAACsB,KAAKO;oBAC9C,IAAIP,KAAK,OAAOK,GAAGL;oBACnBC,QAAQF,IAAI,CAACJ,aAAI,CAACa,SAAS,CAACD;oBAC5B,IAAI7B,QAAQ+B,UAAU,EAAER,QAAQF,IAAI,CAAC,AAAC,GAA0B,OAAxBJ,aAAI,CAACa,SAAS,CAACD,UAAS;oBAChEF;gBACF;;QAEJ;QACAH,MAAMQ,KAAK,CAAC,SAACV;mBAASA,MAAMrB,SAASqB,OAAOrB,SAAS,MAAMsB;;IAC7D;AAEJ"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { ConfigOptions, TransformTypesCallback } from '../types.js';
|
|
2
|
-
export default function transformTypesWorker(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback):
|
|
2
|
+
export default function transformTypesWorker(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback): void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { ConfigOptions, TransformTypesCallback } from '../types.js';
|
|
2
|
-
export default function transformTypesWorker(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback):
|
|
2
|
+
export default function transformTypesWorker(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback): void;
|
|
@@ -136,10 +136,7 @@ function transformTypesWorker(src, dest, options, callback) {
|
|
|
136
136
|
}, {
|
|
137
137
|
concurrency: Infinity
|
|
138
138
|
}, function(err) {
|
|
139
|
-
if (err)
|
|
140
|
-
callback(err);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
139
|
+
if (err) return callback(err);
|
|
143
140
|
// Step 1: Stat all source files to get their modes (async)
|
|
144
141
|
var sourceModes = new Map();
|
|
145
142
|
var statQueue = new _queuecb.default();
|
|
@@ -152,10 +149,7 @@ function transformTypesWorker(src, dest, options, callback) {
|
|
|
152
149
|
});
|
|
153
150
|
});
|
|
154
151
|
statQueue.await(function(statErr) {
|
|
155
|
-
if (statErr)
|
|
156
|
-
callback(statErr);
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
152
|
+
if (statErr) return callback(statErr);
|
|
159
153
|
// Step 2: TypeScript emit (inherently sync - cannot change)
|
|
160
154
|
var compilerOptions = ts.convertCompilerOptionsFromJson(tsconfig.config.compilerOptions, '');
|
|
161
155
|
var config = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformTypes.ts"],"sourcesContent":["import fs from 'fs';\nimport Iterator, { type Entry } from 'fs-iterator';\nimport Module from 'module';\nimport path from 'path';\nimport Queue from 'queue-cb';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\nimport { stringEndsWith } from '../compat.ts';\nimport { typeFileRegEx } from '../constants.ts';\nimport createMatcher from '../createMatcher.ts';\nimport { rewriteExtensions } from '../lib/rewriteExtensions.ts';\n\nimport type { ConfigOptions, TransformTypesCallback } from '../types.ts';\n\nexport default function transformTypesWorker(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback)
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformTypes.ts"],"sourcesContent":["import fs from 'fs';\nimport Iterator, { type Entry } from 'fs-iterator';\nimport Module from 'module';\nimport path from 'path';\nimport Queue from 'queue-cb';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\nimport { stringEndsWith } from '../compat.ts';\nimport { typeFileRegEx } from '../constants.ts';\nimport createMatcher from '../createMatcher.ts';\nimport { rewriteExtensions } from '../lib/rewriteExtensions.ts';\n\nimport type { ConfigOptions, TransformTypesCallback } from '../types.ts';\n\nexport default function transformTypesWorker(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback) {\n const tsconfig = options.tsconfig;\n const matcher = createMatcher(tsconfig);\n const ts = _require('typescript');\n\n const entries = [];\n const iterator = new Iterator(src);\n iterator.forEach(\n (entry: Entry): void => {\n if (!entry.stats.isFile()) return;\n if (entry.basename[0] === '.') return;\n if (typeFileRegEx.test(entry.basename)) return;\n if (!matcher(entry.fullPath)) return;\n entries.push(entry);\n },\n { concurrency: Infinity },\n (err) => {\n if (err) return callback(err);\n\n // Step 1: Stat all source files to get their modes (async)\n const sourceModes = new Map<string, number>();\n const statQueue = new Queue();\n entries.forEach((entry) => {\n statQueue.defer((cb) => {\n fs.stat(entry.fullPath, (statErr, stats) => {\n if (!statErr) sourceModes.set(entry.fullPath, stats.mode);\n cb(); // Continue even on error\n });\n });\n });\n\n statQueue.await((statErr) => {\n if (statErr) return callback(statErr);\n\n // Step 2: TypeScript emit (inherently sync - cannot change)\n const compilerOptions = ts.convertCompilerOptionsFromJson(tsconfig.config.compilerOptions, '');\n const config = {\n fileNames: entries.map((entry) => entry.fullPath),\n options: {\n ...compilerOptions.options,\n outDir: dest,\n noEmit: false,\n allowJs: true,\n declaration: true,\n emitDeclarationOnly: true,\n listEmittedFiles: true,\n },\n projectReferences: tsconfig.config.references,\n };\n const { fileNames, options, projectReferences } = config;\n const host = ts.createCompilerHostWorker(options, /*setParentNodes*/ undefined, ts.sys);\n const programOptions = {\n rootNames: fileNames,\n options,\n projectReferences,\n host,\n configFileParsingDiagnostics: ts.getConfigFileParsingDiagnostics({ fileNames, options }),\n };\n const program = ts.createProgram(programOptions);\n const res = program.emit();\n\n // Step 3: Post-process emitted files (async)\n const postQueue = new Queue();\n\n if (res.emittedFiles) {\n res.emittedFiles.forEach((file) => {\n // 3a: Rewrite extensions (convert from sync to async)\n // TODO: remove patch for https://github.com/microsoft/TypeScript/issues/61037\n if (compilerOptions.options.rewriteRelativeImportExtensions) {\n if (stringEndsWith(file, '.d.ts') || stringEndsWith(file, '.d.cts') || stringEndsWith(file, '.d.mts')) {\n postQueue.defer((cb) => {\n fs.readFile(file, 'utf8', (readErr, content) => {\n if (readErr) return cb(); // Ignore errors, continue\n const updated = rewriteExtensions(content);\n if (updated !== content) {\n fs.writeFile(file, updated, 'utf8', () => cb()); // Ignore write errors\n } else {\n cb();\n }\n });\n });\n }\n }\n\n // 3b: Apply executable permissions from source files\n if (stringEndsWith(file, '.d.ts') || stringEndsWith(file, '.d.cts') || stringEndsWith(file, '.d.mts')) {\n const relativePath = path.relative(dest, file);\n const baseName = relativePath.replace(/\\.d\\.(ts|mts|cts)$/, '');\n\n for (const [srcPath, mode] of sourceModes) {\n const srcRelative = path.relative(src, srcPath);\n const srcBase = srcRelative.replace(/\\.(ts|tsx|mts|cts)$/, '');\n if (baseName === srcBase) {\n const execBits = mode & 0o111;\n if (execBits) {\n postQueue.defer((cb) => {\n fs.chmod(file, 0o644 | execBits, () => cb()); // Ignore chmod errors\n });\n }\n break;\n }\n }\n }\n });\n }\n\n postQueue.await(() => callback(null, res.emittedFiles));\n });\n }\n );\n}\n"],"names":["transformTypesWorker","_require","require","Module","createRequire","src","dest","options","callback","tsconfig","matcher","createMatcher","ts","entries","iterator","Iterator","forEach","entry","stats","isFile","basename","typeFileRegEx","test","fullPath","push","concurrency","Infinity","err","sourceModes","Map","statQueue","Queue","defer","cb","fs","stat","statErr","set","mode","await","compilerOptions","convertCompilerOptionsFromJson","config","fileNames","map","outDir","noEmit","allowJs","declaration","emitDeclarationOnly","listEmittedFiles","projectReferences","references","host","createCompilerHostWorker","undefined","sys","programOptions","rootNames","configFileParsingDiagnostics","getConfigFileParsingDiagnostics","program","createProgram","res","emit","postQueue","emittedFiles","file","rewriteRelativeImportExtensions","stringEndsWith","readFile","readErr","content","updated","rewriteExtensions","writeFile","relativePath","path","relative","baseName","replace","srcPath","srcRelative","srcBase","execBits","chmod"],"mappings":";;;;+BAeA;;;eAAwBA;;;yDAfT;iEACsB;6DAClB;2DACF;8DACC;wBAIa;2BACD;sEAC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;oCACpD,IAAMwC,UAAUC,IAAAA,sCAAiB,EAACF;oCAClC,IAAIC,YAAYD,SAAS;wCACvBtC,WAAE,CAACyC,SAAS,CAACR,MAAMM,SAAS,QAAQ;mDAAMxC;4CAAO,sBAAsB;oCACzE,OAAO;wCACLA;oCACF;gCACF;4BACF;wBACF;oBACF;oBAEA,qDAAqD;oBACrD,IAAIoC,IAAAA,wBAAc,EAACF,MAAM,YAAYE,IAAAA,wBAAc,EAACF,MAAM,aAAaE,IAAAA,wBAAc,EAACF,MAAM,WAAW;wBACrG,IAAMS,eAAeC,aAAI,CAACC,QAAQ,CAACxE,MAAM6D;wBACzC,IAAMY,WAAWH,aAAaI,OAAO,CAAC,sBAAsB;4BAEvD,kCAAA,2BAAA;;;gCAAA,mCAAA,iBAAOC,0BAAS3C;gCACnB,IAAM4C,cAAcL,aAAI,CAACC,QAAQ,CAACzE,KAAK4E;gCACvC,IAAME,UAAUD,YAAYF,OAAO,CAAC,uBAAuB;gCAC3D,IAAID,aAAaI,SAAS;oCACxB,IAAMC,WAAW9C,OAAO;oCACxB,IAAI8C,UAAU;wCACZnB,UAAUjC,KAAK,CAAC,SAACC;4CACfC,WAAE,CAACmD,KAAK,CAAClB,MAAM,MAAQiB,UAAU;uDAAMnD;gDAAO,sBAAsB;wCACtE;oCACF;oCACA,OAAA;gCACF;4BACF;4BAZA,QAAK,YAAyBL,gCAAzB,SAAA,6BAAA,QAAA,yBAAA;;;;;4BAAA;4BAAA;;;qCAAA,6BAAA;oCAAA;;;oCAAA;0CAAA;;;;oBAaP;gBACF;YACF;YAEAqC,UAAU1B,KAAK,CAAC;uBAAM/B,SAAS,MAAMuD,IAAIG,YAAY;;QACvD;IACF;AAEJ"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { installSync } from 'install-optional';
|
|
1
|
+
import { installSync, matchesLibc } from 'install-optional';
|
|
2
2
|
import debounce from 'lodash.debounce';
|
|
3
3
|
import Module from 'module';
|
|
4
4
|
import path from 'path';
|
|
@@ -11,7 +11,8 @@ const installSyncSWC = debounce(installSync, 300, {
|
|
|
11
11
|
});
|
|
12
12
|
export default function prepareSWCOptions(tsconfig) {
|
|
13
13
|
installSyncSWC('@swc/core', `${process.platform}-${process.arch}`, {
|
|
14
|
-
cwd: __dirname
|
|
14
|
+
cwd: __dirname,
|
|
15
|
+
filter: matchesLibc
|
|
15
16
|
});
|
|
16
17
|
try {
|
|
17
18
|
const ts = _require('typescript');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/prepareSWCOptions.ts"],"sourcesContent":["import type { Options } from '@swc/core';\nimport { installSync } from 'install-optional';\nimport debounce from 'lodash.debounce';\nimport Module from 'module';\nimport path from 'path';\nimport url from 'url';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\nconst __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : url.fileURLToPath(import.meta.url));\n\nimport type { TSConfig } from '../types.ts';\n\nexport interface TranspilerOptions {\n tsxOptions: Options;\n nonTsxOptions: Options;\n}\n\nconst installSyncSWC = debounce(installSync, 300, { leading: true, trailing: false });\n\nexport default function prepareSWCOptions(tsconfig: TSConfig): TranspilerOptions {\n installSyncSWC('@swc/core', `${process.platform}-${process.arch}`, { cwd: __dirname });\n try {\n const ts = _require('typescript');\n const swc = _require('@swc/core');\n const transpiler = _require('ts-node/transpilers/swc');\n const parsed = ts.parseJsonConfigFileContent(tsconfig.config, ts.sys, path.dirname(tsconfig.path));\n return transpiler.createSwcOptions(parsed.options, undefined, swc, 'swc');\n } catch (err) {\n console.log(`prepareSWCOptions failed: ${err.message}`);\n return {} as TranspilerOptions;\n }\n}\n"],"names":["installSync","debounce","Module","path","url","_require","require","createRequire","__dirname","dirname","__filename","fileURLToPath","installSyncSWC","leading","trailing","prepareSWCOptions","tsconfig","process","platform","arch","cwd","ts","swc","transpiler","parsed","parseJsonConfigFileContent","config","sys","createSwcOptions","options","undefined","err","console","log","message"],"mappings":"AACA,SAASA,WAAW,QAAQ,mBAAmB;
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/prepareSWCOptions.ts"],"sourcesContent":["import type { Options } from '@swc/core';\nimport { installSync, matchesLibc } from 'install-optional';\nimport debounce from 'lodash.debounce';\nimport Module from 'module';\nimport path from 'path';\nimport url from 'url';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\nconst __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : url.fileURLToPath(import.meta.url));\n\nimport type { TSConfig } from '../types.ts';\n\nexport interface TranspilerOptions {\n tsxOptions: Options;\n nonTsxOptions: Options;\n}\n\nconst installSyncSWC = debounce(installSync, 300, { leading: true, trailing: false });\n\nexport default function prepareSWCOptions(tsconfig: TSConfig): TranspilerOptions {\n installSyncSWC('@swc/core', `${process.platform}-${process.arch}`, { cwd: __dirname, filter: matchesLibc });\n try {\n const ts = _require('typescript');\n const swc = _require('@swc/core');\n const transpiler = _require('ts-node/transpilers/swc');\n const parsed = ts.parseJsonConfigFileContent(tsconfig.config, ts.sys, path.dirname(tsconfig.path));\n return transpiler.createSwcOptions(parsed.options, undefined, swc, 'swc');\n } catch (err) {\n console.log(`prepareSWCOptions failed: ${err.message}`);\n return {} as TranspilerOptions;\n }\n}\n"],"names":["installSync","matchesLibc","debounce","Module","path","url","_require","require","createRequire","__dirname","dirname","__filename","fileURLToPath","installSyncSWC","leading","trailing","prepareSWCOptions","tsconfig","process","platform","arch","cwd","filter","ts","swc","transpiler","parsed","parseJsonConfigFileContent","config","sys","createSwcOptions","options","undefined","err","console","log","message"],"mappings":"AACA,SAASA,WAAW,EAAEC,WAAW,QAAQ,mBAAmB;AAC5D,OAAOC,cAAc,kBAAkB;AACvC,OAAOC,YAAY,SAAS;AAC5B,OAAOC,UAAU,OAAO;AACxB,OAAOC,SAAS,MAAM;AAEtB,MAAMC,WAAW,OAAOC,YAAY,cAAcJ,OAAOK,aAAa,CAAC,YAAYH,GAAG,IAAIE;AAC1F,MAAME,YAAYL,KAAKM,OAAO,CAAC,OAAOC,eAAe,cAAcA,aAAaN,IAAIO,aAAa,CAAC,YAAYP,GAAG;AASjH,MAAMQ,iBAAiBX,SAASF,aAAa,KAAK;IAAEc,SAAS;IAAMC,UAAU;AAAM;AAEnF,eAAe,SAASC,kBAAkBC,QAAkB;IAC1DJ,eAAe,aAAa,GAAGK,QAAQC,QAAQ,CAAC,CAAC,EAAED,QAAQE,IAAI,EAAE,EAAE;QAAEC,KAAKZ;QAAWa,QAAQrB;IAAY;IACzG,IAAI;QACF,MAAMsB,KAAKjB,SAAS;QACpB,MAAMkB,MAAMlB,SAAS;QACrB,MAAMmB,aAAanB,SAAS;QAC5B,MAAMoB,SAASH,GAAGI,0BAA0B,CAACV,SAASW,MAAM,EAAEL,GAAGM,GAAG,EAAEzB,KAAKM,OAAO,CAACO,SAASb,IAAI;QAChG,OAAOqB,WAAWK,gBAAgB,CAACJ,OAAOK,OAAO,EAAEC,WAAWR,KAAK;IACrE,EAAE,OAAOS,KAAK;QACZC,QAAQC,GAAG,CAAC,CAAC,0BAA0B,EAAEF,IAAIG,OAAO,EAAE;QACtD,OAAO,CAAC;IACV;AACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CJS resolver
|
|
3
|
+
*
|
|
4
|
+
* Uses the `resolve` npm package for CommonJS-style module resolution.
|
|
5
|
+
* Handles main field, index.js, etc.
|
|
6
|
+
*
|
|
7
|
+
* Note: CJS does not support subpath imports (#prefix) - those are ESM-only.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Resolve a CJS specifier to an absolute file path
|
|
11
|
+
*
|
|
12
|
+
* @param specifier - The require specifier (e.g., 'lodash', 'lodash/get')
|
|
13
|
+
* @param basedir - The directory to resolve from
|
|
14
|
+
* @returns The resolved absolute file path
|
|
15
|
+
* @throws Error if module cannot be found
|
|
16
|
+
*/
|
|
17
|
+
export default function resolveCJS(specifier: string, basedir: string): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CJS resolver
|
|
3
|
+
*
|
|
4
|
+
* Uses the `resolve` npm package for CommonJS-style module resolution.
|
|
5
|
+
* Handles main field, index.js, etc.
|
|
6
|
+
*
|
|
7
|
+
* Note: CJS does not support subpath imports (#prefix) - those are ESM-only.
|
|
8
|
+
*/ import * as resolve from 'resolve';
|
|
9
|
+
var _resolve_default;
|
|
10
|
+
const resolveSync = ((_resolve_default = resolve.default) !== null && _resolve_default !== void 0 ? _resolve_default : resolve).sync;
|
|
11
|
+
/**
|
|
12
|
+
* Resolve a CJS specifier to an absolute file path
|
|
13
|
+
*
|
|
14
|
+
* @param specifier - The require specifier (e.g., 'lodash', 'lodash/get')
|
|
15
|
+
* @param basedir - The directory to resolve from
|
|
16
|
+
* @returns The resolved absolute file path
|
|
17
|
+
* @throws Error if module cannot be found
|
|
18
|
+
*/ export default function resolveCJS(specifier, basedir) {
|
|
19
|
+
return resolveSync(specifier, {
|
|
20
|
+
basedir,
|
|
21
|
+
extensions: [
|
|
22
|
+
'.js',
|
|
23
|
+
'.json',
|
|
24
|
+
'.node',
|
|
25
|
+
'.mjs'
|
|
26
|
+
]
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/resolveCJS.ts"],"sourcesContent":["/**\n * CJS resolver\n *\n * Uses the `resolve` npm package for CommonJS-style module resolution.\n * Handles main field, index.js, etc.\n *\n * Note: CJS does not support subpath imports (#prefix) - those are ESM-only.\n */\n\nimport * as resolve from 'resolve';\n\nconst resolveSync = (resolve.default ?? resolve).sync;\n\n/**\n * Resolve a CJS specifier to an absolute file path\n *\n * @param specifier - The require specifier (e.g., 'lodash', 'lodash/get')\n * @param basedir - The directory to resolve from\n * @returns The resolved absolute file path\n * @throws Error if module cannot be found\n */\nexport default function resolveCJS(specifier: string, basedir: string): string {\n return resolveSync(specifier, {\n basedir,\n extensions: ['.js', '.json', '.node', '.mjs'],\n });\n}\n"],"names":["resolve","resolveSync","default","sync","resolveCJS","specifier","basedir","extensions"],"mappings":"AAAA;;;;;;;CAOC,GAED,YAAYA,aAAa,UAAU;IAEdA;AAArB,MAAMC,cAAc,EAACD,mBAAAA,QAAQE,OAAO,cAAfF,8BAAAA,mBAAmBA,SAASG,IAAI;AAErD;;;;;;;CAOC,GACD,eAAe,SAASC,WAAWC,SAAiB,EAAEC,OAAe;IACnE,OAAOL,YAAYI,WAAW;QAC5BC;QACAC,YAAY;YAAC;YAAO;YAAS;YAAS;SAAO;IAC/C;AACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Resolve an ESM specifier to an absolute file path
|
|
13
|
+
*
|
|
14
|
+
* @param specifier - The import specifier (e.g., 'lodash', 'lodash/get', '#internal')
|
|
15
|
+
* @param parentPath - The file path of the importing module
|
|
16
|
+
* @param conditions - Export conditions (defaults to ['node', 'import'])
|
|
17
|
+
* @returns The resolved absolute file path, or null if not resolvable
|
|
18
|
+
*/
|
|
19
|
+
export default function resolveESM(specifier: string, parentPath: string, conditions?: string[]): string | null;
|
|
@@ -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;;;;;;;;;CAS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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,3 +1,3 @@
|
|
|
1
1
|
import type { Entry } from 'fs-iterator';
|
|
2
2
|
import type { ConfigOptions, TargetType, TransformFileCallback } from '../types.js';
|
|
3
|
-
export default function transformFile(entry: Entry, dest: string, type: TargetType, options: ConfigOptions, callback: TransformFileCallback):
|
|
3
|
+
export default function transformFile(entry: Entry, dest: string, type: TargetType, options: ConfigOptions, callback: TransformFileCallback): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/transformFile.ts"],"sourcesContent":["import type { Output } from '@swc/core';\nimport fs from 'fs';\nimport type { Entry } from 'fs-iterator';\nimport mkdirp from 'mkdirp-classic';\nimport Module from 'module';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport patchCJS from '../lib/patchCJS.ts';\nimport patchESM from '../lib/patchESM.ts';\nimport prepareSWCOptions from '../lib/prepareSWCOptions.ts';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\nimport type { ConfigOptions, TargetType, TransformFileCallback } from '../types.ts';\n\nexport default function transformFile(entry: Entry, dest: string, type: TargetType, options: ConfigOptions, callback: TransformFileCallback):
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/lib/transformFile.ts"],"sourcesContent":["import type { Output } from '@swc/core';\nimport fs from 'fs';\nimport type { Entry } from 'fs-iterator';\nimport mkdirp from 'mkdirp-classic';\nimport Module from 'module';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport patchCJS from '../lib/patchCJS.ts';\nimport patchESM from '../lib/patchESM.ts';\nimport prepareSWCOptions from '../lib/prepareSWCOptions.ts';\n\nconst _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;\n\nimport type { ConfigOptions, TargetType, TransformFileCallback } from '../types.ts';\n\nexport default function transformFile(entry: Entry, dest: string, type: TargetType, options: ConfigOptions, callback: TransformFileCallback): void {\n let tsconfig = options.tsconfig;\n\n // overrides for cjs\n if (type === 'cjs') {\n tsconfig = { ...tsconfig };\n tsconfig.config = { ...tsconfig.config };\n tsconfig.config.compilerOptions = { ...(tsconfig.config.compilerOptions || {}) };\n tsconfig.config.compilerOptions.module = 'commonjs';\n tsconfig.config.compilerOptions.target = 'es5';\n }\n\n const swcOptions = prepareSWCOptions(tsconfig);\n const swc = _require('@swc/core');\n const ext = path.extname(entry.basename);\n\n swc\n .transformFile(entry.fullPath, {\n ...(ext === '.tsx' || ext === '.jsx' ? swcOptions.tsxOptions : swcOptions.nonTsxOptions),\n filename: entry.basename,\n })\n .then((output: Output) => {\n // Get source file mode to preserve executable permissions\n fs.stat(entry.fullPath, (statErr, stats) => {\n if (statErr) return callback(statErr);\n\n const extTarget = type === 'esm' ? patchESM(entry, output, options) : patchCJS(entry, output, options);\n const ext = path.extname(entry.path);\n const outPath = path.join(dest, (ext ? entry.path.slice(0, -ext.length) : entry.path) + extTarget);\n\n mkdirp(path.dirname(outPath), () => {\n const queue = new Queue();\n queue.defer(fs.writeFile.bind(null, outPath, output.code, 'utf8'));\n if (output.map && options.sourceMaps) queue.defer(fs.writeFile.bind(null, `${outPath}.map`, output.map, 'utf8'));\n queue.await((err) => {\n if (err) return callback(err);\n\n // Preserve executable permissions from source (only +x bits, not full mode)\n const execBits = stats.mode & 0o111;\n if (execBits) {\n fs.chmod(outPath, 0o644 | execBits, (_chmodErr) => {\n // Ignore chmod errors (e.g., on Windows)\n callback(null, outPath);\n });\n } else {\n callback(null, outPath);\n }\n });\n });\n });\n })\n .catch(callback);\n}\n"],"names":["fs","mkdirp","Module","path","Queue","patchCJS","patchESM","prepareSWCOptions","_require","require","createRequire","url","transformFile","entry","dest","type","options","callback","tsconfig","config","compilerOptions","module","target","swcOptions","swc","ext","extname","basename","fullPath","tsxOptions","nonTsxOptions","filename","then","output","stat","statErr","stats","extTarget","outPath","join","slice","length","dirname","queue","defer","writeFile","bind","code","map","sourceMaps","await","err","execBits","mode","chmod","_chmodErr","catch"],"mappings":"AACA,OAAOA,QAAQ,KAAK;AAEpB,OAAOC,YAAY,iBAAiB;AACpC,OAAOC,YAAY,SAAS;AAC5B,OAAOC,UAAU,OAAO;AACxB,OAAOC,WAAW,WAAW;AAC7B,OAAOC,cAAc,qBAAqB;AAC1C,OAAOC,cAAc,qBAAqB;AAC1C,OAAOC,uBAAuB,8BAA8B;AAE5D,MAAMC,WAAW,OAAOC,YAAY,cAAcP,OAAOQ,aAAa,CAAC,YAAYC,GAAG,IAAIF;AAI1F,eAAe,SAASG,cAAcC,KAAY,EAAEC,IAAY,EAAEC,IAAgB,EAAEC,OA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6C,SAAS,CAACC,IAAI,CAAC,MAAMR,SAASL,OAAOc,IAAI,EAAE;gBAC1D,IAAId,OAAOe,GAAG,IAAIhC,QAAQiC,UAAU,EAAEN,MAAMC,KAAK,CAAC5C,GAAG6C,SAAS,CAACC,IAAI,CAAC,MAAM,GAAGR,QAAQ,IAAI,CAAC,EAAEL,OAAOe,GAAG,EAAE;gBACxGL,MAAMO,KAAK,CAAC,CAACC;oBACX,IAAIA,KAAK,OAAOlC,SAASkC;oBAEzB,4EAA4E;oBAC5E,MAAMC,WAAWhB,MAAMiB,IAAI,GAAG;oBAC9B,IAAID,UAAU;wBACZpD,GAAGsD,KAAK,CAAChB,SAAS,QAAQc,UAAU,CAACG;4BACnC,yCAAyC;4BACzCtC,SAAS,MAAMqB;wBACjB;oBACF,OAAO;wBACLrB,SAAS,MAAMqB;oBACjB;gBACF;YACF;QACF;IACF,GACCkB,KAAK,CAACvC;AACX"}
|
package/dist/esm/toPath.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
35
|
-
|
|
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
|
}
|
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
|
|
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,OAAOD,QAAQG,SAAS,GAAGd,KAAKa,OAAO,CAACE,OAAOJ,QAAQG,S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{ ConfigOptions, TargetType, TransformDirectoryCallback } from './types.js';
|
|
2
|
-
export default function transformDirectory(src: string, dest: string, type: TargetType,
|
|
2
|
+
export default function transformDirectory(src: string, dest: string, type: TargetType, callback: TransformDirectoryCallback): void;
|
|
3
|
+
export default function transformDirectory(src: string, dest: string, type: TargetType, options: ConfigOptions, callback: TransformDirectoryCallback): void;
|
|
4
|
+
export default function transformDirectory(src: string, dest: string, type: TargetType): Promise<string[]>;
|
|
5
|
+
export default function transformDirectory(src: string, dest: string, type: TargetType, options: ConfigOptions): Promise<string[]>;
|