ts-swc-transform 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +19 -0
- package/dist/cjs/createMatcher.js +45 -0
- package/dist/cjs/createMatcher.js.map +1 -0
- package/dist/cjs/index.js +34 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/lib/regexDependencies.js +21 -0
- package/dist/cjs/lib/regexDependencies.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/transformDirectory.js +117 -0
- package/dist/cjs/transformDirectory.js.map +1 -0
- package/dist/cjs/transformFile.js +168 -0
- package/dist/cjs/transformFile.js.map +1 -0
- package/dist/cjs/transformSync.js +15 -0
- package/dist/cjs/transformSync.js.map +1 -0
- package/dist/cjs/workers/transformSync.js +24 -0
- package/dist/cjs/workers/transformSync.js.map +1 -0
- package/dist/esm/createMatcher.mjs +29 -0
- package/dist/esm/createMatcher.mjs.map +1 -0
- package/dist/esm/index.mjs +4 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/lib/regexDependencies.mjs +10 -0
- package/dist/esm/lib/regexDependencies.mjs.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/transformDirectory.mjs +50 -0
- package/dist/esm/transformDirectory.mjs.map +1 -0
- package/dist/esm/transformFile.mjs +125 -0
- package/dist/esm/transformFile.mjs.map +1 -0
- package/dist/esm/transformSync.cjs +13 -0
- package/dist/esm/transformSync.cjs.map +1 -0
- package/dist/esm/workers/transformSync.cjs +22 -0
- package/dist/esm/workers/transformSync.cjs.map +1 -0
- package/dist/types/createMatcher.d.mts +8 -0
- package/dist/types/index.d.mts +4 -0
- package/dist/types/lib/regexDependencies.d.mts +1 -0
- package/dist/types/transformDirectory.d.mts +11 -0
- package/dist/types/transformFile.d.mts +11 -0
- package/dist/types/transformSync.d.cts +5 -0
- package/dist/types/workers/transformSync.d.cts +5 -0
- package/package.json +89 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Kevin Malakoff
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
wof this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
## ts-swc-transform
|
|
2
|
+
|
|
3
|
+
Typescript transformers for swc. Supports Node >= 0.8.
|
|
4
|
+
|
|
5
|
+
Promise
|
|
6
|
+
```
|
|
7
|
+
import { transformDirectory } from 'ts-swc-transform';
|
|
8
|
+
|
|
9
|
+
await transformDirectory('src', 'dist', 'cjs', { sourceMaps: true });
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Callback
|
|
13
|
+
```
|
|
14
|
+
import { transformDirectory } from 'ts-swc-transform';
|
|
15
|
+
|
|
16
|
+
transformDirectory('src', 'dist', 'esm', { sourceMaps: true }, function (err) {
|
|
17
|
+
if (err) /* handle error */
|
|
18
|
+
});
|
|
19
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, /**
|
|
6
|
+
* @param {{path: string, config: Object}} config The path to the loaded TS config and typescript config.
|
|
7
|
+
* @returns {(filePath:string) => boolean} The function to test for typescript files being included or excluded
|
|
8
|
+
*/ "default", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function() {
|
|
11
|
+
return createMatcher;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
var _minimatch = /*#__PURE__*/ _interop_require_default(require("minimatch"));
|
|
15
|
+
var _pathposix = /*#__PURE__*/ _interop_require_default(require("path-posix"));
|
|
16
|
+
var _slash = /*#__PURE__*/ _interop_require_default(require("slash"));
|
|
17
|
+
function _interop_require_default(obj) {
|
|
18
|
+
return obj && obj.__esModule ? obj : {
|
|
19
|
+
default: obj
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function createMatcher(config) {
|
|
23
|
+
var configPath = _pathposix.default.dirname((0, _slash.default)(config.path));
|
|
24
|
+
function matchFn(condition) {
|
|
25
|
+
var pattern = (0, _slash.default)(condition);
|
|
26
|
+
if (!_pathposix.default.isAbsolute(pattern) && !pattern.startsWith('*')) pattern = _pathposix.default.join(configPath, pattern);
|
|
27
|
+
return function match(filePath) {
|
|
28
|
+
return filePath.startsWith(pattern) || (0, _minimatch.default)(filePath, pattern);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
var includes = (config.config.include || []).map(matchFn);
|
|
32
|
+
var excludes = (config.config.exclude || []).map(matchFn);
|
|
33
|
+
return function matcher(filePath) {
|
|
34
|
+
if (filePath.endsWith('.json')) return false;
|
|
35
|
+
filePath = (0, _slash.default)(filePath);
|
|
36
|
+
for(var i = 0; i < excludes.length; ++i){
|
|
37
|
+
if (excludes[i](filePath)) return false;
|
|
38
|
+
}
|
|
39
|
+
for(var j = 0; j < includes.length; ++j){
|
|
40
|
+
if (includes[j](filePath)) return true;
|
|
41
|
+
}
|
|
42
|
+
return !includes.length;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createMatcher.mjs"],"sourcesContent":["import minimatch from 'minimatch';\nimport path from 'path-posix';\nimport slash from 'slash';\n\n/**\n * @param {{path: string, config: Object}} config The path to the loaded TS config and typescript config.\n * @returns {(filePath:string) => boolean} The function to test for typescript files being included or excluded\n */\nexport default function createMatcher(config) {\n const configPath = path.dirname(slash(config.path));\n\n function matchFn(condition) {\n let pattern = slash(condition);\n if (!path.isAbsolute(pattern) && !pattern.startsWith('*')) pattern = path.join(configPath, pattern);\n\n return function match(filePath) {\n return filePath.startsWith(pattern) || minimatch(filePath, pattern);\n };\n }\n\n const includes = (config.config.include || []).map(matchFn);\n const excludes = (config.config.exclude || []).map(matchFn);\n\n return function matcher(filePath) {\n if (filePath.endsWith('.json')) return false;\n\n filePath = slash(filePath);\n for (let i = 0; i < excludes.length; ++i) {\n if (excludes[i](filePath)) return false;\n }\n for (let j = 0; j < includes.length; ++j) {\n if (includes[j](filePath)) return true;\n }\n return !includes.length;\n };\n}\n"],"names":["createMatcher","config","configPath","path","dirname","slash","matchFn","condition","pattern","isAbsolute","startsWith","join","match","filePath","minimatch","includes","include","map","excludes","exclude","matcher","endsWith","i","length","j"],"mappings":";;;;+BAIA;;;CAGC,GACD;;;eAAwBA;;;gEARF;gEACL;4DACC;;;;;;AAMH,SAASA,cAAcC,MAAM;IAC1C,IAAMC,aAAaC,kBAAI,CAACC,OAAO,CAACC,IAAAA,cAAK,EAACJ,OAAOE,IAAI;IAEjD,SAASG,QAAQC,SAAS;QACxB,IAAIC,UAAUH,IAAAA,cAAK,EAACE;QACpB,IAAI,CAACJ,kBAAI,CAACM,UAAU,CAACD,YAAY,CAACA,QAAQE,UAAU,CAAC,MAAMF,UAAUL,kBAAI,CAACQ,IAAI,CAACT,YAAYM;QAE3F,OAAO,SAASI,MAAMC,QAAQ;YAC5B,OAAOA,SAASH,UAAU,CAACF,YAAYM,IAAAA,kBAAS,EAACD,UAAUL;QAC7D;IACF;IAEA,IAAMO,WAAW,AAACd,CAAAA,OAAOA,MAAM,CAACe,OAAO,IAAI,EAAE,AAAD,EAAGC,GAAG,CAACX;IACnD,IAAMY,WAAW,AAACjB,CAAAA,OAAOA,MAAM,CAACkB,OAAO,IAAI,EAAE,AAAD,EAAGF,GAAG,CAACX;IAEnD,OAAO,SAASc,QAAQP,QAAQ;QAC9B,IAAIA,SAASQ,QAAQ,CAAC,UAAU,OAAO;QAEvCR,WAAWR,IAAAA,cAAK,EAACQ;QACjB,IAAK,IAAIS,IAAI,GAAGA,IAAIJ,SAASK,MAAM,EAAE,EAAED,EAAG;YACxC,IAAIJ,QAAQ,CAACI,EAAE,CAACT,WAAW,OAAO;QACpC;QACA,IAAK,IAAIW,IAAI,GAAGA,IAAIT,SAASQ,MAAM,EAAE,EAAEC,EAAG;YACxC,IAAIT,QAAQ,CAACS,EAAE,CAACX,WAAW,OAAO;QACpC;QACA,OAAO,CAACE,SAASQ,MAAM;IACzB;AACF"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
createMatcher: function() {
|
|
13
|
+
return _createMatcher.default;
|
|
14
|
+
},
|
|
15
|
+
transformDirectory: function() {
|
|
16
|
+
return _transformDirectory.default;
|
|
17
|
+
},
|
|
18
|
+
transformFile: function() {
|
|
19
|
+
return _transformFile.default;
|
|
20
|
+
},
|
|
21
|
+
transformSync: function() {
|
|
22
|
+
return _transformSynccjs.default;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
var _createMatcher = /*#__PURE__*/ _interop_require_default(require("./createMatcher.js"));
|
|
26
|
+
var _transformFile = /*#__PURE__*/ _interop_require_default(require("./transformFile.js"));
|
|
27
|
+
var _transformDirectory = /*#__PURE__*/ _interop_require_default(require("./transformDirectory.js"));
|
|
28
|
+
var _transformSynccjs = /*#__PURE__*/ _interop_require_default(require("./transformSync.js"));
|
|
29
|
+
function _interop_require_default(obj) {
|
|
30
|
+
return obj && obj.__esModule ? obj : {
|
|
31
|
+
default: obj
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.mjs"],"sourcesContent":["export { default as createMatcher } from './createMatcher.mjs';\nexport { default as transformFile } from './transformFile.mjs';\nexport { default as transformDirectory } from './transformDirectory.mjs';\nexport { default as transformSync } from './transformSync.cjs';\n"],"names":["createMatcher","transformDirectory","transformFile","transformSync"],"mappings":";;;;;;;;;;;IAAoBA,aAAa;eAAbA,sBAAa;;IAEbC,kBAAkB;eAAlBA,2BAAkB;;IADlBC,aAAa;eAAbA,sBAAa;;IAEbC,aAAa;eAAbA,yBAAa;;;oEAHQ;oEACA;yEACK;uEACL"}
|
|
@@ -0,0 +1,21 @@
|
|
|
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 regexDependencies;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
function regexDependencies(esm) {
|
|
12
|
+
var matchingDeps = '\\s*[\'"`]([^\'"`]+)[\'"`]\\s*';
|
|
13
|
+
var matchingName = '\\s*(?:[\\w${},\\s*]+)\\s*';
|
|
14
|
+
var regex = "(?:(?:var|const|let)".concat(matchingName, "=\\s*)?require\\(").concat(matchingDeps, "\\);?");
|
|
15
|
+
if (esm) {
|
|
16
|
+
regex += "|import(?:".concat(matchingName, "from\\s*)?").concat(matchingDeps, ";?");
|
|
17
|
+
regex += "|export(?:".concat(matchingName, "from\\s*)?").concat(matchingDeps, ";?");
|
|
18
|
+
}
|
|
19
|
+
return new RegExp(regex, 'g');
|
|
20
|
+
}
|
|
21
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["regexDependencies.mjs"],"sourcesContent":["export default function regexDependencies(esm) {\n const matchingDeps = '\\\\s*[\\'\"`]([^\\'\"`]+)[\\'\"`]\\\\s*';\n const matchingName = '\\\\s*(?:[\\\\w${},\\\\s*]+)\\\\s*';\n\n let regex = `(?:(?:var|const|let)${matchingName}=\\\\s*)?require\\\\(${matchingDeps}\\\\);?`;\n if (esm) {\n regex += `|import(?:${matchingName}from\\\\s*)?${matchingDeps};?`;\n regex += `|export(?:${matchingName}from\\\\s*)?${matchingDeps};?`;\n }\n return new RegExp(regex, 'g');\n}\n"],"names":["regexDependencies","esm","matchingDeps","matchingName","regex","RegExp"],"mappings":";;;;+BAAA;;;eAAwBA;;;AAAT,SAASA,kBAAkBC,GAAG;IAC3C,IAAMC,eAAe;IACrB,IAAMC,eAAe;IAErB,IAAIC,QAAQ,AAAC,uBAAsDF,OAAhCC,cAAa,qBAAgC,OAAbD,cAAa;IAChF,IAAID,KAAK;QACPG,SAAS,AAAC,aAAqCF,OAAzBC,cAAa,cAAyB,OAAbD,cAAa;QAC5DE,SAAS,AAAC,aAAqCF,OAAzBC,cAAa,cAAyB,OAAbD,cAAa;IAC9D;IACA,OAAO,IAAIG,OAAOD,OAAO;AAC3B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, /**
|
|
6
|
+
* @param {string} src The source directory to traverse.
|
|
7
|
+
* @param {string} dest The output directory to write files to.
|
|
8
|
+
* @param {string} type The type of transform ('esm' or 'cjs').
|
|
9
|
+
* @param {{sourceMaps: boolean}} options Options to pass to swc.
|
|
10
|
+
* @param {(err?: Error) =>} [callback] Optional callback. Uses promise if callback not provided.
|
|
11
|
+
* @returns {void | Promise<any>} Optional promise if callback not provided.
|
|
12
|
+
*/ "default", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function() {
|
|
15
|
+
return transformDirectory;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
var _path = /*#__PURE__*/ _interop_require_default(require("path"));
|
|
19
|
+
var _fsiterator = /*#__PURE__*/ _interop_require_default(require("fs-iterator"));
|
|
20
|
+
var _gettsconfigcompat = /*#__PURE__*/ _interop_require_default(require("get-tsconfig-compat"));
|
|
21
|
+
var _createMatcher = /*#__PURE__*/ _interop_require_default(require("./createMatcher.js"));
|
|
22
|
+
var _transformFile = /*#__PURE__*/ _interop_require_default(require("./transformFile.js"));
|
|
23
|
+
function _define_property(obj, key, value) {
|
|
24
|
+
if (key in obj) {
|
|
25
|
+
Object.defineProperty(obj, key, {
|
|
26
|
+
value: value,
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
obj[key] = value;
|
|
33
|
+
}
|
|
34
|
+
return obj;
|
|
35
|
+
}
|
|
36
|
+
function _interop_require_default(obj) {
|
|
37
|
+
return obj && obj.__esModule ? obj : {
|
|
38
|
+
default: obj
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function _object_spread(target) {
|
|
42
|
+
for(var i = 1; i < arguments.length; i++){
|
|
43
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
44
|
+
var ownKeys = Object.keys(source);
|
|
45
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
46
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
47
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
ownKeys.forEach(function(key) {
|
|
51
|
+
_define_property(target, key, source[key]);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return target;
|
|
55
|
+
}
|
|
56
|
+
function ownKeys(object, enumerableOnly) {
|
|
57
|
+
var keys = Object.keys(object);
|
|
58
|
+
if (Object.getOwnPropertySymbols) {
|
|
59
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
60
|
+
if (enumerableOnly) {
|
|
61
|
+
symbols = symbols.filter(function(sym) {
|
|
62
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
keys.push.apply(keys, symbols);
|
|
66
|
+
}
|
|
67
|
+
return keys;
|
|
68
|
+
}
|
|
69
|
+
function _object_spread_props(target, source) {
|
|
70
|
+
source = source != null ? source : {};
|
|
71
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
72
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
73
|
+
} else {
|
|
74
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
75
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return target;
|
|
79
|
+
}
|
|
80
|
+
function transformDirectoryCallback(src, dest, type, options, callback) {
|
|
81
|
+
if (typeof options === 'function') {
|
|
82
|
+
callback = options;
|
|
83
|
+
options = {};
|
|
84
|
+
}
|
|
85
|
+
if (typeof src !== 'string') throw new Error('transformDirectory: unexpected source');
|
|
86
|
+
if (typeof dest !== 'string') throw new Error('transformDirectory: unexpected destination directory');
|
|
87
|
+
if (typeof type !== 'string') throw new Error('transformDirectory: unexpected type');
|
|
88
|
+
var cwd = options.cwd || process.cwd();
|
|
89
|
+
var config = options.confg ? options.confg : _gettsconfigcompat.default.getTsconfig(_path.default.resolve(cwd, 'tsconfig.json'));
|
|
90
|
+
var matcher = (0, _createMatcher.default)(config);
|
|
91
|
+
options = _object_spread_props(_object_spread({}, options), {
|
|
92
|
+
config: config
|
|
93
|
+
});
|
|
94
|
+
var iterator = new _fsiterator.default(src);
|
|
95
|
+
iterator.forEach(function(entry, cb) {
|
|
96
|
+
if (!entry.stats.isFile()) return cb();
|
|
97
|
+
if (!matcher(entry.fullPath)) return cb();
|
|
98
|
+
(0, _transformFile.default)(entry.fullPath, _path.default.dirname(_path.default.join(dest, entry.path)), type, options, cb);
|
|
99
|
+
}, {
|
|
100
|
+
callbacks: true,
|
|
101
|
+
concurrency: options.concurrency || 1024
|
|
102
|
+
}, callback);
|
|
103
|
+
}
|
|
104
|
+
function transformDirectory(src, dest, type, options, callback) {
|
|
105
|
+
if (typeof options === 'function') {
|
|
106
|
+
callback = options;
|
|
107
|
+
options = null;
|
|
108
|
+
}
|
|
109
|
+
options = options || {};
|
|
110
|
+
if (typeof callback === 'function') return transformDirectoryCallback(src, dest, type, options, callback);
|
|
111
|
+
return new Promise(function(resolve, reject) {
|
|
112
|
+
transformDirectoryCallback(src, dest, type, options, function compileCallback(err, result) {
|
|
113
|
+
err ? reject(err) : resolve(result);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformDirectory.mjs"],"sourcesContent":["import path from 'path';\nimport Iterator from 'fs-iterator';\nimport getTS from 'get-tsconfig-compat';\n\nimport createMatcher from './createMatcher.mjs';\nimport transformFile from './transformFile.mjs';\n\nfunction transformDirectoryCallback(src, dest, type, options, callback) {\n if (typeof options === 'function') {\n callback = options;\n options = {};\n }\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 const cwd = options.cwd || process.cwd();\n const config = options.confg ? options.confg : getTS.getTsconfig(path.resolve(cwd, 'tsconfig.json'));\n const matcher = createMatcher(config);\n\n options = { ...options, config };\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\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\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":["transformDirectory","transformDirectoryCallback","src","dest","type","options","callback","Error","cwd","process","config","confg","getTS","getTsconfig","path","resolve","matcher","createMatcher","iterator","Iterator","forEach","entry","cb","stats","isFile","fullPath","transformFile","dirname","join","callbacks","concurrency","Promise","reject","compileCallback","err","result"],"mappings":";;;;+BAiCA;;;;;;;CAOC,GACD;;;eAAwBA;;;2DAzCP;iEACI;wEACH;oEAEQ;oEACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE1B,SAASC,2BAA2BC,GAAG,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IACpE,IAAI,OAAOD,YAAY,YAAY;QACjCC,WAAWD;QACXA,UAAU,CAAC;IACb;IACA,IAAI,OAAOH,QAAQ,UAAU,MAAM,IAAIK,MAAM;IAC7C,IAAI,OAAOJ,SAAS,UAAU,MAAM,IAAII,MAAM;IAC9C,IAAI,OAAOH,SAAS,UAAU,MAAM,IAAIG,MAAM;IAE9C,IAAMC,MAAMH,QAAQG,GAAG,IAAIC,QAAQD,GAAG;IACtC,IAAME,SAASL,QAAQM,KAAK,GAAGN,QAAQM,KAAK,GAAGC,0BAAK,CAACC,WAAW,CAACC,aAAI,CAACC,OAAO,CAACP,KAAK;IACnF,IAAMQ,UAAUC,IAAAA,sBAAa,EAACP;IAE9BL,UAAU,wCAAKA;QAASK,QAAAA;;IACxB,IAAMQ,WAAW,IAAIC,mBAAQ,CAACjB;IAC9BgB,SAASE,OAAO,CACd,SAACC,OAAOC;QACN,IAAI,CAACD,MAAME,KAAK,CAACC,MAAM,IAAI,OAAOF;QAClC,IAAI,CAACN,QAAQK,MAAMI,QAAQ,GAAG,OAAOH;QACrCI,IAAAA,sBAAa,EAACL,MAAMI,QAAQ,EAAEX,aAAI,CAACa,OAAO,CAACb,aAAI,CAACc,IAAI,CAACzB,MAAMkB,MAAMP,IAAI,IAAIV,MAAMC,SAASiB;IAC1F,GACA;QAAEO,WAAW;QAAMC,aAAazB,QAAQyB,WAAW,IAAI;IAAK,GAC5DxB;AAEJ;AAUe,SAASN,mBAAmBE,GAAG,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IAC3E,IAAI,OAAOD,YAAY,YAAY;QACjCC,WAAWD;QACXA,UAAU;IACZ;IACAA,UAAUA,WAAW,CAAC;IAEtB,IAAI,OAAOC,aAAa,YAAY,OAAOL,2BAA2BC,KAAKC,MAAMC,MAAMC,SAASC;IAChG,OAAO,IAAIyB,QAAQ,SAAChB,SAASiB;QAC3B/B,2BAA2BC,KAAKC,MAAMC,MAAMC,SAAS,SAAS4B,gBAAgBC,GAAG,EAAEC,MAAM;YACvFD,MAAMF,OAAOE,OAAOnB,QAAQoB;QAC9B;IACF;AACF"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, /**
|
|
6
|
+
* @param {string} src The source directory to traverse.
|
|
7
|
+
* @param {string} dest The output directory to write the file to.
|
|
8
|
+
* @param {string} type The type of transform ('esm' or 'cjs').
|
|
9
|
+
* @param {{sourceMaps: boolean}} options Options to pass to swc.
|
|
10
|
+
* @param {(err?: Error, destFilePath: string) =>} [callback] Optional callback returing the path to the transformed file. Uses promise if callback not provided.
|
|
11
|
+
* @returns {void | Promise<string>} Optional promise returing the path to the transformed file if callback not provided.
|
|
12
|
+
*/ "default", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function() {
|
|
15
|
+
return transformFile;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
var _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
|
|
19
|
+
var _path = /*#__PURE__*/ _interop_require_default(require("path"));
|
|
20
|
+
var _calloncefn = /*#__PURE__*/ _interop_require_default(require("call-once-fn"));
|
|
21
|
+
var _gettsconfigcompat = /*#__PURE__*/ _interop_require_default(require("get-tsconfig-compat"));
|
|
22
|
+
var _mkdirp = /*#__PURE__*/ _interop_require_default(require("mkdirp"));
|
|
23
|
+
var _queuecb = /*#__PURE__*/ _interop_require_default(require("queue-cb"));
|
|
24
|
+
var _regexDependencies = /*#__PURE__*/ _interop_require_default(require("./lib/regexDependencies.js"));
|
|
25
|
+
var _transformSynccjs = /*#__PURE__*/ _interop_require_default(require("./transformSync.js"));
|
|
26
|
+
function _define_property(obj, key, value) {
|
|
27
|
+
if (key in obj) {
|
|
28
|
+
Object.defineProperty(obj, key, {
|
|
29
|
+
value: value,
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true
|
|
33
|
+
});
|
|
34
|
+
} else {
|
|
35
|
+
obj[key] = value;
|
|
36
|
+
}
|
|
37
|
+
return obj;
|
|
38
|
+
}
|
|
39
|
+
function _interop_require_default(obj) {
|
|
40
|
+
return obj && obj.__esModule ? obj : {
|
|
41
|
+
default: obj
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function _object_spread(target) {
|
|
45
|
+
for(var i = 1; i < arguments.length; i++){
|
|
46
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
47
|
+
var ownKeys = Object.keys(source);
|
|
48
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
49
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
50
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
ownKeys.forEach(function(key) {
|
|
54
|
+
_define_property(target, key, source[key]);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return target;
|
|
58
|
+
}
|
|
59
|
+
var regexESM = (0, _regexDependencies.default)(true);
|
|
60
|
+
var regexCJS = (0, _regexDependencies.default)();
|
|
61
|
+
var importReplaceMJS = [
|
|
62
|
+
'.js',
|
|
63
|
+
'.ts',
|
|
64
|
+
'.tsx',
|
|
65
|
+
'.mts',
|
|
66
|
+
'.mjs'
|
|
67
|
+
];
|
|
68
|
+
var importReplaceCJS = [
|
|
69
|
+
'.cts'
|
|
70
|
+
];
|
|
71
|
+
var requireReplaceJS = [
|
|
72
|
+
'.mjs',
|
|
73
|
+
'.cjs',
|
|
74
|
+
'.ts',
|
|
75
|
+
'.tsx',
|
|
76
|
+
'.mts',
|
|
77
|
+
'.cts'
|
|
78
|
+
];
|
|
79
|
+
function makeReplacements(code, regex, extensions, extension) {
|
|
80
|
+
var _loop = function() {
|
|
81
|
+
var dependency = match[1] || match[2] || match[3] || match[4];
|
|
82
|
+
var ext = extensions.find(function(x) {
|
|
83
|
+
return dependency.slice(-x.length) === x;
|
|
84
|
+
});
|
|
85
|
+
if (ext) matches.push({
|
|
86
|
+
ext: ext,
|
|
87
|
+
match: match,
|
|
88
|
+
dependency: dependency
|
|
89
|
+
});
|
|
90
|
+
match = regex.exec(code);
|
|
91
|
+
};
|
|
92
|
+
var matches = [];
|
|
93
|
+
var match = regex.exec(code);
|
|
94
|
+
while(match)_loop();
|
|
95
|
+
matches = matches.reverse();
|
|
96
|
+
for(var index in matches){
|
|
97
|
+
var match1 = matches[index];
|
|
98
|
+
var start = match1.match.index + match1.match[0].indexOf(match1.dependency) + match1.dependency.indexOf(match1.ext);
|
|
99
|
+
code = code.substring(0, start) + extension + code.substring(start + match1.ext.length);
|
|
100
|
+
}
|
|
101
|
+
return code;
|
|
102
|
+
}
|
|
103
|
+
// https://github.com/vercel/next.js/blob/20b63e13ab2631d6043277895d373aa31a1b327c/packages/next/taskfile-swc.js#L118-L125
|
|
104
|
+
var interopClientDefaultExport = "/* CJS INTEROP */ if (exports.__esModule && exports.default) { if(typeof exports.default === 'object') Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { if (key !== 'default') exports.default[key] = exports[key]; }; module.exports = exports.default; }";
|
|
105
|
+
function transformFileCallback(src, dest, type, options, callback) {
|
|
106
|
+
if (typeof options === 'function') {
|
|
107
|
+
callback = options;
|
|
108
|
+
options = {};
|
|
109
|
+
}
|
|
110
|
+
if (typeof src !== 'string') throw new Error('transformFile: unexpected source');
|
|
111
|
+
if (typeof dest !== 'string') throw new Error('transformFile: unexpected destination directory');
|
|
112
|
+
if (typeof type !== 'string') throw new Error('transformFile: unexpected type');
|
|
113
|
+
_fs.default.readFile(src, 'utf8', function(err, contents) {
|
|
114
|
+
if (err) return callback(err);
|
|
115
|
+
callback = (0, _calloncefn.default)(callback);
|
|
116
|
+
try {
|
|
117
|
+
var cwd = options.cwd || process.cwd();
|
|
118
|
+
var config = options.confg ? options.confg : _gettsconfigcompat.default.getTsconfig(_path.default.resolve(cwd, 'tsconfig.json'));
|
|
119
|
+
// overrides for cjs
|
|
120
|
+
if (type === 'cjs') {
|
|
121
|
+
config = _object_spread({}, config);
|
|
122
|
+
config.config = _object_spread({}, config.config || {});
|
|
123
|
+
config.config.compilerOptions = _object_spread({}, config.config.compilerOptions || {});
|
|
124
|
+
config.config.compilerOptions.module = 'CommonJS';
|
|
125
|
+
config.config.compilerOptions.target = 'ES5';
|
|
126
|
+
}
|
|
127
|
+
var basename = _path.default.basename(src);
|
|
128
|
+
var output = (0, _transformSynccjs.default)(contents, basename, config);
|
|
129
|
+
// infer extension and patch .mjs imports
|
|
130
|
+
var ext = _path.default.extname(basename);
|
|
131
|
+
if (type === 'esm') {
|
|
132
|
+
ext = importReplaceMJS.indexOf(ext) >= 0 ? '.mjs' : ext;
|
|
133
|
+
output.code = makeReplacements(output.code, regexESM, importReplaceMJS, '.mjs');
|
|
134
|
+
ext = importReplaceCJS.indexOf(ext) >= 0 ? '.cjs' : ext;
|
|
135
|
+
output.code = makeReplacements(output.code, regexESM, importReplaceCJS, '.cjs');
|
|
136
|
+
} else {
|
|
137
|
+
ext = requireReplaceJS.indexOf(ext) >= 0 ? '.js' : ext;
|
|
138
|
+
output.code = makeReplacements(output.code, regexCJS, requireReplaceJS, '.js');
|
|
139
|
+
output.code += interopClientDefaultExport;
|
|
140
|
+
}
|
|
141
|
+
var destFilePath = _path.default.join(dest, basename.replace(/\.[^/.]+$/, '') + ext);
|
|
142
|
+
(0, _mkdirp.default)(_path.default.dirname(destFilePath), function() {
|
|
143
|
+
var queue = new _queuecb.default();
|
|
144
|
+
queue.defer(_fs.default.writeFile.bind(null, destFilePath, output.code, 'utf8'));
|
|
145
|
+
!options.sourceMaps || queue.defer(_fs.default.writeFile.bind(null, "".concat(destFilePath, ".map"), output.map, 'utf8'));
|
|
146
|
+
queue.await(function() {
|
|
147
|
+
return err ? callback(err) : callback(null, destFilePath);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
} catch (err) {
|
|
151
|
+
callback(err);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function transformFile(src, dest, type, options, callback) {
|
|
156
|
+
if (typeof options === 'function') {
|
|
157
|
+
callback = options;
|
|
158
|
+
options = null;
|
|
159
|
+
}
|
|
160
|
+
options = options || {};
|
|
161
|
+
if (typeof callback === 'function') return transformFileCallback(src, dest, type, options, callback);
|
|
162
|
+
return new Promise(function(resolve, reject) {
|
|
163
|
+
transformFileCallback(src, dest, type, options, function compileCallback(err, result) {
|
|
164
|
+
err ? reject(err) : resolve(result);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformFile.mjs"],"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';\nimport Queue from 'queue-cb';\n\nimport regexDependencies from './lib/regexDependencies.mjs';\nimport transformSync from './transformSync.cjs';\n\nconst regexESM = regexDependencies(true);\nconst regexCJS = regexDependencies();\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 =\n \"/* CJS INTEROP */ if (exports.__esModule && exports.default) { if(typeof exports.default === 'object') Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { if (key !== 'default') exports.default[key] = exports[key]; }; module.exports = exports.default; }\";\n\nfunction transformFileCallback(src, dest, type, options, callback) {\n if (typeof options === 'function') {\n callback = options;\n options = {};\n }\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 fs.readFile(src, 'utf8', (err, contents) => {\n if (err) return callback(err);\n callback = once(callback);\n\n try {\n const cwd = options.cwd || process.cwd();\n let config = options.confg ? options.confg : getTS.getTsconfig(path.resolve(cwd, 'tsconfig.json'));\n\n // overrides for cjs\n if (type === 'cjs') {\n config = { ...config };\n config.config = { ...(config.config || {}) };\n config.config.compilerOptions = { ...(config.config.compilerOptions || {}) };\n config.config.compilerOptions.module = 'CommonJS';\n config.config.compilerOptions.target = 'ES5';\n }\n\n const basename = path.basename(src);\n const output = transformSync(contents, basename, config);\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 } catch (err) {\n callback(err);\n }\n });\n}\n\n/**\n * @param {string} src The source directory to traverse.\n * @param {string} dest The output directory to write the file 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, destFilePath: string) =>} [callback] Optional callback returing the path to the transformed file. Uses promise if callback not provided.\n * @returns {void | Promise<string>} Optional promise returing the path to the transformed file 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\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":["transformFile","regexESM","regexDependencies","regexCJS","importReplaceMJS","importReplaceCJS","requireReplaceJS","makeReplacements","code","regex","extensions","extension","dependency","match","ext","find","x","slice","length","matches","push","exec","reverse","index","start","indexOf","substring","interopClientDefaultExport","transformFileCallback","src","dest","type","options","callback","Error","fs","readFile","err","contents","once","cwd","process","config","confg","getTS","getTsconfig","path","resolve","compilerOptions","module","target","basename","output","transformSync","extname","destFilePath","join","replace","mkdirp","dirname","queue","Queue","defer","writeFile","bind","sourceMaps","map","await","Promise","reject","compileCallback","result"],"mappings":";;;;+BA+FA;;;;;;;CAOC,GACD;;;eAAwBA;;;yDAvGT;2DACE;iEACA;wEACC;6DACC;8DACD;wEAEY;uEACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE1B,IAAMC,WAAWC,IAAAA,0BAAiB,EAAC;AACnC,IAAMC,WAAWD,IAAAA,0BAAiB;AAElC,IAAME,mBAAmB;IAAC;IAAO;IAAO;IAAQ;IAAQ;CAAO;AAC/D,IAAMC,mBAAmB;IAAC;CAAO;AACjC,IAAMC,mBAAmB;IAAC;IAAQ;IAAQ;IAAO;IAAQ;IAAQ;CAAO;AAExE,SAASC,iBAAiBC,IAAI,EAAEC,KAAK,EAAEC,UAAU,EAAEC,SAAS;;QAIxD,IAAMC,aAAaC,KAAK,CAAC,EAAE,IAAIA,KAAK,CAAC,EAAE,IAAIA,KAAK,CAAC,EAAE,IAAIA,KAAK,CAAC,EAAE;QAC/D,IAAMC,MAAMJ,WAAWK,IAAI,CAAC,SAACC;mBAAMJ,WAAWK,KAAK,CAAC,CAACD,EAAEE,MAAM,MAAMF;;QACnE,IAAIF,KAAKK,QAAQC,IAAI,CAAC;YAAEN,KAAAA;YAAKD,OAAAA;YAAOD,YAAAA;QAAW;QAC/CC,QAAQJ,MAAMY,IAAI,CAACb;IACrB;IAPA,IAAIW,UAAU,EAAE;IAChB,IAAIN,QAAQJ,MAAMY,IAAI,CAACb;IACvB,MAAOK;IAOPM,UAAUA,QAAQG,OAAO;IACzB,IAAK,IAAMC,SAASJ,QAAS;QAC3B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,SAAS,MAAMsB;;YAC1D;QACF,EAAE,OAAOlB,KAAK;YACZJ,SAASI;QACX;IACF;AACF;AAUe,SAASrC,cAAc6B,GAAG,EAAEC,IAAI,EAAEC,IAAI,EAAEC,OAAO,EAAEC,QAAQ;IACtE,IAAI,OAAOD,YAAY,YAAY;QACjCC,WAAWD;QACXA,UAAU;IACZ;IACAA,UAAUA,WAAW,CAAC;IAEtB,IAAI,OAAOC,aAAa,YAAY,OAAOL,sBAAsBC,KAAKC,MAAMC,MAAMC,SAASC;IAC3F,OAAO,IAAImC,QAAQ,SAACrB,SAASsB;QAC3BzC,sBAAsBC,KAAKC,MAAMC,MAAMC,SAAS,SAASsC,gBAAgBjC,GAAG,EAAEkC,MAAM;YAClFlC,MAAMgC,OAAOhC,OAAOU,QAAQwB;QAC9B;IACF;AACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var path = require('path');
|
|
3
|
+
var major = +process.versions.node.split('.')[0];
|
|
4
|
+
var version = major >= 14 ? 'local' : 'lts';
|
|
5
|
+
var worker = path.resolve(__dirname, 'workers', "transformSync".concat(path.extname(__filename)));
|
|
6
|
+
var call = null; // break dependencies
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} contents The file contents.
|
|
9
|
+
* @param {string} fileName The filename.
|
|
10
|
+
* @returns {{ code: string, map?: string }} Returns object with the transformed code and source map if option sourceMaps was provided.
|
|
11
|
+
*/ module.exports = function transformSync(contents, fileName, config) {
|
|
12
|
+
if (!call) call = require('node-version-call'); // break dependencies
|
|
13
|
+
return call(version, worker, contents, fileName, config);
|
|
14
|
+
};
|
|
15
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformSync.cjs"],"sourcesContent":["const path = require('path');\n\nconst major = +process.versions.node.split('.')[0];\nconst version = major >= 14 ? 'local' : 'lts';\nconst worker = path.resolve(__dirname, 'workers', `transformSync${path.extname(__filename)}`);\n\nlet call = null; // break dependencies\n\n/**\n * @param {string} contents The file contents.\n * @param {string} fileName The filename.\n * @returns {{ code: string, map?: string }} Returns object with the transformed code and source map if option sourceMaps was provided.\n */\nmodule.exports = function transformSync(contents, fileName, config) {\n if (!call) call = require('node-version-call'); // break dependencies\n\n return call(version, worker, contents, fileName, config);\n};\n"],"names":["path","require","major","process","versions","node","split","version","worker","resolve","__dirname","extname","__filename","call","module","exports","transformSync","contents","fileName","config"],"mappings":";AAAA,IAAMA,OAAOC,QAAQ;AAErB,IAAMC,QAAQ,CAACC,QAAQC,QAAQ,CAACC,IAAI,CAACC,KAAK,CAAC,IAAI,CAAC,EAAE;AAClD,IAAMC,UAAUL,SAAS,KAAK,UAAU;AACxC,IAAMM,SAASR,KAAKS,OAAO,CAACC,WAAW,WAAW,AAAC,gBAAwC,OAAzBV,KAAKW,OAAO,CAACC;AAE/E,IAAIC,OAAO,MAAM,qBAAqB;AAEtC;;;;CAIC,GACDC,OAAOC,OAAO,GAAG,SAASC,cAAcC,QAAQ,EAAEC,QAAQ,EAAEC,MAAM;IAChE,IAAI,CAACN,MAAMA,OAAOZ,QAAQ,sBAAsB,qBAAqB;IAErE,OAAOY,KAAKN,SAASC,QAAQS,UAAUC,UAAUC;AACnD"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var path = require('path');
|
|
3
|
+
var swc = require('@swc/core');
|
|
4
|
+
var ts = require('typescript');
|
|
5
|
+
var swcTranspiler = require('ts-node/transpilers/swc');
|
|
6
|
+
module.exports = function transformSync(contents, fileName, config) {
|
|
7
|
+
var parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, path.dirname(config.path));
|
|
8
|
+
var transpile = swcTranspiler.create({
|
|
9
|
+
swc: swc,
|
|
10
|
+
service: {
|
|
11
|
+
config: {
|
|
12
|
+
options: parsed.options
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
var res = transpile.transpile(contents, {
|
|
17
|
+
fileName: fileName
|
|
18
|
+
});
|
|
19
|
+
return {
|
|
20
|
+
code: res.outputText,
|
|
21
|
+
map: res.sourceMapText
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) exports.default[key] = exports[key]; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformSync.cjs"],"sourcesContent":["const path = require('path');\nconst swc = require('@swc/core');\nconst ts = require('typescript');\nconst swcTranspiler = require('ts-node/transpilers/swc');\n\nmodule.exports = function transformSync(contents, fileName, config) {\n const parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, path.dirname(config.path));\n const transpile = swcTranspiler.create({ swc: swc, service: { config: { options: parsed.options } } });\n const res = transpile.transpile(contents, { fileName: fileName });\n return { code: res.outputText, map: res.sourceMapText };\n};\n"],"names":["path","require","swc","ts","swcTranspiler","module","exports","transformSync","contents","fileName","config","parsed","parseJsonConfigFileContent","sys","dirname","transpile","create","service","options","res","code","outputText","map","sourceMapText"],"mappings":";AAAA,IAAMA,OAAOC,QAAQ;AACrB,IAAMC,MAAMD,QAAQ;AACpB,IAAME,KAAKF,QAAQ;AACnB,IAAMG,gBAAgBH,QAAQ;AAE9BI,OAAOC,OAAO,GAAG,SAASC,cAAcC,QAAQ,EAAEC,QAAQ,EAAEC,MAAM;IAChE,IAAMC,SAASR,GAAGS,0BAA0B,CAACF,OAAOA,MAAM,EAAEP,GAAGU,GAAG,EAAEb,KAAKc,OAAO,CAACJ,OAAOV,IAAI;IAC5F,IAAMe,YAAYX,cAAcY,MAAM,CAAC;QAAEd,KAAKA;QAAKe,SAAS;YAAEP,QAAQ;gBAAEQ,SAASP,OAAOO,OAAO;YAAC;QAAE;IAAE;IACpG,IAAMC,MAAMJ,UAAUA,SAAS,CAACP,UAAU;QAAEC,UAAUA;IAAS;IAC/D,OAAO;QAAEW,MAAMD,IAAIE,UAAU;QAAEC,KAAKH,IAAII,aAAa;IAAC;AACxD"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import minimatch from 'minimatch';
|
|
2
|
+
import path from 'path-posix';
|
|
3
|
+
import slash from 'slash';
|
|
4
|
+
/**
|
|
5
|
+
* @param {{path: string, config: Object}} config The path to the loaded TS config and typescript config.
|
|
6
|
+
* @returns {(filePath:string) => boolean} The function to test for typescript files being included or excluded
|
|
7
|
+
*/ export default function createMatcher(config) {
|
|
8
|
+
const configPath = path.dirname(slash(config.path));
|
|
9
|
+
function matchFn(condition) {
|
|
10
|
+
let pattern = slash(condition);
|
|
11
|
+
if (!path.isAbsolute(pattern) && !pattern.startsWith('*')) pattern = path.join(configPath, pattern);
|
|
12
|
+
return function match(filePath) {
|
|
13
|
+
return filePath.startsWith(pattern) || minimatch(filePath, pattern);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
const includes = (config.config.include || []).map(matchFn);
|
|
17
|
+
const excludes = (config.config.exclude || []).map(matchFn);
|
|
18
|
+
return function matcher(filePath) {
|
|
19
|
+
if (filePath.endsWith('.json')) return false;
|
|
20
|
+
filePath = slash(filePath);
|
|
21
|
+
for(let i = 0; i < excludes.length; ++i){
|
|
22
|
+
if (excludes[i](filePath)) return false;
|
|
23
|
+
}
|
|
24
|
+
for(let j = 0; j < includes.length; ++j){
|
|
25
|
+
if (includes[j](filePath)) return true;
|
|
26
|
+
}
|
|
27
|
+
return !includes.length;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["createMatcher.mjs"],"sourcesContent":["import minimatch from 'minimatch';\nimport path from 'path-posix';\nimport slash from 'slash';\n\n/**\n * @param {{path: string, config: Object}} config The path to the loaded TS config and typescript config.\n * @returns {(filePath:string) => boolean} The function to test for typescript files being included or excluded\n */\nexport default function createMatcher(config) {\n const configPath = path.dirname(slash(config.path));\n\n function matchFn(condition) {\n let pattern = slash(condition);\n if (!path.isAbsolute(pattern) && !pattern.startsWith('*')) pattern = path.join(configPath, pattern);\n\n return function match(filePath) {\n return filePath.startsWith(pattern) || minimatch(filePath, pattern);\n };\n }\n\n const includes = (config.config.include || []).map(matchFn);\n const excludes = (config.config.exclude || []).map(matchFn);\n\n return function matcher(filePath) {\n if (filePath.endsWith('.json')) return false;\n\n filePath = slash(filePath);\n for (let i = 0; i < excludes.length; ++i) {\n if (excludes[i](filePath)) return false;\n }\n for (let j = 0; j < includes.length; ++j) {\n if (includes[j](filePath)) return true;\n }\n return !includes.length;\n };\n}\n"],"names":["minimatch","path","slash","createMatcher","config","configPath","dirname","matchFn","condition","pattern","isAbsolute","startsWith","join","match","filePath","includes","include","map","excludes","exclude","matcher","endsWith","i","length","j"],"mappings":"AAAA,OAAOA,eAAe,YAAY;AAClC,OAAOC,UAAU,aAAa;AAC9B,OAAOC,WAAW,QAAQ;AAE1B;;;CAGC,GACD,eAAe,SAASC,cAAcC,MAAM;IAC1C,MAAMC,aAAaJ,KAAKK,OAAO,CAACJ,MAAME,OAAOH,IAAI;IAEjD,SAASM,QAAQC,SAAS;QACxB,IAAIC,UAAUP,MAAMM;QACpB,IAAI,CAACP,KAAKS,UAAU,CAACD,YAAY,CAACA,QAAQE,UAAU,CAAC,MAAMF,UAAUR,KAAKW,IAAI,CAACP,YAAYI;QAE3F,OAAO,SAASI,MAAMC,QAAQ;YAC5B,OAAOA,SAASH,UAAU,CAACF,YAAYT,UAAUc,UAAUL;QAC7D;IACF;IAEA,MAAMM,WAAW,AAACX,CAAAA,OAAOA,MAAM,CAACY,OAAO,IAAI,EAAE,AAAD,EAAGC,GAAG,CAACV;IACnD,MAAMW,WAAW,AAACd,CAAAA,OAAOA,MAAM,CAACe,OAAO,IAAI,EAAE,AAAD,EAAGF,GAAG,CAACV;IAEnD,OAAO,SAASa,QAAQN,QAAQ;QAC9B,IAAIA,SAASO,QAAQ,CAAC,UAAU,OAAO;QAEvCP,WAAWZ,MAAMY;QACjB,IAAK,IAAIQ,IAAI,GAAGA,IAAIJ,SAASK,MAAM,EAAE,EAAED,EAAG;YACxC,IAAIJ,QAAQ,CAACI,EAAE,CAACR,WAAW,OAAO;QACpC;QACA,IAAK,IAAIU,IAAI,GAAGA,IAAIT,SAASQ,MAAM,EAAE,EAAEC,EAAG;YACxC,IAAIT,QAAQ,CAACS,EAAE,CAACV,WAAW,OAAO;QACpC;QACA,OAAO,CAACC,SAASQ,MAAM;IACzB;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.mjs"],"sourcesContent":["export { default as createMatcher } from './createMatcher.mjs';\nexport { default as transformFile } from './transformFile.mjs';\nexport { default as transformDirectory } from './transformDirectory.mjs';\nexport { default as transformSync } from './transformSync.cjs';\n"],"names":["default","createMatcher","transformFile","transformDirectory","transformSync"],"mappings":"AAAA,SAASA,WAAWC,aAAa,QAAQ,sBAAsB;AAC/D,SAASD,WAAWE,aAAa,QAAQ,sBAAsB;AAC/D,SAASF,WAAWG,kBAAkB,QAAQ,2BAA2B;AACzE,SAASH,WAAWI,aAAa,QAAQ,sBAAsB"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default function regexDependencies(esm) {
|
|
2
|
+
const matchingDeps = '\\s*[\'"`]([^\'"`]+)[\'"`]\\s*';
|
|
3
|
+
const matchingName = '\\s*(?:[\\w${},\\s*]+)\\s*';
|
|
4
|
+
let regex = `(?:(?:var|const|let)${matchingName}=\\s*)?require\\(${matchingDeps}\\);?`;
|
|
5
|
+
if (esm) {
|
|
6
|
+
regex += `|import(?:${matchingName}from\\s*)?${matchingDeps};?`;
|
|
7
|
+
regex += `|export(?:${matchingName}from\\s*)?${matchingDeps};?`;
|
|
8
|
+
}
|
|
9
|
+
return new RegExp(regex, 'g');
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["regexDependencies.mjs"],"sourcesContent":["export default function regexDependencies(esm) {\n const matchingDeps = '\\\\s*[\\'\"`]([^\\'\"`]+)[\\'\"`]\\\\s*';\n const matchingName = '\\\\s*(?:[\\\\w${},\\\\s*]+)\\\\s*';\n\n let regex = `(?:(?:var|const|let)${matchingName}=\\\\s*)?require\\\\(${matchingDeps}\\\\);?`;\n if (esm) {\n regex += `|import(?:${matchingName}from\\\\s*)?${matchingDeps};?`;\n regex += `|export(?:${matchingName}from\\\\s*)?${matchingDeps};?`;\n }\n return new RegExp(regex, 'g');\n}\n"],"names":["regexDependencies","esm","matchingDeps","matchingName","regex","RegExp"],"mappings":"AAAA,eAAe,SAASA,kBAAkBC,GAAG;IAC3C,MAAMC,eAAe;IACrB,MAAMC,eAAe;IAErB,IAAIC,QAAQ,CAAC,oBAAoB,EAAED,aAAa,iBAAiB,EAAED,aAAa,KAAK,CAAC;IACtF,IAAID,KAAK;QACPG,SAAS,CAAC,UAAU,EAAED,aAAa,UAAU,EAAED,aAAa,EAAE,CAAC;QAC/DE,SAAS,CAAC,UAAU,EAAED,aAAa,UAAU,EAAED,aAAa,EAAE,CAAC;IACjE;IACA,OAAO,IAAIG,OAAOD,OAAO;AAC3B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
function transformDirectoryCallback(src, dest, type, options, callback) {
|
|
7
|
+
if (typeof options === 'function') {
|
|
8
|
+
callback = options;
|
|
9
|
+
options = {};
|
|
10
|
+
}
|
|
11
|
+
if (typeof src !== 'string') throw new Error('transformDirectory: unexpected source');
|
|
12
|
+
if (typeof dest !== 'string') throw new Error('transformDirectory: unexpected destination directory');
|
|
13
|
+
if (typeof type !== 'string') throw new Error('transformDirectory: unexpected type');
|
|
14
|
+
const cwd = options.cwd || process.cwd();
|
|
15
|
+
const config = options.confg ? options.confg : getTS.getTsconfig(path.resolve(cwd, 'tsconfig.json'));
|
|
16
|
+
const matcher = createMatcher(config);
|
|
17
|
+
options = {
|
|
18
|
+
...options,
|
|
19
|
+
config
|
|
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
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} src The source directory to traverse.
|
|
33
|
+
* @param {string} dest The output directory to write files to.
|
|
34
|
+
* @param {string} type The type of transform ('esm' or 'cjs').
|
|
35
|
+
* @param {{sourceMaps: boolean}} options Options to pass to swc.
|
|
36
|
+
* @param {(err?: Error) =>} [callback] Optional callback. Uses promise if callback not provided.
|
|
37
|
+
* @returns {void | Promise<any>} Optional promise if callback not provided.
|
|
38
|
+
*/ export default function transformDirectory(src, dest, type, options, callback) {
|
|
39
|
+
if (typeof options === 'function') {
|
|
40
|
+
callback = options;
|
|
41
|
+
options = null;
|
|
42
|
+
}
|
|
43
|
+
options = options || {};
|
|
44
|
+
if (typeof callback === 'function') return transformDirectoryCallback(src, dest, type, options, callback);
|
|
45
|
+
return new Promise((resolve, reject)=>{
|
|
46
|
+
transformDirectoryCallback(src, dest, type, options, function compileCallback(err, result) {
|
|
47
|
+
err ? reject(err) : resolve(result);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformDirectory.mjs"],"sourcesContent":["import path from 'path';\nimport Iterator from 'fs-iterator';\nimport getTS from 'get-tsconfig-compat';\n\nimport createMatcher from './createMatcher.mjs';\nimport transformFile from './transformFile.mjs';\n\nfunction transformDirectoryCallback(src, dest, type, options, callback) {\n if (typeof options === 'function') {\n callback = options;\n options = {};\n }\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 const cwd = options.cwd || process.cwd();\n const config = options.confg ? options.confg : getTS.getTsconfig(path.resolve(cwd, 'tsconfig.json'));\n const matcher = createMatcher(config);\n\n options = { ...options, config };\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\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\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","Iterator","getTS","createMatcher","transformFile","transformDirectoryCallback","src","dest","type","options","callback","Error","cwd","process","config","confg","getTsconfig","resolve","matcher","iterator","forEach","entry","cb","stats","isFile","fullPath","dirname","join","callbacks","concurrency","transformDirectory","Promise","reject","compileCallback","err","result"],"mappings":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,UAAU;IACZ;IACAA,UAAUA,WAAW,CAAC;IAEtB,IAAI,OAAOC,aAAa,YAAY,OAAOL,2BAA2BC,KAAKC,MAAMC,MAAMC,SAASC;IAChG,OAAO,IAAIqB,QAAQ,CAACd,SAASe;QAC3B3B,2BAA2BC,KAAKC,MAAMC,MAAMC,SAAS,SAASwB,gBAAgBC,GAAG,EAAEC,MAAM;YACvFD,MAAMF,OAAOE,OAAOjB,QAAQkB;QAC9B;IACF;AACF"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import once from 'call-once-fn';
|
|
4
|
+
import getTS from 'get-tsconfig-compat';
|
|
5
|
+
import mkdirp from 'mkdirp';
|
|
6
|
+
import Queue from 'queue-cb';
|
|
7
|
+
import regexDependencies from './lib/regexDependencies.mjs';
|
|
8
|
+
import transformSync from './transformSync.cjs';
|
|
9
|
+
const regexESM = regexDependencies(true);
|
|
10
|
+
const regexCJS = regexDependencies();
|
|
11
|
+
const importReplaceMJS = [
|
|
12
|
+
'.js',
|
|
13
|
+
'.ts',
|
|
14
|
+
'.tsx',
|
|
15
|
+
'.mts',
|
|
16
|
+
'.mjs'
|
|
17
|
+
];
|
|
18
|
+
const importReplaceCJS = [
|
|
19
|
+
'.cts'
|
|
20
|
+
];
|
|
21
|
+
const requireReplaceJS = [
|
|
22
|
+
'.mjs',
|
|
23
|
+
'.cjs',
|
|
24
|
+
'.ts',
|
|
25
|
+
'.tsx',
|
|
26
|
+
'.mts',
|
|
27
|
+
'.cts'
|
|
28
|
+
];
|
|
29
|
+
function makeReplacements(code, regex, extensions, extension) {
|
|
30
|
+
let matches = [];
|
|
31
|
+
let match = regex.exec(code);
|
|
32
|
+
while(match){
|
|
33
|
+
const dependency = match[1] || match[2] || match[3] || match[4];
|
|
34
|
+
const ext = extensions.find((x)=>dependency.slice(-x.length) === x);
|
|
35
|
+
if (ext) matches.push({
|
|
36
|
+
ext,
|
|
37
|
+
match,
|
|
38
|
+
dependency
|
|
39
|
+
});
|
|
40
|
+
match = regex.exec(code);
|
|
41
|
+
}
|
|
42
|
+
matches = matches.reverse();
|
|
43
|
+
for(const index in matches){
|
|
44
|
+
const match = matches[index];
|
|
45
|
+
const start = match.match.index + match.match[0].indexOf(match.dependency) + match.dependency.indexOf(match.ext);
|
|
46
|
+
code = code.substring(0, start) + extension + code.substring(start + match.ext.length);
|
|
47
|
+
}
|
|
48
|
+
return code;
|
|
49
|
+
}
|
|
50
|
+
// https://github.com/vercel/next.js/blob/20b63e13ab2631d6043277895d373aa31a1b327c/packages/next/taskfile-swc.js#L118-L125
|
|
51
|
+
const interopClientDefaultExport = "/* CJS INTEROP */ if (exports.__esModule && exports.default) { if(typeof exports.default === 'object') Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { if (key !== 'default') exports.default[key] = exports[key]; }; module.exports = exports.default; }";
|
|
52
|
+
function transformFileCallback(src, dest, type, options, callback) {
|
|
53
|
+
if (typeof options === 'function') {
|
|
54
|
+
callback = options;
|
|
55
|
+
options = {};
|
|
56
|
+
}
|
|
57
|
+
if (typeof src !== 'string') throw new Error('transformFile: unexpected source');
|
|
58
|
+
if (typeof dest !== 'string') throw new Error('transformFile: unexpected destination directory');
|
|
59
|
+
if (typeof type !== 'string') throw new Error('transformFile: unexpected type');
|
|
60
|
+
fs.readFile(src, 'utf8', (err, contents)=>{
|
|
61
|
+
if (err) return callback(err);
|
|
62
|
+
callback = once(callback);
|
|
63
|
+
try {
|
|
64
|
+
const cwd = options.cwd || process.cwd();
|
|
65
|
+
let config = options.confg ? options.confg : getTS.getTsconfig(path.resolve(cwd, 'tsconfig.json'));
|
|
66
|
+
// overrides for cjs
|
|
67
|
+
if (type === 'cjs') {
|
|
68
|
+
config = {
|
|
69
|
+
...config
|
|
70
|
+
};
|
|
71
|
+
config.config = {
|
|
72
|
+
...config.config || {}
|
|
73
|
+
};
|
|
74
|
+
config.config.compilerOptions = {
|
|
75
|
+
...config.config.compilerOptions || {}
|
|
76
|
+
};
|
|
77
|
+
config.config.compilerOptions.module = 'CommonJS';
|
|
78
|
+
config.config.compilerOptions.target = 'ES5';
|
|
79
|
+
}
|
|
80
|
+
const basename = path.basename(src);
|
|
81
|
+
const output = transformSync(contents, basename, config);
|
|
82
|
+
// infer extension and patch .mjs imports
|
|
83
|
+
let ext = path.extname(basename);
|
|
84
|
+
if (type === 'esm') {
|
|
85
|
+
ext = importReplaceMJS.indexOf(ext) >= 0 ? '.mjs' : ext;
|
|
86
|
+
output.code = makeReplacements(output.code, regexESM, importReplaceMJS, '.mjs');
|
|
87
|
+
ext = importReplaceCJS.indexOf(ext) >= 0 ? '.cjs' : ext;
|
|
88
|
+
output.code = makeReplacements(output.code, regexESM, importReplaceCJS, '.cjs');
|
|
89
|
+
} else {
|
|
90
|
+
ext = requireReplaceJS.indexOf(ext) >= 0 ? '.js' : ext;
|
|
91
|
+
output.code = makeReplacements(output.code, regexCJS, requireReplaceJS, '.js');
|
|
92
|
+
output.code += interopClientDefaultExport;
|
|
93
|
+
}
|
|
94
|
+
const destFilePath = path.join(dest, basename.replace(/\.[^/.]+$/, '') + ext);
|
|
95
|
+
mkdirp(path.dirname(destFilePath), ()=>{
|
|
96
|
+
const queue = new Queue();
|
|
97
|
+
queue.defer(fs.writeFile.bind(null, destFilePath, output.code, 'utf8'));
|
|
98
|
+
!options.sourceMaps || queue.defer(fs.writeFile.bind(null, `${destFilePath}.map`, output.map, 'utf8'));
|
|
99
|
+
queue.await(()=>err ? callback(err) : callback(null, destFilePath));
|
|
100
|
+
});
|
|
101
|
+
} catch (err) {
|
|
102
|
+
callback(err);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @param {string} src The source directory to traverse.
|
|
108
|
+
* @param {string} dest The output directory to write the file to.
|
|
109
|
+
* @param {string} type The type of transform ('esm' or 'cjs').
|
|
110
|
+
* @param {{sourceMaps: boolean}} options Options to pass to swc.
|
|
111
|
+
* @param {(err?: Error, destFilePath: string) =>} [callback] Optional callback returing the path to the transformed file. Uses promise if callback not provided.
|
|
112
|
+
* @returns {void | Promise<string>} Optional promise returing the path to the transformed file if callback not provided.
|
|
113
|
+
*/ export default function transformFile(src, dest, type, options, callback) {
|
|
114
|
+
if (typeof options === 'function') {
|
|
115
|
+
callback = options;
|
|
116
|
+
options = null;
|
|
117
|
+
}
|
|
118
|
+
options = options || {};
|
|
119
|
+
if (typeof callback === 'function') return transformFileCallback(src, dest, type, options, callback);
|
|
120
|
+
return new Promise((resolve, reject)=>{
|
|
121
|
+
transformFileCallback(src, dest, type, options, function compileCallback(err, result) {
|
|
122
|
+
err ? reject(err) : resolve(result);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformFile.mjs"],"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';\nimport Queue from 'queue-cb';\n\nimport regexDependencies from './lib/regexDependencies.mjs';\nimport transformSync from './transformSync.cjs';\n\nconst regexESM = regexDependencies(true);\nconst regexCJS = regexDependencies();\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 =\n \"/* CJS INTEROP */ if (exports.__esModule && exports.default) { if(typeof exports.default === 'object') Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { if (key !== 'default') exports.default[key] = exports[key]; }; module.exports = exports.default; }\";\n\nfunction transformFileCallback(src, dest, type, options, callback) {\n if (typeof options === 'function') {\n callback = options;\n options = {};\n }\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 fs.readFile(src, 'utf8', (err, contents) => {\n if (err) return callback(err);\n callback = once(callback);\n\n try {\n const cwd = options.cwd || process.cwd();\n let config = options.confg ? options.confg : getTS.getTsconfig(path.resolve(cwd, 'tsconfig.json'));\n\n // overrides for cjs\n if (type === 'cjs') {\n config = { ...config };\n config.config = { ...(config.config || {}) };\n config.config.compilerOptions = { ...(config.config.compilerOptions || {}) };\n config.config.compilerOptions.module = 'CommonJS';\n config.config.compilerOptions.target = 'ES5';\n }\n\n const basename = path.basename(src);\n const output = transformSync(contents, basename, config);\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 } catch (err) {\n callback(err);\n }\n });\n}\n\n/**\n * @param {string} src The source directory to traverse.\n * @param {string} dest The output directory to write the file 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, destFilePath: string) =>} [callback] Optional callback returing the path to the transformed file. Uses promise if callback not provided.\n * @returns {void | Promise<string>} Optional promise returing the path to the transformed file 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\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":["fs","path","once","getTS","mkdirp","Queue","regexDependencies","transformSync","regexESM","regexCJS","importReplaceMJS","importReplaceCJS","requireReplaceJS","makeReplacements","code","regex","extensions","extension","matches","match","exec","dependency","ext","find","x","slice","length","push","reverse","index","start","indexOf","substring","interopClientDefaultExport","transformFileCallback","src","dest","type","options","callback","Error","readFile","err","contents","cwd","process","config","confg","getTsconfig","resolve","compilerOptions","module","target","basename","output","extname","destFilePath","join","replace","dirname","queue","defer","writeFile","bind","sourceMaps","map","await","transformFile","Promise","reject","compileCallback","result"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,OAAOC,UAAU,OAAO;AACxB,OAAOC,UAAU,eAAe;AAChC,OAAOC,WAAW,sBAAsB;AACxC,OAAOC,YAAY,SAAS;AAC5B,OAAOC,WAAW,WAAW;AAE7B,OAAOC,uBAAuB,8BAA8B;AAC5D,OAAOC,mBAAmB,sBAAsB;AAEhD,MAAMC,WAAWF,kBAAkB;AACnC,MAAMG,WAAWH;AAEjB,MAA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@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const major = +process.versions.node.split('.')[0];
|
|
3
|
+
const version = major >= 14 ? 'local' : 'lts';
|
|
4
|
+
const worker = path.resolve(__dirname, 'workers', `transformSync${path.extname(__filename)}`);
|
|
5
|
+
let call = null; // break dependencies
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} contents The file contents.
|
|
8
|
+
* @param {string} fileName The filename.
|
|
9
|
+
* @returns {{ code: string, map?: string }} Returns object with the transformed code and source map if option sourceMaps was provided.
|
|
10
|
+
*/ module.exports = function transformSync(contents, fileName, config) {
|
|
11
|
+
if (!call) call = require('node-version-call'); // break dependencies
|
|
12
|
+
return call(version, worker, contents, fileName, config);
|
|
13
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformSync.cjs"],"sourcesContent":["const path = require('path');\n\nconst major = +process.versions.node.split('.')[0];\nconst version = major >= 14 ? 'local' : 'lts';\nconst worker = path.resolve(__dirname, 'workers', `transformSync${path.extname(__filename)}`);\n\nlet call = null; // break dependencies\n\n/**\n * @param {string} contents The file contents.\n * @param {string} fileName The filename.\n * @returns {{ code: string, map?: string }} Returns object with the transformed code and source map if option sourceMaps was provided.\n */\nmodule.exports = function transformSync(contents, fileName, config) {\n if (!call) call = require('node-version-call'); // break dependencies\n\n return call(version, worker, contents, fileName, config);\n};\n"],"names":["path","require","major","process","versions","node","split","version","worker","resolve","__dirname","extname","__filename","call","module","exports","transformSync","contents","fileName","config"],"mappings":"AAAA,MAAMA,OAAOC,QAAQ;AAErB,MAAMC,QAAQ,CAACC,QAAQC,QAAQ,CAACC,IAAI,CAACC,KAAK,CAAC,IAAI,CAAC,EAAE;AAClD,MAAMC,UAAUL,SAAS,KAAK,UAAU;AACxC,MAAMM,SAASR,KAAKS,OAAO,CAACC,WAAW,WAAW,CAAC,aAAa,EAAEV,KAAKW,OAAO,CAACC,aAAa;AAE5F,IAAIC,OAAO,MAAM,qBAAqB;AAEtC;;;;CAIC,GACDC,OAAOC,OAAO,GAAG,SAASC,cAAcC,QAAQ,EAAEC,QAAQ,EAAEC,MAAM;IAChE,IAAI,CAACN,MAAMA,OAAOZ,QAAQ,sBAAsB,qBAAqB;IAErE,OAAOY,KAAKN,SAASC,QAAQS,UAAUC,UAAUC;AACnD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const swc = require('@swc/core');
|
|
3
|
+
const ts = require('typescript');
|
|
4
|
+
const swcTranspiler = require('ts-node/transpilers/swc');
|
|
5
|
+
module.exports = function transformSync(contents, fileName, config) {
|
|
6
|
+
const parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, path.dirname(config.path));
|
|
7
|
+
const transpile = swcTranspiler.create({
|
|
8
|
+
swc: swc,
|
|
9
|
+
service: {
|
|
10
|
+
config: {
|
|
11
|
+
options: parsed.options
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const res = transpile.transpile(contents, {
|
|
16
|
+
fileName: fileName
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
code: res.outputText,
|
|
20
|
+
map: res.sourceMapText
|
|
21
|
+
};
|
|
22
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["transformSync.cjs"],"sourcesContent":["const path = require('path');\nconst swc = require('@swc/core');\nconst ts = require('typescript');\nconst swcTranspiler = require('ts-node/transpilers/swc');\n\nmodule.exports = function transformSync(contents, fileName, config) {\n const parsed = ts.parseJsonConfigFileContent(config.config, ts.sys, path.dirname(config.path));\n const transpile = swcTranspiler.create({ swc: swc, service: { config: { options: parsed.options } } });\n const res = transpile.transpile(contents, { fileName: fileName });\n return { code: res.outputText, map: res.sourceMapText };\n};\n"],"names":["path","require","swc","ts","swcTranspiler","module","exports","transformSync","contents","fileName","config","parsed","parseJsonConfigFileContent","sys","dirname","transpile","create","service","options","res","code","outputText","map","sourceMapText"],"mappings":"AAAA,MAAMA,OAAOC,QAAQ;AACrB,MAAMC,MAAMD,QAAQ;AACpB,MAAME,KAAKF,QAAQ;AACnB,MAAMG,gBAAgBH,QAAQ;AAE9BI,OAAOC,OAAO,GAAG,SAASC,cAAcC,QAAQ,EAAEC,QAAQ,EAAEC,MAAM;IAChE,MAAMC,SAASR,GAAGS,0BAA0B,CAACF,OAAOA,MAAM,EAAEP,GAAGU,GAAG,EAAEb,KAAKc,OAAO,CAACJ,OAAOV,IAAI;IAC5F,MAAMe,YAAYX,cAAcY,MAAM,CAAC;QAAEd,KAAKA;QAAKe,SAAS;YAAEP,QAAQ;gBAAEQ,SAASP,OAAOO,OAAO;YAAC;QAAE;IAAE;IACpG,MAAMC,MAAMJ,UAAUA,SAAS,CAACP,UAAU;QAAEC,UAAUA;IAAS;IAC/D,OAAO;QAAEW,MAAMD,IAAIE,UAAU;QAAEC,KAAKH,IAAII,aAAa;IAAC;AACxD"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {{path: string, config: Object}} config The path to the loaded TS config and typescript config.
|
|
3
|
+
* @returns {(filePath:string) => boolean} The function to test for typescript files being included or excluded
|
|
4
|
+
*/
|
|
5
|
+
export default function createMatcher(config: {
|
|
6
|
+
path: string;
|
|
7
|
+
config: any;
|
|
8
|
+
}): (filePath: string) => boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function regexDependencies(esm: any): RegExp;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {string} src The source directory to traverse.
|
|
3
|
+
* @param {string} dest The output directory to write files to.
|
|
4
|
+
* @param {string} type The type of transform ('esm' or 'cjs').
|
|
5
|
+
* @param {{sourceMaps: boolean}} options Options to pass to swc.
|
|
6
|
+
* @param {(err?: Error) =>} [callback] Optional callback. Uses promise if callback not provided.
|
|
7
|
+
* @returns {void | Promise<any>} Optional promise if callback not provided.
|
|
8
|
+
*/
|
|
9
|
+
export default function transformDirectory(src: string, dest: string, type: string, options: {
|
|
10
|
+
sourceMaps: boolean;
|
|
11
|
+
}, callback?: (err?: Error) => any): void | Promise<any>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {string} src The source directory to traverse.
|
|
3
|
+
* @param {string} dest The output directory to write the file to.
|
|
4
|
+
* @param {string} type The type of transform ('esm' or 'cjs').
|
|
5
|
+
* @param {{sourceMaps: boolean}} options Options to pass to swc.
|
|
6
|
+
* @param {(err?: Error, destFilePath: string) =>} [callback] Optional callback returing the path to the transformed file. Uses promise if callback not provided.
|
|
7
|
+
* @returns {void | Promise<string>} Optional promise returing the path to the transformed file if callback not provided.
|
|
8
|
+
*/
|
|
9
|
+
export default function transformFile(src: string, dest: string, type: string, options: {
|
|
10
|
+
sourceMaps: boolean;
|
|
11
|
+
}, callback?: (err?: Error, destFilePath: string) => any): void | Promise<string>;
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-swc-transform",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Typescript transformers for swc. Supports Node >= 0.8",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"matcher",
|
|
7
|
+
"swc",
|
|
8
|
+
"typescript",
|
|
9
|
+
"transform",
|
|
10
|
+
"transformSync",
|
|
11
|
+
"transformDirectory",
|
|
12
|
+
"directory",
|
|
13
|
+
"transformFile",
|
|
14
|
+
"file"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/kmalakoff/ts-swc-transform",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git@github.com:kmalakoff/ts-swc-transform.git"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "Kevin Malakoff <kmalakoff@gmail.com> (https://github.com/kmalakoff)",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": "./dist/esm/index.mjs",
|
|
27
|
+
"require": "./dist/cjs/index.js",
|
|
28
|
+
"types": "./dist/types/index.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./*": "./*"
|
|
31
|
+
},
|
|
32
|
+
"main": "dist/cjs/index.js",
|
|
33
|
+
"module": "dist/dist/esm/index.mjs",
|
|
34
|
+
"types": "dist/types/index.d.ts",
|
|
35
|
+
"bin": {
|
|
36
|
+
"tst": "bin/cli.js"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsds build",
|
|
43
|
+
"depcheck": "depcheck",
|
|
44
|
+
"deploy": "tsds deploy",
|
|
45
|
+
"format": "biome check --write --unsafe src/ test/",
|
|
46
|
+
"test": "tsds test:node --timeout=20000",
|
|
47
|
+
"test:engines": "nvu engines npm test",
|
|
48
|
+
"version": "tsds version"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@swc/core": "*",
|
|
52
|
+
"call-once-fn": "^1.0.1",
|
|
53
|
+
"exit": "^0.1.2",
|
|
54
|
+
"get-tsconfig-compat": "^0.1.0",
|
|
55
|
+
"minimatch": "^3.1.2",
|
|
56
|
+
"mkdirp": "^0.5.6",
|
|
57
|
+
"node-version-call": "^1.1.0",
|
|
58
|
+
"path-posix": "^1.0.0",
|
|
59
|
+
"slash": "^1.0.0",
|
|
60
|
+
"ts-node": "*",
|
|
61
|
+
"typescript": "*"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@biomejs/biome": "^1.9.4",
|
|
65
|
+
"@swc/core": "^1.9.3",
|
|
66
|
+
"@types/mocha": "^10.0.10",
|
|
67
|
+
"@types/node": "^22.10.1",
|
|
68
|
+
"core-js": "^3.39.0",
|
|
69
|
+
"cr": "^0.1.0",
|
|
70
|
+
"cross-spawn-cb": "^1.1.1",
|
|
71
|
+
"fs-iterator": "^5.1.1",
|
|
72
|
+
"queue-cb": "^1.2.1",
|
|
73
|
+
"react": "^18.3.1",
|
|
74
|
+
"rimraf2": "^2.8.2",
|
|
75
|
+
"ts-dev-stack": "^0.17.1",
|
|
76
|
+
"ts-node": "^10.8.2",
|
|
77
|
+
"typescript": "^5.7.2"
|
|
78
|
+
},
|
|
79
|
+
"engines": {
|
|
80
|
+
"node": ">=0.8"
|
|
81
|
+
},
|
|
82
|
+
"tsds": {
|
|
83
|
+
"source": "src/index.mjs",
|
|
84
|
+
"targets": [
|
|
85
|
+
"cjs",
|
|
86
|
+
"esm"
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
}
|