ts-swc-transform 2.11.13 → 2.12.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/README.md
CHANGED
|
@@ -1,19 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
# ts-swc-transform
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript transformers for SWC. Supports Node.js 0.8 and newer.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ts-swc-transform
|
|
6
9
|
```
|
|
10
|
+
|
|
11
|
+
## Promise
|
|
12
|
+
|
|
13
|
+
```js
|
|
7
14
|
import { transformDirectory } from 'ts-swc-transform';
|
|
8
15
|
|
|
9
|
-
await transformDirectory('src', 'dist', '
|
|
16
|
+
const files = await transformDirectory('src', 'dist', 'esm', { sourceMaps: true });
|
|
17
|
+
console.log(`Wrote ${files.length} files`);
|
|
10
18
|
```
|
|
11
19
|
|
|
12
|
-
Callback
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
## Callback
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
var transformDirectory = require('ts-swc-transform').transformDirectory;
|
|
15
24
|
|
|
16
|
-
transformDirectory('src', 'dist', '
|
|
17
|
-
if (err)
|
|
25
|
+
transformDirectory('src', 'dist', 'cjs', { sourceMaps: true }, function (err, files) {
|
|
26
|
+
if (err) throw err;
|
|
27
|
+
console.log('Wrote ' + files.length + ' files');
|
|
18
28
|
});
|
|
19
29
|
```
|
|
30
|
+
|
|
31
|
+
The target must be `cjs` or `esm`. The package also exports lower-level
|
|
32
|
+
single-file, declaration, path-resolution, and matching helpers; see the
|
|
33
|
+
[API documentation](https://kmalakoff.github.io/ts-swc-transform/) for their
|
|
34
|
+
signatures.
|
|
@@ -184,11 +184,15 @@ function transformTypesWorker(src, dest, options, callback) {
|
|
|
184
184
|
}, {
|
|
185
185
|
concurrency: concurrency
|
|
186
186
|
}, function(err1) {
|
|
187
|
-
var _tsconfig_config_compilerOptions;
|
|
187
|
+
var _tsconfig_config_compilerOptions, _tsconfig_config_compilerOptions1;
|
|
188
188
|
if (err1) return callback(err1);
|
|
189
189
|
if (rootFiles.length === 0) return callback(null, []);
|
|
190
|
+
// An explicit rootDir in the tsconfig wins: a package whose sources import siblings above
|
|
191
|
+
// it (a skills tree) names the common ancestor; otherwise the emit is rooted at src.
|
|
192
|
+
var configured = (_tsconfig_config_compilerOptions = tsconfig.config.compilerOptions) === null || _tsconfig_config_compilerOptions === void 0 ? void 0 : _tsconfig_config_compilerOptions.rootDir;
|
|
193
|
+
var rootDir = configured === undefined ? src : _path.default.resolve(_path.default.dirname(tsconfig.path), configured);
|
|
190
194
|
var compilerOptions = sanitizeForCompiler(_object_spread_props(_object_spread({}, tsconfig.config.compilerOptions), {
|
|
191
|
-
rootDir:
|
|
195
|
+
rootDir: rootDir,
|
|
192
196
|
outDir: dest,
|
|
193
197
|
noEmit: false,
|
|
194
198
|
allowJs: true,
|
|
@@ -197,7 +201,7 @@ function transformTypesWorker(src, dest, options, callback) {
|
|
|
197
201
|
// Suppress TS6 deprecation warnings (TS5101/TS5107) for consumers still on legacy Node-targeted options.
|
|
198
202
|
ignoreDeprecations: '6.0'
|
|
199
203
|
}));
|
|
200
|
-
var rewrite = ((
|
|
204
|
+
var rewrite = ((_tsconfig_config_compilerOptions1 = tsconfig.config.compilerOptions) === null || _tsconfig_config_compilerOptions1 === void 0 ? void 0 : _tsconfig_config_compilerOptions1.rewriteRelativeImportExtensions) === true;
|
|
201
205
|
// Avoid collisions across concurrent runs.
|
|
202
206
|
var tempDir = _path.default.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));
|
|
203
207
|
var tempConfigPath = _path.default.join(tempDir, 'tsconfig.json');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformTypes.ts"],"sourcesContent":["import spawn, { type SpawnCallback, type SpawnResult } from 'cross-spawn-cb';\nimport fs from 'fs';\nimport Iterator, { type Entry } from 'fs-iterator';\nimport { safeRm } from 'fs-remove-compat';\nimport os from 'os';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport resolveBin from 'resolve-bin-sync';\n\nconst concurrency = Math.min(64, Math.max(8, (os.cpus()?.length ?? 4) * 8));\n\nconst tscPath = resolveBin('typescript', 'tsc');\n\nfunction compilerMajor(): number {\n try {\n const pkg = JSON.parse(fs.readFileSync(path.join(path.dirname(tscPath), '..', 'package.json'), 'utf8'));\n return +String(pkg.version).split('.')[0] || 0;\n } catch {\n return 0;\n }\n}\n\n// TypeScript 7 removed options that only ever affected JS emit, which swc handles.\n// Older compilers still accept them, so only rewrite when the installed tsc would reject them.\nfunction sanitizeForCompiler(options: Record<string, unknown>): Record<string, unknown> {\n if (compilerMajor() < 7) return options;\n const sanitized = { ...options };\n delete sanitized.downlevelIteration;\n if (sanitized.moduleResolution === 'node' || sanitized.moduleResolution === 'node10') sanitized.moduleResolution = 'bundler';\n if (typeof sanitized.target === 'string' && /^es[35]$/i.test(sanitized.target)) sanitized.target = 'es2015';\n return sanitized;\n}\n\nimport createMatcher from '../createMatcher.ts';\nimport { rewriteExtensions } from '../lib/rewriteExtensions.ts';\n\nimport type { InternalConfigOptions, TransformTypesCallback } from '../types.ts';\n\n/* ---------------- root file filtering ---------------- */\n\nfunction isAllowedRootFile(basename: string): boolean {\n return (\n basename.endsWith('.d.ts') ||\n basename.endsWith('.d.mts') ||\n basename.endsWith('.d.cts') ||\n basename.endsWith('.ts') ||\n basename.endsWith('.tsx') ||\n basename.endsWith('.mts') ||\n basename.endsWith('.cts') ||\n basename.endsWith('.js') ||\n basename.endsWith('.jsx') ||\n basename.endsWith('.mjs') ||\n basename.endsWith('.cjs')\n );\n}\n\n/* ---------------- compiler execution ---------------- */\n\nfunction runCompiler(cmdPath: string, args: string[], cb: SpawnCallback): void {\n spawn(process.execPath, [cmdPath, ...args], { encoding: 'utf8' }, cb);\n}\n\n/* ---------------- emitted file parsing ---------------- */\n\nfunction parseEmittedFiles(res: SpawnResult, dest: string): string[] {\n const out: string[] = [];\n const seen = new Set<string>();\n const lines = `${res.stdout}\\n${res.stderr}`.split(/\\r?\\n/);\n\n for (let line of lines) {\n line = line.trim();\n if (!line) continue;\n\n if (/^TSFILE:\\s+/i.test(line)) line = line.replace(/^TSFILE:\\s+/i, '').trim();\n\n if (!path.isAbsolute(line)) continue;\n if (!(line.endsWith('.d.ts') || line.endsWith('.d.mts') || line.endsWith('.d.cts'))) continue;\n\n // Optional safety: only accept outputs under dest\n const rel = path.relative(dest, line);\n if (rel.startsWith('..') || path.isAbsolute(rel)) continue;\n\n if (!seen.has(line)) {\n seen.add(line);\n out.push(line);\n }\n }\n\n return out;\n}\n\nexport default function transformTypesWorker(src: string, dest: string, options: InternalConfigOptions, callback: TransformTypesCallback) {\n const tsconfig = options.tsconfig;\n const matcher = createMatcher(tsconfig);\n\n const rootFiles: string[] = [];\n const iterator = new Iterator(src);\n\n iterator.forEach(\n (entry: Entry): void => {\n const stats = entry.stats as fs.Stats | undefined;\n if (!stats || !stats.isFile()) return;\n const basename = entry.basename as string;\n if (basename[0] === '.') return;\n if (!isAllowedRootFile(basename)) return;\n if (!matcher(entry.fullPath)) return;\n\n rootFiles.push(entry.fullPath);\n },\n { concurrency },\n (err) => {\n if (err) return callback(err);\n if (rootFiles.length === 0) return callback(null, []);\n\n const compilerOptions = sanitizeForCompiler({\n ...tsconfig.config.compilerOptions,\n rootDir: src,\n outDir: dest,\n noEmit: false,\n allowJs: true,\n declaration: true,\n emitDeclarationOnly: true,\n // Suppress TS6 deprecation warnings (TS5101/TS5107) for consumers still on legacy Node-targeted options.\n ignoreDeprecations: '6.0',\n });\n\n const rewrite = (tsconfig.config.compilerOptions as { rewriteRelativeImportExtensions?: boolean } | undefined)?.rewriteRelativeImportExtensions === true;\n\n // Avoid collisions across concurrent runs.\n const tempDir = path.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));\n const tempConfigPath = path.join(tempDir, 'tsconfig.json');\n\n const tempConfig = {\n compilerOptions,\n files: rootFiles,\n include: [],\n exclude: [],\n ...(tsconfig.config.references && { references: tsconfig.config.references }),\n };\n\n fs.mkdir(tempDir, { recursive: true }, (mkdirErr) => {\n if (mkdirErr) return callback(mkdirErr);\n\n fs.writeFile(tempConfigPath, JSON.stringify(tempConfig, null, 2), 'utf8', (writeErr) => {\n if (writeErr) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(writeErr));\n return;\n }\n\n const args = ['--project', tempConfigPath, '--listEmittedFiles', '--pretty', 'false'];\n\n runCompiler(tscPath, args, (runErr, res) => {\n if (runErr || !res || res.status !== 0) {\n // cross-spawn-cb sets runErr (with stdout/stderr/status copied onto it) on non-zero exit\n // and does NOT pass res — read from runErr first, then fall back to res.\n const source = (runErr as unknown as SpawnResult | undefined) ?? res;\n const status = source?.status;\n const stdout = String(source?.stdout ?? '');\n const stderr = String(source?.stderr ?? '');\n const detail = `TypeScript compiler failed (status=${status}).\\nstdout:\\n${stdout.slice(0, 100_000)}\\nstderr:\\n${stderr.slice(0, 20_000)}`;\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error(detail)));\n return;\n }\n\n const emittedFiles = parseEmittedFiles(res, dest);\n if (emittedFiles.length === 0) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error('TypeScript compiler produced no emitted declaration files')));\n return;\n }\n\n const postQueue = new Queue();\n\n if (rewrite) {\n for (const file of emittedFiles) {\n postQueue.defer((cb) => {\n fs.readFile(file, 'utf8', (readErr, content) => {\n if (readErr) return cb();\n const updated = rewriteExtensions(content);\n if (updated === content) cb();\n else fs.writeFile(file, updated, 'utf8', (err) => cb(err));\n });\n });\n }\n }\n\n postQueue.await(() => {\n safeRm(tempDir, { recursive: true, force: true }, (rmErr) => callback(rmErr || null, emittedFiles));\n });\n });\n });\n });\n }\n );\n}\n"],"names":["transformTypesWorker","os","concurrency","Math","min","max","cpus","length","tscPath","resolveBin","compilerMajor","pkg","JSON","parse","fs","readFileSync","path","join","dirname","String","version","split","sanitizeForCompiler","options","sanitized","downlevelIteration","moduleResolution","target","test","isAllowedRootFile","basename","endsWith","runCompiler","cmdPath","args","cb","spawn","process","execPath","encoding","parseEmittedFiles","res","dest","out","seen","Set","lines","stdout","stderr","line","trim","replace","isAbsolute","rel","relative","startsWith","has","add","push","src","callback","tsconfig","matcher","createMatcher","rootFiles","iterator","Iterator","forEach","entry","stats","isFile","fullPath","err","compilerOptions","config","rootDir","outDir","noEmit","allowJs","declaration","emitDeclarationOnly","ignoreDeprecations","rewrite","rewriteRelativeImportExtensions","tempDir","pid","Date","now","tempConfigPath","tempConfig","files","include","exclude","references","mkdir","recursive","mkdirErr","writeFile","stringify","writeErr","safeRm","force","runErr","status","source","detail","slice","Error","emittedFiles","postQueue","Queue","file","defer","readFile","readErr","content","updated","rewriteExtensions","await","rmErr"],"mappings":";;;;+BA2FA;;;eAAwBA;;;mEA3FoC;yDAC7C;iEACsB;8BACd;yDACR;2DACE;8DACC;qEACK;sEA0BG;mCACQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAzBYC;AAA9C,IAAMC,cAAcC,KAAKC,GAAG,CAAC,IAAID,KAAKE,GAAG,CAAC,GAAG,UAACJ,WAAAA,WAAE,CAACK,IAAI,gBAAPL,+BAAAA,SAAWM,MAAM,uCAAI,KAAK;AAExE,IAAMC,UAAUC,IAAAA,uBAAU,EAAC,cAAc;AAEzC,SAASC;IACP,IAAI;QACF,IAAMC,MAAMC,KAAKC,KAAK,CAACC,WAAE,CAACC,YAAY,CAACC,aAAI,CAACC,IAAI,CAACD,aAAI,CAACE,OAAO,CAACV,UAAU,MAAM,iBAAiB;QAC/F,OAAO,CAACW,OAAOR,IAAIS,OAAO,EAAEC,KAAK,CAAC,IAAI,CAAC,EAAE,IA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;gCACF;4BACF;4BATA,QAAK,YAAcoC,iCAAd,SAAA,6BAAA,QAAA,yBAAA;;4BAAA;4BAAA;;;qCAAA,6BAAA;oCAAA;;;oCAAA;0CAAA;;;;oBAUP;oBAEAC,UAAUS,KAAK,CAAC;wBACdlB,IAAAA,sBAAM,EAAChB,SAAS;4BAAEW,WAAW;4BAAMM,OAAO;wBAAK,GAAG,SAACkB;mCAAU3D,SAAS2D,SAAS,MAAMX;;oBACvF;gBACF;YACF;QACF;IACF;AAEJ"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformTypes.ts"],"sourcesContent":["import spawn, { type SpawnCallback, type SpawnResult } from 'cross-spawn-cb';\nimport fs from 'fs';\nimport Iterator, { type Entry } from 'fs-iterator';\nimport { safeRm } from 'fs-remove-compat';\nimport os from 'os';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport resolveBin from 'resolve-bin-sync';\n\nconst concurrency = Math.min(64, Math.max(8, (os.cpus()?.length ?? 4) * 8));\n\nconst tscPath = resolveBin('typescript', 'tsc');\n\nfunction compilerMajor(): number {\n try {\n const pkg = JSON.parse(fs.readFileSync(path.join(path.dirname(tscPath), '..', 'package.json'), 'utf8'));\n return +String(pkg.version).split('.')[0] || 0;\n } catch {\n return 0;\n }\n}\n\n// TypeScript 7 removed options that only ever affected JS emit, which swc handles.\n// Older compilers still accept them, so only rewrite when the installed tsc would reject them.\nfunction sanitizeForCompiler(options: Record<string, unknown>): Record<string, unknown> {\n if (compilerMajor() < 7) return options;\n const sanitized = { ...options };\n delete sanitized.downlevelIteration;\n if (sanitized.moduleResolution === 'node' || sanitized.moduleResolution === 'node10') sanitized.moduleResolution = 'bundler';\n if (typeof sanitized.target === 'string' && /^es[35]$/i.test(sanitized.target)) sanitized.target = 'es2015';\n return sanitized;\n}\n\nimport createMatcher from '../createMatcher.ts';\nimport { rewriteExtensions } from '../lib/rewriteExtensions.ts';\n\nimport type { InternalConfigOptions, TransformTypesCallback } from '../types.ts';\n\n/* ---------------- root file filtering ---------------- */\n\nfunction isAllowedRootFile(basename: string): boolean {\n return (\n basename.endsWith('.d.ts') ||\n basename.endsWith('.d.mts') ||\n basename.endsWith('.d.cts') ||\n basename.endsWith('.ts') ||\n basename.endsWith('.tsx') ||\n basename.endsWith('.mts') ||\n basename.endsWith('.cts') ||\n basename.endsWith('.js') ||\n basename.endsWith('.jsx') ||\n basename.endsWith('.mjs') ||\n basename.endsWith('.cjs')\n );\n}\n\n/* ---------------- compiler execution ---------------- */\n\nfunction runCompiler(cmdPath: string, args: string[], cb: SpawnCallback): void {\n spawn(process.execPath, [cmdPath, ...args], { encoding: 'utf8' }, cb);\n}\n\n/* ---------------- emitted file parsing ---------------- */\n\nfunction parseEmittedFiles(res: SpawnResult, dest: string): string[] {\n const out: string[] = [];\n const seen = new Set<string>();\n const lines = `${res.stdout}\\n${res.stderr}`.split(/\\r?\\n/);\n\n for (let line of lines) {\n line = line.trim();\n if (!line) continue;\n\n if (/^TSFILE:\\s+/i.test(line)) line = line.replace(/^TSFILE:\\s+/i, '').trim();\n\n if (!path.isAbsolute(line)) continue;\n if (!(line.endsWith('.d.ts') || line.endsWith('.d.mts') || line.endsWith('.d.cts'))) continue;\n\n // Optional safety: only accept outputs under dest\n const rel = path.relative(dest, line);\n if (rel.startsWith('..') || path.isAbsolute(rel)) continue;\n\n if (!seen.has(line)) {\n seen.add(line);\n out.push(line);\n }\n }\n\n return out;\n}\n\nexport default function transformTypesWorker(src: string, dest: string, options: InternalConfigOptions, callback: TransformTypesCallback) {\n const tsconfig = options.tsconfig;\n const matcher = createMatcher(tsconfig);\n\n const rootFiles: string[] = [];\n const iterator = new Iterator(src);\n\n iterator.forEach(\n (entry: Entry): void => {\n const stats = entry.stats as fs.Stats | undefined;\n if (!stats || !stats.isFile()) return;\n const basename = entry.basename as string;\n if (basename[0] === '.') return;\n if (!isAllowedRootFile(basename)) return;\n if (!matcher(entry.fullPath)) return;\n\n rootFiles.push(entry.fullPath);\n },\n { concurrency },\n (err) => {\n if (err) return callback(err);\n if (rootFiles.length === 0) return callback(null, []);\n\n // An explicit rootDir in the tsconfig wins: a package whose sources import siblings above\n // it (a skills tree) names the common ancestor; otherwise the emit is rooted at src.\n const configured = (tsconfig.config.compilerOptions as { rootDir?: string } | undefined)?.rootDir;\n const rootDir = configured === undefined ? src : path.resolve(path.dirname(tsconfig.path), configured);\n const compilerOptions = sanitizeForCompiler({\n ...tsconfig.config.compilerOptions,\n rootDir,\n outDir: dest,\n noEmit: false,\n allowJs: true,\n declaration: true,\n emitDeclarationOnly: true,\n // Suppress TS6 deprecation warnings (TS5101/TS5107) for consumers still on legacy Node-targeted options.\n ignoreDeprecations: '6.0',\n });\n\n const rewrite = (tsconfig.config.compilerOptions as { rewriteRelativeImportExtensions?: boolean } | undefined)?.rewriteRelativeImportExtensions === true;\n\n // Avoid collisions across concurrent runs.\n const tempDir = path.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));\n const tempConfigPath = path.join(tempDir, 'tsconfig.json');\n\n const tempConfig = {\n compilerOptions,\n files: rootFiles,\n include: [],\n exclude: [],\n ...(tsconfig.config.references && { references: tsconfig.config.references }),\n };\n\n fs.mkdir(tempDir, { recursive: true }, (mkdirErr) => {\n if (mkdirErr) return callback(mkdirErr);\n\n fs.writeFile(tempConfigPath, JSON.stringify(tempConfig, null, 2), 'utf8', (writeErr) => {\n if (writeErr) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(writeErr));\n return;\n }\n\n const args = ['--project', tempConfigPath, '--listEmittedFiles', '--pretty', 'false'];\n\n runCompiler(tscPath, args, (runErr, res) => {\n if (runErr || !res || res.status !== 0) {\n // cross-spawn-cb sets runErr (with stdout/stderr/status copied onto it) on non-zero exit\n // and does NOT pass res — read from runErr first, then fall back to res.\n const source = (runErr as unknown as SpawnResult | undefined) ?? res;\n const status = source?.status;\n const stdout = String(source?.stdout ?? '');\n const stderr = String(source?.stderr ?? '');\n const detail = `TypeScript compiler failed (status=${status}).\\nstdout:\\n${stdout.slice(0, 100_000)}\\nstderr:\\n${stderr.slice(0, 20_000)}`;\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error(detail)));\n return;\n }\n\n const emittedFiles = parseEmittedFiles(res, dest);\n if (emittedFiles.length === 0) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error('TypeScript compiler produced no emitted declaration files')));\n return;\n }\n\n const postQueue = new Queue();\n\n if (rewrite) {\n for (const file of emittedFiles) {\n postQueue.defer((cb) => {\n fs.readFile(file, 'utf8', (readErr, content) => {\n if (readErr) return cb();\n const updated = rewriteExtensions(content);\n if (updated === content) cb();\n else fs.writeFile(file, updated, 'utf8', (err) => cb(err));\n });\n });\n }\n }\n\n postQueue.await(() => {\n safeRm(tempDir, { recursive: true, force: true }, (rmErr) => callback(rmErr || null, emittedFiles));\n });\n });\n });\n });\n }\n );\n}\n"],"names":["transformTypesWorker","os","concurrency","Math","min","max","cpus","length","tscPath","resolveBin","compilerMajor","pkg","JSON","parse","fs","readFileSync","path","join","dirname","String","version","split","sanitizeForCompiler","options","sanitized","downlevelIteration","moduleResolution","target","test","isAllowedRootFile","basename","endsWith","runCompiler","cmdPath","args","cb","spawn","process","execPath","encoding","parseEmittedFiles","res","dest","out","seen","Set","lines","stdout","stderr","line","trim","replace","isAbsolute","rel","relative","startsWith","has","add","push","src","callback","tsconfig","matcher","createMatcher","rootFiles","iterator","Iterator","forEach","entry","stats","isFile","fullPath","err","configured","config","compilerOptions","rootDir","undefined","resolve","outDir","noEmit","allowJs","declaration","emitDeclarationOnly","ignoreDeprecations","rewrite","rewriteRelativeImportExtensions","tempDir","pid","Date","now","tempConfigPath","tempConfig","files","include","exclude","references","mkdir","recursive","mkdirErr","writeFile","stringify","writeErr","safeRm","force","runErr","status","source","detail","slice","Error","emittedFiles","postQueue","Queue","file","defer","readFile","readErr","content","updated","rewriteExtensions","await","rmErr"],"mappings":";;;;+BA2FA;;;eAAwBA;;;mEA3F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
79
79
|
}, {
|
|
80
80
|
concurrency
|
|
81
81
|
}, (err)=>{
|
|
82
|
-
var _tsconfig_config_compilerOptions;
|
|
82
|
+
var _tsconfig_config_compilerOptions, _tsconfig_config_compilerOptions1;
|
|
83
83
|
if (err) return callback(err);
|
|
84
84
|
if (rootFiles.length === 0) return callback(null, []);
|
|
85
|
+
// An explicit rootDir in the tsconfig wins: a package whose sources import siblings above
|
|
86
|
+
// it (a skills tree) names the common ancestor; otherwise the emit is rooted at src.
|
|
87
|
+
const configured = (_tsconfig_config_compilerOptions = tsconfig.config.compilerOptions) === null || _tsconfig_config_compilerOptions === void 0 ? void 0 : _tsconfig_config_compilerOptions.rootDir;
|
|
88
|
+
const rootDir = configured === undefined ? src : path.resolve(path.dirname(tsconfig.path), configured);
|
|
85
89
|
const compilerOptions = sanitizeForCompiler({
|
|
86
90
|
...tsconfig.config.compilerOptions,
|
|
87
|
-
rootDir
|
|
91
|
+
rootDir,
|
|
88
92
|
outDir: dest,
|
|
89
93
|
noEmit: false,
|
|
90
94
|
allowJs: true,
|
|
@@ -93,7 +97,7 @@ export default function transformTypesWorker(src, dest, options, callback) {
|
|
|
93
97
|
// Suppress TS6 deprecation warnings (TS5101/TS5107) for consumers still on legacy Node-targeted options.
|
|
94
98
|
ignoreDeprecations: '6.0'
|
|
95
99
|
});
|
|
96
|
-
const rewrite = ((
|
|
100
|
+
const rewrite = ((_tsconfig_config_compilerOptions1 = tsconfig.config.compilerOptions) === null || _tsconfig_config_compilerOptions1 === void 0 ? void 0 : _tsconfig_config_compilerOptions1.rewriteRelativeImportExtensions) === true;
|
|
97
101
|
// Avoid collisions across concurrent runs.
|
|
98
102
|
const tempDir = path.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));
|
|
99
103
|
const tempConfigPath = path.join(tempDir, 'tsconfig.json');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformTypes.ts"],"sourcesContent":["import spawn, { type SpawnCallback, type SpawnResult } from 'cross-spawn-cb';\nimport fs from 'fs';\nimport Iterator, { type Entry } from 'fs-iterator';\nimport { safeRm } from 'fs-remove-compat';\nimport os from 'os';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport resolveBin from 'resolve-bin-sync';\n\nconst concurrency = Math.min(64, Math.max(8, (os.cpus()?.length ?? 4) * 8));\n\nconst tscPath = resolveBin('typescript', 'tsc');\n\nfunction compilerMajor(): number {\n try {\n const pkg = JSON.parse(fs.readFileSync(path.join(path.dirname(tscPath), '..', 'package.json'), 'utf8'));\n return +String(pkg.version).split('.')[0] || 0;\n } catch {\n return 0;\n }\n}\n\n// TypeScript 7 removed options that only ever affected JS emit, which swc handles.\n// Older compilers still accept them, so only rewrite when the installed tsc would reject them.\nfunction sanitizeForCompiler(options: Record<string, unknown>): Record<string, unknown> {\n if (compilerMajor() < 7) return options;\n const sanitized = { ...options };\n delete sanitized.downlevelIteration;\n if (sanitized.moduleResolution === 'node' || sanitized.moduleResolution === 'node10') sanitized.moduleResolution = 'bundler';\n if (typeof sanitized.target === 'string' && /^es[35]$/i.test(sanitized.target)) sanitized.target = 'es2015';\n return sanitized;\n}\n\nimport createMatcher from '../createMatcher.ts';\nimport { rewriteExtensions } from '../lib/rewriteExtensions.ts';\n\nimport type { InternalConfigOptions, TransformTypesCallback } from '../types.ts';\n\n/* ---------------- root file filtering ---------------- */\n\nfunction isAllowedRootFile(basename: string): boolean {\n return (\n basename.endsWith('.d.ts') ||\n basename.endsWith('.d.mts') ||\n basename.endsWith('.d.cts') ||\n basename.endsWith('.ts') ||\n basename.endsWith('.tsx') ||\n basename.endsWith('.mts') ||\n basename.endsWith('.cts') ||\n basename.endsWith('.js') ||\n basename.endsWith('.jsx') ||\n basename.endsWith('.mjs') ||\n basename.endsWith('.cjs')\n );\n}\n\n/* ---------------- compiler execution ---------------- */\n\nfunction runCompiler(cmdPath: string, args: string[], cb: SpawnCallback): void {\n spawn(process.execPath, [cmdPath, ...args], { encoding: 'utf8' }, cb);\n}\n\n/* ---------------- emitted file parsing ---------------- */\n\nfunction parseEmittedFiles(res: SpawnResult, dest: string): string[] {\n const out: string[] = [];\n const seen = new Set<string>();\n const lines = `${res.stdout}\\n${res.stderr}`.split(/\\r?\\n/);\n\n for (let line of lines) {\n line = line.trim();\n if (!line) continue;\n\n if (/^TSFILE:\\s+/i.test(line)) line = line.replace(/^TSFILE:\\s+/i, '').trim();\n\n if (!path.isAbsolute(line)) continue;\n if (!(line.endsWith('.d.ts') || line.endsWith('.d.mts') || line.endsWith('.d.cts'))) continue;\n\n // Optional safety: only accept outputs under dest\n const rel = path.relative(dest, line);\n if (rel.startsWith('..') || path.isAbsolute(rel)) continue;\n\n if (!seen.has(line)) {\n seen.add(line);\n out.push(line);\n }\n }\n\n return out;\n}\n\nexport default function transformTypesWorker(src: string, dest: string, options: InternalConfigOptions, callback: TransformTypesCallback) {\n const tsconfig = options.tsconfig;\n const matcher = createMatcher(tsconfig);\n\n const rootFiles: string[] = [];\n const iterator = new Iterator(src);\n\n iterator.forEach(\n (entry: Entry): void => {\n const stats = entry.stats as fs.Stats | undefined;\n if (!stats || !stats.isFile()) return;\n const basename = entry.basename as string;\n if (basename[0] === '.') return;\n if (!isAllowedRootFile(basename)) return;\n if (!matcher(entry.fullPath)) return;\n\n rootFiles.push(entry.fullPath);\n },\n { concurrency },\n (err) => {\n if (err) return callback(err);\n if (rootFiles.length === 0) return callback(null, []);\n\n const compilerOptions = sanitizeForCompiler({\n ...tsconfig.config.compilerOptions,\n rootDir: src,\n outDir: dest,\n noEmit: false,\n allowJs: true,\n declaration: true,\n emitDeclarationOnly: true,\n // Suppress TS6 deprecation warnings (TS5101/TS5107) for consumers still on legacy Node-targeted options.\n ignoreDeprecations: '6.0',\n });\n\n const rewrite = (tsconfig.config.compilerOptions as { rewriteRelativeImportExtensions?: boolean } | undefined)?.rewriteRelativeImportExtensions === true;\n\n // Avoid collisions across concurrent runs.\n const tempDir = path.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));\n const tempConfigPath = path.join(tempDir, 'tsconfig.json');\n\n const tempConfig = {\n compilerOptions,\n files: rootFiles,\n include: [],\n exclude: [],\n ...(tsconfig.config.references && { references: tsconfig.config.references }),\n };\n\n fs.mkdir(tempDir, { recursive: true }, (mkdirErr) => {\n if (mkdirErr) return callback(mkdirErr);\n\n fs.writeFile(tempConfigPath, JSON.stringify(tempConfig, null, 2), 'utf8', (writeErr) => {\n if (writeErr) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(writeErr));\n return;\n }\n\n const args = ['--project', tempConfigPath, '--listEmittedFiles', '--pretty', 'false'];\n\n runCompiler(tscPath, args, (runErr, res) => {\n if (runErr || !res || res.status !== 0) {\n // cross-spawn-cb sets runErr (with stdout/stderr/status copied onto it) on non-zero exit\n // and does NOT pass res — read from runErr first, then fall back to res.\n const source = (runErr as unknown as SpawnResult | undefined) ?? res;\n const status = source?.status;\n const stdout = String(source?.stdout ?? '');\n const stderr = String(source?.stderr ?? '');\n const detail = `TypeScript compiler failed (status=${status}).\\nstdout:\\n${stdout.slice(0, 100_000)}\\nstderr:\\n${stderr.slice(0, 20_000)}`;\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error(detail)));\n return;\n }\n\n const emittedFiles = parseEmittedFiles(res, dest);\n if (emittedFiles.length === 0) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error('TypeScript compiler produced no emitted declaration files')));\n return;\n }\n\n const postQueue = new Queue();\n\n if (rewrite) {\n for (const file of emittedFiles) {\n postQueue.defer((cb) => {\n fs.readFile(file, 'utf8', (readErr, content) => {\n if (readErr) return cb();\n const updated = rewriteExtensions(content);\n if (updated === content) cb();\n else fs.writeFile(file, updated, 'utf8', (err) => cb(err));\n });\n });\n }\n }\n\n postQueue.await(() => {\n safeRm(tempDir, { recursive: true, force: true }, (rmErr) => callback(rmErr || null, emittedFiles));\n });\n });\n });\n });\n }\n );\n}\n"],"names":["os","spawn","fs","Iterator","safeRm","path","Queue","resolveBin","concurrency","Math","min","max","cpus","length","tscPath","compilerMajor","pkg","JSON","parse","readFileSync","join","dirname","String","version","split","sanitizeForCompiler","options","sanitized","downlevelIteration","moduleResolution","target","test","createMatcher","rewriteExtensions","isAllowedRootFile","basename","endsWith","runCompiler","cmdPath","args","cb","process","execPath","encoding","parseEmittedFiles","res","dest","out","seen","Set","lines","stdout","stderr","line","trim","replace","isAbsolute","rel","relative","startsWith","has","add","push","transformTypesWorker","src","callback","tsconfig","matcher","rootFiles","iterator","forEach","entry","stats","isFile","fullPath","err","compilerOptions","config","rootDir","outDir","noEmit","allowJs","declaration","emitDeclarationOnly","ignoreDeprecations","rewrite","rewriteRelativeImportExtensions","tempDir","pid","Date","now","tempConfigPath","tempConfig","files","include","exclude","references","mkdir","recursive","mkdirErr","writeFile","stringify","writeErr","force","runErr","status","source","detail","slice","Error","emittedFiles","postQueue","file","defer","readFile","readErr","content","updated","await","rmErr"],"mappings":";IAS8CA;AAT9C,OAAOC,WAAqD,iBAAiB;AAC7E,OAAOC,QAAQ,KAAK;AACpB,OAAOC,cAA8B,cAAc;AACnD,SAASC,MAAM,QAAQ,mBAAmB;AAC1C,OAAOJ,QAAQ,KAAK;AACpB,OAAOK,UAAU,OAAO;AACxB,OAAOC,WAAW,WAAW;AAC7B,OAAOC,gBAAgB,mBAAmB;AAE1C,MAAMC,cAAcC,KAAKC,GAAG,CAAC,IAAID,KAAKE,GAAG,CAAC,GAAG,UAACX,WAAAA,GAAGY,IAAI,gBAAPZ,+BAAAA,SAAWa,MAAM,uCAAI,KAAK;AAExE,MAAMC,UAAUP,WAAW,cAAc;AAEzC,SAASQ;IACP,IAAI;QACF,MAAMC,MAAMC,KAAKC,KAAK,CAAChB,GAAGiB,YAAY,CAACd,KAAKe,IAAI,CAACf,KAAKgB,OAAO,CAACP,UAAU,MAAM,iBAAiB;QAC/F,OAAO,CAACQ,OAAON,IAAIO,OAAO,EAAEC,KAAK,CAAC,I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1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/typescript/ts-swc-transform/src/workers/transformTypes.ts"],"sourcesContent":["import spawn, { type SpawnCallback, type SpawnResult } from 'cross-spawn-cb';\nimport fs from 'fs';\nimport Iterator, { type Entry } from 'fs-iterator';\nimport { safeRm } from 'fs-remove-compat';\nimport os from 'os';\nimport path from 'path';\nimport Queue from 'queue-cb';\nimport resolveBin from 'resolve-bin-sync';\n\nconst concurrency = Math.min(64, Math.max(8, (os.cpus()?.length ?? 4) * 8));\n\nconst tscPath = resolveBin('typescript', 'tsc');\n\nfunction compilerMajor(): number {\n try {\n const pkg = JSON.parse(fs.readFileSync(path.join(path.dirname(tscPath), '..', 'package.json'), 'utf8'));\n return +String(pkg.version).split('.')[0] || 0;\n } catch {\n return 0;\n }\n}\n\n// TypeScript 7 removed options that only ever affected JS emit, which swc handles.\n// Older compilers still accept them, so only rewrite when the installed tsc would reject them.\nfunction sanitizeForCompiler(options: Record<string, unknown>): Record<string, unknown> {\n if (compilerMajor() < 7) return options;\n const sanitized = { ...options };\n delete sanitized.downlevelIteration;\n if (sanitized.moduleResolution === 'node' || sanitized.moduleResolution === 'node10') sanitized.moduleResolution = 'bundler';\n if (typeof sanitized.target === 'string' && /^es[35]$/i.test(sanitized.target)) sanitized.target = 'es2015';\n return sanitized;\n}\n\nimport createMatcher from '../createMatcher.ts';\nimport { rewriteExtensions } from '../lib/rewriteExtensions.ts';\n\nimport type { InternalConfigOptions, TransformTypesCallback } from '../types.ts';\n\n/* ---------------- root file filtering ---------------- */\n\nfunction isAllowedRootFile(basename: string): boolean {\n return (\n basename.endsWith('.d.ts') ||\n basename.endsWith('.d.mts') ||\n basename.endsWith('.d.cts') ||\n basename.endsWith('.ts') ||\n basename.endsWith('.tsx') ||\n basename.endsWith('.mts') ||\n basename.endsWith('.cts') ||\n basename.endsWith('.js') ||\n basename.endsWith('.jsx') ||\n basename.endsWith('.mjs') ||\n basename.endsWith('.cjs')\n );\n}\n\n/* ---------------- compiler execution ---------------- */\n\nfunction runCompiler(cmdPath: string, args: string[], cb: SpawnCallback): void {\n spawn(process.execPath, [cmdPath, ...args], { encoding: 'utf8' }, cb);\n}\n\n/* ---------------- emitted file parsing ---------------- */\n\nfunction parseEmittedFiles(res: SpawnResult, dest: string): string[] {\n const out: string[] = [];\n const seen = new Set<string>();\n const lines = `${res.stdout}\\n${res.stderr}`.split(/\\r?\\n/);\n\n for (let line of lines) {\n line = line.trim();\n if (!line) continue;\n\n if (/^TSFILE:\\s+/i.test(line)) line = line.replace(/^TSFILE:\\s+/i, '').trim();\n\n if (!path.isAbsolute(line)) continue;\n if (!(line.endsWith('.d.ts') || line.endsWith('.d.mts') || line.endsWith('.d.cts'))) continue;\n\n // Optional safety: only accept outputs under dest\n const rel = path.relative(dest, line);\n if (rel.startsWith('..') || path.isAbsolute(rel)) continue;\n\n if (!seen.has(line)) {\n seen.add(line);\n out.push(line);\n }\n }\n\n return out;\n}\n\nexport default function transformTypesWorker(src: string, dest: string, options: InternalConfigOptions, callback: TransformTypesCallback) {\n const tsconfig = options.tsconfig;\n const matcher = createMatcher(tsconfig);\n\n const rootFiles: string[] = [];\n const iterator = new Iterator(src);\n\n iterator.forEach(\n (entry: Entry): void => {\n const stats = entry.stats as fs.Stats | undefined;\n if (!stats || !stats.isFile()) return;\n const basename = entry.basename as string;\n if (basename[0] === '.') return;\n if (!isAllowedRootFile(basename)) return;\n if (!matcher(entry.fullPath)) return;\n\n rootFiles.push(entry.fullPath);\n },\n { concurrency },\n (err) => {\n if (err) return callback(err);\n if (rootFiles.length === 0) return callback(null, []);\n\n // An explicit rootDir in the tsconfig wins: a package whose sources import siblings above\n // it (a skills tree) names the common ancestor; otherwise the emit is rooted at src.\n const configured = (tsconfig.config.compilerOptions as { rootDir?: string } | undefined)?.rootDir;\n const rootDir = configured === undefined ? src : path.resolve(path.dirname(tsconfig.path), configured);\n const compilerOptions = sanitizeForCompiler({\n ...tsconfig.config.compilerOptions,\n rootDir,\n outDir: dest,\n noEmit: false,\n allowJs: true,\n declaration: true,\n emitDeclarationOnly: true,\n // Suppress TS6 deprecation warnings (TS5101/TS5107) for consumers still on legacy Node-targeted options.\n ignoreDeprecations: '6.0',\n });\n\n const rewrite = (tsconfig.config.compilerOptions as { rewriteRelativeImportExtensions?: boolean } | undefined)?.rewriteRelativeImportExtensions === true;\n\n // Avoid collisions across concurrent runs.\n const tempDir = path.join(dest, '.ts-swc-transform-temp', String(process.pid), String(Date.now()));\n const tempConfigPath = path.join(tempDir, 'tsconfig.json');\n\n const tempConfig = {\n compilerOptions,\n files: rootFiles,\n include: [],\n exclude: [],\n ...(tsconfig.config.references && { references: tsconfig.config.references }),\n };\n\n fs.mkdir(tempDir, { recursive: true }, (mkdirErr) => {\n if (mkdirErr) return callback(mkdirErr);\n\n fs.writeFile(tempConfigPath, JSON.stringify(tempConfig, null, 2), 'utf8', (writeErr) => {\n if (writeErr) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(writeErr));\n return;\n }\n\n const args = ['--project', tempConfigPath, '--listEmittedFiles', '--pretty', 'false'];\n\n runCompiler(tscPath, args, (runErr, res) => {\n if (runErr || !res || res.status !== 0) {\n // cross-spawn-cb sets runErr (with stdout/stderr/status copied onto it) on non-zero exit\n // and does NOT pass res — read from runErr first, then fall back to res.\n const source = (runErr as unknown as SpawnResult | undefined) ?? res;\n const status = source?.status;\n const stdout = String(source?.stdout ?? '');\n const stderr = String(source?.stderr ?? '');\n const detail = `TypeScript compiler failed (status=${status}).\\nstdout:\\n${stdout.slice(0, 100_000)}\\nstderr:\\n${stderr.slice(0, 20_000)}`;\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error(detail)));\n return;\n }\n\n const emittedFiles = parseEmittedFiles(res, dest);\n if (emittedFiles.length === 0) {\n safeRm(tempDir, { recursive: true, force: true }, () => callback(new Error('TypeScript compiler produced no emitted declaration files')));\n return;\n }\n\n const postQueue = new Queue();\n\n if (rewrite) {\n for (const file of emittedFiles) {\n postQueue.defer((cb) => {\n fs.readFile(file, 'utf8', (readErr, content) => {\n if (readErr) return cb();\n const updated = rewriteExtensions(content);\n if (updated === content) cb();\n else fs.writeFile(file, updated, 'utf8', (err) => cb(err));\n });\n });\n }\n }\n\n postQueue.await(() => {\n safeRm(tempDir, { recursive: true, force: true }, (rmErr) => callback(rmErr || null, emittedFiles));\n });\n });\n });\n });\n }\n );\n}\n"],"names":["os","spawn","fs","Iterator","safeRm","path","Queue","resolveBin","concurrency","Math","min","max","cpus","length","tscPath","compilerMajor","pkg","JSON","parse","readFileSync","join","dirname","String","version","split","sanitizeForCompiler","options","sanitized","downlevelIteration","moduleResolution","target","test","createMatcher","rewriteExtensions","isAllowedRootFile","basename","endsWith","runCompiler","cmdPath","args","cb","process","execPath","encoding","parseEmittedFiles","res","dest","out","seen","Set","lines","stdout","stderr","line","trim","replace","isAbsolute","rel","relative","startsWith","has","add","push","transformTypesWorker","src","callback","tsconfig","matcher","rootFiles","iterator","forEach","entry","stats","isFile","fullPath","err","configured","config","compilerOptions","rootDir","undefined","resolve","outDir","noEmit","allowJs","declaration","emitDeclarationOnly","ignoreDeprecations","rewrite","rewriteRelativeImportExtensions","tempDir","pid","Date","now","tempConfigPath","tempConfig","files","include","exclude","references","mkdir","recursive","mkdirErr","writeFile","stringify","writeErr","force","runErr","status","source","detail","slice","Error","emittedFiles","postQueue","file","defer","readFile","readErr","content","updated","await","rmErr"],"mappings":";IAS8CA;AAT9C,OAAOC,WAAqD,iBAAiB;AAC7E,OAAOC,QAAQ,KAAK;AACpB,OAAOC,cAA8B,cAAc;AACnD,SAASC,MAAM,QAAQ,mBAAmB;AAC1C,OAAOJ,QAAQ,KAAK;AACpB,OAAOK,UAAU,OAAO;AACxB,OAAOC,WAAW,WAAW;AAC7B,OAAOC,gBAAgB,mBAAmB;AAE1C,MAAMC,cAAcC,KAAKC,GAAG,CAAC,IAAID,KAAKE,GAAG,CAAC,GAAG,UAACX,WAAAA,GAAGY,IAAI,gBAAPZ,+BAAAA,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AIkF,SAAS;wBACX,KAAK,MAAM2B,QAAQF,aAAc;4BAC/BC,UAAUE,KAAK,CAAC,CAAC5E;gCACftC,GAAGmH,QAAQ,CAACF,MAAM,QAAQ,CAACG,SAASC;oCAClC,IAAID,SAAS,OAAO9E;oCACpB,MAAMgF,UAAUvF,kBAAkBsF;oCAClC,IAAIC,YAAYD,SAAS/E;yCACpBtC,GAAGqG,SAAS,CAACY,MAAMK,SAAS,QAAQ,CAAC7C,MAAQnC,GAAGmC;gCACvD;4BACF;wBACF;oBACF;oBAEAuC,UAAUO,KAAK,CAAC;wBACdrH,OAAOsF,SAAS;4BAAEW,WAAW;4BAAMK,OAAO;wBAAK,GAAG,CAACgB,QAAUzD,SAASyD,SAAS,MAAMT;oBACvF;gBACF;YACF;QACF;IACF;AAEJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-swc-transform",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12.0",
|
|
4
4
|
"description": "Typescript transformers for swc. Supports Node >= 0.8",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"matcher",
|
|
@@ -56,13 +56,13 @@
|
|
|
56
56
|
"node-version-call": "^3.0.0",
|
|
57
57
|
"queue-cb": "^1.0.0",
|
|
58
58
|
"read-tsconfig-sync": "^1.1.0",
|
|
59
|
-
"resolve": "
|
|
59
|
+
"resolve": "1.22.12",
|
|
60
60
|
"resolve-bin-sync": "^1.0.13",
|
|
61
61
|
"resolve.exports": "^2.0.3",
|
|
62
62
|
"test-match": "^1.0.0",
|
|
63
63
|
"ts-constants": "^1.1.13",
|
|
64
64
|
"ts-node": "^10.9.0",
|
|
65
|
-
"typescript": "
|
|
65
|
+
"typescript": "6.0.3"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@types/is-absolute": "^1.0.2",
|
|
@@ -84,5 +84,9 @@
|
|
|
84
84
|
},
|
|
85
85
|
"engines": {
|
|
86
86
|
"node": ">=0.8"
|
|
87
|
+
},
|
|
88
|
+
"allowScripts": {
|
|
89
|
+
"core-js-pure": true,
|
|
90
|
+
"@swc/core": true
|
|
87
91
|
}
|
|
88
92
|
}
|