unplugin-isolated-decl 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/api.cjs +18 -11
- package/dist/api.d.cts +1 -1
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/core/options.d.cts +7 -6
- package/dist/core/options.d.ts +7 -6
- package/dist/core/transformer.d.cts +1 -1
- package/dist/core/transformer.d.ts +1 -1
- package/dist/esbuild.cjs +4 -7
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +8 -4
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/rolldown.cjs +4 -7
- package/dist/rolldown.d.cts +1 -1
- package/dist/rolldown.d.ts +1 -1
- package/dist/rolldown.js +2 -2
- package/dist/rollup.cjs +4 -7
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +2 -2
- package/dist/src_index-2hhWj0Ml.js +146 -0
- package/dist/src_index-XND_hrJh.cjs +152 -0
- package/dist/{transformer-m4c_uotZ.js → transformer-ScZEjI0h.js} +11 -11
- package/dist/{transformer-ZIY9xt38.cjs → transformer-oh0-JTHH.cjs} +34 -27
- package/dist/vite.cjs +4 -7
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +2 -2
- package/package.json +14 -14
- package/dist/src_index-JrSKjSmw.cjs +0 -132
- package/dist/src_index-OeLmVd0z.js +0 -127
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
|
-
- 🚀 **Fast**:
|
|
9
|
+
- 🚀 **Fast**: Generates `.d.ts` files significantly faster than `tsc`.
|
|
10
10
|
- 🎨 **Transformer**: Support Oxc, SWC, and TypeScript transformer.
|
|
11
11
|
- 📦 **Zero Config**: No configuration required, works out of the box.
|
|
12
12
|
- ✨ **Bundler Support**: Works with Vite, Rollup, and esbuild.
|
|
@@ -89,6 +89,7 @@ export interface Options {
|
|
|
89
89
|
transformer?: 'oxc' | 'swc' | 'typescript'
|
|
90
90
|
/** Only for typescript transformer */
|
|
91
91
|
transformOptions?: TranspileOptions
|
|
92
|
+
ignoreErrors?: boolean
|
|
92
93
|
}
|
|
93
94
|
```
|
|
94
95
|
|
package/dist/api.cjs
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
const require_transformer = require('./transformer-oh0-JTHH.cjs');
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
oxcTransform: () => oxcTransform,
|
|
9
|
-
swcTransform: () => swcTransform,
|
|
10
|
-
tsTransform: () => tsTransform
|
|
4
|
+
Object.defineProperty(exports, 'oxcTransform', {
|
|
5
|
+
enumerable: true,
|
|
6
|
+
get: function () {
|
|
7
|
+
return require_transformer.oxcTransform;
|
|
8
|
+
}
|
|
11
9
|
});
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
Object.defineProperty(exports, 'swcTransform', {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return require_transformer.swcTransform;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(exports, 'tsTransform', {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () {
|
|
19
|
+
return require_transformer.tsTransform;
|
|
20
|
+
}
|
|
21
|
+
});
|
package/dist/api.d.cts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { oxcTransform, swcTransform, tsTransform } from
|
|
1
|
+
export { oxcTransform, swcTransform, tsTransform } from "./core/transformer";
|
package/dist/api.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { oxcTransform, swcTransform, tsTransform } from
|
|
1
|
+
export { oxcTransform, swcTransform, tsTransform } from "./core/transformer";
|
package/dist/api.js
CHANGED
package/dist/core/options.d.cts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { FilterPattern } from "@rollup/pluginutils";
|
|
2
|
+
import type { TranspileOptions } from "typescript";
|
|
3
3
|
export type Options = {
|
|
4
4
|
include?: FilterPattern;
|
|
5
5
|
exclude?: FilterPattern;
|
|
6
|
-
enforce?:
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
enforce?: "pre" | "post" | undefined;
|
|
7
|
+
ignoreErrors?: boolean;
|
|
8
|
+
} & ({transformer?: "oxc" | "swc"} | {
|
|
9
|
+
transformer: "typescript";
|
|
9
10
|
transformOptions?: TranspileOptions;
|
|
10
11
|
});
|
|
11
12
|
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
12
|
-
export type OptionsResolved = Overwrite<Required<Options>, Pick<Options,
|
|
13
|
+
export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, "enforce">>;
|
|
13
14
|
export declare function resolveOptions(options: Options): OptionsResolved;
|
|
14
15
|
export {};
|
package/dist/core/options.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { FilterPattern } from "@rollup/pluginutils";
|
|
2
|
+
import type { TranspileOptions } from "typescript";
|
|
3
3
|
export type Options = {
|
|
4
4
|
include?: FilterPattern;
|
|
5
5
|
exclude?: FilterPattern;
|
|
6
|
-
enforce?:
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
enforce?: "pre" | "post" | undefined;
|
|
7
|
+
ignoreErrors?: boolean;
|
|
8
|
+
} & ({transformer?: "oxc" | "swc"} | {
|
|
9
|
+
transformer: "typescript";
|
|
9
10
|
transformOptions?: TranspileOptions;
|
|
10
11
|
});
|
|
11
12
|
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
12
|
-
export type OptionsResolved = Overwrite<Required<Options>, Pick<Options,
|
|
13
|
+
export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, "enforce">>;
|
|
13
14
|
export declare function resolveOptions(options: Options): OptionsResolved;
|
|
14
15
|
export {};
|
package/dist/esbuild.cjs
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const { IsolatedDecl } = require("./src_index-JrSKjSmw.cjs");
|
|
2
|
+
const require_transformer = require('./transformer-oh0-JTHH.cjs');
|
|
3
|
+
const require_src_index = require('./src_index-XND_hrJh.cjs');
|
|
5
4
|
|
|
6
5
|
//#region src/esbuild.ts
|
|
7
|
-
var
|
|
8
|
-
__export(esbuild_ns, {default: () => esbuild_default});
|
|
9
|
-
var esbuild_default = IsolatedDecl.esbuild;
|
|
6
|
+
var esbuild_default = require_src_index.IsolatedDecl.esbuild;
|
|
10
7
|
|
|
11
8
|
//#endregion
|
|
12
|
-
module.exports =
|
|
9
|
+
module.exports = esbuild_default;
|
package/dist/esbuild.d.cts
CHANGED
package/dist/esbuild.d.ts
CHANGED
package/dist/esbuild.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
const require_transformer = require('./transformer-oh0-JTHH.cjs');
|
|
3
|
+
const require_src_index = require('./src_index-XND_hrJh.cjs');
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
Object.defineProperty(exports, 'IsolatedDecl', {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function () {
|
|
8
|
+
return require_src_index.IsolatedDecl;
|
|
9
|
+
}
|
|
10
|
+
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type UnpluginInstance } from
|
|
2
|
-
import { type Options } from
|
|
1
|
+
import { type UnpluginInstance } from "unplugin";
|
|
2
|
+
import { type Options } from "./core/options";
|
|
3
3
|
export type { Options };
|
|
4
4
|
export declare const IsolatedDecl: UnpluginInstance<Options | undefined, false>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type UnpluginInstance } from
|
|
2
|
-
import { type Options } from
|
|
1
|
+
import { type UnpluginInstance } from "unplugin";
|
|
2
|
+
import { type Options } from "./core/options";
|
|
3
3
|
export type { Options };
|
|
4
4
|
export declare const IsolatedDecl: UnpluginInstance<Options | undefined, false>;
|
package/dist/index.js
CHANGED
package/dist/rolldown.cjs
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const { IsolatedDecl } = require("./src_index-JrSKjSmw.cjs");
|
|
2
|
+
const require_transformer = require('./transformer-oh0-JTHH.cjs');
|
|
3
|
+
const require_src_index = require('./src_index-XND_hrJh.cjs');
|
|
5
4
|
|
|
6
5
|
//#region src/rolldown.ts
|
|
7
|
-
var
|
|
8
|
-
__export(rolldown_ns, {default: () => rolldown_default});
|
|
9
|
-
var rolldown_default = IsolatedDecl.rolldown;
|
|
6
|
+
var rolldown_default = require_src_index.IsolatedDecl.rolldown;
|
|
10
7
|
|
|
11
8
|
//#endregion
|
|
12
|
-
module.exports =
|
|
9
|
+
module.exports = rolldown_default;
|
package/dist/rolldown.d.cts
CHANGED
package/dist/rolldown.d.ts
CHANGED
package/dist/rolldown.js
CHANGED
package/dist/rollup.cjs
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const { IsolatedDecl } = require("./src_index-JrSKjSmw.cjs");
|
|
2
|
+
const require_transformer = require('./transformer-oh0-JTHH.cjs');
|
|
3
|
+
const require_src_index = require('./src_index-XND_hrJh.cjs');
|
|
5
4
|
|
|
6
5
|
//#region src/rollup.ts
|
|
7
|
-
var
|
|
8
|
-
__export(rollup_ns, {default: () => rollup_default});
|
|
9
|
-
var rollup_default = IsolatedDecl.rollup;
|
|
6
|
+
var rollup_default = require_src_index.IsolatedDecl.rollup;
|
|
10
7
|
|
|
11
8
|
//#endregion
|
|
12
|
-
module.exports =
|
|
9
|
+
module.exports = rollup_default;
|
package/dist/rollup.d.cts
CHANGED
package/dist/rollup.d.ts
CHANGED
package/dist/rollup.js
CHANGED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { oxcTransform, swcTransform, tsTransform } from "./transformer-ScZEjI0h.js";
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { default as path } from "node:path";
|
|
4
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
5
|
+
import { createUnplugin } from "unplugin";
|
|
6
|
+
|
|
7
|
+
//#region src/core/options.ts
|
|
8
|
+
function resolveOptions(options) {
|
|
9
|
+
return {
|
|
10
|
+
include: options.include || [/\.[cm]?ts$/],
|
|
11
|
+
exclude: options.exclude || [/node_modules/],
|
|
12
|
+
enforce: "enforce" in options ? options.enforce : "pre",
|
|
13
|
+
transformer: options.transformer || "oxc",
|
|
14
|
+
ignoreErrors: options.ignoreErrors || false
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/index.ts
|
|
20
|
+
const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
21
|
+
const options = resolveOptions(rawOptions);
|
|
22
|
+
const filter = createFilter(options.include, options.exclude);
|
|
23
|
+
const outputFiles = {};
|
|
24
|
+
function addOutput(filename, source) {
|
|
25
|
+
outputFiles[filename.replace(/\.(.?)[jt]s$/, "")] = source;
|
|
26
|
+
}
|
|
27
|
+
const rollup = { renderStart(outputOptions, inputOptions) {
|
|
28
|
+
let outBase = "";
|
|
29
|
+
let input = inputOptions.input;
|
|
30
|
+
input = typeof input === "string" ? [input] : input;
|
|
31
|
+
if (Array.isArray(input)) {
|
|
32
|
+
outBase = lowestCommonAncestor(...input);
|
|
33
|
+
}
|
|
34
|
+
if (typeof outputOptions.entryFileNames !== "string") {
|
|
35
|
+
return this.error("entryFileNames must be a string");
|
|
36
|
+
}
|
|
37
|
+
const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ""}ts`);
|
|
38
|
+
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
39
|
+
this.emitFile({
|
|
40
|
+
type: "asset",
|
|
41
|
+
fileName: entryFileNames.replace("[name]", path.relative(outBase, filename)),
|
|
42
|
+
source
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
} };
|
|
46
|
+
return {
|
|
47
|
+
name: "unplugin-isolated-decl",
|
|
48
|
+
transformInclude(id) {
|
|
49
|
+
return filter(id);
|
|
50
|
+
},
|
|
51
|
+
async transform(code, id) {
|
|
52
|
+
let result;
|
|
53
|
+
switch (options.transformer) {
|
|
54
|
+
case "oxc":
|
|
55
|
+
result = oxcTransform(id, code);
|
|
56
|
+
break;
|
|
57
|
+
case "swc":
|
|
58
|
+
result = await swcTransform(id, code);
|
|
59
|
+
break;
|
|
60
|
+
case "typescript": result = await tsTransform(id, code, options.transformOptions);
|
|
61
|
+
}
|
|
62
|
+
const { sourceText, errors } = result;
|
|
63
|
+
if (errors.length) {
|
|
64
|
+
if (options.ignoreErrors) {
|
|
65
|
+
this.warn(errors[0]);
|
|
66
|
+
} else {
|
|
67
|
+
this.error(errors[0]);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
addOutput(id, sourceText);
|
|
72
|
+
},
|
|
73
|
+
esbuild: { setup(build) {
|
|
74
|
+
build.onEnd(async (result) => {
|
|
75
|
+
const esbuildOptions = build.initialOptions;
|
|
76
|
+
const entries = esbuildOptions.entryPoints;
|
|
77
|
+
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
|
|
78
|
+
const outBase = lowestCommonAncestor(...entries);
|
|
79
|
+
const jsExt = esbuildOptions.outExtension?.[".js"];
|
|
80
|
+
let outExt;
|
|
81
|
+
switch (jsExt) {
|
|
82
|
+
case ".cjs":
|
|
83
|
+
outExt = "cts";
|
|
84
|
+
break;
|
|
85
|
+
case ".mjs":
|
|
86
|
+
outExt = "mts";
|
|
87
|
+
break;
|
|
88
|
+
default:
|
|
89
|
+
outExt = "ts";
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
if (build.initialOptions.write ?? true) {
|
|
93
|
+
if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
|
|
94
|
+
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
95
|
+
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
96
|
+
const filePath = path.resolve(build.initialOptions.outdir, outFile);
|
|
97
|
+
await mkdir(path.dirname(filePath), { recursive: true });
|
|
98
|
+
await writeFile(filePath, source);
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
result.outputFiles ||= [];
|
|
102
|
+
const textEncoder = new TextEncoder();
|
|
103
|
+
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
104
|
+
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
105
|
+
result.outputFiles.push({
|
|
106
|
+
path: outFile,
|
|
107
|
+
contents: textEncoder.encode(source),
|
|
108
|
+
hash: "",
|
|
109
|
+
text: source
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
} },
|
|
115
|
+
rollup,
|
|
116
|
+
rolldown: rollup,
|
|
117
|
+
vite: {
|
|
118
|
+
apply: "build",
|
|
119
|
+
enforce: "pre",
|
|
120
|
+
...rollup
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
function lowestCommonAncestor(...filepaths) {
|
|
125
|
+
if (filepaths.length === 0) return "";
|
|
126
|
+
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
127
|
+
const [first, ...rest] = filepaths;
|
|
128
|
+
let ancestor = first.split(path.sep);
|
|
129
|
+
for (const filepath of rest) {
|
|
130
|
+
const directories = filepath.split(path.sep, ancestor.length);
|
|
131
|
+
let index = 0;
|
|
132
|
+
for (const directory of directories) {
|
|
133
|
+
if (directory === ancestor[index]) {
|
|
134
|
+
index += 1;
|
|
135
|
+
} else {
|
|
136
|
+
ancestor = ancestor.slice(0, index);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
ancestor = ancestor.slice(0, index);
|
|
141
|
+
}
|
|
142
|
+
return ancestor.length <= 1 && ancestor[0] === "" ? path.sep + ancestor[0] : ancestor.join(path.sep);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
//#endregion
|
|
146
|
+
export { IsolatedDecl };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_transformer = require('./transformer-oh0-JTHH.cjs');
|
|
3
|
+
const { mkdir, writeFile } = __toESM(require("node:fs/promises"));
|
|
4
|
+
const { default: path } = __toESM(require("node:path"));
|
|
5
|
+
const { createFilter } = __toESM(require("@rollup/pluginutils"));
|
|
6
|
+
const { createUnplugin } = __toESM(require("unplugin"));
|
|
7
|
+
|
|
8
|
+
//#region src/core/options.ts
|
|
9
|
+
function resolveOptions(options) {
|
|
10
|
+
return {
|
|
11
|
+
include: options.include || [/\.[cm]?ts$/],
|
|
12
|
+
exclude: options.exclude || [/node_modules/],
|
|
13
|
+
enforce: "enforce" in options ? options.enforce : "pre",
|
|
14
|
+
transformer: options.transformer || "oxc",
|
|
15
|
+
ignoreErrors: options.ignoreErrors || false
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/index.ts
|
|
21
|
+
const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
22
|
+
const options = resolveOptions(rawOptions);
|
|
23
|
+
const filter = createFilter(options.include, options.exclude);
|
|
24
|
+
const outputFiles = {};
|
|
25
|
+
function addOutput(filename, source) {
|
|
26
|
+
outputFiles[filename.replace(/\.(.?)[jt]s$/, "")] = source;
|
|
27
|
+
}
|
|
28
|
+
const rollup = { renderStart(outputOptions, inputOptions) {
|
|
29
|
+
let outBase = "";
|
|
30
|
+
let input = inputOptions.input;
|
|
31
|
+
input = typeof input === "string" ? [input] : input;
|
|
32
|
+
if (Array.isArray(input)) {
|
|
33
|
+
outBase = lowestCommonAncestor(...input);
|
|
34
|
+
}
|
|
35
|
+
if (typeof outputOptions.entryFileNames !== "string") {
|
|
36
|
+
return this.error("entryFileNames must be a string");
|
|
37
|
+
}
|
|
38
|
+
const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ""}ts`);
|
|
39
|
+
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
40
|
+
this.emitFile({
|
|
41
|
+
type: "asset",
|
|
42
|
+
fileName: entryFileNames.replace("[name]", path.relative(outBase, filename)),
|
|
43
|
+
source
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
} };
|
|
47
|
+
return {
|
|
48
|
+
name: "unplugin-isolated-decl",
|
|
49
|
+
transformInclude(id) {
|
|
50
|
+
return filter(id);
|
|
51
|
+
},
|
|
52
|
+
async transform(code, id) {
|
|
53
|
+
let result;
|
|
54
|
+
switch (options.transformer) {
|
|
55
|
+
case "oxc":
|
|
56
|
+
result = require_transformer.oxcTransform(id, code);
|
|
57
|
+
break;
|
|
58
|
+
case "swc":
|
|
59
|
+
result = await require_transformer.swcTransform(id, code);
|
|
60
|
+
break;
|
|
61
|
+
case "typescript": result = await require_transformer.tsTransform(id, code, options.transformOptions);
|
|
62
|
+
}
|
|
63
|
+
const { sourceText, errors } = result;
|
|
64
|
+
if (errors.length) {
|
|
65
|
+
if (options.ignoreErrors) {
|
|
66
|
+
this.warn(errors[0]);
|
|
67
|
+
} else {
|
|
68
|
+
this.error(errors[0]);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
addOutput(id, sourceText);
|
|
73
|
+
},
|
|
74
|
+
esbuild: { setup(build) {
|
|
75
|
+
build.onEnd(async (result) => {
|
|
76
|
+
const esbuildOptions = build.initialOptions;
|
|
77
|
+
const entries = esbuildOptions.entryPoints;
|
|
78
|
+
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
|
|
79
|
+
const outBase = lowestCommonAncestor(...entries);
|
|
80
|
+
const jsExt = esbuildOptions.outExtension?.[".js"];
|
|
81
|
+
let outExt;
|
|
82
|
+
switch (jsExt) {
|
|
83
|
+
case ".cjs":
|
|
84
|
+
outExt = "cts";
|
|
85
|
+
break;
|
|
86
|
+
case ".mjs":
|
|
87
|
+
outExt = "mts";
|
|
88
|
+
break;
|
|
89
|
+
default:
|
|
90
|
+
outExt = "ts";
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
if (build.initialOptions.write ?? true) {
|
|
94
|
+
if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
|
|
95
|
+
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
96
|
+
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
97
|
+
const filePath = path.resolve(build.initialOptions.outdir, outFile);
|
|
98
|
+
await mkdir(path.dirname(filePath), { recursive: true });
|
|
99
|
+
await writeFile(filePath, source);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
result.outputFiles ||= [];
|
|
103
|
+
const textEncoder = new TextEncoder();
|
|
104
|
+
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
105
|
+
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
106
|
+
result.outputFiles.push({
|
|
107
|
+
path: outFile,
|
|
108
|
+
contents: textEncoder.encode(source),
|
|
109
|
+
hash: "",
|
|
110
|
+
text: source
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
} },
|
|
116
|
+
rollup,
|
|
117
|
+
rolldown: rollup,
|
|
118
|
+
vite: {
|
|
119
|
+
apply: "build",
|
|
120
|
+
enforce: "pre",
|
|
121
|
+
...rollup
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
function lowestCommonAncestor(...filepaths) {
|
|
126
|
+
if (filepaths.length === 0) return "";
|
|
127
|
+
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
128
|
+
const [first, ...rest] = filepaths;
|
|
129
|
+
let ancestor = first.split(path.sep);
|
|
130
|
+
for (const filepath of rest) {
|
|
131
|
+
const directories = filepath.split(path.sep, ancestor.length);
|
|
132
|
+
let index = 0;
|
|
133
|
+
for (const directory of directories) {
|
|
134
|
+
if (directory === ancestor[index]) {
|
|
135
|
+
index += 1;
|
|
136
|
+
} else {
|
|
137
|
+
ancestor = ancestor.slice(0, index);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
ancestor = ancestor.slice(0, index);
|
|
142
|
+
}
|
|
143
|
+
return ancestor.length <= 1 && ancestor[0] === "" ? path.sep + ancestor[0] : ancestor.join(path.sep);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
Object.defineProperty(exports, 'IsolatedDecl', {
|
|
148
|
+
enumerable: true,
|
|
149
|
+
get: function () {
|
|
150
|
+
return IsolatedDecl;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
@@ -7,11 +7,11 @@ function oxcTransform(id, code) {
|
|
|
7
7
|
async function swcTransform(id, code) {
|
|
8
8
|
let swc;
|
|
9
9
|
try {
|
|
10
|
-
swc = await import(
|
|
10
|
+
swc = await import("@swc/core");
|
|
11
11
|
} catch {
|
|
12
12
|
return {
|
|
13
|
-
sourceText:
|
|
14
|
-
errors: [
|
|
13
|
+
sourceText: "",
|
|
14
|
+
errors: ["SWC is required for transforming TypeScript, please install `@swc/core`.",]
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
17
|
try {
|
|
@@ -19,10 +19,10 @@ async function swcTransform(id, code) {
|
|
|
19
19
|
filename: id,
|
|
20
20
|
jsc: {
|
|
21
21
|
parser: {
|
|
22
|
-
syntax:
|
|
22
|
+
syntax: "typescript",
|
|
23
23
|
tsx: false
|
|
24
24
|
},
|
|
25
|
-
experimental: {emitIsolatedDts: true}
|
|
25
|
+
experimental: { emitIsolatedDts: true }
|
|
26
26
|
}
|
|
27
27
|
});
|
|
28
28
|
const output = JSON.parse(result.output);
|
|
@@ -32,7 +32,7 @@ async function swcTransform(id, code) {
|
|
|
32
32
|
};
|
|
33
33
|
} catch (error) {
|
|
34
34
|
return {
|
|
35
|
-
sourceText:
|
|
35
|
+
sourceText: "",
|
|
36
36
|
errors: [error.toString()]
|
|
37
37
|
};
|
|
38
38
|
}
|
|
@@ -40,17 +40,17 @@ async function swcTransform(id, code) {
|
|
|
40
40
|
async function tsTransform(id, code, transformOptions) {
|
|
41
41
|
let ts;
|
|
42
42
|
try {
|
|
43
|
-
ts = await import(
|
|
43
|
+
ts = await import("typescript");
|
|
44
44
|
} catch {
|
|
45
45
|
return {
|
|
46
|
-
sourceText:
|
|
47
|
-
errors: [
|
|
46
|
+
sourceText: "",
|
|
47
|
+
errors: ["TypeScript is required for transforming TypeScript, please install `typescript`.",]
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
if (!ts.transpileDeclaration) {
|
|
51
51
|
return {
|
|
52
|
-
sourceText:
|
|
53
|
-
errors: [
|
|
52
|
+
sourceText: "",
|
|
53
|
+
errors: ["TypeScript version is too low, please upgrade to TypeScript 5.5.2+.",]
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
56
|
const { outputText, diagnostics } = ts.transpileDeclaration(code, {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
2
|
//#region rolldown:runtime
|
|
4
3
|
var __create = Object.create;
|
|
5
4
|
var __defProp = Object.defineProperty;
|
|
@@ -7,14 +6,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
7
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
9
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __export = (target, all) => {
|
|
11
|
-
for (var name in all) __defProp(target, name, {
|
|
12
|
-
get: all[name],
|
|
13
|
-
enumerable: true
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
9
|
var __copyProps = (to, from, except, desc) => {
|
|
17
|
-
if (from && typeof from ===
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
18
11
|
key = keys[i];
|
|
19
12
|
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
20
13
|
get: ((k) => from[k]).bind(null, key),
|
|
@@ -23,11 +16,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
23
16
|
}
|
|
24
17
|
return to;
|
|
25
18
|
};
|
|
26
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target,
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
27
20
|
value: mod,
|
|
28
21
|
enumerable: true
|
|
29
22
|
}) : target, mod));
|
|
30
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, '__esModule', {value: true}), mod);
|
|
31
23
|
|
|
32
24
|
//#endregion
|
|
33
25
|
const { isolatedDeclaration } = __toESM(require("oxc-transform"));
|
|
@@ -39,11 +31,11 @@ function oxcTransform(id, code) {
|
|
|
39
31
|
async function swcTransform(id, code) {
|
|
40
32
|
let swc;
|
|
41
33
|
try {
|
|
42
|
-
swc = await import(
|
|
34
|
+
swc = await import("@swc/core");
|
|
43
35
|
} catch {
|
|
44
36
|
return {
|
|
45
|
-
sourceText:
|
|
46
|
-
errors: [
|
|
37
|
+
sourceText: "",
|
|
38
|
+
errors: ["SWC is required for transforming TypeScript, please install `@swc/core`.",]
|
|
47
39
|
};
|
|
48
40
|
}
|
|
49
41
|
try {
|
|
@@ -51,10 +43,10 @@ async function swcTransform(id, code) {
|
|
|
51
43
|
filename: id,
|
|
52
44
|
jsc: {
|
|
53
45
|
parser: {
|
|
54
|
-
syntax:
|
|
46
|
+
syntax: "typescript",
|
|
55
47
|
tsx: false
|
|
56
48
|
},
|
|
57
|
-
experimental: {emitIsolatedDts: true}
|
|
49
|
+
experimental: { emitIsolatedDts: true }
|
|
58
50
|
}
|
|
59
51
|
});
|
|
60
52
|
const output = JSON.parse(result.output);
|
|
@@ -64,7 +56,7 @@ async function swcTransform(id, code) {
|
|
|
64
56
|
};
|
|
65
57
|
} catch (error) {
|
|
66
58
|
return {
|
|
67
|
-
sourceText:
|
|
59
|
+
sourceText: "",
|
|
68
60
|
errors: [error.toString()]
|
|
69
61
|
};
|
|
70
62
|
}
|
|
@@ -72,17 +64,17 @@ async function swcTransform(id, code) {
|
|
|
72
64
|
async function tsTransform(id, code, transformOptions) {
|
|
73
65
|
let ts;
|
|
74
66
|
try {
|
|
75
|
-
ts = await import(
|
|
67
|
+
ts = await import("typescript");
|
|
76
68
|
} catch {
|
|
77
69
|
return {
|
|
78
|
-
sourceText:
|
|
79
|
-
errors: [
|
|
70
|
+
sourceText: "",
|
|
71
|
+
errors: ["TypeScript is required for transforming TypeScript, please install `typescript`.",]
|
|
80
72
|
};
|
|
81
73
|
}
|
|
82
74
|
if (!ts.transpileDeclaration) {
|
|
83
75
|
return {
|
|
84
|
-
sourceText:
|
|
85
|
-
errors: [
|
|
76
|
+
sourceText: "",
|
|
77
|
+
errors: ["TypeScript version is too low, please upgrade to TypeScript 5.5.2+.",]
|
|
86
78
|
};
|
|
87
79
|
}
|
|
88
80
|
const { outputText, diagnostics } = ts.transpileDeclaration(code, {
|
|
@@ -102,9 +94,24 @@ async function tsTransform(id, code, transformOptions) {
|
|
|
102
94
|
}
|
|
103
95
|
|
|
104
96
|
//#endregion
|
|
105
|
-
exports
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
exports
|
|
97
|
+
Object.defineProperty(exports, '__toESM', {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
get: function () {
|
|
100
|
+
return __toESM;
|
|
101
|
+
}
|
|
102
|
+
});Object.defineProperty(exports, 'oxcTransform', {
|
|
103
|
+
enumerable: true,
|
|
104
|
+
get: function () {
|
|
105
|
+
return oxcTransform;
|
|
106
|
+
}
|
|
107
|
+
});Object.defineProperty(exports, 'swcTransform', {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
get: function () {
|
|
110
|
+
return swcTransform;
|
|
111
|
+
}
|
|
112
|
+
});Object.defineProperty(exports, 'tsTransform', {
|
|
113
|
+
enumerable: true,
|
|
114
|
+
get: function () {
|
|
115
|
+
return tsTransform;
|
|
116
|
+
}
|
|
117
|
+
});
|
package/dist/vite.cjs
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const { IsolatedDecl } = require("./src_index-JrSKjSmw.cjs");
|
|
2
|
+
const require_transformer = require('./transformer-oh0-JTHH.cjs');
|
|
3
|
+
const require_src_index = require('./src_index-XND_hrJh.cjs');
|
|
5
4
|
|
|
6
5
|
//#region src/vite.ts
|
|
7
|
-
var
|
|
8
|
-
__export(vite_ns, {default: () => vite_default});
|
|
9
|
-
var vite_default = IsolatedDecl.vite;
|
|
6
|
+
var vite_default = require_src_index.IsolatedDecl.vite;
|
|
10
7
|
|
|
11
8
|
//#endregion
|
|
12
|
-
module.exports =
|
|
9
|
+
module.exports = vite_default;
|
package/dist/vite.d.cts
CHANGED
package/dist/vite.d.ts
CHANGED
package/dist/vite.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unplugin-isolated-decl",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "A blazing-fast tool for generating isolated declarations, powered by Oxc.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -81,25 +81,25 @@
|
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
83
|
"@rollup/pluginutils": "^5.1.0",
|
|
84
|
-
"oxc-transform": "^0.
|
|
85
|
-
"unplugin": "^1.
|
|
84
|
+
"oxc-transform": "^0.23.0",
|
|
85
|
+
"unplugin": "^1.12.1"
|
|
86
86
|
},
|
|
87
87
|
"devDependencies": {
|
|
88
|
-
"@swc/core": "^1.
|
|
89
|
-
"@sxzz/eslint-config": "^3.
|
|
88
|
+
"@swc/core": "^1.7.6",
|
|
89
|
+
"@sxzz/eslint-config": "^3.17.0",
|
|
90
90
|
"@sxzz/prettier-config": "^2.0.2",
|
|
91
|
-
"@types/node": "^20.14.
|
|
92
|
-
"bumpp": "^9.4.
|
|
91
|
+
"@types/node": "^20.14.14",
|
|
92
|
+
"bumpp": "^9.4.2",
|
|
93
93
|
"esbuild": "^0.23.0",
|
|
94
|
-
"eslint": "^9.
|
|
94
|
+
"eslint": "^9.8.0",
|
|
95
95
|
"fast-glob": "^3.3.2",
|
|
96
|
-
"prettier": "^3.3.
|
|
97
|
-
"rollup": "^4.
|
|
96
|
+
"prettier": "^3.3.3",
|
|
97
|
+
"rollup": "^4.20.0",
|
|
98
98
|
"tsdown": "^0.2.3",
|
|
99
|
-
"tsx": "^4.16.
|
|
100
|
-
"typescript": "^5.5.
|
|
101
|
-
"vite": "^5.
|
|
102
|
-
"vitest": "^
|
|
99
|
+
"tsx": "^4.16.5",
|
|
100
|
+
"typescript": "^5.5.4",
|
|
101
|
+
"vite": "^5.4.0",
|
|
102
|
+
"vitest": "^2.0.5"
|
|
103
103
|
},
|
|
104
104
|
"engines": {
|
|
105
105
|
"node": ">=18.12.0"
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { __export, __toESM, oxcTransform, swcTransform, tsTransform } = require("./transformer-ZIY9xt38.cjs");
|
|
4
|
-
const { default: path } = __toESM(require("node:path"));
|
|
5
|
-
const { mkdir, writeFile } = __toESM(require("node:fs/promises"));
|
|
6
|
-
const { createUnplugin } = __toESM(require("unplugin"));
|
|
7
|
-
const { createFilter } = __toESM(require("@rollup/pluginutils"));
|
|
8
|
-
|
|
9
|
-
//#region src/core/options.ts
|
|
10
|
-
function resolveOptions(options) {
|
|
11
|
-
return {
|
|
12
|
-
include: options.include || [/\.[cm]?ts$/],
|
|
13
|
-
exclude: options.exclude || [/node_modules/],
|
|
14
|
-
enforce: 'enforce'in options ? options.enforce : 'pre',
|
|
15
|
-
transformer: options.transformer || 'oxc'
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region src/index.ts
|
|
21
|
-
var src_index_ns = {};
|
|
22
|
-
__export(src_index_ns, {IsolatedDecl: () => IsolatedDecl});
|
|
23
|
-
const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
|
|
24
|
-
const options = resolveOptions(rawOptions);
|
|
25
|
-
const filter = createFilter(options.include, options.exclude);
|
|
26
|
-
const outputFiles = {};
|
|
27
|
-
function addOutput(filename, source) {
|
|
28
|
-
outputFiles[filename.replace(/\.(.?)[jt]s$/, '')] = source;
|
|
29
|
-
}
|
|
30
|
-
const rollup = {renderStart(outputOptions, inputOptions) {
|
|
31
|
-
let outBase = '';
|
|
32
|
-
let input = inputOptions.input;
|
|
33
|
-
input = typeof input === 'string' ? [input] : input;
|
|
34
|
-
if (Array.isArray(input)) {
|
|
35
|
-
outBase = lowestCommonAncestor(...input);
|
|
36
|
-
}
|
|
37
|
-
if (typeof outputOptions.entryFileNames !== 'string') {
|
|
38
|
-
return this.error('entryFileNames must be a string');
|
|
39
|
-
}
|
|
40
|
-
const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ''}ts`);
|
|
41
|
-
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
42
|
-
this.emitFile({
|
|
43
|
-
type: 'asset',
|
|
44
|
-
fileName: entryFileNames.replace('[name]', path.relative(outBase, filename)),
|
|
45
|
-
source
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}};
|
|
49
|
-
return {
|
|
50
|
-
name: 'unplugin-isolated-decl',
|
|
51
|
-
transformInclude(id) {
|
|
52
|
-
return filter(id);
|
|
53
|
-
},
|
|
54
|
-
async transform(code, id) {
|
|
55
|
-
let result;
|
|
56
|
-
switch (options.transformer) {
|
|
57
|
-
case 'oxc':
|
|
58
|
-
result = oxcTransform(id, code);
|
|
59
|
-
break;
|
|
60
|
-
case 'swc':
|
|
61
|
-
result = await swcTransform(id, code);
|
|
62
|
-
break;
|
|
63
|
-
case 'typescript': result = await tsTransform(id, code, options.transformOptions);
|
|
64
|
-
}
|
|
65
|
-
const { sourceText, errors } = result;
|
|
66
|
-
if (errors.length) {
|
|
67
|
-
this.error(errors[0]);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
addOutput(id, sourceText);
|
|
71
|
-
},
|
|
72
|
-
async buildEnd() {
|
|
73
|
-
if (meta.framework === 'esbuild') {
|
|
74
|
-
const esbuildOptions = meta.build.initialOptions;
|
|
75
|
-
const entries = esbuildOptions.entryPoints;
|
|
76
|
-
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === 'string'))) throw new Error('unsupported entryPoints, must be an string[]');
|
|
77
|
-
const outBase = lowestCommonAncestor(...entries);
|
|
78
|
-
const jsExt = esbuildOptions.outExtension?.['.js'];
|
|
79
|
-
let outExt;
|
|
80
|
-
switch (jsExt) {
|
|
81
|
-
case '.cjs':
|
|
82
|
-
outExt = 'cts';
|
|
83
|
-
break;
|
|
84
|
-
case '.mjs':
|
|
85
|
-
outExt = 'mts';
|
|
86
|
-
break;
|
|
87
|
-
default:
|
|
88
|
-
outExt = 'ts';
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
const build = meta.build;
|
|
92
|
-
if (build.initialOptions.outdir && (build.initialOptions.write ?? true)) for (const [filename, source] of Object.entries(outputFiles)) {
|
|
93
|
-
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
94
|
-
const filePath = path.resolve(build.initialOptions.outdir, outFile);
|
|
95
|
-
await mkdir(path.dirname(filePath), {recursive: true});
|
|
96
|
-
await writeFile(filePath, source);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
rollup,
|
|
101
|
-
rolldown: rollup,
|
|
102
|
-
vite: {
|
|
103
|
-
apply: 'build',
|
|
104
|
-
enforce: 'pre',
|
|
105
|
-
...rollup
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
});
|
|
109
|
-
function lowestCommonAncestor(...filepaths) {
|
|
110
|
-
if (filepaths.length === 0) return '';
|
|
111
|
-
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
112
|
-
const [first, ...rest] = filepaths;
|
|
113
|
-
let ancestor = first.split(path.sep);
|
|
114
|
-
for (const filepath of rest) {
|
|
115
|
-
const directories = filepath.split(path.sep, ancestor.length);
|
|
116
|
-
let index = 0;
|
|
117
|
-
for (const directory of directories) {
|
|
118
|
-
if (directory === ancestor[index]) {
|
|
119
|
-
index += 1;
|
|
120
|
-
} else {
|
|
121
|
-
ancestor = ancestor.slice(0, index);
|
|
122
|
-
break;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
ancestor = ancestor.slice(0, index);
|
|
126
|
-
}
|
|
127
|
-
return ancestor.length <= 1 && ancestor[0] === '' ? path.sep + ancestor[0] : ancestor.join(path.sep);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
//#endregion
|
|
131
|
-
exports.IsolatedDecl = IsolatedDecl;
|
|
132
|
-
exports.src_index_ns = src_index_ns;
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { oxcTransform, swcTransform, tsTransform } from "./transformer-m4c_uotZ.js";
|
|
2
|
-
import { default as path } from "node:path";
|
|
3
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
4
|
-
import { createUnplugin } from "unplugin";
|
|
5
|
-
import { createFilter } from "@rollup/pluginutils";
|
|
6
|
-
|
|
7
|
-
//#region src/core/options.ts
|
|
8
|
-
function resolveOptions(options) {
|
|
9
|
-
return {
|
|
10
|
-
include: options.include || [/\.[cm]?ts$/],
|
|
11
|
-
exclude: options.exclude || [/node_modules/],
|
|
12
|
-
enforce: 'enforce'in options ? options.enforce : 'pre',
|
|
13
|
-
transformer: options.transformer || 'oxc'
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
//#endregion
|
|
18
|
-
//#region src/index.ts
|
|
19
|
-
const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
|
|
20
|
-
const options = resolveOptions(rawOptions);
|
|
21
|
-
const filter = createFilter(options.include, options.exclude);
|
|
22
|
-
const outputFiles = {};
|
|
23
|
-
function addOutput(filename, source) {
|
|
24
|
-
outputFiles[filename.replace(/\.(.?)[jt]s$/, '')] = source;
|
|
25
|
-
}
|
|
26
|
-
const rollup = {renderStart(outputOptions, inputOptions) {
|
|
27
|
-
let outBase = '';
|
|
28
|
-
let input = inputOptions.input;
|
|
29
|
-
input = typeof input === 'string' ? [input] : input;
|
|
30
|
-
if (Array.isArray(input)) {
|
|
31
|
-
outBase = lowestCommonAncestor(...input);
|
|
32
|
-
}
|
|
33
|
-
if (typeof outputOptions.entryFileNames !== 'string') {
|
|
34
|
-
return this.error('entryFileNames must be a string');
|
|
35
|
-
}
|
|
36
|
-
const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ''}ts`);
|
|
37
|
-
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
38
|
-
this.emitFile({
|
|
39
|
-
type: 'asset',
|
|
40
|
-
fileName: entryFileNames.replace('[name]', path.relative(outBase, filename)),
|
|
41
|
-
source
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}};
|
|
45
|
-
return {
|
|
46
|
-
name: 'unplugin-isolated-decl',
|
|
47
|
-
transformInclude(id) {
|
|
48
|
-
return filter(id);
|
|
49
|
-
},
|
|
50
|
-
async transform(code, id) {
|
|
51
|
-
let result;
|
|
52
|
-
switch (options.transformer) {
|
|
53
|
-
case 'oxc':
|
|
54
|
-
result = oxcTransform(id, code);
|
|
55
|
-
break;
|
|
56
|
-
case 'swc':
|
|
57
|
-
result = await swcTransform(id, code);
|
|
58
|
-
break;
|
|
59
|
-
case 'typescript': result = await tsTransform(id, code, options.transformOptions);
|
|
60
|
-
}
|
|
61
|
-
const { sourceText, errors } = result;
|
|
62
|
-
if (errors.length) {
|
|
63
|
-
this.error(errors[0]);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
addOutput(id, sourceText);
|
|
67
|
-
},
|
|
68
|
-
async buildEnd() {
|
|
69
|
-
if (meta.framework === 'esbuild') {
|
|
70
|
-
const esbuildOptions = meta.build.initialOptions;
|
|
71
|
-
const entries = esbuildOptions.entryPoints;
|
|
72
|
-
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === 'string'))) throw new Error('unsupported entryPoints, must be an string[]');
|
|
73
|
-
const outBase = lowestCommonAncestor(...entries);
|
|
74
|
-
const jsExt = esbuildOptions.outExtension?.['.js'];
|
|
75
|
-
let outExt;
|
|
76
|
-
switch (jsExt) {
|
|
77
|
-
case '.cjs':
|
|
78
|
-
outExt = 'cts';
|
|
79
|
-
break;
|
|
80
|
-
case '.mjs':
|
|
81
|
-
outExt = 'mts';
|
|
82
|
-
break;
|
|
83
|
-
default:
|
|
84
|
-
outExt = 'ts';
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
const build = meta.build;
|
|
88
|
-
if (build.initialOptions.outdir && (build.initialOptions.write ?? true)) for (const [filename, source] of Object.entries(outputFiles)) {
|
|
89
|
-
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
90
|
-
const filePath = path.resolve(build.initialOptions.outdir, outFile);
|
|
91
|
-
await mkdir(path.dirname(filePath), {recursive: true});
|
|
92
|
-
await writeFile(filePath, source);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
rollup,
|
|
97
|
-
rolldown: rollup,
|
|
98
|
-
vite: {
|
|
99
|
-
apply: 'build',
|
|
100
|
-
enforce: 'pre',
|
|
101
|
-
...rollup
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
});
|
|
105
|
-
function lowestCommonAncestor(...filepaths) {
|
|
106
|
-
if (filepaths.length === 0) return '';
|
|
107
|
-
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
108
|
-
const [first, ...rest] = filepaths;
|
|
109
|
-
let ancestor = first.split(path.sep);
|
|
110
|
-
for (const filepath of rest) {
|
|
111
|
-
const directories = filepath.split(path.sep, ancestor.length);
|
|
112
|
-
let index = 0;
|
|
113
|
-
for (const directory of directories) {
|
|
114
|
-
if (directory === ancestor[index]) {
|
|
115
|
-
index += 1;
|
|
116
|
-
} else {
|
|
117
|
-
ancestor = ancestor.slice(0, index);
|
|
118
|
-
break;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
ancestor = ancestor.slice(0, index);
|
|
122
|
-
}
|
|
123
|
-
return ancestor.length <= 1 && ancestor[0] === '' ? path.sep + ancestor[0] : ancestor.join(path.sep);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
//#endregion
|
|
127
|
-
export { IsolatedDecl };
|