unplugin-isolated-decl 0.9.0 → 0.9.2
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 +13 -1
- package/dist/api.cjs +1 -1
- package/dist/api.js +1 -1
- package/dist/core/options.d.cts +8 -0
- package/dist/core/options.d.ts +8 -0
- package/dist/core/transformer.d.cts +5 -2
- package/dist/core/transformer.d.ts +5 -2
- package/dist/esbuild.cjs +2 -2
- package/dist/esbuild.js +2 -2
- package/dist/farm.cjs +2 -2
- package/dist/farm.js +2 -2
- package/dist/index.cjs +2 -2
- package/dist/index.js +2 -2
- package/dist/rolldown.cjs +2 -2
- package/dist/rolldown.js +2 -2
- package/dist/rollup.cjs +2 -2
- package/dist/rollup.js +2 -2
- package/dist/{src-CglEH7vG.js → src-CoC_1xj6.js} +49 -20
- package/dist/{src-wtIilNZo.cjs → src-DVALVxuy.cjs} +55 -49
- package/dist/transformer-BI1dF8s7.cjs +160 -0
- package/dist/{transformer-C4rLQvdl.cjs → transformer-DfgXLWZQ.js} +39 -23
- package/dist/vite.cjs +2 -2
- package/dist/vite.js +2 -2
- package/package.json +8 -7
- package/dist/transformer-DDte86lZ.js +0 -74
package/README.md
CHANGED
|
@@ -102,13 +102,25 @@ export interface Options {
|
|
|
102
102
|
enforce?: 'pre' | 'post' | undefined
|
|
103
103
|
/**
|
|
104
104
|
* You need to install one of the supported transformers yourself.
|
|
105
|
+
* oxc: @oxc/transformer
|
|
106
|
+
* swc: @swc/core
|
|
107
|
+
* typescript: typescript
|
|
108
|
+
*
|
|
105
109
|
* @default typescript
|
|
106
110
|
*/
|
|
107
111
|
transformer?: 'oxc' | 'swc' | 'typescript'
|
|
112
|
+
/**
|
|
113
|
+
* Whether to generate declaration source maps.
|
|
114
|
+
*
|
|
115
|
+
* Supported by `typescript` and `oxc` transformer only.
|
|
116
|
+
*
|
|
117
|
+
* @link https://www.typescriptlang.org/tsconfig/#declarationMap
|
|
118
|
+
*/
|
|
119
|
+
sourceMap?: boolean
|
|
120
|
+
|
|
108
121
|
/** Only for typescript transformer */
|
|
109
122
|
transformOptions?: TranspileOptions
|
|
110
123
|
ignoreErrors?: boolean
|
|
111
|
-
|
|
112
124
|
/** An extra directory layer for output files. */
|
|
113
125
|
extraOutdir?: string
|
|
114
126
|
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
|
package/dist/api.cjs
CHANGED
package/dist/api.js
CHANGED
package/dist/core/options.d.cts
CHANGED
|
@@ -7,6 +7,14 @@ export type Options = {
|
|
|
7
7
|
include?: FilterPattern;
|
|
8
8
|
exclude?: FilterPattern;
|
|
9
9
|
enforce?: 'pre' | 'post' | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to generate declaration source maps.
|
|
12
|
+
*
|
|
13
|
+
* Supported by `typescript` and `oxc` transformer only.
|
|
14
|
+
*
|
|
15
|
+
* @link https://www.typescriptlang.org/tsconfig/#declarationMap
|
|
16
|
+
*/
|
|
17
|
+
sourceMap?: boolean;
|
|
10
18
|
ignoreErrors?: boolean;
|
|
11
19
|
/** An extra directory layer for output files. */
|
|
12
20
|
extraOutdir?: string;
|
package/dist/core/options.d.ts
CHANGED
|
@@ -7,6 +7,14 @@ export type Options = {
|
|
|
7
7
|
include?: FilterPattern;
|
|
8
8
|
exclude?: FilterPattern;
|
|
9
9
|
enforce?: 'pre' | 'post' | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to generate declaration source maps.
|
|
12
|
+
*
|
|
13
|
+
* Supported by `typescript` and `oxc` transformer only.
|
|
14
|
+
*
|
|
15
|
+
* @link https://www.typescriptlang.org/tsconfig/#declarationMap
|
|
16
|
+
*/
|
|
17
|
+
sourceMap?: boolean;
|
|
10
18
|
ignoreErrors?: boolean;
|
|
11
19
|
/** An extra directory layer for output files. */
|
|
12
20
|
extraOutdir?: string;
|
|
@@ -2,11 +2,12 @@ import type { TranspileOptions } from 'typescript';
|
|
|
2
2
|
export interface TransformResult {
|
|
3
3
|
code: string;
|
|
4
4
|
errors: Array<string>;
|
|
5
|
+
map?: string;
|
|
5
6
|
}
|
|
6
7
|
/**
|
|
7
8
|
* Transform isolated declarations with `oxc-transform`.
|
|
8
9
|
*/
|
|
9
|
-
export declare function oxcTransform(id: string, code: string): Promise<TransformResult>;
|
|
10
|
+
export declare function oxcTransform(id: string, code: string, sourceMap?: boolean): Promise<TransformResult>;
|
|
10
11
|
/**
|
|
11
12
|
* Transform isolated declarations with `@swc/core`.
|
|
12
13
|
*/
|
|
@@ -14,4 +15,6 @@ export declare function swcTransform(id: string, code: string): Promise<Transfor
|
|
|
14
15
|
/**
|
|
15
16
|
* Transform isolated declarations with `typescript`.
|
|
16
17
|
*/
|
|
17
|
-
export declare function tsTransform(id: string, code: string, transformOptions?: TranspileOptions): Promise<TransformResult>;
|
|
18
|
+
export declare function tsTransform(id: string, code: string, transformOptions?: TranspileOptions, sourceMap?: boolean): Promise<TransformResult>;
|
|
19
|
+
export declare function appendMapUrl(map: string, filename: string): string;
|
|
20
|
+
export declare function generateDtsMap(mappings: string, src: string, dts: string): string;
|
|
@@ -2,11 +2,12 @@ import type { TranspileOptions } from 'typescript';
|
|
|
2
2
|
export interface TransformResult {
|
|
3
3
|
code: string;
|
|
4
4
|
errors: Array<string>;
|
|
5
|
+
map?: string;
|
|
5
6
|
}
|
|
6
7
|
/**
|
|
7
8
|
* Transform isolated declarations with `oxc-transform`.
|
|
8
9
|
*/
|
|
9
|
-
export declare function oxcTransform(id: string, code: string): Promise<TransformResult>;
|
|
10
|
+
export declare function oxcTransform(id: string, code: string, sourceMap?: boolean): Promise<TransformResult>;
|
|
10
11
|
/**
|
|
11
12
|
* Transform isolated declarations with `@swc/core`.
|
|
12
13
|
*/
|
|
@@ -14,4 +15,6 @@ export declare function swcTransform(id: string, code: string): Promise<Transfor
|
|
|
14
15
|
/**
|
|
15
16
|
* Transform isolated declarations with `typescript`.
|
|
16
17
|
*/
|
|
17
|
-
export declare function tsTransform(id: string, code: string, transformOptions?: TranspileOptions): Promise<TransformResult>;
|
|
18
|
+
export declare function tsTransform(id: string, code: string, transformOptions?: TranspileOptions, sourceMap?: boolean): Promise<TransformResult>;
|
|
19
|
+
export declare function appendMapUrl(map: string, filename: string): string;
|
|
20
|
+
export declare function generateDtsMap(mappings: string, src: string, dts: string): string;
|
package/dist/esbuild.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
require('./
|
|
2
|
+
require('./transformer-BI1dF8s7.cjs');
|
|
3
|
+
const require_src = require('./src-DVALVxuy.cjs');
|
|
4
4
|
|
|
5
5
|
//#region src/esbuild.ts
|
|
6
6
|
var esbuild_default = require_src.IsolatedDecl.esbuild;
|
package/dist/esbuild.js
CHANGED
package/dist/farm.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
require('./
|
|
2
|
+
require('./transformer-BI1dF8s7.cjs');
|
|
3
|
+
const require_src = require('./src-DVALVxuy.cjs');
|
|
4
4
|
|
|
5
5
|
//#region src/farm.ts
|
|
6
6
|
var farm_default = require_src.IsolatedDecl.farm;
|
package/dist/farm.js
CHANGED
package/dist/index.cjs
CHANGED
package/dist/index.js
CHANGED
package/dist/rolldown.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
require('./
|
|
2
|
+
require('./transformer-BI1dF8s7.cjs');
|
|
3
|
+
const require_src = require('./src-DVALVxuy.cjs');
|
|
4
4
|
|
|
5
5
|
//#region src/rolldown.ts
|
|
6
6
|
var rolldown_default = require_src.IsolatedDecl.rolldown;
|
package/dist/rolldown.js
CHANGED
package/dist/rollup.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
require('./
|
|
2
|
+
require('./transformer-BI1dF8s7.cjs');
|
|
3
|
+
const require_src = require('./src-DVALVxuy.cjs');
|
|
4
4
|
|
|
5
5
|
//#region src/rollup.ts
|
|
6
6
|
var rollup_default = require_src.IsolatedDecl.rollup;
|
package/dist/rollup.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { oxcTransform, swcTransform, tsTransform } from "./transformer-
|
|
2
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
1
|
+
import { appendMapUrl, generateDtsMap, oxcTransform, swcTransform, tsTransform } from "./transformer-DfgXLWZQ.js";
|
|
3
2
|
import path, { default as path$1, default as path$2 } from "node:path";
|
|
3
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
4
4
|
import { createFilter } from "@rollup/pluginutils";
|
|
5
5
|
import MagicString from "magic-string";
|
|
6
6
|
import { parseAsync } from "oxc-parser";
|
|
@@ -95,6 +95,7 @@ function resolveOptions(options) {
|
|
|
95
95
|
include: options.include || [/\.[cm]?tsx?$/],
|
|
96
96
|
exclude: options.exclude || [/node_modules/],
|
|
97
97
|
enforce: "enforce" in options ? options.enforce : "pre",
|
|
98
|
+
sourceMap: options.sourceMap || false,
|
|
98
99
|
transformer: options.transformer || "typescript",
|
|
99
100
|
ignoreErrors: options.ignoreErrors || false,
|
|
100
101
|
extraOutdir: options.extraOutdir,
|
|
@@ -114,8 +115,12 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
114
115
|
const outputFiles = {};
|
|
115
116
|
function addOutput(filename, output) {
|
|
116
117
|
const name = stripExt(filename);
|
|
118
|
+
const ext = path.extname(filename);
|
|
117
119
|
debug("Add output:", name);
|
|
118
|
-
outputFiles[name] =
|
|
120
|
+
outputFiles[name] = {
|
|
121
|
+
...output,
|
|
122
|
+
ext
|
|
123
|
+
};
|
|
119
124
|
}
|
|
120
125
|
const rollup = { renderStart: rollupRenderStart };
|
|
121
126
|
const farm = { renderStart: { executor: farmRenderStart } };
|
|
@@ -144,14 +149,16 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
144
149
|
let result;
|
|
145
150
|
switch (options.transformer) {
|
|
146
151
|
case "oxc":
|
|
147
|
-
result = await oxcTransform(id, code);
|
|
152
|
+
result = await oxcTransform(id, code, options.sourceMap);
|
|
148
153
|
break;
|
|
149
154
|
case "swc":
|
|
150
155
|
result = await swcTransform(id, code);
|
|
151
156
|
break;
|
|
152
|
-
case "typescript":
|
|
157
|
+
case "typescript":
|
|
158
|
+
result = await tsTransform(id, code, options.transformOptions, options.sourceMap);
|
|
159
|
+
break;
|
|
153
160
|
}
|
|
154
|
-
const { code: dts, errors } = result;
|
|
161
|
+
const { code: dts, errors, map } = result;
|
|
155
162
|
debug(label, "transformed", id, errors.length ? "with errors" : "successfully");
|
|
156
163
|
if (errors.length) if (options.ignoreErrors) context.warn(errors[0]);
|
|
157
164
|
else {
|
|
@@ -179,7 +186,8 @@ else {
|
|
|
179
186
|
}
|
|
180
187
|
addOutput(id, {
|
|
181
188
|
s,
|
|
182
|
-
imports
|
|
189
|
+
imports,
|
|
190
|
+
map
|
|
183
191
|
});
|
|
184
192
|
const typeImports = program.body.filter((node) => {
|
|
185
193
|
if (!("source" in node) || !node.source) return false;
|
|
@@ -206,14 +214,22 @@ else {
|
|
|
206
214
|
const { input } = inputOptions;
|
|
207
215
|
const { inputBase, entryMap } = resolveEntry(input, options.inputBase);
|
|
208
216
|
debug("[rollup] input base:", inputBase);
|
|
209
|
-
let { entryFileNames = "[name].js" } = outputOptions;
|
|
217
|
+
let { entryFileNames = "[name].js", dir: outDir } = outputOptions;
|
|
210
218
|
if (typeof entryFileNames !== "string") return this.error("entryFileNames must be a string");
|
|
211
219
|
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
212
|
-
for (const [srcFilename, { s, imports }] of Object.entries(outputFiles)) {
|
|
220
|
+
for (const [srcFilename, { s, imports, map, ext }] of Object.entries(outputFiles)) {
|
|
213
221
|
const emitName = rewriteImports(s, imports, entryMap, inputBase, entryFileNames, srcFilename);
|
|
214
222
|
let source = s.toString();
|
|
215
223
|
if (options.patchCjsDefaultExport && emitName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
216
224
|
debug("[rollup] emit dts file:", emitName);
|
|
225
|
+
if (options.sourceMap && map && outDir) {
|
|
226
|
+
source = appendMapUrl(source, emitName);
|
|
227
|
+
this.emitFile({
|
|
228
|
+
type: "asset",
|
|
229
|
+
fileName: `${emitName}.map`,
|
|
230
|
+
source: generateDtsMap(map, srcFilename + ext, path.join(outDir, emitName))
|
|
231
|
+
});
|
|
232
|
+
}
|
|
217
233
|
this.emitFile({
|
|
218
234
|
type: "asset",
|
|
219
235
|
fileName: emitName,
|
|
@@ -235,11 +251,20 @@ else {
|
|
|
235
251
|
output.entryFilename = output.entryFilename.replace("[ext]", extFormatMap.get(output.format || "esm") || "js");
|
|
236
252
|
let entryFileNames = output.entryFilename;
|
|
237
253
|
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
238
|
-
for (const [srcFilename, { s, imports }] of Object.entries(outputFiles)) {
|
|
254
|
+
for (const [srcFilename, { s, imports, map, ext }] of Object.entries(outputFiles)) {
|
|
239
255
|
const emitName = rewriteImports(s, imports, entryMap, inputBase, entryFileNames, srcFilename);
|
|
240
256
|
let source = s.toString();
|
|
241
257
|
if (options.patchCjsDefaultExport && emitName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
242
258
|
debug("[farm] emit dts file:", emitName);
|
|
259
|
+
const outDir = output.path;
|
|
260
|
+
if (options.sourceMap && map && outDir) {
|
|
261
|
+
source = appendMapUrl(source, emitName);
|
|
262
|
+
farmPluginContext.emitFile({
|
|
263
|
+
type: "asset",
|
|
264
|
+
fileName: `${emitName}.map`,
|
|
265
|
+
source: generateDtsMap(map, srcFilename + ext, path.join(outDir, emitName))
|
|
266
|
+
});
|
|
267
|
+
}
|
|
243
268
|
farmPluginContext.emitFile({
|
|
244
269
|
type: "asset",
|
|
245
270
|
fileName: emitName,
|
|
@@ -272,21 +297,25 @@ else {
|
|
|
272
297
|
if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
|
|
273
298
|
} else result.outputFiles ||= [];
|
|
274
299
|
const textEncoder = new TextEncoder();
|
|
275
|
-
for (const [
|
|
300
|
+
for (const [srcFilename, { s, map, ext }] of Object.entries(outputFiles)) {
|
|
276
301
|
const outDir = build.initialOptions.outdir;
|
|
277
|
-
let
|
|
278
|
-
if (options.extraOutdir)
|
|
279
|
-
const
|
|
302
|
+
let outName = `${path.relative(inputBase, srcFilename)}.d.${outExt}`;
|
|
303
|
+
if (options.extraOutdir) outName = path.join(options.extraOutdir, outName);
|
|
304
|
+
const outPath = outDir ? path.resolve(outDir, outName) : outName;
|
|
280
305
|
let source = s.toString();
|
|
281
|
-
if (options.patchCjsDefaultExport &&
|
|
306
|
+
if (options.patchCjsDefaultExport && outPath.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
282
307
|
if (write) {
|
|
283
|
-
await mkdir(path.dirname(
|
|
284
|
-
|
|
285
|
-
|
|
308
|
+
await mkdir(path.dirname(outPath), { recursive: true });
|
|
309
|
+
if (options.sourceMap && map) {
|
|
310
|
+
source = appendMapUrl(source, outPath);
|
|
311
|
+
await writeFile(`${outPath}.map`, generateDtsMap(map, srcFilename + ext, path.join(outPath)));
|
|
312
|
+
}
|
|
313
|
+
await writeFile(outPath, source);
|
|
314
|
+
debug("[esbuild] write dts file:", outPath);
|
|
286
315
|
} else {
|
|
287
|
-
debug("[esbuild] emit dts file:",
|
|
316
|
+
debug("[esbuild] emit dts file:", outPath);
|
|
288
317
|
result.outputFiles.push({
|
|
289
|
-
path:
|
|
318
|
+
path: outPath,
|
|
290
319
|
contents: textEncoder.encode(source),
|
|
291
320
|
hash: "",
|
|
292
321
|
text: source
|
|
@@ -1,35 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
-
key = keys[i];
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
-
get: ((k) => from[k]).bind(null, key),
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
-
value: mod,
|
|
21
|
-
enumerable: true
|
|
22
|
-
}) : target, mod));
|
|
23
|
-
|
|
24
|
-
//#endregion
|
|
25
|
-
const require_transformer = require('./transformer-C4rLQvdl.cjs');
|
|
26
|
-
const node_fs_promises = __toESM(require("node:fs/promises"));
|
|
27
|
-
const node_path = __toESM(require("node:path"));
|
|
28
|
-
const __rollup_pluginutils = __toESM(require("@rollup/pluginutils"));
|
|
29
|
-
const magic_string = __toESM(require("magic-string"));
|
|
30
|
-
const oxc_parser = __toESM(require("oxc-parser"));
|
|
31
|
-
const unplugin = __toESM(require("unplugin"));
|
|
32
|
-
const debug = __toESM(require("debug"));
|
|
2
|
+
const require_transformer = require('./transformer-BI1dF8s7.cjs');
|
|
3
|
+
const node_path = require_transformer.__toESM(require("node:path"));
|
|
4
|
+
const node_fs_promises = require_transformer.__toESM(require("node:fs/promises"));
|
|
5
|
+
const __rollup_pluginutils = require_transformer.__toESM(require("@rollup/pluginutils"));
|
|
6
|
+
const magic_string = require_transformer.__toESM(require("magic-string"));
|
|
7
|
+
const oxc_parser = require_transformer.__toESM(require("oxc-parser"));
|
|
8
|
+
const unplugin = require_transformer.__toESM(require("unplugin"));
|
|
9
|
+
const debug = require_transformer.__toESM(require("debug"));
|
|
33
10
|
|
|
34
11
|
//#region src/core/utils.ts
|
|
35
12
|
const debug$1 = (0, debug.default)("unplugin-isolated-decl");
|
|
@@ -119,6 +96,7 @@ function resolveOptions(options) {
|
|
|
119
96
|
include: options.include || [/\.[cm]?tsx?$/],
|
|
120
97
|
exclude: options.exclude || [/node_modules/],
|
|
121
98
|
enforce: "enforce" in options ? options.enforce : "pre",
|
|
99
|
+
sourceMap: options.sourceMap || false,
|
|
122
100
|
transformer: options.transformer || "typescript",
|
|
123
101
|
ignoreErrors: options.ignoreErrors || false,
|
|
124
102
|
extraOutdir: options.extraOutdir,
|
|
@@ -138,8 +116,12 @@ const IsolatedDecl = (0, unplugin.createUnplugin)((rawOptions = {}) => {
|
|
|
138
116
|
const outputFiles = {};
|
|
139
117
|
function addOutput(filename, output) {
|
|
140
118
|
const name = stripExt(filename);
|
|
119
|
+
const ext = node_path.default.extname(filename);
|
|
141
120
|
debug$1("Add output:", name);
|
|
142
|
-
outputFiles[name] =
|
|
121
|
+
outputFiles[name] = {
|
|
122
|
+
...output,
|
|
123
|
+
ext
|
|
124
|
+
};
|
|
143
125
|
}
|
|
144
126
|
const rollup = { renderStart: rollupRenderStart };
|
|
145
127
|
const farm = { renderStart: { executor: farmRenderStart } };
|
|
@@ -168,14 +150,16 @@ const IsolatedDecl = (0, unplugin.createUnplugin)((rawOptions = {}) => {
|
|
|
168
150
|
let result;
|
|
169
151
|
switch (options.transformer) {
|
|
170
152
|
case "oxc":
|
|
171
|
-
result = await require_transformer.oxcTransform(id, code);
|
|
153
|
+
result = await require_transformer.oxcTransform(id, code, options.sourceMap);
|
|
172
154
|
break;
|
|
173
155
|
case "swc":
|
|
174
156
|
result = await require_transformer.swcTransform(id, code);
|
|
175
157
|
break;
|
|
176
|
-
case "typescript":
|
|
158
|
+
case "typescript":
|
|
159
|
+
result = await require_transformer.tsTransform(id, code, options.transformOptions, options.sourceMap);
|
|
160
|
+
break;
|
|
177
161
|
}
|
|
178
|
-
const { code: dts, errors } = result;
|
|
162
|
+
const { code: dts, errors, map } = result;
|
|
179
163
|
debug$1(label, "transformed", id, errors.length ? "with errors" : "successfully");
|
|
180
164
|
if (errors.length) if (options.ignoreErrors) context.warn(errors[0]);
|
|
181
165
|
else {
|
|
@@ -203,7 +187,8 @@ else {
|
|
|
203
187
|
}
|
|
204
188
|
addOutput(id, {
|
|
205
189
|
s,
|
|
206
|
-
imports
|
|
190
|
+
imports,
|
|
191
|
+
map
|
|
207
192
|
});
|
|
208
193
|
const typeImports = program.body.filter((node) => {
|
|
209
194
|
if (!("source" in node) || !node.source) return false;
|
|
@@ -230,14 +215,22 @@ else {
|
|
|
230
215
|
const { input } = inputOptions;
|
|
231
216
|
const { inputBase, entryMap } = resolveEntry(input, options.inputBase);
|
|
232
217
|
debug$1("[rollup] input base:", inputBase);
|
|
233
|
-
let { entryFileNames = "[name].js" } = outputOptions;
|
|
218
|
+
let { entryFileNames = "[name].js", dir: outDir } = outputOptions;
|
|
234
219
|
if (typeof entryFileNames !== "string") return this.error("entryFileNames must be a string");
|
|
235
220
|
if (options.extraOutdir) entryFileNames = node_path.default.join(options.extraOutdir, entryFileNames);
|
|
236
|
-
for (const [srcFilename, { s, imports }] of Object.entries(outputFiles)) {
|
|
221
|
+
for (const [srcFilename, { s, imports, map, ext }] of Object.entries(outputFiles)) {
|
|
237
222
|
const emitName = rewriteImports(s, imports, entryMap, inputBase, entryFileNames, srcFilename);
|
|
238
223
|
let source = s.toString();
|
|
239
224
|
if (options.patchCjsDefaultExport && emitName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
240
225
|
debug$1("[rollup] emit dts file:", emitName);
|
|
226
|
+
if (options.sourceMap && map && outDir) {
|
|
227
|
+
source = require_transformer.appendMapUrl(source, emitName);
|
|
228
|
+
this.emitFile({
|
|
229
|
+
type: "asset",
|
|
230
|
+
fileName: `${emitName}.map`,
|
|
231
|
+
source: require_transformer.generateDtsMap(map, srcFilename + ext, node_path.default.join(outDir, emitName))
|
|
232
|
+
});
|
|
233
|
+
}
|
|
241
234
|
this.emitFile({
|
|
242
235
|
type: "asset",
|
|
243
236
|
fileName: emitName,
|
|
@@ -259,11 +252,20 @@ else {
|
|
|
259
252
|
output.entryFilename = output.entryFilename.replace("[ext]", extFormatMap.get(output.format || "esm") || "js");
|
|
260
253
|
let entryFileNames = output.entryFilename;
|
|
261
254
|
if (options.extraOutdir) entryFileNames = node_path.default.join(options.extraOutdir, entryFileNames);
|
|
262
|
-
for (const [srcFilename, { s, imports }] of Object.entries(outputFiles)) {
|
|
255
|
+
for (const [srcFilename, { s, imports, map, ext }] of Object.entries(outputFiles)) {
|
|
263
256
|
const emitName = rewriteImports(s, imports, entryMap, inputBase, entryFileNames, srcFilename);
|
|
264
257
|
let source = s.toString();
|
|
265
258
|
if (options.patchCjsDefaultExport && emitName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
266
259
|
debug$1("[farm] emit dts file:", emitName);
|
|
260
|
+
const outDir = output.path;
|
|
261
|
+
if (options.sourceMap && map && outDir) {
|
|
262
|
+
source = require_transformer.appendMapUrl(source, emitName);
|
|
263
|
+
farmPluginContext.emitFile({
|
|
264
|
+
type: "asset",
|
|
265
|
+
fileName: `${emitName}.map`,
|
|
266
|
+
source: require_transformer.generateDtsMap(map, srcFilename + ext, node_path.default.join(outDir, emitName))
|
|
267
|
+
});
|
|
268
|
+
}
|
|
267
269
|
farmPluginContext.emitFile({
|
|
268
270
|
type: "asset",
|
|
269
271
|
fileName: emitName,
|
|
@@ -296,21 +298,25 @@ else {
|
|
|
296
298
|
if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
|
|
297
299
|
} else result.outputFiles ||= [];
|
|
298
300
|
const textEncoder = new TextEncoder();
|
|
299
|
-
for (const [
|
|
301
|
+
for (const [srcFilename, { s, map, ext }] of Object.entries(outputFiles)) {
|
|
300
302
|
const outDir = build.initialOptions.outdir;
|
|
301
|
-
let
|
|
302
|
-
if (options.extraOutdir)
|
|
303
|
-
const
|
|
303
|
+
let outName = `${node_path.default.relative(inputBase, srcFilename)}.d.${outExt}`;
|
|
304
|
+
if (options.extraOutdir) outName = node_path.default.join(options.extraOutdir, outName);
|
|
305
|
+
const outPath = outDir ? node_path.default.resolve(outDir, outName) : outName;
|
|
304
306
|
let source = s.toString();
|
|
305
|
-
if (options.patchCjsDefaultExport &&
|
|
307
|
+
if (options.patchCjsDefaultExport && outPath.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
306
308
|
if (write) {
|
|
307
|
-
await (0, node_fs_promises.mkdir)(node_path.default.dirname(
|
|
308
|
-
|
|
309
|
-
|
|
309
|
+
await (0, node_fs_promises.mkdir)(node_path.default.dirname(outPath), { recursive: true });
|
|
310
|
+
if (options.sourceMap && map) {
|
|
311
|
+
source = require_transformer.appendMapUrl(source, outPath);
|
|
312
|
+
await (0, node_fs_promises.writeFile)(`${outPath}.map`, require_transformer.generateDtsMap(map, srcFilename + ext, node_path.default.join(outPath)));
|
|
313
|
+
}
|
|
314
|
+
await (0, node_fs_promises.writeFile)(outPath, source);
|
|
315
|
+
debug$1("[esbuild] write dts file:", outPath);
|
|
310
316
|
} else {
|
|
311
|
-
debug$1("[esbuild] emit dts file:",
|
|
317
|
+
debug$1("[esbuild] emit dts file:", outPath);
|
|
312
318
|
result.outputFiles.push({
|
|
313
|
-
path:
|
|
319
|
+
path: outPath,
|
|
314
320
|
contents: textEncoder.encode(source),
|
|
315
321
|
hash: "",
|
|
316
322
|
text: source
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
const node_path = __toESM(require("node:path"));
|
|
26
|
+
|
|
27
|
+
//#region src/core/transformer.ts
|
|
28
|
+
function tryImport(pkg) {
|
|
29
|
+
try {
|
|
30
|
+
return import(pkg);
|
|
31
|
+
} catch {
|
|
32
|
+
return Promise.resolve(null);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function oxcTransform(id, code, sourceMap) {
|
|
36
|
+
const oxc = await tryImport("oxc-transform");
|
|
37
|
+
if (!oxc) return {
|
|
38
|
+
code: "",
|
|
39
|
+
errors: ["oxc-transform is required for transforming TypeScript, please install `oxc-transform`."]
|
|
40
|
+
};
|
|
41
|
+
const result = oxc.isolatedDeclaration(id, code, { sourcemap: sourceMap });
|
|
42
|
+
return {
|
|
43
|
+
...result,
|
|
44
|
+
map: result.map?.mappings
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function swcTransform(id, code) {
|
|
48
|
+
const swc = await tryImport("@swc/core");
|
|
49
|
+
if (!swc) return {
|
|
50
|
+
code: "",
|
|
51
|
+
errors: ["SWC is required for transforming TypeScript, please install `@swc/core`."]
|
|
52
|
+
};
|
|
53
|
+
try {
|
|
54
|
+
const result = await swc.transform(code, {
|
|
55
|
+
filename: id,
|
|
56
|
+
jsc: {
|
|
57
|
+
parser: {
|
|
58
|
+
syntax: "typescript",
|
|
59
|
+
tsx: false
|
|
60
|
+
},
|
|
61
|
+
experimental: { emitIsolatedDts: true }
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
const output = JSON.parse(result.output);
|
|
65
|
+
return {
|
|
66
|
+
code: output.__swc_isolated_declarations__,
|
|
67
|
+
errors: []
|
|
68
|
+
};
|
|
69
|
+
} catch (error) {
|
|
70
|
+
return {
|
|
71
|
+
code: "",
|
|
72
|
+
errors: [error.toString()]
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function tsTransform(id, code, transformOptions, sourceMap) {
|
|
77
|
+
const ts = await tryImport("typescript");
|
|
78
|
+
if (!ts) return {
|
|
79
|
+
code: "",
|
|
80
|
+
errors: ["TypeScript is required for transforming TypeScript, please install `typescript`."]
|
|
81
|
+
};
|
|
82
|
+
if (!ts.transpileDeclaration) return {
|
|
83
|
+
code: "",
|
|
84
|
+
errors: ["TypeScript version is too low, please upgrade to TypeScript 5.5.2+."]
|
|
85
|
+
};
|
|
86
|
+
const compilerOptions = {
|
|
87
|
+
declarationMap: sourceMap,
|
|
88
|
+
...transformOptions?.compilerOptions
|
|
89
|
+
};
|
|
90
|
+
let { outputText, diagnostics, sourceMapText } = ts.transpileDeclaration(code, {
|
|
91
|
+
fileName: id,
|
|
92
|
+
reportDiagnostics: true,
|
|
93
|
+
...transformOptions,
|
|
94
|
+
compilerOptions
|
|
95
|
+
});
|
|
96
|
+
if (compilerOptions.declarationMap) outputText = stripMapUrl(outputText);
|
|
97
|
+
const errors = diagnostics?.length ? [ts.formatDiagnostics(diagnostics, {
|
|
98
|
+
getCanonicalFileName: (fileName) => ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
|
|
99
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
100
|
+
getNewLine: () => ts.sys.newLine
|
|
101
|
+
})] : [];
|
|
102
|
+
if (sourceMapText) sourceMapText = JSON.parse(sourceMapText).mappings;
|
|
103
|
+
return {
|
|
104
|
+
code: outputText,
|
|
105
|
+
errors,
|
|
106
|
+
map: sourceMapText
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function stripMapUrl(code) {
|
|
110
|
+
const lines = code.split("\n");
|
|
111
|
+
const lastLine = lines.at(-1);
|
|
112
|
+
if (lastLine?.startsWith("//# sourceMappingURL=")) return lines.slice(0, -1).join("\n");
|
|
113
|
+
return code;
|
|
114
|
+
}
|
|
115
|
+
function appendMapUrl(map, filename) {
|
|
116
|
+
return `${map}\n//# sourceMappingURL=${node_path.default.basename(filename)}.map`;
|
|
117
|
+
}
|
|
118
|
+
function generateDtsMap(mappings, src, dts) {
|
|
119
|
+
return JSON.stringify({
|
|
120
|
+
version: 3,
|
|
121
|
+
file: node_path.default.basename(dts),
|
|
122
|
+
sourceRoot: "",
|
|
123
|
+
sources: [node_path.default.relative(node_path.default.dirname(dts), src).replaceAll("\\", "/")],
|
|
124
|
+
names: [],
|
|
125
|
+
mappings
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
//#endregion
|
|
130
|
+
Object.defineProperty(exports, '__toESM', {
|
|
131
|
+
enumerable: true,
|
|
132
|
+
get: function () {
|
|
133
|
+
return __toESM;
|
|
134
|
+
}
|
|
135
|
+
});Object.defineProperty(exports, 'appendMapUrl', {
|
|
136
|
+
enumerable: true,
|
|
137
|
+
get: function () {
|
|
138
|
+
return appendMapUrl;
|
|
139
|
+
}
|
|
140
|
+
});Object.defineProperty(exports, 'generateDtsMap', {
|
|
141
|
+
enumerable: true,
|
|
142
|
+
get: function () {
|
|
143
|
+
return generateDtsMap;
|
|
144
|
+
}
|
|
145
|
+
});Object.defineProperty(exports, 'oxcTransform', {
|
|
146
|
+
enumerable: true,
|
|
147
|
+
get: function () {
|
|
148
|
+
return oxcTransform;
|
|
149
|
+
}
|
|
150
|
+
});Object.defineProperty(exports, 'swcTransform', {
|
|
151
|
+
enumerable: true,
|
|
152
|
+
get: function () {
|
|
153
|
+
return swcTransform;
|
|
154
|
+
}
|
|
155
|
+
});Object.defineProperty(exports, 'tsTransform', {
|
|
156
|
+
enumerable: true,
|
|
157
|
+
get: function () {
|
|
158
|
+
return tsTransform;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import path from "node:path";
|
|
2
2
|
|
|
3
3
|
//#region src/core/transformer.ts
|
|
4
4
|
function tryImport(pkg) {
|
|
@@ -8,13 +8,17 @@ function tryImport(pkg) {
|
|
|
8
8
|
return Promise.resolve(null);
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
|
-
async function oxcTransform(id, code) {
|
|
11
|
+
async function oxcTransform(id, code, sourceMap) {
|
|
12
12
|
const oxc = await tryImport("oxc-transform");
|
|
13
13
|
if (!oxc) return {
|
|
14
14
|
code: "",
|
|
15
15
|
errors: ["oxc-transform is required for transforming TypeScript, please install `oxc-transform`."]
|
|
16
16
|
};
|
|
17
|
-
|
|
17
|
+
const result = oxc.isolatedDeclaration(id, code, { sourcemap: sourceMap });
|
|
18
|
+
return {
|
|
19
|
+
...result,
|
|
20
|
+
map: result.map?.mappings
|
|
21
|
+
};
|
|
18
22
|
}
|
|
19
23
|
async function swcTransform(id, code) {
|
|
20
24
|
const swc = await tryImport("@swc/core");
|
|
@@ -45,7 +49,7 @@ async function swcTransform(id, code) {
|
|
|
45
49
|
};
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
|
-
async function tsTransform(id, code, transformOptions) {
|
|
52
|
+
async function tsTransform(id, code, transformOptions, sourceMap) {
|
|
49
53
|
const ts = await tryImport("typescript");
|
|
50
54
|
if (!ts) return {
|
|
51
55
|
code: "",
|
|
@@ -55,36 +59,48 @@ async function tsTransform(id, code, transformOptions) {
|
|
|
55
59
|
code: "",
|
|
56
60
|
errors: ["TypeScript version is too low, please upgrade to TypeScript 5.5.2+."]
|
|
57
61
|
};
|
|
58
|
-
const
|
|
62
|
+
const compilerOptions = {
|
|
63
|
+
declarationMap: sourceMap,
|
|
64
|
+
...transformOptions?.compilerOptions
|
|
65
|
+
};
|
|
66
|
+
let { outputText, diagnostics, sourceMapText } = ts.transpileDeclaration(code, {
|
|
59
67
|
fileName: id,
|
|
60
68
|
reportDiagnostics: true,
|
|
61
|
-
...transformOptions
|
|
69
|
+
...transformOptions,
|
|
70
|
+
compilerOptions
|
|
62
71
|
});
|
|
72
|
+
if (compilerOptions.declarationMap) outputText = stripMapUrl(outputText);
|
|
63
73
|
const errors = diagnostics?.length ? [ts.formatDiagnostics(diagnostics, {
|
|
64
74
|
getCanonicalFileName: (fileName) => ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
|
|
65
75
|
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
66
76
|
getNewLine: () => ts.sys.newLine
|
|
67
77
|
})] : [];
|
|
78
|
+
if (sourceMapText) sourceMapText = JSON.parse(sourceMapText).mappings;
|
|
68
79
|
return {
|
|
69
80
|
code: outputText,
|
|
70
|
-
errors
|
|
81
|
+
errors,
|
|
82
|
+
map: sourceMapText
|
|
71
83
|
};
|
|
72
84
|
}
|
|
85
|
+
function stripMapUrl(code) {
|
|
86
|
+
const lines = code.split("\n");
|
|
87
|
+
const lastLine = lines.at(-1);
|
|
88
|
+
if (lastLine?.startsWith("//# sourceMappingURL=")) return lines.slice(0, -1).join("\n");
|
|
89
|
+
return code;
|
|
90
|
+
}
|
|
91
|
+
function appendMapUrl(map, filename) {
|
|
92
|
+
return `${map}\n//# sourceMappingURL=${path.basename(filename)}.map`;
|
|
93
|
+
}
|
|
94
|
+
function generateDtsMap(mappings, src, dts) {
|
|
95
|
+
return JSON.stringify({
|
|
96
|
+
version: 3,
|
|
97
|
+
file: path.basename(dts),
|
|
98
|
+
sourceRoot: "",
|
|
99
|
+
sources: [path.relative(path.dirname(dts), src).replaceAll("\\", "/")],
|
|
100
|
+
names: [],
|
|
101
|
+
mappings
|
|
102
|
+
});
|
|
103
|
+
}
|
|
73
104
|
|
|
74
105
|
//#endregion
|
|
75
|
-
|
|
76
|
-
enumerable: true,
|
|
77
|
-
get: function () {
|
|
78
|
-
return oxcTransform;
|
|
79
|
-
}
|
|
80
|
-
});Object.defineProperty(exports, 'swcTransform', {
|
|
81
|
-
enumerable: true,
|
|
82
|
-
get: function () {
|
|
83
|
-
return swcTransform;
|
|
84
|
-
}
|
|
85
|
-
});Object.defineProperty(exports, 'tsTransform', {
|
|
86
|
-
enumerable: true,
|
|
87
|
-
get: function () {
|
|
88
|
-
return tsTransform;
|
|
89
|
-
}
|
|
90
|
-
});
|
|
106
|
+
export { appendMapUrl, generateDtsMap, oxcTransform, swcTransform, tsTransform };
|
package/dist/vite.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
require('./
|
|
2
|
+
require('./transformer-BI1dF8s7.cjs');
|
|
3
|
+
const require_src = require('./src-DVALVxuy.cjs');
|
|
4
4
|
|
|
5
5
|
//#region src/vite.ts
|
|
6
6
|
var vite_default = require_src.IsolatedDecl.vite;
|
package/dist/vite.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unplugin-isolated-decl",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
4
4
|
"description": "A blazing-fast tool for generating isolated declarations.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -95,28 +95,29 @@
|
|
|
95
95
|
"unplugin": "^2.0.0-beta.1"
|
|
96
96
|
},
|
|
97
97
|
"devDependencies": {
|
|
98
|
-
"@farmfe/core": "^1.4.
|
|
98
|
+
"@farmfe/core": "^1.4.5",
|
|
99
99
|
"@oxc-project/types": "^0.38.0",
|
|
100
100
|
"@rollup/plugin-alias": "^5.1.1",
|
|
101
101
|
"@swc/core": "^1.9.3",
|
|
102
|
-
"@sxzz/eslint-config": "^4.5.
|
|
102
|
+
"@sxzz/eslint-config": "^4.5.1",
|
|
103
103
|
"@sxzz/prettier-config": "^2.0.2",
|
|
104
|
-
"@sxzz/test-utils": "^0.3.
|
|
104
|
+
"@sxzz/test-utils": "^0.3.8",
|
|
105
105
|
"@types/debug": "^4.1.12",
|
|
106
106
|
"@types/node": "^22.10.1",
|
|
107
107
|
"bumpp": "^9.8.1",
|
|
108
108
|
"esbuild": "^0.24.0",
|
|
109
|
-
"eslint": "^9.
|
|
109
|
+
"eslint": "^9.16.0",
|
|
110
110
|
"fast-glob": "^3.3.2",
|
|
111
111
|
"oxc-transform": "^0.38.0",
|
|
112
112
|
"prettier": "^3.4.1",
|
|
113
113
|
"rolldown": "nightly",
|
|
114
|
-
"rollup": "^4.
|
|
114
|
+
"rollup": "^4.28.0",
|
|
115
115
|
"rollup-plugin-esbuild": "^6.1.1",
|
|
116
|
+
"tinyglobby": "^0.2.10",
|
|
116
117
|
"tsdown": "^0.3.1",
|
|
117
118
|
"tsx": "^4.19.2",
|
|
118
119
|
"typescript": "^5.7.2",
|
|
119
|
-
"vite": "^
|
|
120
|
+
"vite": "^6.0.1",
|
|
120
121
|
"vitest": "^2.1.6"
|
|
121
122
|
},
|
|
122
123
|
"engines": {
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/core/transformer.ts
|
|
3
|
-
function tryImport(pkg) {
|
|
4
|
-
try {
|
|
5
|
-
return import(pkg);
|
|
6
|
-
} catch {
|
|
7
|
-
return Promise.resolve(null);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
async function oxcTransform(id, code) {
|
|
11
|
-
const oxc = await tryImport("oxc-transform");
|
|
12
|
-
if (!oxc) return {
|
|
13
|
-
code: "",
|
|
14
|
-
errors: ["oxc-transform is required for transforming TypeScript, please install `oxc-transform`."]
|
|
15
|
-
};
|
|
16
|
-
return oxc.isolatedDeclaration(id, code, { sourcemap: false });
|
|
17
|
-
}
|
|
18
|
-
async function swcTransform(id, code) {
|
|
19
|
-
const swc = await tryImport("@swc/core");
|
|
20
|
-
if (!swc) return {
|
|
21
|
-
code: "",
|
|
22
|
-
errors: ["SWC is required for transforming TypeScript, please install `@swc/core`."]
|
|
23
|
-
};
|
|
24
|
-
try {
|
|
25
|
-
const result = await swc.transform(code, {
|
|
26
|
-
filename: id,
|
|
27
|
-
jsc: {
|
|
28
|
-
parser: {
|
|
29
|
-
syntax: "typescript",
|
|
30
|
-
tsx: false
|
|
31
|
-
},
|
|
32
|
-
experimental: { emitIsolatedDts: true }
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
const output = JSON.parse(result.output);
|
|
36
|
-
return {
|
|
37
|
-
code: output.__swc_isolated_declarations__,
|
|
38
|
-
errors: []
|
|
39
|
-
};
|
|
40
|
-
} catch (error) {
|
|
41
|
-
return {
|
|
42
|
-
code: "",
|
|
43
|
-
errors: [error.toString()]
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async function tsTransform(id, code, transformOptions) {
|
|
48
|
-
const ts = await tryImport("typescript");
|
|
49
|
-
if (!ts) return {
|
|
50
|
-
code: "",
|
|
51
|
-
errors: ["TypeScript is required for transforming TypeScript, please install `typescript`."]
|
|
52
|
-
};
|
|
53
|
-
if (!ts.transpileDeclaration) return {
|
|
54
|
-
code: "",
|
|
55
|
-
errors: ["TypeScript version is too low, please upgrade to TypeScript 5.5.2+."]
|
|
56
|
-
};
|
|
57
|
-
const { outputText, diagnostics } = ts.transpileDeclaration(code, {
|
|
58
|
-
fileName: id,
|
|
59
|
-
reportDiagnostics: true,
|
|
60
|
-
...transformOptions
|
|
61
|
-
});
|
|
62
|
-
const errors = diagnostics?.length ? [ts.formatDiagnostics(diagnostics, {
|
|
63
|
-
getCanonicalFileName: (fileName) => ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(),
|
|
64
|
-
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
65
|
-
getNewLine: () => ts.sys.newLine
|
|
66
|
-
})] : [];
|
|
67
|
-
return {
|
|
68
|
-
code: outputText,
|
|
69
|
-
errors
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
//#endregion
|
|
74
|
-
export { oxcTransform, swcTransform, tsTransform };
|