ts-swc-transform 2.11.2 → 2.11.4
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/transformFile.d.cts +1 -1
- package/dist/cjs/lib/transformFile.d.ts +1 -1
- package/dist/cjs/lib/transformFile.js +15 -19
- package/dist/cjs/lib/transformFile.js.map +1 -1
- package/dist/cjs/workers/transformDirectory.js +10 -2
- package/dist/cjs/workers/transformDirectory.js.map +1 -1
- package/dist/cjs/workers/transformTypes.js +167 -127
- package/dist/cjs/workers/transformTypes.js.map +1 -1
- package/dist/esm/lib/transformFile.d.ts +1 -1
- package/dist/esm/lib/transformFile.js +15 -19
- package/dist/esm/lib/transformFile.js.map +1 -1
- package/dist/esm/workers/transformDirectory.js +10 -2
- package/dist/esm/workers/transformDirectory.js.map +1 -1
- package/dist/esm/workers/transformTypes.js +132 -83
- package/dist/esm/workers/transformTypes.js.map +1 -1
- package/package.json +3 -1
|
@@ -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): void;
|
|
3
|
+
export default function transformFile(entry: Entry, dest: string, type: TargetType, options: ConfigOptions, mode: number | undefined, callback: TransformFileCallback): void;
|
|
@@ -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): void;
|
|
3
|
+
export default function transformFile(entry: Entry, dest: string, type: TargetType, options: ConfigOptions, mode: number | undefined, callback: TransformFileCallback): void;
|
|
@@ -74,7 +74,7 @@ function _object_spread_props(target, source) {
|
|
|
74
74
|
return target;
|
|
75
75
|
}
|
|
76
76
|
var _require = typeof require === 'undefined' ? _module.default.createRequire(require("url").pathToFileURL(__filename).toString()) : require;
|
|
77
|
-
function transformFile(entry, dest, type, options, callback) {
|
|
77
|
+
function transformFile(entry, dest, type, options, mode, callback) {
|
|
78
78
|
var tsconfig = options.tsconfig;
|
|
79
79
|
// overrides for cjs
|
|
80
80
|
if (type === 'cjs') {
|
|
@@ -90,29 +90,25 @@ function transformFile(entry, dest, type, options, callback) {
|
|
|
90
90
|
swc.transformFile(entry.fullPath, _object_spread_props(_object_spread({}, ext === '.tsx' || ext === '.jsx' ? swcOptions.tsxOptions : swcOptions.nonTsxOptions), {
|
|
91
91
|
filename: entry.basename
|
|
92
92
|
})).then(function(output) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
var
|
|
98
|
-
|
|
99
|
-
(
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
if (err) return callback(err);
|
|
105
|
-
// Preserve executable permissions from source (only +x bits, not full mode)
|
|
106
|
-
var execBits = stats.mode & 73;
|
|
93
|
+
var extTarget = type === 'esm' ? (0, _patchESMts.default)(entry, output, options) : (0, _patchCJSts.default)(entry, output, options);
|
|
94
|
+
var ext = _path.default.extname(entry.path);
|
|
95
|
+
var outPath = _path.default.join(dest, (ext ? entry.path.slice(0, -ext.length) : entry.path) + extTarget);
|
|
96
|
+
(0, _mkdirpclassic.default)(_path.default.dirname(outPath), function() {
|
|
97
|
+
var queue = new _queuecb.default();
|
|
98
|
+
queue.defer(_fs.default.writeFile.bind(null, outPath, output.code, 'utf8'));
|
|
99
|
+
if (output.map && options.sourceMaps) queue.defer(_fs.default.writeFile.bind(null, "".concat(outPath, ".map"), output.map, 'utf8'));
|
|
100
|
+
queue.await(function(err) {
|
|
101
|
+
if (err) return callback(err);
|
|
102
|
+
if (mode) {
|
|
103
|
+
var execBits = mode & 73;
|
|
107
104
|
if (execBits) {
|
|
108
105
|
_fs.default.chmod(outPath, 420 | execBits, function(_chmodErr) {
|
|
109
|
-
// Ignore chmod errors (e.g., on Windows)
|
|
110
106
|
callback(null, outPath);
|
|
111
107
|
});
|
|
112
|
-
|
|
113
|
-
callback(null, outPath);
|
|
108
|
+
return;
|
|
114
109
|
}
|
|
115
|
-
}
|
|
110
|
+
}
|
|
111
|
+
callback(null, outPath);
|
|
116
112
|
});
|
|
117
113
|
});
|
|
118
114
|
}).catch(callback);
|
|
@@ -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): 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
|
|
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, mode: number | undefined, 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 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 if (mode) {\n const execBits = mode & 0o111;\n if (execBits) {\n fs.chmod(outPath, 0o644 | execBits, (_chmodErr) => {\n callback(null, outPath);\n });\n return;\n }\n }\n callback(null, outPath);\n });\n });\n })\n .catch(callback);\n}\n"],"names":["transformFile","_require","require","Module","createRequire","entry","dest","type","options","mode","callback","tsconfig","config","compilerOptions","module","target","swcOptions","prepareSWCOptions","swc","ext","path","extname","basename","fullPath","tsxOptions","nonTsxOptions","filename","then","output","extTarget","patchESM","patchCJS","outPath","join","slice","length","mkdirp","dirname","queue","Queue","defer","fs","writeFile","bind","code","map","sourceMaps","await","err","execBits","chmod","_chmodErr","catch"],"mappings":";;;;+BAeA;;;eAAwBA;;;yDAdT;oEAEI;6DACA;2DACF;8DACC;iEACG;iEACA;0EACS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE9B,IAAMC,WAAW,OAAOC,YAAY,cAAcC,eAAM,CAACC,aAAa,CAAC,uDAAmBF;AAI3E,SAASF,cAAcK,KAAY,EAAEC,IAAY,EAAEC,IAAgB,EAAEC,OAAsB,EAAEC,IAAwB,EAAEC,QAA+B;IACnK,IAAIC,WAAWH,QAAQG,QAAQ;IAE/B,oBAAoB;IACpB,IAAIJ,SAAS,OAAO;QAClBI,WAAW,mBAAKA;QAChBA,SAASC,MAAM,GAAG,mBAAKD,SAASC,MAAM;QACtCD,SAASC,MAAM,CAACC,eAAe,GAAG,mBAAMF,SAASC,MAAM,CAACC,eAAe,IAAI,CAAC;QAC5EF,SAASC,MAAM,CAACC,eAAe,CAACC,MAAM,GAAG;QACzCH,SAASC,MAAM,CAACC,eAAe,CAACE,MAAM,GAAG;IAC3C;IAEA,IAAMC,aAAaC,IAAAA,4BAAiB,EAACN;IACrC,IAAMO,MAAMjB,SAAS;IACrB,IAAMkB,MAAMC,aAAI,CAACC,OAAO,CAAChB,MAAMiB,QAAQ;IAEvCJ,IACGlB,aAAa,CAACK,MAAMkB,QAAQ,EAAE,wCACzBJ,QAAQ,UAAUA,QAAQ,SAASH,WAAWQ,UAAU,GAAGR,WAAWS,aAAa;QACvFC,UAAUrB,MAAMiB,QAAQ;QAEzBK,IAAI,CAAC,SAACC;QACL,IAAMC,YAAYtB,SAAS,QAAQuB,IAAAA,mBAAQ,EAACzB,OAAOuB,QAAQpB,WAAWuB,IAAAA,mBAAQ,EAAC1B,OAAOuB,QAAQpB;QAC9F,IAAMW,MAAMC,aAAI,CAACC,OAAO,CAAChB,MAAMe,IAAI;QACnC,IAAMY,UAAUZ,aAAI,CAACa,IAAI,CAAC3B,MAAM,AAACa,CAAAA,MAAMd,MAAMe,IAAI,CAACc,KAAK,CAAC,GAAG,CAACf,IAAIgB,MAAM,IAAI9B,MAAMe,IAAI,AAAD,IAAKS;QAExFO,IAAAA,sBAAM,EAAChB,aAAI,CAACiB,OAAO,CAACL,UAAU;YAC5B,IAAMM,QAAQ,IAAIC,gBAAK;YACvBD,MAAME,KAAK,CAACC,WAAE,CAACC,SAAS,CAACC,IAAI,CAAC,MAAMX,SAASJ,OAAOgB,IAAI,EAAE;YAC1D,IAAIhB,OAAOiB,GAAG,IAAIrC,QAAQsC,UAAU,EAAER,MAAME,KAAK,CAACC,WAAE,CAACC,SAAS,CAACC,IAAI,CAAC,MAAM,AAAC,GAAU,OAARX,SAAQ,SAAOJ,OAAOiB,GAAG,EAAE;YACxGP,MAAMS,KAAK,CAAC,SAACC;gBACX,IAAIA,KAAK,OAAOtC,SAASsC;gBAEzB,IAAIvC,MAAM;oBACR,IAAMwC,WAAWxC,OAAO;oBACxB,IAAIwC,UAAU;wBACZR,WAAE,CAACS,KAAK,CAAClB,SAAS,MAAQiB,UAAU,SAACE;4BACnCzC,SAAS,MAAMsB;wBACjB;wBACA;oBACF;gBACF;gBACAtB,SAAS,MAAMsB;YACjB;QACF;IACF,GACCoB,KAAK,CAAC1C;AACX"}
|
|
@@ -75,7 +75,10 @@ function transformDirectoryWorker(src, dest, type, options, callback) {
|
|
|
75
75
|
var tsconfig = options.tsconfig;
|
|
76
76
|
var matcher = (0, _createMatcherts.default)(tsconfig);
|
|
77
77
|
var entries = [];
|
|
78
|
-
var
|
|
78
|
+
var modeByPath = new Map();
|
|
79
|
+
var iterator = new _fsiterator.default(src, {
|
|
80
|
+
alwaysStat: true
|
|
81
|
+
});
|
|
79
82
|
iterator.forEach(function(entry) {
|
|
80
83
|
if (!entry.stats.isFile()) return;
|
|
81
84
|
if (entry.basename[0] === '.') return;
|
|
@@ -84,6 +87,10 @@ function transformDirectoryWorker(src, dest, type, options, callback) {
|
|
|
84
87
|
var ext = _path.default.extname(entry.basename);
|
|
85
88
|
if (ext && _constantsts.extensions.indexOf(ext) < 0) return;
|
|
86
89
|
entries.push(entry);
|
|
90
|
+
var stats = entry.stats;
|
|
91
|
+
if (stats.mode) {
|
|
92
|
+
modeByPath.set(entry.fullPath, stats.mode);
|
|
93
|
+
}
|
|
87
94
|
}, function(err) {
|
|
88
95
|
if (err) return callback(err);
|
|
89
96
|
var results = [];
|
|
@@ -92,8 +99,9 @@ function transformDirectoryWorker(src, dest, type, options, callback) {
|
|
|
92
99
|
});
|
|
93
100
|
var queue = new _queuecb.default();
|
|
94
101
|
entries.forEach(function(entry) {
|
|
102
|
+
var mode = modeByPath.get(entry.fullPath);
|
|
95
103
|
queue.defer(function(cb) {
|
|
96
|
-
return (0, _transformFilets.default)(entry, dest, type, options, function(err, outPath) {
|
|
104
|
+
return (0, _transformFilets.default)(entry, dest, type, options, mode, function(err, outPath) {
|
|
97
105
|
if (err) return cb(err);
|
|
98
106
|
results.push(_path.default.normalize(outPath));
|
|
99
107
|
if (options.sourceMaps) results.push("".concat(_path.default.normalize(outPath), ".map"));
|
|
@@ -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) {\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":";;;;+
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformDirectory.ts"],"sourcesContent":["import type fs from 'fs';\nimport 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 modeByPath = new Map<string, number>();\n const iterator = new Iterator(src, { alwaysStat: true });\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 const stats = entry.stats as fs.Stats;\n if (stats.mode) {\n modeByPath.set(entry.fullPath, stats.mode);\n }\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 const mode = modeByPath.get(entry.fullPath);\n queue.defer((cb) =>\n transformFile(entry, dest, type, options, mode, (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","modeByPath","Map","iterator","Iterator","alwaysStat","forEach","entry","stats","isFile","basename","typeFileRegEx","test","fullPath","ext","path","extname","extensions","indexOf","push","mode","set","err","results","queue","Queue","get","defer","cb","transformFile","outPath","normalize","sourceMaps","await"],"mappings":";;;;+BAWA;;;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,aAAa,IAAIC;IACvB,I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2B;;IAC7D;AAEJ"}
|
|
@@ -8,12 +8,14 @@ Object.defineProperty(exports, "default", {
|
|
|
8
8
|
return transformTypesWorker;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
+
var _crossspawncb = /*#__PURE__*/ _interop_require_default(require("cross-spawn-cb"));
|
|
11
12
|
var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
|
|
12
13
|
var _fsiterator = /*#__PURE__*/ _interop_require_default(require("fs-iterator"));
|
|
13
|
-
var
|
|
14
|
+
var _fsremovecompat = require("fs-remove-compat");
|
|
15
|
+
var _os = /*#__PURE__*/ _interop_require_default(require("os"));
|
|
14
16
|
var _path = /*#__PURE__*/ _interop_require_default(require("path"));
|
|
15
17
|
var _queuecb = /*#__PURE__*/ _interop_require_default(require("queue-cb"));
|
|
16
|
-
var
|
|
18
|
+
var _resolvebinsync = /*#__PURE__*/ _interop_require_default(require("resolve-bin-sync"));
|
|
17
19
|
var _constantsts = require("../constants.js");
|
|
18
20
|
var _createMatcherts = /*#__PURE__*/ _interop_require_default(require("../createMatcher.js"));
|
|
19
21
|
var _rewriteExtensionsts = require("../lib/rewriteExtensions.js");
|
|
@@ -22,8 +24,8 @@ function _array_like_to_array(arr, len) {
|
|
|
22
24
|
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
23
25
|
return arr2;
|
|
24
26
|
}
|
|
25
|
-
function
|
|
26
|
-
if (Array.isArray(arr)) return arr;
|
|
27
|
+
function _array_without_holes(arr) {
|
|
28
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
27
29
|
}
|
|
28
30
|
function _define_property(obj, key, value) {
|
|
29
31
|
if (key in obj) {
|
|
@@ -43,32 +45,11 @@ function _interop_require_default(obj) {
|
|
|
43
45
|
default: obj
|
|
44
46
|
};
|
|
45
47
|
}
|
|
46
|
-
function
|
|
47
|
-
|
|
48
|
-
if (_i == null) return;
|
|
49
|
-
var _arr = [];
|
|
50
|
-
var _n = true;
|
|
51
|
-
var _d = false;
|
|
52
|
-
var _s, _e;
|
|
53
|
-
try {
|
|
54
|
-
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
55
|
-
_arr.push(_s.value);
|
|
56
|
-
if (i && _arr.length === i) break;
|
|
57
|
-
}
|
|
58
|
-
} catch (err) {
|
|
59
|
-
_d = true;
|
|
60
|
-
_e = err;
|
|
61
|
-
} finally{
|
|
62
|
-
try {
|
|
63
|
-
if (!_n && _i["return"] != null) _i["return"]();
|
|
64
|
-
} finally{
|
|
65
|
-
if (_d) throw _e;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return _arr;
|
|
48
|
+
function _iterable_to_array(iter) {
|
|
49
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
69
50
|
}
|
|
70
|
-
function
|
|
71
|
-
throw new TypeError("Invalid attempt to
|
|
51
|
+
function _non_iterable_spread() {
|
|
52
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
72
53
|
}
|
|
73
54
|
function _object_spread(target) {
|
|
74
55
|
for(var i = 1; i < arguments.length; i++){
|
|
@@ -109,8 +90,8 @@ function _object_spread_props(target, source) {
|
|
|
109
90
|
}
|
|
110
91
|
return target;
|
|
111
92
|
}
|
|
112
|
-
function
|
|
113
|
-
return
|
|
93
|
+
function _to_consumable_array(arr) {
|
|
94
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
114
95
|
}
|
|
115
96
|
function _unsupported_iterable_to_array(o, minLen) {
|
|
116
97
|
if (!o) return;
|
|
@@ -120,115 +101,169 @@ function _unsupported_iterable_to_array(o, minLen) {
|
|
|
120
101
|
if (n === "Map" || n === "Set") return Array.from(n);
|
|
121
102
|
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
122
103
|
}
|
|
123
|
-
var
|
|
104
|
+
var _ref;
|
|
105
|
+
var _os_cpus;
|
|
106
|
+
var concurrency = Math.min(64, Math.max(8, ((_ref = (_os_cpus = _os.default.cpus()) === null || _os_cpus === void 0 ? void 0 : _os_cpus.length) !== null && _ref !== void 0 ? _ref : 4) * 8));
|
|
107
|
+
var tscPath = (0, _resolvebinsync.default)('typescript', 'tsc');
|
|
108
|
+
var tsgoPath = null;
|
|
109
|
+
try {
|
|
110
|
+
tsgoPath = (0, _resolvebinsync.default)('@typescript/native-preview', 'tsgo');
|
|
111
|
+
} catch (unused) {
|
|
112
|
+
tsgoPath = null;
|
|
113
|
+
}
|
|
114
|
+
/* ---------------- root file filtering ---------------- */ function isAllowedRootFile(basename) {
|
|
115
|
+
return basename.endsWith('.d.ts') || basename.endsWith('.d.mts') || basename.endsWith('.d.cts') || basename.endsWith('.ts') || basename.endsWith('.tsx') || basename.endsWith('.mts') || basename.endsWith('.cts') || basename.endsWith('.js') || basename.endsWith('.jsx') || basename.endsWith('.mjs') || basename.endsWith('.cjs');
|
|
116
|
+
}
|
|
117
|
+
/* ---------------- compiler execution ---------------- */ function runCompiler(cmdPath, args, cb) {
|
|
118
|
+
(0, _crossspawncb.default)(process.execPath, [
|
|
119
|
+
cmdPath
|
|
120
|
+
].concat(_to_consumable_array(args)), {
|
|
121
|
+
encoding: 'utf8'
|
|
122
|
+
}, cb);
|
|
123
|
+
}
|
|
124
|
+
function runTsgoThenTsc(args, cb) {
|
|
125
|
+
if (tsgoPath) {
|
|
126
|
+
runCompiler(tsgoPath, args, function(err, res) {
|
|
127
|
+
// Prefer status code over "err" for deciding success/fallback
|
|
128
|
+
if (!err && res.status === 0) return cb(null, res);
|
|
129
|
+
runCompiler(tscPath, args, cb);
|
|
130
|
+
});
|
|
131
|
+
} else {
|
|
132
|
+
runCompiler(tscPath, args, cb);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/* ---------------- emitted file parsing ---------------- */ function parseEmittedFiles(res, dest) {
|
|
136
|
+
var out = [];
|
|
137
|
+
var seen = new Set();
|
|
138
|
+
var lines = "".concat(res.stdout, "\n").concat(res.stderr).split(/\r?\n/);
|
|
139
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
140
|
+
try {
|
|
141
|
+
for(var _iterator = lines[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
142
|
+
var line = _step.value;
|
|
143
|
+
line = line.trim();
|
|
144
|
+
if (!line) continue;
|
|
145
|
+
if (/^TSFILE:\s+/i.test(line)) line = line.replace(/^TSFILE:\s+/i, '').trim();
|
|
146
|
+
if (!_path.default.isAbsolute(line)) continue;
|
|
147
|
+
if (!(line.endsWith('.d.ts') || line.endsWith('.d.mts') || line.endsWith('.d.cts'))) continue;
|
|
148
|
+
// Optional safety: only accept outputs under dest
|
|
149
|
+
var rel = _path.default.relative(dest, line);
|
|
150
|
+
if (rel.startsWith('..') || _path.default.isAbsolute(rel)) continue;
|
|
151
|
+
if (!seen.has(line)) {
|
|
152
|
+
seen.add(line);
|
|
153
|
+
out.push(line);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} catch (err) {
|
|
157
|
+
_didIteratorError = true;
|
|
158
|
+
_iteratorError = err;
|
|
159
|
+
} finally{
|
|
160
|
+
try {
|
|
161
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
162
|
+
_iterator.return();
|
|
163
|
+
}
|
|
164
|
+
} finally{
|
|
165
|
+
if (_didIteratorError) {
|
|
166
|
+
throw _iteratorError;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
124
172
|
function transformTypesWorker(src, dest, options, callback) {
|
|
125
173
|
var tsconfig = options.tsconfig;
|
|
126
174
|
var matcher = (0, _createMatcherts.default)(tsconfig);
|
|
127
|
-
var
|
|
128
|
-
var entries = [];
|
|
175
|
+
var rootFiles = [];
|
|
129
176
|
var iterator = new _fsiterator.default(src);
|
|
130
177
|
iterator.forEach(function(entry) {
|
|
131
178
|
if (!entry.stats.isFile()) return;
|
|
132
179
|
if (entry.basename[0] === '.') return;
|
|
133
180
|
if (_constantsts.typeFileRegEx.test(entry.basename)) return;
|
|
181
|
+
if (!isAllowedRootFile(entry.basename)) return;
|
|
134
182
|
if (!matcher(entry.fullPath)) return;
|
|
135
|
-
|
|
183
|
+
rootFiles.push(entry.fullPath);
|
|
136
184
|
}, {
|
|
137
|
-
concurrency:
|
|
138
|
-
}, function(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
var
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
});
|
|
149
|
-
});
|
|
185
|
+
concurrency: concurrency
|
|
186
|
+
}, function(err1) {
|
|
187
|
+
var _tsconfig_config_compilerOptions;
|
|
188
|
+
if (err1) return callback(err1);
|
|
189
|
+
if (rootFiles.length === 0) return callback(null, []);
|
|
190
|
+
var compilerOptions = _object_spread_props(_object_spread({}, tsconfig.config.compilerOptions), {
|
|
191
|
+
outDir: dest,
|
|
192
|
+
noEmit: false,
|
|
193
|
+
allowJs: true,
|
|
194
|
+
declaration: true,
|
|
195
|
+
emitDeclarationOnly: true
|
|
150
196
|
});
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
197
|
+
var rewrite = ((_tsconfig_config_compilerOptions = tsconfig.config.compilerOptions) === null || _tsconfig_config_compilerOptions === void 0 ? void 0 : _tsconfig_config_compilerOptions.rewriteRelativeImportExtensions) === true;
|
|
198
|
+
// Avoid collisions across concurrent runs.
|
|
199
|
+
var tempDir = _path.default.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));
|
|
200
|
+
var tempConfigPath = _path.default.join(tempDir, 'tsconfig.json');
|
|
201
|
+
var tempConfig = _object_spread({
|
|
202
|
+
compilerOptions: compilerOptions,
|
|
203
|
+
files: rootFiles,
|
|
204
|
+
include: [],
|
|
205
|
+
exclude: []
|
|
206
|
+
}, tsconfig.config.references && {
|
|
207
|
+
references: tsconfig.config.references
|
|
208
|
+
});
|
|
209
|
+
_fs.default.mkdir(tempDir, {
|
|
210
|
+
recursive: true
|
|
211
|
+
}, function(mkdirErr) {
|
|
212
|
+
if (mkdirErr) return callback(mkdirErr);
|
|
213
|
+
_fs.default.writeFile(tempConfigPath, JSON.stringify(tempConfig, null, 2), 'utf8', function(writeErr) {
|
|
214
|
+
if (writeErr) {
|
|
215
|
+
(0, _fsremovecompat.safeRm)(tempDir, {
|
|
216
|
+
recursive: true,
|
|
217
|
+
force: true
|
|
218
|
+
}, function() {
|
|
219
|
+
return callback(writeErr);
|
|
220
|
+
});
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
var args = [
|
|
224
|
+
'--project',
|
|
225
|
+
tempConfigPath,
|
|
226
|
+
'--listEmittedFiles',
|
|
227
|
+
'--pretty',
|
|
228
|
+
'false'
|
|
229
|
+
];
|
|
230
|
+
runTsgoThenTsc(args, function(runErr, res) {
|
|
231
|
+
if (runErr || res.status !== 0) {
|
|
232
|
+
var _ref;
|
|
233
|
+
var msg = "TypeScript compiler failed (status=".concat(res === null || res === void 0 ? void 0 : res.status, ").\n").concat("stderr:\n".concat(String((_ref = res === null || res === void 0 ? void 0 : res.stderr) !== null && _ref !== void 0 ? _ref : '')).slice(0, 20000));
|
|
234
|
+
(0, _fsremovecompat.safeRm)(tempDir, {
|
|
235
|
+
recursive: true,
|
|
236
|
+
force: true
|
|
237
|
+
}, function() {
|
|
238
|
+
return callback(runErr !== null && runErr !== void 0 ? runErr : new Error(msg));
|
|
239
|
+
});
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
var emittedFiles = parseEmittedFiles(res, dest);
|
|
243
|
+
if (emittedFiles.length === 0) {
|
|
244
|
+
(0, _fsremovecompat.safeRm)(tempDir, {
|
|
245
|
+
recursive: true,
|
|
246
|
+
force: true
|
|
247
|
+
}, function() {
|
|
248
|
+
return callback(new Error('TypeScript compiler produced no emitted declaration files'));
|
|
249
|
+
});
|
|
250
|
+
return;
|
|
205
251
|
}
|
|
206
|
-
|
|
207
|
-
if (
|
|
208
|
-
var relativePath = _path.default.relative(dest, file);
|
|
209
|
-
var baseName = relativePath.replace(/\.d\.(ts|mts|cts)$/, '');
|
|
252
|
+
var postQueue = new _queuecb.default();
|
|
253
|
+
if (rewrite) {
|
|
210
254
|
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
211
255
|
try {
|
|
212
256
|
var _loop = function() {
|
|
213
|
-
var
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return cb();
|
|
222
|
-
}); // Ignore chmod errors
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
return "break";
|
|
226
|
-
}
|
|
257
|
+
var file = _step.value;
|
|
258
|
+
postQueue.defer(function(cb) {
|
|
259
|
+
_fs.default.readFile(file, 'utf8', function(readErr, content) {
|
|
260
|
+
if (readErr) return cb();
|
|
261
|
+
var updated = (0, _rewriteExtensionsts.rewriteExtensions)(content);
|
|
262
|
+
updated === content ? cb() : _fs.default.writeFile(file, updated, 'utf8', cb);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
227
265
|
};
|
|
228
|
-
for(var _iterator =
|
|
229
|
-
var _ret = _loop();
|
|
230
|
-
if (_ret === "break") break;
|
|
231
|
-
}
|
|
266
|
+
for(var _iterator = emittedFiles[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
|
|
232
267
|
} catch (err) {
|
|
233
268
|
_didIteratorError = true;
|
|
234
269
|
_iteratorError = err;
|
|
@@ -244,10 +279,15 @@ function transformTypesWorker(src, dest, options, callback) {
|
|
|
244
279
|
}
|
|
245
280
|
}
|
|
246
281
|
}
|
|
282
|
+
postQueue.await(function() {
|
|
283
|
+
(0, _fsremovecompat.safeRm)(tempDir, {
|
|
284
|
+
recursive: true,
|
|
285
|
+
force: true
|
|
286
|
+
}, function(rmErr) {
|
|
287
|
+
return callback(rmErr || null, emittedFiles);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
247
290
|
});
|
|
248
|
-
}
|
|
249
|
-
postQueue.await(function() {
|
|
250
|
-
return callback(null, res.emittedFiles);
|
|
251
291
|
});
|
|
252
292
|
});
|
|
253
293
|
});
|
|
@@ -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) {\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;sEACJ;mCACQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AALlC,IAAMC,WAAW,OAAOC,YAAY,cAAcC,eAAM,CAACC,aAAa,CAAC,uDAAmBF;AAS3E,SAASF,qBAAqBK,GAAW,EAAEC,IAAY,EAAEC,OAAsB,EAAEC,QAAgC;IAC9H,IAAMC,WAAWF,QAAQE,QAAQ;IACjC,IAAMC,UAAUC,IAAAA,wBAAa,EAACF;IAC9B,IAAMG,KAAKX,SAAS;IAEpB,IAAMY,UAAU,EAAE;IAClB,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,CAACV,QAAQO,MAAMM,QAAQ,GAAG;QAC9BV,QAAQW,IAAI,CAACP;IACf,GACA;QAAEQ,aAAaC;IAAS,GACxB,SAACC;QACC,IAAIA,KAAK,OAAOnB,SAASmB;QAEzB,2DAA2D;QAC3D,IAAMC,cAAc,IAAIC;QACxB,IAAMC,YAAY,IAAIC,gBAAK;QAC3BlB,QAAQG,OAAO,CAAC,SAACC;YACfa,UAAUE,KAAK,CAAC,SAACC;gBACfC,WAAE,CAACC,IAAI,CAAClB,MAAMM,QAAQ,EAAE,SAACa,SAASlB;oBAChC,IAAI,CAACkB,SAASR,YAAYS,GAAG,CAACpB,MAAMM,QAAQ,EAAEL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+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformTypes.ts"],"sourcesContent":["import spawn, { type SpawnCallback, type SpawnResult } from 'cross-spawn-cb';\nimport fs from 'fs';\nimport Iterator, { type Entry } from 'fs-iterator';\nimport { safeRm } from 'fs-remove-compat';\nimport os from 'os';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport resolveBin from 'resolve-bin-sync';\n\nconst concurrency = Math.min(64, Math.max(8, (os.cpus()?.length ?? 4) * 8));\n\nconst tscPath = resolveBin('typescript', 'tsc');\nlet tsgoPath: string | null = null;\ntry {\n tsgoPath = resolveBin('@typescript/native-preview', 'tsgo');\n} catch {\n tsgoPath = null;\n}\n\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\n/* ---------------- root file filtering ---------------- */\n\nfunction isAllowedRootFile(basename: string): boolean {\n return (\n basename.endsWith('.d.ts') ||\n basename.endsWith('.d.mts') ||\n basename.endsWith('.d.cts') ||\n basename.endsWith('.ts') ||\n basename.endsWith('.tsx') ||\n basename.endsWith('.mts') ||\n basename.endsWith('.cts') ||\n basename.endsWith('.js') ||\n basename.endsWith('.jsx') ||\n basename.endsWith('.mjs') ||\n basename.endsWith('.cjs')\n );\n}\n\n/* ---------------- compiler execution ---------------- */\n\nfunction runCompiler(cmdPath: string, args: string[], cb: SpawnCallback): void {\n spawn(process.execPath, [cmdPath, ...args], { encoding: 'utf8' }, cb);\n}\n\nfunction runTsgoThenTsc(args: string[], cb: SpawnCallback): void {\n if (tsgoPath) {\n runCompiler(tsgoPath, args, (err, res) => {\n // Prefer status code over \"err\" for deciding success/fallback\n if (!err && res.status === 0) return cb(null, res);\n runCompiler(tscPath, args, cb);\n });\n } else {\n runCompiler(tscPath, args, cb);\n }\n}\n\n/* ---------------- emitted file parsing ---------------- */\n\nfunction parseEmittedFiles(res: SpawnResult, dest: string): string[] {\n const out: string[] = [];\n const seen = new Set<string>();\n const lines = `${res.stdout}\\n${res.stderr}`.split(/\\r?\\n/);\n\n for (let line of lines) {\n line = line.trim();\n if (!line) continue;\n\n if (/^TSFILE:\\s+/i.test(line)) line = line.replace(/^TSFILE:\\s+/i, '').trim();\n\n if (!path.isAbsolute(line)) continue;\n if (!(line.endsWith('.d.ts') || line.endsWith('.d.mts') || line.endsWith('.d.cts'))) continue;\n\n // Optional safety: only accept outputs under dest\n const rel = path.relative(dest, line);\n if (rel.startsWith('..') || path.isAbsolute(rel)) continue;\n\n if (!seen.has(line)) {\n seen.add(line);\n out.push(line);\n }\n }\n\n return out;\n}\n\nexport default function transformTypesWorker(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback) {\n const tsconfig = options.tsconfig;\n const matcher = createMatcher(tsconfig);\n\n const rootFiles: string[] = [];\n const iterator = new Iterator(src);\n\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 (!isAllowedRootFile(entry.basename)) return;\n if (!matcher(entry.fullPath)) return;\n\n rootFiles.push(entry.fullPath);\n },\n { concurrency },\n (err) => {\n if (err) return callback(err);\n if (rootFiles.length === 0) return callback(null, []);\n\n const compilerOptions = {\n ...tsconfig.config.compilerOptions,\n outDir: dest,\n noEmit: false,\n allowJs: true,\n declaration: true,\n emitDeclarationOnly: true,\n };\n\n const rewrite = tsconfig.config.compilerOptions?.rewriteRelativeImportExtensions === true;\n\n // Avoid collisions across concurrent runs.\n const tempDir = path.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));\n const tempConfigPath = path.join(tempDir, 'tsconfig.json');\n\n const tempConfig = {\n compilerOptions,\n files: rootFiles,\n include: [],\n exclude: [],\n ...(tsconfig.config.references && { references: tsconfig.config.references }),\n };\n\n fs.mkdir(tempDir, { recursive: true }, (mkdirErr) => {\n if (mkdirErr) return callback(mkdirErr);\n\n fs.writeFile(tempConfigPath, JSON.stringify(tempConfig, null, 2), 'utf8', (writeErr) => {\n if (writeErr) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(writeErr));\n return;\n }\n\n const args = ['--project', tempConfigPath, '--listEmittedFiles', '--pretty', 'false'];\n\n runTsgoThenTsc(args, (runErr, res) => {\n if (runErr || res.status !== 0) {\n const msg = `TypeScript compiler failed (status=${res?.status}).\\n${`stderr:\\n${String(res?.stderr ?? '')}`.slice(0, 20_000)}`;\n safeRm(tempDir, { recursive: true, force: true }, () => callback(runErr ?? new Error(msg)));\n return;\n }\n\n const emittedFiles = parseEmittedFiles(res, dest);\n if (emittedFiles.length === 0) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error('TypeScript compiler produced no emitted declaration files')));\n return;\n }\n\n const postQueue = new Queue();\n\n if (rewrite) {\n for (const file of emittedFiles) {\n postQueue.defer((cb) => {\n fs.readFile(file, 'utf8', (readErr, content) => {\n if (readErr) return cb();\n const updated = rewriteExtensions(content);\n updated === content ? cb() : fs.writeFile(file, updated, 'utf8', cb);\n });\n });\n }\n }\n\n postQueue.await(() => {\n safeRm(tempDir, { recursive: true, force: true }, (rmErr) => callback(rmErr || null, emittedFiles));\n });\n });\n });\n });\n }\n );\n}\n"],"names":["transformTypesWorker","os","concurrency","Math","min","max","cpus","length","tscPath","resolveBin","tsgoPath","isAllowedRootFile","basename","endsWith","runCompiler","cmdPath","args","cb","spawn","process","execPath","encoding","runTsgoThenTsc","err","res","status","parseEmittedFiles","dest","out","seen","Set","lines","stdout","stderr","split","line","trim","test","replace","path","isAbsolute","rel","relative","startsWith","has","add","push","src","options","callback","tsconfig","matcher","createMatcher","rootFiles","iterator","Iterator","forEach","entry","stats","isFile","typeFileRegEx","fullPath","compilerOptions","config","outDir","noEmit","allowJs","declaration","emitDeclarationOnly","rewrite","rewriteRelativeImportExtensions","tempDir","join","String","pid","Date","now","tempConfigPath","tempConfig","files","include","exclude","references","fs","mkdir","recursive","mkdirErr","writeFile","JSON","stringify","writeErr","safeRm","force","runErr","msg","slice","Error","emittedFiles","postQueue","Queue","file","defer","readFile","readErr","content","updated","rewriteExtensions","await","rmErr"],"mappings":";;;;+BA0FA;;;eAAwBA;;;mEA1FoC;yDAC7C;iEACsB;8BACd;yDACR;2DACE;8DACC;qEACK;2BAYO;sEACJ;mCACQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAZYC;AAA9C,IAAMC,cAAcC,KAAKC,GAAG,CAAC,IAAID,KAAKE,GAAG,CAAC,GAAG,UAACJ,WAAAA,WAAE,CAACK,IAAI,gBAAPL,+BAAAA,SAAWM,MAAM,uCAAI,KAAK;AAExE,IAAMC,UAAUC,IAAAA,uBAAU,EAAC,cAAc;AACzC,IAAIC,WAA0B;AAC9B,IAAI;IACFA,WAAWD,IAAAA,uBAAU,EAAC,8BAA8B;AACtD,EAAE,eAAM;IACNC,WAAW;AACb;AAQA,yDAAyD,GAEzD,SAASC,kBAAkBC,QAAgB;IACzC,OACEA,SAASC,QAAQ,CAAC,YAClBD,SAASC,QAAQ,CAAC,aAClBD,SAASC,QAAQ,CAAC,aAClBD,SAASC,QAAQ,CAAC,UAClBD,SAASC,QAAQ,CAAC,WAClBD,SAASC,QAAQ,CAAC,WAClBD,SAASC,QAAQ,CAAC,WAClBD,SAASC,QAAQ,CAAC,UAClBD,SAASC,QAAQ,CAAC,WAClBD,SAASC,QAAQ,CAAC,WAClBD,SAASC,QAAQ,CAAC;AAEtB;AAEA,wDAAwD,GAExD,SAASC,YAAYC,OAAe,EAAEC,IAAc,EAAEC,EAAiB;IACrEC,IAAAA,qBAAK,EAACC,QAAQC,QAAQ,EAAE;QAACL;KAAiB,CAAlB,OAAU,qBAAGC,QAAO;QAAEK,UAAU;IAAO,GAAGJ;AACpE;AAEA,SAASK,eAAeN,IAAc,EAAEC,EAAiB;IAC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AE,CAACmB,QAAQ,CAACF,MAAM,QAAQ,SAACG,SAASC;wCAClC,IAAID,SAAS,OAAOtF;wCACpB,IAAMwF,UAAUC,IAAAA,sCAAiB,EAACF;wCAClCC,YAAYD,UAAUvF,OAAOkE,WAAE,CAACI,SAAS,CAACa,MAAMK,SAAS,QAAQxF;oCACnE;gCACF;4BACF;4BARA,QAAK,YAAcgF,iCAAd,SAAA,6BAAA,QAAA,yBAAA;;4BAAA;4BAAA;;;qCAAA,6BAAA;oCAAA;;;oCAAA;0CAAA;;;;oBASP;oBAEAC,UAAUS,KAAK,CAAC;wBACdhB,IAAAA,sBAAM,EAACpB,SAAS;4BAAEc,WAAW;4BAAMO,OAAO;wBAAK,GAAG,SAACgB;mCAAU3D,SAAS2D,SAAS,MAAMX;;oBACvF;gBACF;YACF;QACF;IACF;AAEJ"}
|
|
@@ -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): void;
|
|
3
|
+
export default function transformFile(entry: Entry, dest: string, type: TargetType, options: ConfigOptions, mode: number | undefined, callback: TransformFileCallback): void;
|
|
@@ -7,7 +7,7 @@ import patchCJS from '../lib/patchCJS.js';
|
|
|
7
7
|
import patchESM from '../lib/patchESM.js';
|
|
8
8
|
import prepareSWCOptions from '../lib/prepareSWCOptions.js';
|
|
9
9
|
const _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;
|
|
10
|
-
export default function transformFile(entry, dest, type, options, callback) {
|
|
10
|
+
export default function transformFile(entry, dest, type, options, mode, callback) {
|
|
11
11
|
let tsconfig = options.tsconfig;
|
|
12
12
|
// overrides for cjs
|
|
13
13
|
if (type === 'cjs') {
|
|
@@ -30,29 +30,25 @@ export default function transformFile(entry, dest, type, options, callback) {
|
|
|
30
30
|
...ext === '.tsx' || ext === '.jsx' ? swcOptions.tsxOptions : swcOptions.nonTsxOptions,
|
|
31
31
|
filename: entry.basename
|
|
32
32
|
}).then((output)=>{
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
if (err) return callback(err);
|
|
45
|
-
// Preserve executable permissions from source (only +x bits, not full mode)
|
|
46
|
-
const execBits = stats.mode & 0o111;
|
|
33
|
+
const extTarget = type === 'esm' ? patchESM(entry, output, options) : patchCJS(entry, output, options);
|
|
34
|
+
const ext = path.extname(entry.path);
|
|
35
|
+
const outPath = path.join(dest, (ext ? entry.path.slice(0, -ext.length) : entry.path) + extTarget);
|
|
36
|
+
mkdirp(path.dirname(outPath), ()=>{
|
|
37
|
+
const queue = new Queue();
|
|
38
|
+
queue.defer(fs.writeFile.bind(null, outPath, output.code, 'utf8'));
|
|
39
|
+
if (output.map && options.sourceMaps) queue.defer(fs.writeFile.bind(null, `${outPath}.map`, output.map, 'utf8'));
|
|
40
|
+
queue.await((err)=>{
|
|
41
|
+
if (err) return callback(err);
|
|
42
|
+
if (mode) {
|
|
43
|
+
const execBits = mode & 0o111;
|
|
47
44
|
if (execBits) {
|
|
48
45
|
fs.chmod(outPath, 0o644 | execBits, (_chmodErr)=>{
|
|
49
|
-
// Ignore chmod errors (e.g., on Windows)
|
|
50
46
|
callback(null, outPath);
|
|
51
47
|
});
|
|
52
|
-
|
|
53
|
-
callback(null, outPath);
|
|
48
|
+
return;
|
|
54
49
|
}
|
|
55
|
-
}
|
|
50
|
+
}
|
|
51
|
+
callback(null, outPath);
|
|
56
52
|
});
|
|
57
53
|
});
|
|
58
54
|
}).catch(callback);
|
|
@@ -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): 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
|
|
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, mode: number | undefined, 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 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 if (mode) {\n const execBits = mode & 0o111;\n if (execBits) {\n fs.chmod(outPath, 0o644 | execBits, (_chmodErr) => {\n callback(null, outPath);\n });\n return;\n }\n }\n callback(null, outPath);\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","mode","callback","tsconfig","config","compilerOptions","module","target","swcOptions","swc","ext","extname","basename","fullPath","tsxOptions","nonTsxOptions","filename","then","output","extTarget","outPath","join","slice","length","dirname","queue","defer","writeFile","bind","code","map","sourceMaps","await","err","execBits","chmod","_chmodErr","catch"],"mappings":"AACA,OAAOA,QAAQ,KAAK;AAEpB,OAAOC,YAAY,iBAAiB;AACpC,OAAOC,YAAY,SAAS;AAC5B,OAA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AAK,CAAC,GAAG,CAACZ,IAAIa,MAAM,IAAI1B,MAAMV,IAAI,AAAD,IAAKgC;QAExFlC,OAAOE,KAAKqC,OAAO,CAACJ,UAAU;YAC5B,MAAMK,QAAQ,IAAIrC;YAClBqC,MAAMC,KAAK,CAAC1C,GAAG2C,SAAS,CAACC,IAAI,CAAC,MAAMR,SAASF,OAAOW,IAAI,EAAE;YAC1D,IAAIX,OAAOY,GAAG,IAAI9B,QAAQ+B,UAAU,EAAEN,MAAMC,KAAK,CAAC1C,GAAG2C,SAAS,CAACC,IAAI,CAAC,MAAM,GAAGR,QAAQ,IAAI,CAAC,EAAEF,OAAOY,GAAG,EAAE;YACxGL,MAAMO,KAAK,CAAC,CAACC;gBACX,IAAIA,KAAK,OAAO/B,SAAS+B;gBAEzB,IAAIhC,MAAM;oBACR,MAAMiC,WAAWjC,OAAO;oBACxB,IAAIiC,UAAU;wBACZlD,GAAGmD,KAAK,CAACf,SAAS,QAAQc,UAAU,CAACE;4BACnClC,SAAS,MAAMkB;wBACjB;wBACA;oBACF;gBACF;gBACAlB,SAAS,MAAMkB;YACjB;QACF;IACF,GACCiB,KAAK,CAACnC;AACX"}
|
|
@@ -8,7 +8,10 @@ export default function transformDirectoryWorker(src, dest, type, options, callb
|
|
|
8
8
|
const tsconfig = options.tsconfig;
|
|
9
9
|
const matcher = createMatcher(tsconfig);
|
|
10
10
|
const entries = [];
|
|
11
|
-
const
|
|
11
|
+
const modeByPath = new Map();
|
|
12
|
+
const iterator = new Iterator(src, {
|
|
13
|
+
alwaysStat: true
|
|
14
|
+
});
|
|
12
15
|
iterator.forEach((entry)=>{
|
|
13
16
|
if (!entry.stats.isFile()) return;
|
|
14
17
|
if (entry.basename[0] === '.') return;
|
|
@@ -17,6 +20,10 @@ export default function transformDirectoryWorker(src, dest, type, options, callb
|
|
|
17
20
|
const ext = path.extname(entry.basename);
|
|
18
21
|
if (ext && extensions.indexOf(ext) < 0) return;
|
|
19
22
|
entries.push(entry);
|
|
23
|
+
const stats = entry.stats;
|
|
24
|
+
if (stats.mode) {
|
|
25
|
+
modeByPath.set(entry.fullPath, stats.mode);
|
|
26
|
+
}
|
|
20
27
|
}, (err)=>{
|
|
21
28
|
if (err) return callback(err);
|
|
22
29
|
const results = [];
|
|
@@ -26,7 +33,8 @@ export default function transformDirectoryWorker(src, dest, type, options, callb
|
|
|
26
33
|
};
|
|
27
34
|
const queue = new Queue();
|
|
28
35
|
entries.forEach((entry)=>{
|
|
29
|
-
|
|
36
|
+
const mode = modeByPath.get(entry.fullPath);
|
|
37
|
+
queue.defer((cb)=>transformFile(entry, dest, type, options, mode, (err, outPath)=>{
|
|
30
38
|
if (err) return cb(err);
|
|
31
39
|
results.push(path.normalize(outPath));
|
|
32
40
|
if (options.sourceMaps) results.push(`${path.normalize(outPath)}.map`);
|
|
@@ -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) {\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":["Iterator","path","Queue","extensions","typeFileRegEx","createMatcher","transformFile","transformDirectoryWorker","src","dest","type","options","callback","tsconfig","matcher","entries","iterator","forEach","entry","stats","isFile","basename","test","fullPath","ext","extname","indexOf","push","err","results","queue","defer","cb","outPath","normalize","sourceMaps","await"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformDirectory.ts"],"sourcesContent":["import type fs from 'fs';\nimport 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 modeByPath = new Map<string, number>();\n const iterator = new Iterator(src, { alwaysStat: true });\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 const stats = entry.stats as fs.Stats;\n if (stats.mode) {\n modeByPath.set(entry.fullPath, stats.mode);\n }\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 const mode = modeByPath.get(entry.fullPath);\n queue.defer((cb) =>\n transformFile(entry, dest, type, options, mode, (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":["Iterator","path","Queue","extensions","typeFileRegEx","createMatcher","transformFile","transformDirectoryWorker","src","dest","type","options","callback","tsconfig","matcher","entries","modeByPath","Map","iterator","alwaysStat","forEach","entry","stats","isFile","basename","test","fullPath","ext","extname","indexOf","push","mode","set","err","results","queue","get","defer","cb","outPath","normalize","sourceMaps","await"],"mappings":"AACA,OAAOA,cAA8B,cAAc;AACnD,OAAOC,UAAU,OAAO;AACxB,OAAOC,WAAW,WAAW;AAE7B,SAASC,UAAU,EAAEC,aAAa,QAAQ,kBAAkB;AAC5D,OAAOC,mBAAmB,sBAAsB;AAChD,OAAOC,mBAAmB,0BAA0B;AAIpD,eAAe,SAASC,yBAAyBC,GAAW,EAAEC,IAAY,EAAEC,IAAgB,EAAEC,OAAsB,EAAEC,QAAoC;IACxJ,MAAMC,WAAWF,QAAQE,QAAQ;IACjC,MAAMC,UAAUT,cAAcQ;IAE9B,MAAME,UAAmB,EAAE;IAC3B,MAAMC,aAAa,IAAIC;IACvB,MAAMC,WAAW,IAAIlB,SAASQ,KAAK;QAAEW,YAAY;IAAK;IACtDD,SAASE,OAAO,CACd,CAACC;QACC,IAAI,CAACA,MAAMC,KAAK,CAACC,MAAM,IAAI;QAC3B,IAAIF,MAAMG,QAAQ,CAAC,EAAE,KAAK,KAAK;QAC/B,IAAIpB,cAAcqB,IAAI,CAACJ,MAAMG,QAAQ,GAAG;QACxC,IAAI,CAACV,QAAQO,MAAMK,QAAQ,GAAG;QAC9B,MAAMC,MAAM1B,KAAK2B,OAAO,CAACP,MAAMG,QAAQ;QACvC,IAAIG,OAAOxB,WAAW0B,OAAO,CAACF,OAAO,GAAG;QACxCZ,QAAQe,IAAI,CAACT;QAEb,MAAMC,QAAQD,MAAMC,KAAK;QACzB,IAAIA,MAAMS,IAAI,EAAE;YACdf,WAAWgB,GAAG,CAACX,MAAMK,QAAQ,EAAEJ,MAAMS,IAAI;QAC3C;IACF,GACA,CAACE;QACC,IAAIA,KAAK,OAAOrB,SAASqB;QACzB,MAAMC,UAAU,EAAE;QAClBvB,UAAU;YAAE,GAAGA,OAAO;YAAEE;QAAS;QAEjC,MAAMsB,QAAQ,IAAIjC;QAClBa,QAAQK,OAAO,CAAC,CAACC;YACf,MAAMU,OAAOf,WAAWoB,GAAG,CAACf,MAAMK,QAAQ;YAC1CS,MAAME,KAAK,CAAC,CAACC,KACXhC,cAAce,OAAOZ,MAAMC,MAAMC,SAASoB,MAAM,CAACE,KAAKM;oBACpD,IAAIN,KAAK,OAAOK,GAAGL;oBACnBC,QAAQJ,IAAI,CAAC7B,KAAKuC,SAAS,CAACD;oBAC5B,IAAI5B,QAAQ8B,UAAU,EAAEP,QAAQJ,IAAI,CAAC,GAAG7B,KAAKuC,SAAS,CAACD,SAAS,IAAI,CAAC;oBACrED;gBACF;QAEJ;QACAH,MAAMO,KAAK,CAAC,CAACT,MAASA,MAAMrB,SAASqB,OAAOrB,SAAS,MAAMsB;IAC7D;AAEJ"}
|
|
@@ -1,113 +1,162 @@
|
|
|
1
|
+
var _ref;
|
|
2
|
+
var _os_cpus;
|
|
3
|
+
import spawn from 'cross-spawn-cb';
|
|
1
4
|
import fs from 'fs';
|
|
2
5
|
import Iterator from 'fs-iterator';
|
|
3
|
-
import
|
|
6
|
+
import { safeRm } from 'fs-remove-compat';
|
|
7
|
+
import os from 'os';
|
|
4
8
|
import path from 'path';
|
|
5
9
|
import Queue from 'queue-cb';
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
import resolveBin from 'resolve-bin-sync';
|
|
11
|
+
const concurrency = Math.min(64, Math.max(8, ((_ref = (_os_cpus = os.cpus()) === null || _os_cpus === void 0 ? void 0 : _os_cpus.length) !== null && _ref !== void 0 ? _ref : 4) * 8));
|
|
12
|
+
const tscPath = resolveBin('typescript', 'tsc');
|
|
13
|
+
let tsgoPath = null;
|
|
14
|
+
try {
|
|
15
|
+
tsgoPath = resolveBin('@typescript/native-preview', 'tsgo');
|
|
16
|
+
} catch {
|
|
17
|
+
tsgoPath = null;
|
|
18
|
+
}
|
|
8
19
|
import { typeFileRegEx } from '../constants.js';
|
|
9
20
|
import createMatcher from '../createMatcher.js';
|
|
10
21
|
import { rewriteExtensions } from '../lib/rewriteExtensions.js';
|
|
22
|
+
/* ---------------- root file filtering ---------------- */ function isAllowedRootFile(basename) {
|
|
23
|
+
return basename.endsWith('.d.ts') || basename.endsWith('.d.mts') || basename.endsWith('.d.cts') || basename.endsWith('.ts') || basename.endsWith('.tsx') || basename.endsWith('.mts') || basename.endsWith('.cts') || basename.endsWith('.js') || basename.endsWith('.jsx') || basename.endsWith('.mjs') || basename.endsWith('.cjs');
|
|
24
|
+
}
|
|
25
|
+
/* ---------------- compiler execution ---------------- */ function runCompiler(cmdPath, args, cb) {
|
|
26
|
+
spawn(process.execPath, [
|
|
27
|
+
cmdPath,
|
|
28
|
+
...args
|
|
29
|
+
], {
|
|
30
|
+
encoding: 'utf8'
|
|
31
|
+
}, cb);
|
|
32
|
+
}
|
|
33
|
+
function runTsgoThenTsc(args, cb) {
|
|
34
|
+
if (tsgoPath) {
|
|
35
|
+
runCompiler(tsgoPath, args, (err, res)=>{
|
|
36
|
+
// Prefer status code over "err" for deciding success/fallback
|
|
37
|
+
if (!err && res.status === 0) return cb(null, res);
|
|
38
|
+
runCompiler(tscPath, args, cb);
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
runCompiler(tscPath, args, cb);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/* ---------------- emitted file parsing ---------------- */ function parseEmittedFiles(res, dest) {
|
|
45
|
+
const out = [];
|
|
46
|
+
const seen = new Set();
|
|
47
|
+
const lines = `${res.stdout}\n${res.stderr}`.split(/\r?\n/);
|
|
48
|
+
for (let line of lines){
|
|
49
|
+
line = line.trim();
|
|
50
|
+
if (!line) continue;
|
|
51
|
+
if (/^TSFILE:\s+/i.test(line)) line = line.replace(/^TSFILE:\s+/i, '').trim();
|
|
52
|
+
if (!path.isAbsolute(line)) continue;
|
|
53
|
+
if (!(line.endsWith('.d.ts') || line.endsWith('.d.mts') || line.endsWith('.d.cts'))) continue;
|
|
54
|
+
// Optional safety: only accept outputs under dest
|
|
55
|
+
const rel = path.relative(dest, line);
|
|
56
|
+
if (rel.startsWith('..') || path.isAbsolute(rel)) continue;
|
|
57
|
+
if (!seen.has(line)) {
|
|
58
|
+
seen.add(line);
|
|
59
|
+
out.push(line);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
11
64
|
export default function transformTypesWorker(src, dest, options, callback) {
|
|
12
65
|
const tsconfig = options.tsconfig;
|
|
13
66
|
const matcher = createMatcher(tsconfig);
|
|
14
|
-
const
|
|
15
|
-
const entries = [];
|
|
67
|
+
const rootFiles = [];
|
|
16
68
|
const iterator = new Iterator(src);
|
|
17
69
|
iterator.forEach((entry)=>{
|
|
18
70
|
if (!entry.stats.isFile()) return;
|
|
19
71
|
if (entry.basename[0] === '.') return;
|
|
20
72
|
if (typeFileRegEx.test(entry.basename)) return;
|
|
73
|
+
if (!isAllowedRootFile(entry.basename)) return;
|
|
21
74
|
if (!matcher(entry.fullPath)) return;
|
|
22
|
-
|
|
75
|
+
rootFiles.push(entry.fullPath);
|
|
23
76
|
}, {
|
|
24
|
-
concurrency
|
|
77
|
+
concurrency
|
|
25
78
|
}, (err)=>{
|
|
79
|
+
var _tsconfig_config_compilerOptions;
|
|
26
80
|
if (err) return callback(err);
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
81
|
+
if (rootFiles.length === 0) return callback(null, []);
|
|
82
|
+
const compilerOptions = {
|
|
83
|
+
...tsconfig.config.compilerOptions,
|
|
84
|
+
outDir: dest,
|
|
85
|
+
noEmit: false,
|
|
86
|
+
allowJs: true,
|
|
87
|
+
declaration: true,
|
|
88
|
+
emitDeclarationOnly: true
|
|
89
|
+
};
|
|
90
|
+
const rewrite = ((_tsconfig_config_compilerOptions = tsconfig.config.compilerOptions) === null || _tsconfig_config_compilerOptions === void 0 ? void 0 : _tsconfig_config_compilerOptions.rewriteRelativeImportExtensions) === true;
|
|
91
|
+
// Avoid collisions across concurrent runs.
|
|
92
|
+
const tempDir = path.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));
|
|
93
|
+
const tempConfigPath = path.join(tempDir, 'tsconfig.json');
|
|
94
|
+
const tempConfig = {
|
|
95
|
+
compilerOptions,
|
|
96
|
+
files: rootFiles,
|
|
97
|
+
include: [],
|
|
98
|
+
exclude: [],
|
|
99
|
+
...tsconfig.config.references && {
|
|
100
|
+
references: tsconfig.config.references
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
fs.mkdir(tempDir, {
|
|
104
|
+
recursive: true
|
|
105
|
+
}, (mkdirErr)=>{
|
|
106
|
+
if (mkdirErr) return callback(mkdirErr);
|
|
107
|
+
fs.writeFile(tempConfigPath, JSON.stringify(tempConfig, null, 2), 'utf8', (writeErr)=>{
|
|
108
|
+
if (writeErr) {
|
|
109
|
+
safeRm(tempDir, {
|
|
110
|
+
recursive: true,
|
|
111
|
+
force: true
|
|
112
|
+
}, ()=>callback(writeErr));
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const args = [
|
|
116
|
+
'--project',
|
|
117
|
+
tempConfigPath,
|
|
118
|
+
'--listEmittedFiles',
|
|
119
|
+
'--pretty',
|
|
120
|
+
'false'
|
|
121
|
+
];
|
|
122
|
+
runTsgoThenTsc(args, (runErr, res)=>{
|
|
123
|
+
if (runErr || res.status !== 0) {
|
|
124
|
+
var _ref;
|
|
125
|
+
const msg = `TypeScript compiler failed (status=${res === null || res === void 0 ? void 0 : res.status}).\n${`stderr:\n${String((_ref = res === null || res === void 0 ? void 0 : res.stderr) !== null && _ref !== void 0 ? _ref : '')}`.slice(0, 20000)}`;
|
|
126
|
+
safeRm(tempDir, {
|
|
127
|
+
recursive: true,
|
|
128
|
+
force: true
|
|
129
|
+
}, ()=>callback(runErr !== null && runErr !== void 0 ? runErr : new Error(msg)));
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const emittedFiles = parseEmittedFiles(res, dest);
|
|
133
|
+
if (emittedFiles.length === 0) {
|
|
134
|
+
safeRm(tempDir, {
|
|
135
|
+
recursive: true,
|
|
136
|
+
force: true
|
|
137
|
+
}, ()=>callback(new Error('TypeScript compiler produced no emitted declaration files')));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const postQueue = new Queue();
|
|
141
|
+
if (rewrite) {
|
|
142
|
+
for (const file of emittedFiles){
|
|
77
143
|
postQueue.defer((cb)=>{
|
|
78
144
|
fs.readFile(file, 'utf8', (readErr, content)=>{
|
|
79
|
-
if (readErr) return cb();
|
|
145
|
+
if (readErr) return cb();
|
|
80
146
|
const updated = rewriteExtensions(content);
|
|
81
|
-
|
|
82
|
-
fs.writeFile(file, updated, 'utf8', ()=>cb()); // Ignore write errors
|
|
83
|
-
} else {
|
|
84
|
-
cb();
|
|
85
|
-
}
|
|
147
|
+
updated === content ? cb() : fs.writeFile(file, updated, 'utf8', cb);
|
|
86
148
|
});
|
|
87
149
|
});
|
|
88
150
|
}
|
|
89
151
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const srcBase = srcRelative.replace(/\.(ts|tsx|mts|cts)$/, '');
|
|
97
|
-
if (baseName === srcBase) {
|
|
98
|
-
const execBits = mode & 0o111;
|
|
99
|
-
if (execBits) {
|
|
100
|
-
postQueue.defer((cb)=>{
|
|
101
|
-
fs.chmod(file, 0o644 | execBits, ()=>cb()); // Ignore chmod errors
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
152
|
+
postQueue.await(()=>{
|
|
153
|
+
safeRm(tempDir, {
|
|
154
|
+
recursive: true,
|
|
155
|
+
force: true
|
|
156
|
+
}, (rmErr)=>callback(rmErr || null, emittedFiles));
|
|
157
|
+
});
|
|
108
158
|
});
|
|
109
|
-
}
|
|
110
|
-
postQueue.await(()=>callback(null, res.emittedFiles));
|
|
159
|
+
});
|
|
111
160
|
});
|
|
112
161
|
});
|
|
113
162
|
}
|
|
@@ -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) {\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":["fs","Iterator","Module","path","Queue","_require","require","createRequire","url","stringEndsWith","typeFileRegEx","createMatcher","rewriteExtensions","transformTypesWorker","src","dest","options","callback","tsconfig","matcher","ts","entries","iterator","forEach","entry","stats","isFile","basename","test","fullPath","push","concurrency","Infinity","err","sourceModes","Map","statQueue","defer","cb","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","readFile","readErr","content","updated","writeFile","relativePath","relative","baseName","replace","srcPath","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,SAASC;oCAClC,IAAID,SAAS,OAAOpC,MAAM,0BAA0B;oCACpD,MAAMsC,UAAUhE,kBAAkB+D;oCAClC,IAAIC,YAAYD,SAAS;wCACvB3E,GAAG6E,SAAS,CAACN,MAAMK,SAAS,QAAQ,IAAMtC,OAAO,sBAAsB;oCACzE,OAAO;wCACLA;oCACF;gCACF;4BACF;wBACF;oBACF;oBAEA,qDAAqD;oBACrD,IAAI7B,eAAe8D,MAAM,YAAY9D,eAAe8D,MAAM,aAAa9D,eAAe8D,MAAM,WAAW;wBACrG,MAAMO,eAAe3E,KAAK4E,QAAQ,CAAChE,MAAMwD;wBACzC,MAAMS,WAAWF,aAAaG,OAAO,CAAC,sBAAsB;wBAE5D,KAAK,MAAM,CAACC,SAASxC,KAAK,IAAIR,YAAa;4BACzC,MAAMiD,cAAchF,KAAK4E,QAAQ,CAACjE,KAAKoE;4BACvC,MAAME,UAAUD,YAAYF,OAAO,CAAC,uBAAuB;4BAC3D,IAAID,aAAaI,SAAS;gCACxB,MAAMC,WAAW3C,OAAO;gCACxB,IAAI2C,UAAU;oCACZhB,UAAUhC,KAAK,CAAC,CAACC;wCACftC,GAAGsF,KAAK,CAACf,MAAM,QAAQc,UAAU,IAAM/C,OAAO,sBAAsB;oCACtE;gCACF;gCACA;4BACF;wBACF;oBACF;gBACF;YACF;YAEA+B,UAAU1B,KAAK,CAAC,IAAM1B,SAAS,MAAMkD,IAAIG,YAAY;QACvD;IACF;AAEJ"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformTypes.ts"],"sourcesContent":["import spawn, { type SpawnCallback, type SpawnResult } from 'cross-spawn-cb';\nimport fs from 'fs';\nimport Iterator, { type Entry } from 'fs-iterator';\nimport { safeRm } from 'fs-remove-compat';\nimport os from 'os';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport resolveBin from 'resolve-bin-sync';\n\nconst concurrency = Math.min(64, Math.max(8, (os.cpus()?.length ?? 4) * 8));\n\nconst tscPath = resolveBin('typescript', 'tsc');\nlet tsgoPath: string | null = null;\ntry {\n tsgoPath = resolveBin('@typescript/native-preview', 'tsgo');\n} catch {\n tsgoPath = null;\n}\n\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\n/* ---------------- root file filtering ---------------- */\n\nfunction isAllowedRootFile(basename: string): boolean {\n return (\n basename.endsWith('.d.ts') ||\n basename.endsWith('.d.mts') ||\n basename.endsWith('.d.cts') ||\n basename.endsWith('.ts') ||\n basename.endsWith('.tsx') ||\n basename.endsWith('.mts') ||\n basename.endsWith('.cts') ||\n basename.endsWith('.js') ||\n basename.endsWith('.jsx') ||\n basename.endsWith('.mjs') ||\n basename.endsWith('.cjs')\n );\n}\n\n/* ---------------- compiler execution ---------------- */\n\nfunction runCompiler(cmdPath: string, args: string[], cb: SpawnCallback): void {\n spawn(process.execPath, [cmdPath, ...args], { encoding: 'utf8' }, cb);\n}\n\nfunction runTsgoThenTsc(args: string[], cb: SpawnCallback): void {\n if (tsgoPath) {\n runCompiler(tsgoPath, args, (err, res) => {\n // Prefer status code over \"err\" for deciding success/fallback\n if (!err && res.status === 0) return cb(null, res);\n runCompiler(tscPath, args, cb);\n });\n } else {\n runCompiler(tscPath, args, cb);\n }\n}\n\n/* ---------------- emitted file parsing ---------------- */\n\nfunction parseEmittedFiles(res: SpawnResult, dest: string): string[] {\n const out: string[] = [];\n const seen = new Set<string>();\n const lines = `${res.stdout}\\n${res.stderr}`.split(/\\r?\\n/);\n\n for (let line of lines) {\n line = line.trim();\n if (!line) continue;\n\n if (/^TSFILE:\\s+/i.test(line)) line = line.replace(/^TSFILE:\\s+/i, '').trim();\n\n if (!path.isAbsolute(line)) continue;\n if (!(line.endsWith('.d.ts') || line.endsWith('.d.mts') || line.endsWith('.d.cts'))) continue;\n\n // Optional safety: only accept outputs under dest\n const rel = path.relative(dest, line);\n if (rel.startsWith('..') || path.isAbsolute(rel)) continue;\n\n if (!seen.has(line)) {\n seen.add(line);\n out.push(line);\n }\n }\n\n return out;\n}\n\nexport default function transformTypesWorker(src: string, dest: string, options: ConfigOptions, callback: TransformTypesCallback) {\n const tsconfig = options.tsconfig;\n const matcher = createMatcher(tsconfig);\n\n const rootFiles: string[] = [];\n const iterator = new Iterator(src);\n\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 (!isAllowedRootFile(entry.basename)) return;\n if (!matcher(entry.fullPath)) return;\n\n rootFiles.push(entry.fullPath);\n },\n { concurrency },\n (err) => {\n if (err) return callback(err);\n if (rootFiles.length === 0) return callback(null, []);\n\n const compilerOptions = {\n ...tsconfig.config.compilerOptions,\n outDir: dest,\n noEmit: false,\n allowJs: true,\n declaration: true,\n emitDeclarationOnly: true,\n };\n\n const rewrite = tsconfig.config.compilerOptions?.rewriteRelativeImportExtensions === true;\n\n // Avoid collisions across concurrent runs.\n const tempDir = path.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));\n const tempConfigPath = path.join(tempDir, 'tsconfig.json');\n\n const tempConfig = {\n compilerOptions,\n files: rootFiles,\n include: [],\n exclude: [],\n ...(tsconfig.config.references && { references: tsconfig.config.references }),\n };\n\n fs.mkdir(tempDir, { recursive: true }, (mkdirErr) => {\n if (mkdirErr) return callback(mkdirErr);\n\n fs.writeFile(tempConfigPath, JSON.stringify(tempConfig, null, 2), 'utf8', (writeErr) => {\n if (writeErr) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(writeErr));\n return;\n }\n\n const args = ['--project', tempConfigPath, '--listEmittedFiles', '--pretty', 'false'];\n\n runTsgoThenTsc(args, (runErr, res) => {\n if (runErr || res.status !== 0) {\n const msg = `TypeScript compiler failed (status=${res?.status}).\\n${`stderr:\\n${String(res?.stderr ?? '')}`.slice(0, 20_000)}`;\n safeRm(tempDir, { recursive: true, force: true }, () => callback(runErr ?? new Error(msg)));\n return;\n }\n\n const emittedFiles = parseEmittedFiles(res, dest);\n if (emittedFiles.length === 0) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error('TypeScript compiler produced no emitted declaration files')));\n return;\n }\n\n const postQueue = new Queue();\n\n if (rewrite) {\n for (const file of emittedFiles) {\n postQueue.defer((cb) => {\n fs.readFile(file, 'utf8', (readErr, content) => {\n if (readErr) return cb();\n const updated = rewriteExtensions(content);\n updated === content ? cb() : fs.writeFile(file, updated, 'utf8', cb);\n });\n });\n }\n }\n\n postQueue.await(() => {\n safeRm(tempDir, { recursive: true, force: true }, (rmErr) => callback(rmErr || null, emittedFiles));\n });\n });\n });\n });\n }\n );\n}\n"],"names":["os","spawn","fs","Iterator","safeRm","path","Queue","resolveBin","concurrency","Math","min","max","cpus","length","tscPath","tsgoPath","typeFileRegEx","createMatcher","rewriteExtensions","isAllowedRootFile","basename","endsWith","runCompiler","cmdPath","args","cb","process","execPath","encoding","runTsgoThenTsc","err","res","status","parseEmittedFiles","dest","out","seen","Set","lines","stdout","stderr","split","line","trim","test","replace","isAbsolute","rel","relative","startsWith","has","add","push","transformTypesWorker","src","options","callback","tsconfig","matcher","rootFiles","iterator","forEach","entry","stats","isFile","fullPath","compilerOptions","config","outDir","noEmit","allowJs","declaration","emitDeclarationOnly","rewrite","rewriteRelativeImportExtensions","tempDir","join","String","pid","Date","now","tempConfigPath","tempConfig","files","include","exclude","references","mkdir","recursive","mkdirErr","writeFile","JSON","stringify","writeErr","force","runErr","msg","slice","Error","emittedFiles","postQueue","file","defer","readFile","readErr","content","updated","await","rmErr"],"mappings":";IAS8CA;AAT9C,OAAOC,WAAqD,iBAAiB;AAC7E,OAAOC,QAAQ,KAAK;AACpB,OAAOC,cAA8B,cAAc;AACnD,SAASC,MAAM,QAAQ,mBAAmB;AAC1C,OAAOJ,QAAQ,KAAK;AACpB,OAAOK,UAAU,OA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|
3
|
+
"version": "2.11.4",
|
|
4
4
|
"description": "Typescript transformers for swc. Supports Node >= 0.8",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"matcher",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@swc/core": "^1.15.5",
|
|
48
|
+
"@typescript/native-preview": "^7.0.0-dev.20260108.1",
|
|
48
49
|
"core-js-pure": "^3.20.0",
|
|
49
50
|
"exit-compat": "^1.0.0",
|
|
50
51
|
"fs-iterator": "^7.0.0",
|
|
@@ -57,6 +58,7 @@
|
|
|
57
58
|
"queue-cb": "^1.0.0",
|
|
58
59
|
"read-tsconfig-sync": "^1.1.0",
|
|
59
60
|
"resolve": "^1.3.1",
|
|
61
|
+
"resolve-bin-sync": "^1.0.13",
|
|
60
62
|
"resolve.exports": "^2.0.3",
|
|
61
63
|
"test-match": "^1.0.0",
|
|
62
64
|
"ts-node": "^10.9.0",
|