ts-swc-transform 1.4.2 → 1.4.3
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/loadSWC.js +15 -3
- package/dist/cjs/lib/loadSWC.js.map +1 -1
- package/dist/cjs/lib/transformVersion.js +20 -0
- package/dist/cjs/lib/transformVersion.js.map +1 -0
- package/dist/cjs/transformDirectory.js +19 -77
- package/dist/cjs/transformDirectory.js.map +1 -1
- package/dist/cjs/transformFile.js +22 -132
- package/dist/cjs/transformFile.js.map +1 -1
- package/dist/cjs/transformSync.js +3 -4
- package/dist/cjs/transformSync.js.map +1 -1
- package/dist/cjs/workers/transformDirectory.js +89 -0
- package/dist/cjs/workers/transformDirectory.js.map +1 -0
- package/dist/cjs/workers/transformFile.js +143 -0
- package/dist/cjs/workers/transformFile.js.map +1 -0
- package/dist/cjs/workers/transformSync.js +2 -2
- package/dist/cjs/workers/transformSync.js.map +1 -1
- package/dist/esm/lib/loadSWC.mjs +15 -3
- package/dist/esm/lib/loadSWC.mjs.map +1 -1
- package/dist/esm/lib/transformVersion.mjs +4 -0
- package/dist/esm/lib/transformVersion.mjs.map +1 -0
- package/dist/esm/transformDirectory.mjs +20 -26
- package/dist/esm/transformDirectory.mjs.map +1 -1
- package/dist/esm/transformFile.mjs +23 -105
- package/dist/esm/transformFile.mjs.map +1 -1
- package/dist/esm/transformSync.mjs +2 -4
- package/dist/esm/transformSync.mjs.map +1 -1
- package/dist/esm/workers/transformDirectory.mjs +22 -0
- package/dist/esm/workers/transformDirectory.mjs.map +1 -0
- package/dist/esm/workers/transformFile.mjs +100 -0
- package/dist/esm/workers/transformFile.mjs.map +1 -0
- package/dist/esm/workers/transformSync.mjs +1 -1
- package/dist/esm/workers/transformSync.mjs.map +1 -1
- package/dist/types/lib/loadSWC.d.ts +1 -1
- package/dist/types/lib/transformVersion.d.ts +2 -0
- package/dist/types/transformDirectory.d.ts +1 -1
- package/dist/types/transformFile.d.ts +4 -4
- package/dist/types/transformSync.d.ts +3 -1
- package/dist/types/workers/transformDirectory.d.ts +1 -0
- package/dist/types/workers/transformFile.d.ts +1 -0
- package/dist/types/workers/transformSync.d.ts +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return transformFileWorker;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
|
|
12
|
+
var _path = /*#__PURE__*/ _interop_require_default(require("path"));
|
|
13
|
+
var _calloncefn = /*#__PURE__*/ _interop_require_default(require("call-once-fn"));
|
|
14
|
+
var _gettsconfigcompat = /*#__PURE__*/ _interop_require_default(require("get-tsconfig-compat"));
|
|
15
|
+
var _mkdirpclassic = /*#__PURE__*/ _interop_require_default(require("mkdirp-classic"));
|
|
16
|
+
var _queuecb = /*#__PURE__*/ _interop_require_default(require("queue-cb"));
|
|
17
|
+
var _transformSync = /*#__PURE__*/ _interop_require_default(require("./transformSync.js"));
|
|
18
|
+
function _define_property(obj, key, value) {
|
|
19
|
+
if (key in obj) {
|
|
20
|
+
Object.defineProperty(obj, key, {
|
|
21
|
+
value: value,
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true
|
|
25
|
+
});
|
|
26
|
+
} else {
|
|
27
|
+
obj[key] = value;
|
|
28
|
+
}
|
|
29
|
+
return obj;
|
|
30
|
+
}
|
|
31
|
+
function _interop_require_default(obj) {
|
|
32
|
+
return obj && obj.__esModule ? obj : {
|
|
33
|
+
default: obj
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function _object_spread(target) {
|
|
37
|
+
for(var i = 1; i < arguments.length; i++){
|
|
38
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
39
|
+
var ownKeys = Object.keys(source);
|
|
40
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
41
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
42
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
ownKeys.forEach(function(key) {
|
|
46
|
+
_define_property(target, key, source[key]);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return target;
|
|
50
|
+
}
|
|
51
|
+
var matchingDeps = '\\s*[\'"`]([^\'"`]+)[\'"`]\\s*';
|
|
52
|
+
var matchingName = '\\s*(?:[\\w${},\\s*]+)\\s*';
|
|
53
|
+
var regexCJS = new RegExp("(?:(?:var|const|let)".concat(matchingName, "=\\s*)?require\\(").concat(matchingDeps, "\\);?"), 'g');
|
|
54
|
+
var regexESM = new RegExp("".concat(regexCJS, "|import(?:").concat(matchingName, "from\\s*)?").concat(matchingDeps, ";?|export(?:").concat(matchingName, "from\\s*)?").concat(matchingDeps, ";?"), 'g');
|
|
55
|
+
var importReplaceMJS = [
|
|
56
|
+
'.js',
|
|
57
|
+
'.ts',
|
|
58
|
+
'.tsx',
|
|
59
|
+
'.mts',
|
|
60
|
+
'.mjs'
|
|
61
|
+
];
|
|
62
|
+
var importReplaceCJS = [
|
|
63
|
+
'.cts'
|
|
64
|
+
];
|
|
65
|
+
var requireReplaceJS = [
|
|
66
|
+
'.mjs',
|
|
67
|
+
'.cjs',
|
|
68
|
+
'.ts',
|
|
69
|
+
'.tsx',
|
|
70
|
+
'.mts',
|
|
71
|
+
'.cts'
|
|
72
|
+
];
|
|
73
|
+
function makeReplacements(code, regex, extensions, extension) {
|
|
74
|
+
var _loop = function() {
|
|
75
|
+
var dependency = match[1] || match[2] || match[3] || match[4];
|
|
76
|
+
var ext = extensions.find(function(x) {
|
|
77
|
+
return dependency.slice(-x.length) === x;
|
|
78
|
+
});
|
|
79
|
+
if (ext) matches.push({
|
|
80
|
+
ext: ext,
|
|
81
|
+
match: match,
|
|
82
|
+
dependency: dependency
|
|
83
|
+
});
|
|
84
|
+
match = regex.exec(code);
|
|
85
|
+
};
|
|
86
|
+
var matches = [];
|
|
87
|
+
var match = regex.exec(code);
|
|
88
|
+
while(match)_loop();
|
|
89
|
+
matches = matches.reverse();
|
|
90
|
+
for(var index in matches){
|
|
91
|
+
var match1 = matches[index];
|
|
92
|
+
var start = match1.match.index + match1.match[0].indexOf(match1.dependency) + match1.dependency.indexOf(match1.ext);
|
|
93
|
+
code = code.substring(0, start) + extension + code.substring(start + match1.ext.length);
|
|
94
|
+
}
|
|
95
|
+
return code;
|
|
96
|
+
}
|
|
97
|
+
// https://github.com/vercel/next.js/blob/20b63e13ab2631d6043277895d373aa31a1b327c/packages/next/taskfile-swc.js#L118-L125
|
|
98
|
+
var interopClientDefaultExport = "/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }";
|
|
99
|
+
function transformFileWorker(src, dest, type, options, callback) {
|
|
100
|
+
_fs.default.readFile(src, 'utf8', function(err, contents) {
|
|
101
|
+
if (err) return callback(err);
|
|
102
|
+
callback = (0, _calloncefn.default)(callback);
|
|
103
|
+
try {
|
|
104
|
+
var tsconfig = options.tsconfig ? options.tsconfig : _gettsconfigcompat.default.getTsconfig(src);
|
|
105
|
+
// overrides for cjs
|
|
106
|
+
if (type === 'cjs') {
|
|
107
|
+
tsconfig = _object_spread({}, tsconfig);
|
|
108
|
+
tsconfig.tsconfig = _object_spread({}, tsconfig.config || {});
|
|
109
|
+
tsconfig.config.compilerOptions = _object_spread({}, tsconfig.config.compilerOptions || {});
|
|
110
|
+
tsconfig.config.compilerOptions.module = 'CommonJS';
|
|
111
|
+
tsconfig.config.compilerOptions.target = 'ES5';
|
|
112
|
+
}
|
|
113
|
+
var basename = _path.default.basename(src);
|
|
114
|
+
(0, _transformSync.default)(contents, basename, tsconfig, function(err, output) {
|
|
115
|
+
if (err) return callback(err);
|
|
116
|
+
// infer extension and patch .mjs imports
|
|
117
|
+
var ext = _path.default.extname(basename);
|
|
118
|
+
if (type === 'esm') {
|
|
119
|
+
ext = importReplaceMJS.indexOf(ext) >= 0 ? '.mjs' : ext;
|
|
120
|
+
output.code = makeReplacements(output.code, regexESM, importReplaceMJS, '.mjs');
|
|
121
|
+
ext = importReplaceCJS.indexOf(ext) >= 0 ? '.cjs' : ext;
|
|
122
|
+
output.code = makeReplacements(output.code, regexESM, importReplaceCJS, '.cjs');
|
|
123
|
+
} else {
|
|
124
|
+
ext = requireReplaceJS.indexOf(ext) >= 0 ? '.js' : ext;
|
|
125
|
+
output.code = makeReplacements(output.code, regexCJS, requireReplaceJS, '.js');
|
|
126
|
+
output.code += interopClientDefaultExport;
|
|
127
|
+
}
|
|
128
|
+
var destFilePath = _path.default.join(dest, basename.replace(/\.[^/.]+$/, '') + ext);
|
|
129
|
+
(0, _mkdirpclassic.default)(_path.default.dirname(destFilePath), function() {
|
|
130
|
+
var queue = new _queuecb.default();
|
|
131
|
+
queue.defer(_fs.default.writeFile.bind(null, destFilePath, output.code, 'utf8'));
|
|
132
|
+
!options.sourceMaps || queue.defer(_fs.default.writeFile.bind(null, "".concat(destFilePath, ".map"), output.map, 'utf8'));
|
|
133
|
+
queue.await(function() {
|
|
134
|
+
return err ? callback(err) : callback(null, destFilePath);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
} catch (err) {
|
|
139
|
+
callback(err);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformFile.ts"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport once from 'call-once-fn';\nimport getTS from 'get-tsconfig-compat';\nimport mkdirp from 'mkdirp-classic';\nimport Queue from 'queue-cb';\n\nimport transformSync from './transformSync.js';\n\nconst matchingDeps = '\\\\s*[\\'\"`]([^\\'\"`]+)[\\'\"`]\\\\s*';\nconst matchingName = '\\\\s*(?:[\\\\w${},\\\\s*]+)\\\\s*';\nconst regexCJS = new RegExp(`(?:(?:var|const|let)${matchingName}=\\\\s*)?require\\\\(${matchingDeps}\\\\);?`, 'g');\nconst regexESM = new RegExp(`${regexCJS}|import(?:${matchingName}from\\\\s*)?${matchingDeps};?|export(?:${matchingName}from\\\\s*)?${matchingDeps};?`, 'g');\n\nconst importReplaceMJS = ['.js', '.ts', '.tsx', '.mts', '.mjs'];\nconst importReplaceCJS = ['.cts'];\nconst requireReplaceJS = ['.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts'];\n\nfunction makeReplacements(code, regex, extensions, extension) {\n let matches = [];\n let match = regex.exec(code);\n while (match) {\n const dependency = match[1] || match[2] || match[3] || match[4];\n const ext = extensions.find((x) => dependency.slice(-x.length) === x);\n if (ext) matches.push({ ext, match, dependency });\n match = regex.exec(code);\n }\n\n matches = matches.reverse();\n for (const index in matches) {\n const match = matches[index];\n const start = match.match.index + match.match[0].indexOf(match.dependency) + match.dependency.indexOf(match.ext);\n code = code.substring(0, start) + extension + code.substring(start + match.ext.length);\n }\n return code;\n}\n\n// https://github.com/vercel/next.js/blob/20b63e13ab2631d6043277895d373aa31a1b327c/packages/next/taskfile-swc.js#L118-L125\nconst interopClientDefaultExport = \"/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }\";\n\nexport default function transformFileWorker(src, dest, type, options, callback) {\n fs.readFile(src, 'utf8', (err, contents) => {\n if (err) return callback(err);\n callback = once(callback);\n\n try {\n let tsconfig = options.tsconfig ? options.tsconfig : getTS.getTsconfig(src);\n\n // overrides for cjs\n if (type === 'cjs') {\n tsconfig = { ...tsconfig };\n tsconfig.tsconfig = { ...(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 basename = path.basename(src);\n transformSync(contents, basename, tsconfig, (err, output) => {\n if (err) return callback(err);\n\n // infer extension and patch .mjs imports\n let ext = path.extname(basename);\n if (type === 'esm') {\n ext = importReplaceMJS.indexOf(ext) >= 0 ? '.mjs' : ext;\n output.code = makeReplacements(output.code, regexESM, importReplaceMJS, '.mjs');\n ext = importReplaceCJS.indexOf(ext) >= 0 ? '.cjs' : ext;\n output.code = makeReplacements(output.code, regexESM, importReplaceCJS, '.cjs');\n } else {\n ext = requireReplaceJS.indexOf(ext) >= 0 ? '.js' : ext;\n output.code = makeReplacements(output.code, regexCJS, requireReplaceJS, '.js');\n output.code += interopClientDefaultExport;\n }\n const destFilePath = path.join(dest, basename.replace(/\\.[^/.]+$/, '') + ext);\n\n mkdirp(path.dirname(destFilePath), () => {\n const queue = new Queue();\n queue.defer(fs.writeFile.bind(null, destFilePath, output.code, 'utf8'));\n !options.sourceMaps || queue.defer(fs.writeFile.bind(null, `${destFilePath}.map`, output.map, 'utf8'));\n queue.await(() => (err ? callback(err) : callback(null, destFilePath)));\n });\n });\n } catch (err) {\n callback(err);\n }\n });\n}\n"],"names":["transformFileWorker","matchingDeps","matchingName","regexCJS","RegExp","regexESM","importReplaceMJS","importReplaceCJS","requireReplaceJS","makeReplacements","code","regex","extensions","extension","dependency","match","ext","find","x","slice","length","matches","push","exec","reverse","index","start","indexOf","substring","interopClientDefaultExport","src","dest","type","options","callback","fs","readFile","err","contents","once","tsconfig","getTS","getTsconfig","config","compilerOptions","module","target","basename","path","transformSync","output","extname","destFilePath","join","replace","mkdirp","dirname","queue","Queue","defer","writeFile","bind","sourceMaps","map","await"],"mappings":"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"}
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
Object.defineProperty(exports, "default", {
|
|
6
6
|
enumerable: true,
|
|
7
7
|
get: function() {
|
|
8
|
-
return
|
|
8
|
+
return transformSyncWorker;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
var _path = /*#__PURE__*/ _interop_require_default(require("path"));
|
|
@@ -58,7 +58,7 @@ function _interop_require_wildcard(obj, nodeInterop) {
|
|
|
58
58
|
}
|
|
59
59
|
return newObj;
|
|
60
60
|
}
|
|
61
|
-
function
|
|
61
|
+
function transformSyncWorker(contents, fileName, config, callback) {
|
|
62
62
|
(0, _loadSWC.default)(function(err, swc) {
|
|
63
63
|
if (err) return callback(err);
|
|
64
64
|
var parsed = _typescript.parseJsonConfigFileContent(config.config, _typescript.sys, _path.default.dirname(config.path));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["transformSync.ts"],"sourcesContent":["import path from 'path';\nimport type { TsConfigResult } from 'get-tsconfig-compat';\nimport * as transpiler from 'ts-node/transpilers/swc';\nimport * as ts from 'typescript';\n// @ts-ignore\nimport loadSWC from '../lib/loadSWC.js';\n\nexport default function
|
|
1
|
+
{"version":3,"sources":["transformSync.ts"],"sourcesContent":["import path from 'path';\nimport type { TsConfigResult } from 'get-tsconfig-compat';\nimport * as transpiler from 'ts-node/transpilers/swc';\nimport * as ts from 'typescript';\n// @ts-ignore\nimport loadSWC from '../lib/loadSWC.js';\n\nexport default function transformSyncWorker(contents: string, fileName: string, config: TsConfigResult, callback) {\n loadSWC((err, swc) => {\n if (err) return callback(err);\n const parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, path.dirname(config.path));\n const transpile = transpiler.create({ swc: swc, service: { config: { options: parsed.options } } });\n const res = transpile.transpile(contents, { fileName });\n callback(null, { code: res.outputText, map: res.sourceMapText });\n });\n}\n"],"names":["transformSyncWorker","contents","fileName","config","callback","loadSWC","err","swc","parsed","ts","parseJsonConfigFileContent","sys","path","dirname","transpile","transpiler","create","service","options","res","code","outputText","map","sourceMapText"],"mappings":";;;;+BAOA;;;eAAwBA;;;2DAPP;2DAEW;kEACR;8DAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEL,SAASA,oBAAoBC,QAAgB,EAAEC,QAAgB,EAAEC,MAAsB,EAAEC,QAAQ;IAC9GC,IAAAA,gBAAO,EAAC,SAACC,KAAKC;QACZ,IAAID,KAAK,OAAOF,SAASE;QACzB,IAAME,SAASC,YAAGC,0BAA0B,CAACP,OAAOA,MAAM,EAAEM,YAAGE,GAAG,EAAEC,aAAI,CAACC,OAAO,CAACV,OAAOS,IAAI;QAC5F,IAAME,YAAYC,KAAWC,MAAM,CAAC;YAAET,KAAKA;YAAKU,SAAS;gBAAEd,QAAQ;oBAAEe,SAASV,OAAOU,OAAO;gBAAC;YAAE;QAAE;QACjG,IAAMC,MAAML,UAAUA,SAAS,CAACb,UAAU;YAAEC,UAAAA;QAAS;QACrDE,SAAS,MAAM;YAAEgB,MAAMD,IAAIE,UAAU;YAAEC,KAAKH,IAAII,aAAa;QAAC;IAChE;AACF"}
|
package/dist/esm/lib/loadSWC.mjs
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import installBindings from './installBindings.mjs';
|
|
2
2
|
// @ts-ignore
|
|
3
3
|
import lazy from './lazy.cjs';
|
|
4
|
-
const
|
|
4
|
+
const lazySWC = lazy('@swc/core');
|
|
5
|
+
let swc = null;
|
|
6
|
+
let err = null;
|
|
5
7
|
export default function loadSWC(callback) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
if (swc) return callback(null, swc);
|
|
9
|
+
if (err) return callback(err);
|
|
10
|
+
installBindings((err_)=>{
|
|
11
|
+
err = err || err_;
|
|
12
|
+
if (err) return callback(err);
|
|
13
|
+
try {
|
|
14
|
+
swc = swc || lazySWC();
|
|
15
|
+
return callback(null, swc);
|
|
16
|
+
} catch (err_) {
|
|
17
|
+
err = err || err_;
|
|
18
|
+
return callback(err);
|
|
19
|
+
}
|
|
8
20
|
});
|
|
9
21
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["loadSWC.ts"],"sourcesContent":["import installBindings from './installBindings.js';\n// @ts-ignore\nimport lazy from './lazy.cjs';\nconst
|
|
1
|
+
{"version":3,"sources":["loadSWC.ts"],"sourcesContent":["import installBindings from './installBindings.js';\n// @ts-ignore\nimport lazy from './lazy.cjs';\nconst lazySWC = lazy('@swc/core');\n\nlet swc = null;\nlet err: Error = null;\nexport default function loadSWC(callback) {\n if (swc) return callback(null, swc);\n if (err) return callback(err);\n\n installBindings((err_) => {\n err = err || err_;\n if (err) return callback(err);\n try {\n swc = swc || lazySWC();\n return callback(null, swc);\n } catch (err_) {\n err = err || err_;\n return callback(err);\n }\n });\n}\n"],"names":["installBindings","lazy","lazySWC","swc","err","loadSWC","callback","err_"],"mappings":"AAAA,OAAOA,qBAAqB,uBAAuB;AACnD,aAAa;AACb,OAAOC,UAAU,aAAa;AAC9B,MAAMC,UAAUD,KAAK;AAErB,IAAIE,MAAM;AACV,IAAIC,MAAa;AACjB,eAAe,SAASC,QAAQC,QAAQ;IACtC,IAAIH,KAAK,OAAOG,SAAS,MAAMH;IAC/B,IAAIC,KAAK,OAAOE,SAASF;IAEzBJ,gBAAgB,CAACO;QACfH,MAAMA,OAAOG;QACb,IAAIH,KAAK,OAAOE,SAASF;QACzB,IAAI;YACFD,MAAMA,OAAOD;YACb,OAAOI,SAAS,MAAMH;QACxB,EAAE,OAAOI,MAAM;YACbH,MAAMA,OAAOG;YACb,OAAOD,SAASF;QAClB;IACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformVersion.ts"],"sourcesContent":["// @ts-ignore\nimport process from './process.cjs';\n\nconst major = +process.versions.node.split('.')[0];\nexport default major < 14 ? '14' : 'local';\n"],"names":["process","major","versions","node","split"],"mappings":"AAAA,aAAa;AACb,OAAOA,aAAa,gBAAgB;AAEpC,MAAMC,QAAQ,CAACD,QAAQE,QAAQ,CAACC,IAAI,CAACC,KAAK,CAAC,IAAI,CAAC,EAAE;AAClD,eAAeH,QAAQ,KAAK,OAAO,QAAQ"}
|
|
@@ -1,32 +1,23 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
import
|
|
2
|
+
import url from 'url';
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import lazy from './lib/lazy.cjs';
|
|
5
|
+
import packageRoot from './lib/packageRoot.mjs';
|
|
6
|
+
import version from './lib/transformVersion.mjs';
|
|
7
|
+
const __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : url.fileURLToPath(import.meta.url));
|
|
8
|
+
const root = packageRoot(__dirname);
|
|
9
|
+
const worker = path.resolve(root, 'dist', 'cjs', 'workers', 'transformDirectory.js');
|
|
10
|
+
const call = lazy('node-version-call');
|
|
6
11
|
function transformDirectoryCallback(src, dest, type, options, callback) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
try {
|
|
13
|
+
const res = call()({
|
|
14
|
+
version,
|
|
15
|
+
callbacks: true
|
|
16
|
+
}, worker, src, dest, type, options);
|
|
17
|
+
return callback(null, res);
|
|
18
|
+
} catch (err) {
|
|
19
|
+
return callback(err);
|
|
10
20
|
}
|
|
11
|
-
options = options || {};
|
|
12
|
-
if (typeof src !== 'string') throw new Error('transformDirectory: unexpected source');
|
|
13
|
-
if (typeof dest !== 'string') throw new Error('transformDirectory: unexpected destination directory');
|
|
14
|
-
if (typeof type !== 'string') throw new Error('transformDirectory: unexpected type');
|
|
15
|
-
const tsconfig = options.tsconfig ? options.tsconfig : getTS.getTsconfig(src);
|
|
16
|
-
const matcher = createMatcher(tsconfig);
|
|
17
|
-
options = {
|
|
18
|
-
...options,
|
|
19
|
-
tsconfig
|
|
20
|
-
};
|
|
21
|
-
const iterator = new Iterator(src);
|
|
22
|
-
iterator.forEach((entry, cb)=>{
|
|
23
|
-
if (!entry.stats.isFile()) return cb();
|
|
24
|
-
if (!matcher(entry.fullPath)) return cb();
|
|
25
|
-
transformFile(entry.fullPath, path.dirname(path.join(dest, entry.path)), type, options, cb);
|
|
26
|
-
}, {
|
|
27
|
-
callbacks: true,
|
|
28
|
-
concurrency: options.concurrency || 1024
|
|
29
|
-
}, callback);
|
|
30
21
|
}
|
|
31
22
|
/**
|
|
32
23
|
* @param {string} src The source directory to traverse.
|
|
@@ -41,6 +32,9 @@ function transformDirectoryCallback(src, dest, type, options, callback) {
|
|
|
41
32
|
options = null;
|
|
42
33
|
}
|
|
43
34
|
options = options || {};
|
|
35
|
+
if (typeof src !== 'string') throw new Error('transformDirectory: unexpected source');
|
|
36
|
+
if (typeof dest !== 'string') throw new Error('transformDirectory: unexpected destination directory');
|
|
37
|
+
if (typeof type !== 'string') throw new Error('transformDirectory: unexpected type');
|
|
44
38
|
if (typeof callback === 'function') return transformDirectoryCallback(src, dest, type, options, callback);
|
|
45
39
|
return new Promise((resolve, reject)=>{
|
|
46
40
|
transformDirectoryCallback(src, dest, type, options, function compileCallback(err, result) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["transformDirectory.ts"],"sourcesContent":["import path from 'path';\nimport
|
|
1
|
+
{"version":3,"sources":["transformDirectory.ts"],"sourcesContent":["import path from 'path';\nimport url from 'url';\n\n// @ts-ignore\nimport lazy from './lib/lazy.cjs';\nimport packageRoot from './lib/packageRoot.js';\nimport version from './lib/transformVersion.js';\n\nconst __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : url.fileURLToPath(import.meta.url));\nconst root = packageRoot(__dirname);\nconst worker = path.resolve(root, 'dist', 'cjs', 'workers', 'transformDirectory.js');\nconst call = lazy('node-version-call');\n\nfunction transformDirectoryCallback(src, dest, type, options, callback) {\n try {\n const res = call()({ version, callbacks: true }, worker, src, dest, type, options);\n return callback(null, res);\n } catch (err) {\n return callback(err);\n }\n}\n\n/**\n * @param {string} src The source directory to traverse.\n * @param {string} dest The output directory to write files to.\n * @param {string} type The type of transform ('esm' or 'cjs').\n * @param {{sourceMaps: boolean}} options Options to pass to swc.\n * @param {(err?: Error) =>} [callback] Optional callback. Uses promise if callback not provided.\n * @returns {void | Promise<any>} Optional promise if callback not provided.\n */\nexport default function transformDirectory(src, dest, type, options, callback) {\n if (typeof options === 'function') {\n callback = options;\n options = null;\n }\n options = options || {};\n if (typeof src !== 'string') throw new Error('transformDirectory: unexpected source');\n if (typeof dest !== 'string') throw new Error('transformDirectory: unexpected destination directory');\n if (typeof type !== 'string') throw new Error('transformDirectory: unexpected type');\n\n if (typeof callback === 'function') return transformDirectoryCallback(src, dest, type, options, callback);\n return new Promise((resolve, reject) => {\n transformDirectoryCallback(src, dest, type, options, function compileCallback(err, result) {\n err ? reject(err) : resolve(result);\n });\n });\n}\n"],"names":["path","url","lazy","packageRoot","version","__dirname","dirname","__filename","fileURLToPath","root","worker","resolve","call","transformDirectoryCallback","src","dest","type","options","callback","res","callbacks","err","transformDirectory","Error","Promise","reject","compileCallback","result"],"mappings":"AAAA,OAAOA,UAAU,OAAO;AACxB,OAAOC,SAAS,MAAM;AAEtB,aAAa;AACb,OAAOC,UAAU,iBAAiB;AAClC,OAAOC,iBAAiB,uBAAuB;AAC/C,OAAOC,aAAa,4BAA4B;AAEhD,MAAMC,YAAYL,KAAKM,OAAO,CAAC,OAAOC,eAAe,cAAcA,aAAaN,IAAIO,aAAa,CAAC,YAAYP,GAAG;AACjH,MAAMQ,OAAON,YAAYE;AACzB,MAAMK,SAASV,KAAKW,OAAO,CAACF,MAAM,QAAQ,OAAO,WAAW;AAC5D,MAAMG,OAAOV,KAAK;AAElB,SAASW,2BAA2BC,GAAG,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IACpE,IAAI;QACF,MAAMC,MAAMP,OAAO;YAAER;YAASgB,WAAW;QAAK,GAAGV,QAAQI,KAAKC,MAAMC,MAAMC;QAC1E,OAAOC,SAAS,MAAMC;IACxB,EAAE,OAAOE,KAAK;QACZ,OAAOH,SAASG;IAClB;AACF;AAEA;;;;;;;CAOC,GACD,eAAe,SAASC,mBAAmBR,GAAG,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IAC3E,IAAI,OAAOD,YAAY,YAAY;QACjCC,WAAWD;QACXA,UAAU;IACZ;IACAA,UAAUA,WAAW,CAAC;IACtB,IAAI,OAAOH,QAAQ,UAAU,MAAM,IAAIS,MAAM;IAC7C,IAAI,OAAOR,SAAS,UAAU,MAAM,IAAIQ,MAAM;IAC9C,IAAI,OAAOP,SAAS,UAAU,MAAM,IAAIO,MAAM;IAE9C,IAAI,OAAOL,aAAa,YAAY,OAAOL,2BAA2BC,KAAKC,MAAMC,MAAMC,SAASC;IAChG,OAAO,IAAIM,QAAQ,CAACb,SAASc;QAC3BZ,2BAA2BC,KAAKC,MAAMC,MAAMC,SAAS,SAASS,gBAAgBL,GAAG,EAAEM,MAAM;YACvFN,MAAMI,OAAOJ,OAAOV,QAAQgB;QAC9B;IACF;AACF"}
|
|
@@ -1,122 +1,40 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
1
|
import path from 'path';
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const importReplaceMJS = [
|
|
13
|
-
'.js',
|
|
14
|
-
'.ts',
|
|
15
|
-
'.tsx',
|
|
16
|
-
'.mts',
|
|
17
|
-
'.mjs'
|
|
18
|
-
];
|
|
19
|
-
const importReplaceCJS = [
|
|
20
|
-
'.cts'
|
|
21
|
-
];
|
|
22
|
-
const requireReplaceJS = [
|
|
23
|
-
'.mjs',
|
|
24
|
-
'.cjs',
|
|
25
|
-
'.ts',
|
|
26
|
-
'.tsx',
|
|
27
|
-
'.mts',
|
|
28
|
-
'.cts'
|
|
29
|
-
];
|
|
30
|
-
function makeReplacements(code, regex, extensions, extension) {
|
|
31
|
-
let matches = [];
|
|
32
|
-
let match = regex.exec(code);
|
|
33
|
-
while(match){
|
|
34
|
-
const dependency = match[1] || match[2] || match[3] || match[4];
|
|
35
|
-
const ext = extensions.find((x)=>dependency.slice(-x.length) === x);
|
|
36
|
-
if (ext) matches.push({
|
|
37
|
-
ext,
|
|
38
|
-
match,
|
|
39
|
-
dependency
|
|
40
|
-
});
|
|
41
|
-
match = regex.exec(code);
|
|
42
|
-
}
|
|
43
|
-
matches = matches.reverse();
|
|
44
|
-
for(const index in matches){
|
|
45
|
-
const match = matches[index];
|
|
46
|
-
const start = match.match.index + match.match[0].indexOf(match.dependency) + match.dependency.indexOf(match.ext);
|
|
47
|
-
code = code.substring(0, start) + extension + code.substring(start + match.ext.length);
|
|
48
|
-
}
|
|
49
|
-
return code;
|
|
50
|
-
}
|
|
51
|
-
// https://github.com/vercel/next.js/blob/20b63e13ab2631d6043277895d373aa31a1b327c/packages/next/taskfile-swc.js#L118-L125
|
|
52
|
-
const interopClientDefaultExport = "/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }";
|
|
2
|
+
import url from 'url';
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import lazy from './lib/lazy.cjs';
|
|
5
|
+
import packageRoot from './lib/packageRoot.mjs';
|
|
6
|
+
import version from './lib/transformVersion.mjs';
|
|
7
|
+
const __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : url.fileURLToPath(import.meta.url));
|
|
8
|
+
const root = packageRoot(__dirname);
|
|
9
|
+
const worker = path.resolve(root, 'dist', 'cjs', 'workers', 'transformFile.js');
|
|
10
|
+
const call = lazy('node-version-call');
|
|
53
11
|
function transformFileCallback(src, dest, type, options, callback) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
12
|
+
try {
|
|
13
|
+
const res = call()({
|
|
14
|
+
version,
|
|
15
|
+
callbacks: true
|
|
16
|
+
}, worker, src, dest, type, options);
|
|
17
|
+
return callback(null, res);
|
|
18
|
+
} catch (err) {
|
|
19
|
+
return callback(err);
|
|
57
20
|
}
|
|
58
|
-
options = options || {};
|
|
59
|
-
if (typeof src !== 'string') throw new Error('transformFile: unexpected source');
|
|
60
|
-
if (typeof dest !== 'string') throw new Error('transformFile: unexpected destination directory');
|
|
61
|
-
if (typeof type !== 'string') throw new Error('transformFile: unexpected type');
|
|
62
|
-
fs.readFile(src, 'utf8', (err, contents)=>{
|
|
63
|
-
if (err) return callback(err);
|
|
64
|
-
callback = once(callback);
|
|
65
|
-
try {
|
|
66
|
-
let tsconfig = options.tsconfig ? options.tsconfig : getTS.getTsconfig(src);
|
|
67
|
-
// overrides for cjs
|
|
68
|
-
if (type === 'cjs') {
|
|
69
|
-
tsconfig = {
|
|
70
|
-
...tsconfig
|
|
71
|
-
};
|
|
72
|
-
tsconfig.tsconfig = {
|
|
73
|
-
...tsconfig.config || {}
|
|
74
|
-
};
|
|
75
|
-
tsconfig.config.compilerOptions = {
|
|
76
|
-
...tsconfig.config.compilerOptions || {}
|
|
77
|
-
};
|
|
78
|
-
tsconfig.config.compilerOptions.module = 'CommonJS';
|
|
79
|
-
tsconfig.config.compilerOptions.target = 'ES5';
|
|
80
|
-
}
|
|
81
|
-
const basename = path.basename(src);
|
|
82
|
-
const output = transformSync(contents, basename, tsconfig);
|
|
83
|
-
// infer extension and patch .mjs imports
|
|
84
|
-
let ext = path.extname(basename);
|
|
85
|
-
if (type === 'esm') {
|
|
86
|
-
ext = importReplaceMJS.indexOf(ext) >= 0 ? '.mjs' : ext;
|
|
87
|
-
output.code = makeReplacements(output.code, regexESM, importReplaceMJS, '.mjs');
|
|
88
|
-
ext = importReplaceCJS.indexOf(ext) >= 0 ? '.cjs' : ext;
|
|
89
|
-
output.code = makeReplacements(output.code, regexESM, importReplaceCJS, '.cjs');
|
|
90
|
-
} else {
|
|
91
|
-
ext = requireReplaceJS.indexOf(ext) >= 0 ? '.js' : ext;
|
|
92
|
-
output.code = makeReplacements(output.code, regexCJS, requireReplaceJS, '.js');
|
|
93
|
-
output.code += interopClientDefaultExport;
|
|
94
|
-
}
|
|
95
|
-
const destFilePath = path.join(dest, basename.replace(/\.[^/.]+$/, '') + ext);
|
|
96
|
-
mkdirp(path.dirname(destFilePath), ()=>{
|
|
97
|
-
const queue = new Queue();
|
|
98
|
-
queue.defer(fs.writeFile.bind(null, destFilePath, output.code, 'utf8'));
|
|
99
|
-
!options.sourceMaps || queue.defer(fs.writeFile.bind(null, `${destFilePath}.map`, output.map, 'utf8'));
|
|
100
|
-
queue.await(()=>err ? callback(err) : callback(null, destFilePath));
|
|
101
|
-
});
|
|
102
|
-
} catch (err) {
|
|
103
|
-
callback(err);
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
21
|
}
|
|
107
22
|
/**
|
|
108
23
|
* @param {string} src The source directory to traverse.
|
|
109
|
-
* @param {string} dest The output directory to write
|
|
24
|
+
* @param {string} dest The output directory to write files to.
|
|
110
25
|
* @param {string} type The type of transform ('esm' or 'cjs').
|
|
111
26
|
* @param {{sourceMaps: boolean}} options Options to pass to swc.
|
|
112
|
-
* @param {(err
|
|
113
|
-
* @returns {void | Promise<
|
|
27
|
+
* @param {(err?: Error) =>} [callback] Optional callback. Uses promise if callback not provided.
|
|
28
|
+
* @returns {void | Promise<any>} Optional promise if callback not provided.
|
|
114
29
|
*/ export default function transformFile(src, dest, type, options, callback) {
|
|
115
30
|
if (typeof options === 'function') {
|
|
116
31
|
callback = options;
|
|
117
32
|
options = null;
|
|
118
33
|
}
|
|
119
34
|
options = options || {};
|
|
35
|
+
if (typeof src !== 'string') throw new Error('transformFile: unexpected source');
|
|
36
|
+
if (typeof dest !== 'string') throw new Error('transformFile: unexpected destination directory');
|
|
37
|
+
if (typeof type !== 'string') throw new Error('transformFile: unexpected type');
|
|
120
38
|
if (typeof callback === 'function') return transformFileCallback(src, dest, type, options, callback);
|
|
121
39
|
return new Promise((resolve, reject)=>{
|
|
122
40
|
transformFileCallback(src, dest, type, options, function compileCallback(err, result) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["transformFile.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["transformFile.ts"],"sourcesContent":["import path from 'path';\nimport url from 'url';\n\n// @ts-ignore\nimport lazy from './lib/lazy.cjs';\nimport packageRoot from './lib/packageRoot.js';\nimport version from './lib/transformVersion.js';\n\nconst __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : url.fileURLToPath(import.meta.url));\nconst root = packageRoot(__dirname);\nconst worker = path.resolve(root, 'dist', 'cjs', 'workers', 'transformFile.js');\nconst call = lazy('node-version-call');\n\nfunction transformFileCallback(src, dest, type, options, callback) {\n try {\n const res = call()({ version, callbacks: true }, worker, src, dest, type, options);\n return callback(null, res);\n } catch (err) {\n return callback(err);\n }\n}\n\n/**\n * @param {string} src The source directory to traverse.\n * @param {string} dest The output directory to write files to.\n * @param {string} type The type of transform ('esm' or 'cjs').\n * @param {{sourceMaps: boolean}} options Options to pass to swc.\n * @param {(err?: Error) =>} [callback] Optional callback. Uses promise if callback not provided.\n * @returns {void | Promise<any>} Optional promise if callback not provided.\n */\nexport default function transformFile(src, dest, type, options, callback) {\n if (typeof options === 'function') {\n callback = options;\n options = null;\n }\n options = options || {};\n if (typeof src !== 'string') throw new Error('transformFile: unexpected source');\n if (typeof dest !== 'string') throw new Error('transformFile: unexpected destination directory');\n if (typeof type !== 'string') throw new Error('transformFile: unexpected type');\n\n if (typeof callback === 'function') return transformFileCallback(src, dest, type, options, callback);\n return new Promise((resolve, reject) => {\n transformFileCallback(src, dest, type, options, function compileCallback(err, result) {\n err ? reject(err) : resolve(result);\n });\n });\n}\n"],"names":["path","url","lazy","packageRoot","version","__dirname","dirname","__filename","fileURLToPath","root","worker","resolve","call","transformFileCallback","src","dest","type","options","callback","res","callbacks","err","transformFile","Error","Promise","reject","compileCallback","result"],"mappings":"AAAA,OAAOA,UAAU,OAAO;AACxB,OAAOC,SAAS,MAAM;AAEtB,aAAa;AACb,OAAOC,UAAU,iBAAiB;AAClC,OAAOC,iBAAiB,uBAAuB;AAC/C,OAAOC,aAAa,4BAA4B;AAEhD,MAAMC,YAAYL,KAAKM,OAAO,CAAC,OAAOC,eAAe,cAAcA,aAAaN,IAAIO,aAAa,CAAC,YAAYP,GAAG;AACjH,MAAMQ,OAAON,YAAYE;AACzB,MAAMK,SAASV,KAAKW,OAAO,CAACF,MAAM,QAAQ,OAAO,WAAW;AAC5D,MAAMG,OAAOV,KAAK;AAElB,SAASW,sBAAsBC,GAAG,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IAC/D,IAAI;QACF,MAAMC,MAAMP,OAAO;YAAER;YAASgB,WAAW;QAAK,GAAGV,QAAQI,KAAKC,MAAMC,MAAMC;QAC1E,OAAOC,SAAS,MAAMC;IACxB,EAAE,OAAOE,KAAK;QACZ,OAAOH,SAASG;IAClB;AACF;AAEA;;;;;;;CAOC,GACD,eAAe,SAASC,cAAcR,GAAG,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IACtE,IAAI,OAAOD,YAAY,YAAY;QACjCC,WAAWD;QACXA,UAAU;IACZ;IACAA,UAAUA,WAAW,CAAC;IACtB,IAAI,OAAOH,QAAQ,UAAU,MAAM,IAAIS,MAAM;IAC7C,IAAI,OAAOR,SAAS,UAAU,MAAM,IAAIQ,MAAM;IAC9C,IAAI,OAAOP,SAAS,UAAU,MAAM,IAAIO,MAAM;IAE9C,IAAI,OAAOL,aAAa,YAAY,OAAOL,sBAAsBC,KAAKC,MAAMC,MAAMC,SAASC;IAC3F,OAAO,IAAIM,QAAQ,CAACb,SAASc;QAC3BZ,sBAAsBC,KAAKC,MAAMC,MAAMC,SAAS,SAASS,gBAAgBL,GAAG,EAAEM,MAAM;YAClFN,MAAMI,OAAOJ,OAAOV,QAAQgB;QAC9B;IACF;AACF"}
|
|
@@ -3,17 +3,15 @@ import url from 'url';
|
|
|
3
3
|
// @ts-ignore
|
|
4
4
|
import lazy from './lib/lazy.cjs';
|
|
5
5
|
import packageRoot from './lib/packageRoot.mjs';
|
|
6
|
-
|
|
7
|
-
import process from './lib/process.cjs';
|
|
6
|
+
import version from './lib/transformVersion.mjs';
|
|
8
7
|
const __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : url.fileURLToPath(import.meta.url));
|
|
9
|
-
const major = +process.versions.node.split('.')[0];
|
|
10
|
-
const version = major < 14 ? '14' : 'local';
|
|
11
8
|
const root = packageRoot(__dirname);
|
|
12
9
|
const worker = path.resolve(root, 'dist', 'cjs', 'workers', 'transformSync.js');
|
|
13
10
|
const call = lazy('node-version-call');
|
|
14
11
|
/**
|
|
15
12
|
* @param {string} contents The file contents.
|
|
16
13
|
* @param {string} fileName The filename.
|
|
14
|
+
* @param {TsConfigResult} config The configuration.
|
|
17
15
|
* @returns {{ code: string, map?: string }} Returns object with the transformed code and source map if option sourceMaps was provided.
|
|
18
16
|
*/ export default function transformSync(contents, fileName, config) {
|
|
19
17
|
return call()({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["transformSync.ts"],"sourcesContent":["import path from 'path';\nimport url from 'url';\n\n// @ts-ignore\nimport lazy from './lib/lazy.cjs';\nimport packageRoot from './lib/packageRoot.js';\
|
|
1
|
+
{"version":3,"sources":["transformSync.ts"],"sourcesContent":["import path from 'path';\nimport url from 'url';\nimport type { TsConfigResult } from 'get-tsconfig-compat';\n\n// @ts-ignore\nimport lazy from './lib/lazy.cjs';\nimport packageRoot from './lib/packageRoot.js';\nimport version from './lib/transformVersion.js';\n\nconst __dirname = path.dirname(typeof __filename !== 'undefined' ? __filename : url.fileURLToPath(import.meta.url));\nconst root = packageRoot(__dirname);\nconst worker = path.resolve(root, 'dist', 'cjs', 'workers', 'transformSync.js');\nconst call = lazy('node-version-call');\n\n/**\n * @param {string} contents The file contents.\n * @param {string} fileName The filename.\n * @param {TsConfigResult} config The configuration.\n * @returns {{ code: string, map?: string }} Returns object with the transformed code and source map if option sourceMaps was provided.\n */\nexport default function transformSync(contents: string, fileName: string, config: TsConfigResult) {\n return call()({ version, callbacks: true }, worker, contents, fileName, config);\n}\n"],"names":["path","url","lazy","packageRoot","version","__dirname","dirname","__filename","fileURLToPath","root","worker","resolve","call","transformSync","contents","fileName","config","callbacks"],"mappings":"AAAA,OAAOA,UAAU,OAAO;AACxB,OAAOC,SAAS,MAAM;AAGtB,aAAa;AACb,OAAOC,UAAU,iBAAiB;AAClC,OAAOC,iBAAiB,uBAAuB;AAC/C,OAAOC,aAAa,4BAA4B;AAEhD,MAAMC,YAAYL,KAAKM,OAAO,CAAC,OAAOC,eAAe,cAAcA,aAAaN,IAAIO,aAAa,CAAC,YAAYP,GAAG;AACjH,MAAMQ,OAAON,YAAYE;AACzB,MAAMK,SAASV,KAAKW,OAAO,CAACF,MAAM,QAAQ,OAAO,WAAW;AAC5D,MAAMG,OAAOV,KAAK;AAElB;;;;;CAKC,GACD,eAAe,SAASW,cAAcC,QAAgB,EAAEC,QAAgB,EAAEC,MAAsB;IAC9F,OAAOJ,OAAO;QAAER;QAASa,WAAW;IAAK,GAAGP,QAAQI,UAAUC,UAAUC;AAC1E"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import Iterator from 'fs-iterator';
|
|
3
|
+
import getTS from 'get-tsconfig-compat';
|
|
4
|
+
import createMatcher from '../createMatcher.mjs';
|
|
5
|
+
import transformFile from '../transformFile.mjs';
|
|
6
|
+
export default function transformDirectoryWorker(src, dest, type, options, callback) {
|
|
7
|
+
const tsconfig = options.tsconfig ? options.tsconfig : getTS.getTsconfig(src);
|
|
8
|
+
const matcher = createMatcher(tsconfig);
|
|
9
|
+
options = {
|
|
10
|
+
...options,
|
|
11
|
+
tsconfig
|
|
12
|
+
};
|
|
13
|
+
const iterator = new Iterator(src);
|
|
14
|
+
iterator.forEach((entry, cb)=>{
|
|
15
|
+
if (!entry.stats.isFile()) return cb();
|
|
16
|
+
if (!matcher(entry.fullPath)) return cb();
|
|
17
|
+
transformFile(entry.fullPath, path.dirname(path.join(dest, entry.path)), type, options, cb);
|
|
18
|
+
}, {
|
|
19
|
+
callbacks: true,
|
|
20
|
+
concurrency: options.concurrency || 1024
|
|
21
|
+
}, callback);
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformDirectory.ts"],"sourcesContent":["import path from 'path';\nimport Iterator from 'fs-iterator';\nimport getTS from 'get-tsconfig-compat';\n\nimport createMatcher from '../createMatcher.js';\nimport transformFile from '../transformFile.js';\n\nexport default function transformDirectoryWorker(src, dest, type, options, callback) {\n const tsconfig = options.tsconfig ? options.tsconfig : getTS.getTsconfig(src);\n const matcher = createMatcher(tsconfig);\n\n options = { ...options, tsconfig };\n const iterator = new Iterator(src);\n iterator.forEach(\n (entry, cb) => {\n if (!entry.stats.isFile()) return cb();\n if (!matcher(entry.fullPath)) return cb();\n transformFile(entry.fullPath, path.dirname(path.join(dest, entry.path)), type, options, cb);\n },\n { callbacks: true, concurrency: options.concurrency || 1024 },\n callback\n );\n}\n"],"names":["path","Iterator","getTS","createMatcher","transformFile","transformDirectoryWorker","src","dest","type","options","callback","tsconfig","getTsconfig","matcher","iterator","forEach","entry","cb","stats","isFile","fullPath","dirname","join","callbacks","concurrency"],"mappings":"AAAA,OAAOA,UAAU,OAAO;AACxB,OAAOC,cAAc,cAAc;AACnC,OAAOC,WAAW,sBAAsB;AAExC,OAAOC,mBAAmB,sBAAsB;AAChD,OAAOC,mBAAmB,sBAAsB;AAEhD,eAAe,SAASC,yBAAyBC,GAAG,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IACjF,MAAMC,WAAWF,QAAQE,QAAQ,GAAGF,QAAQE,QAAQ,GAAGT,MAAMU,WAAW,CAACN;IACzE,MAAMO,UAAUV,cAAcQ;IAE9BF,UAAU;QAAE,GAAGA,OAAO;QAAEE;IAAS;IACjC,MAAMG,WAAW,IAAIb,SAASK;IAC9BQ,SAASC,OAAO,CACd,CAACC,OAAOC;QACN,IAAI,CAACD,MAAME,KAAK,CAACC,MAAM,IAAI,OAAOF;QAClC,IAAI,CAACJ,QAAQG,MAAMI,QAAQ,GAAG,OAAOH;QACrCb,cAAcY,MAAMI,QAAQ,EAAEpB,KAAKqB,OAAO,CAACrB,KAAKsB,IAAI,CAACf,MAAMS,MAAMhB,IAAI,IAAIQ,MAAMC,SAASQ;IAC1F,GACA;QAAEM,WAAW;QAAMC,aAAaf,QAAQe,WAAW,IAAI;IAAK,GAC5Dd;AAEJ"}
|