unplugin-isolated-decl 0.7.0 → 0.7.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 +20 -2
- package/dist/api.d.cts +1 -1
- package/dist/api.d.ts +1 -1
- package/dist/core/ast.d.cts +7 -0
- package/dist/core/ast.d.ts +7 -0
- package/dist/core/utils.d.cts +6 -0
- package/dist/core/utils.d.ts +6 -0
- package/dist/esbuild.cjs +1 -1
- package/dist/esbuild.d.cts +1 -1
- package/dist/esbuild.d.ts +1 -1
- package/dist/esbuild.js +1 -1
- package/dist/farm.cjs +1 -1
- package/dist/farm.d.cts +1 -1
- package/dist/farm.d.ts +1 -1
- package/dist/farm.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/rolldown.cjs +1 -1
- package/dist/rolldown.d.cts +1 -1
- package/dist/rolldown.d.ts +1 -1
- package/dist/rolldown.js +1 -1
- package/dist/rollup.cjs +1 -1
- package/dist/rollup.d.cts +1 -1
- package/dist/rollup.d.ts +1 -1
- package/dist/rollup.js +1 -1
- package/dist/{src-C3bhcfQ9.cjs → src-CK3yN-QH.cjs} +110 -58
- package/dist/{src-B4aBGZxL.js → src-CrmBEB0d.js} +110 -58
- package/dist/vite.cjs +1 -1
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +1 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# unplugin-isolated-decl [](https://npmjs.com/package/unplugin-isolated-decl)
|
|
1
|
+
# unplugin-isolated-decl [](https://npmjs.com/package/unplugin-isolated-decl) [](https://jsr.io/@unplugin/isolated-decl)
|
|
2
2
|
|
|
3
3
|
[](https://github.com/unplugin/unplugin-isolated-decl/actions/workflows/unit-test.yml)
|
|
4
4
|
|
|
@@ -9,12 +9,16 @@
|
|
|
9
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
|
-
- ✨ **Bundler Support**: Works with Vite, Rollup, and
|
|
12
|
+
- ✨ **Bundler Support**: Works with Vite, Rollup, esbuild and Farm. (PR of Webpack/Rspack support is welcome)
|
|
13
13
|
|
|
14
14
|
## Installation
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
|
+
# npm
|
|
17
18
|
npm i -D unplugin-isolated-decl
|
|
19
|
+
|
|
20
|
+
# jsr
|
|
21
|
+
npx jsr add -D @unplugin/isolated-decl
|
|
18
22
|
```
|
|
19
23
|
|
|
20
24
|
## Usage
|
|
@@ -75,6 +79,20 @@ build({
|
|
|
75
79
|
|
|
76
80
|
<br></details>
|
|
77
81
|
|
|
82
|
+
<details>
|
|
83
|
+
<summary>Farm</summary><br>
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
// farm.config.ts
|
|
87
|
+
import UnpluginIsolatedDecl from 'unplugin-isolated-decl/farm'
|
|
88
|
+
|
|
89
|
+
export default defineConfig({
|
|
90
|
+
plugins: [UnpluginIsolatedDecl()],
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
<br></details>
|
|
95
|
+
|
|
78
96
|
## Options
|
|
79
97
|
|
|
80
98
|
```ts
|
package/dist/api.d.cts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { oxcTransform, swcTransform, tsTransform } from
|
|
1
|
+
export { oxcTransform, swcTransform, tsTransform } from './core/transformer.js';
|
package/dist/api.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { oxcTransform, swcTransform, tsTransform } from
|
|
1
|
+
export { oxcTransform, swcTransform, tsTransform } from './core/transformer.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import MagicString from 'magic-string';
|
|
2
|
+
import type * as OxcTypes from '@oxc-project/types';
|
|
3
|
+
export type OxcImport = (OxcTypes.ImportDeclaration | OxcTypes.ExportAllDeclaration | OxcTypes.ExportNamedDeclaration) & {
|
|
4
|
+
source: OxcTypes.StringLiteral;
|
|
5
|
+
};
|
|
6
|
+
export declare function filterImports(program: OxcTypes.Program): OxcImport[];
|
|
7
|
+
export declare function patchEntryAlias(source: string, s: MagicString | undefined, imports: OxcImport[], outname: string, offset: string): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import MagicString from 'magic-string';
|
|
2
|
+
import type * as OxcTypes from '@oxc-project/types';
|
|
3
|
+
export type OxcImport = (OxcTypes.ImportDeclaration | OxcTypes.ExportAllDeclaration | OxcTypes.ExportNamedDeclaration) & {
|
|
4
|
+
source: OxcTypes.StringLiteral;
|
|
5
|
+
};
|
|
6
|
+
export declare function filterImports(program: OxcTypes.Program): OxcImport[];
|
|
7
|
+
export declare function patchEntryAlias(source: string, s: MagicString | undefined, imports: OxcImport[], outname: string, offset: string): string;
|
package/dist/core/utils.d.cts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
import Debug from 'debug';
|
|
2
|
+
export declare const debug: Debug.Debugger;
|
|
1
3
|
export declare function lowestCommonAncestor(...filepaths: string[]): string;
|
|
2
4
|
export declare function stripExt(filename: string): string;
|
|
5
|
+
export declare function resolveEntry(input: string[] | Record<string, string>): {
|
|
6
|
+
map: Record<string, string> | undefined;
|
|
7
|
+
outBase: string;
|
|
8
|
+
};
|
package/dist/core/utils.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
import Debug from 'debug';
|
|
2
|
+
export declare const debug: Debug.Debugger;
|
|
1
3
|
export declare function lowestCommonAncestor(...filepaths: string[]): string;
|
|
2
4
|
export declare function stripExt(filename: string): string;
|
|
5
|
+
export declare function resolveEntry(input: string[] | Record<string, string>): {
|
|
6
|
+
map: Record<string, string> | undefined;
|
|
7
|
+
outBase: string;
|
|
8
|
+
};
|
package/dist/esbuild.cjs
CHANGED
package/dist/esbuild.d.cts
CHANGED
package/dist/esbuild.d.ts
CHANGED
package/dist/esbuild.js
CHANGED
package/dist/farm.cjs
CHANGED
package/dist/farm.d.cts
CHANGED
package/dist/farm.d.ts
CHANGED
package/dist/farm.js
CHANGED
package/dist/index.cjs
CHANGED
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/rolldown.cjs
CHANGED
package/dist/rolldown.d.cts
CHANGED
package/dist/rolldown.d.ts
CHANGED
package/dist/rolldown.js
CHANGED
package/dist/rollup.cjs
CHANGED
package/dist/rollup.d.cts
CHANGED
package/dist/rollup.d.ts
CHANGED
package/dist/rollup.js
CHANGED
|
@@ -24,31 +24,18 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
//#endregion
|
|
25
25
|
const require_transformer = require('./transformer-C4rLQvdl.cjs');
|
|
26
26
|
const { mkdir, readFile, writeFile } = __toESM(require("node:fs/promises"));
|
|
27
|
-
const { default: path, default: path$1 } = __toESM(require("node:path"));
|
|
27
|
+
const { default: path, default: path$1, default: path$2 } = __toESM(require("node:path"));
|
|
28
28
|
const { createFilter } = __toESM(require("@rollup/pluginutils"));
|
|
29
|
-
const { default: MagicString } = __toESM(require("magic-string"));
|
|
29
|
+
const { default: MagicString, default: MagicString$1 } = __toESM(require("magic-string"));
|
|
30
30
|
const { parseAsync } = __toESM(require("oxc-parser"));
|
|
31
31
|
const { createUnplugin } = __toESM(require("unplugin"));
|
|
32
|
+
const { default: Debug } = __toESM(require("debug"));
|
|
32
33
|
|
|
33
|
-
//#region src/core/options.ts
|
|
34
|
-
function resolveOptions(options) {
|
|
35
|
-
return {
|
|
36
|
-
include: options.include || [/\.[cm]?tsx?$/],
|
|
37
|
-
exclude: options.exclude || [/node_modules/],
|
|
38
|
-
enforce: "enforce" in options ? options.enforce : "pre",
|
|
39
|
-
transformer: options.transformer || "typescript",
|
|
40
|
-
ignoreErrors: options.ignoreErrors || false,
|
|
41
|
-
extraOutdir: options.extraOutdir,
|
|
42
|
-
autoAddExts: options.autoAddExts || false,
|
|
43
|
-
patchCjsDefaultExport: options.patchCjsDefaultExport || false
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
//#endregion
|
|
48
34
|
//#region src/core/utils.ts
|
|
35
|
+
const debug = Debug("unplugin-isolated-decl");
|
|
49
36
|
function lowestCommonAncestor(...filepaths) {
|
|
50
37
|
if (filepaths.length === 0) return "";
|
|
51
|
-
if (filepaths.length === 1) return path$
|
|
38
|
+
if (filepaths.length === 1) return path$2.dirname(filepaths[0]);
|
|
52
39
|
filepaths = filepaths.map((p) => p.replaceAll("\\", "/"));
|
|
53
40
|
const [first, ...rest] = filepaths;
|
|
54
41
|
let ancestor = first.split("/");
|
|
@@ -67,6 +54,46 @@ else {
|
|
|
67
54
|
function stripExt(filename) {
|
|
68
55
|
return filename.replace(/\.(.?)[jt]sx?$/, "");
|
|
69
56
|
}
|
|
57
|
+
function resolveEntry(input) {
|
|
58
|
+
const map = !Array.isArray(input) ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path$2.resolve(stripExt(v)), k])) : undefined;
|
|
59
|
+
const arr = Array.isArray(input) && input ? input : Object.values(input);
|
|
60
|
+
const outBase = lowestCommonAncestor(...arr);
|
|
61
|
+
return {
|
|
62
|
+
map,
|
|
63
|
+
outBase
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/core/ast.ts
|
|
69
|
+
function filterImports(program) {
|
|
70
|
+
return program.body.filter((node) => (node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration") && !!node.source);
|
|
71
|
+
}
|
|
72
|
+
function patchEntryAlias(source, s, imports, outname, offset) {
|
|
73
|
+
debug("Patching entry alias:", outname, "offset:", offset);
|
|
74
|
+
s ||= new MagicString$1(source);
|
|
75
|
+
for (const i of imports) if (i.source.value[0] === ".") s.overwrite(i.source.start + 1, i.source.end - 1, `./${path$1.join(offset, i.source.value).replaceAll("\\", "/")}`);
|
|
76
|
+
if (s.hasChanged()) {
|
|
77
|
+
debug("Patched entry alias:", outname);
|
|
78
|
+
return s.toString();
|
|
79
|
+
}
|
|
80
|
+
return source;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/core/options.ts
|
|
85
|
+
function resolveOptions(options) {
|
|
86
|
+
return {
|
|
87
|
+
include: options.include || [/\.[cm]?tsx?$/],
|
|
88
|
+
exclude: options.exclude || [/node_modules/],
|
|
89
|
+
enforce: "enforce" in options ? options.enforce : "pre",
|
|
90
|
+
transformer: options.transformer || "typescript",
|
|
91
|
+
ignoreErrors: options.ignoreErrors || false,
|
|
92
|
+
extraOutdir: options.extraOutdir,
|
|
93
|
+
autoAddExts: options.autoAddExts || false,
|
|
94
|
+
patchCjsDefaultExport: options.patchCjsDefaultExport || false
|
|
95
|
+
};
|
|
96
|
+
}
|
|
70
97
|
|
|
71
98
|
//#endregion
|
|
72
99
|
//#region src/index.ts
|
|
@@ -75,8 +102,10 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
75
102
|
const filter = createFilter(options.include, options.exclude);
|
|
76
103
|
let farmPluginContext;
|
|
77
104
|
const outputFiles = {};
|
|
78
|
-
function addOutput(filename,
|
|
79
|
-
|
|
105
|
+
function addOutput(filename, output) {
|
|
106
|
+
const name = stripExt(filename);
|
|
107
|
+
debug("Add output:", name);
|
|
108
|
+
outputFiles[name] = output;
|
|
80
109
|
}
|
|
81
110
|
const rollup = { renderStart: rollupRenderStart };
|
|
82
111
|
const farm = { renderStart: { executor: farmRenderStart } };
|
|
@@ -100,21 +129,8 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
100
129
|
farm
|
|
101
130
|
};
|
|
102
131
|
async function transform(context, code, id) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
program = (await parseAsync(code, { sourceFilename: id })).program;
|
|
106
|
-
} catch {}
|
|
107
|
-
if (options.autoAddExts && program) {
|
|
108
|
-
const imports = program.body.filter((node) => node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration");
|
|
109
|
-
const s = new MagicString(code);
|
|
110
|
-
for (const i of imports) {
|
|
111
|
-
if (!i.source || path.basename(i.source.value).includes(".")) continue;
|
|
112
|
-
const resolved = await resolve(context, i.source.value, id);
|
|
113
|
-
if (!resolved || resolved.external) continue;
|
|
114
|
-
if (resolved.id.endsWith(".ts") || resolved.id.endsWith(".tsx")) s.overwrite(i.source.start, i.source.end, JSON.stringify(`${i.source.value}.js`));
|
|
115
|
-
}
|
|
116
|
-
code = s.toString();
|
|
117
|
-
}
|
|
132
|
+
const label = debug.enabled && `[${options.transformer}]`;
|
|
133
|
+
debug(label, "transform", id);
|
|
118
134
|
let result;
|
|
119
135
|
switch (options.transformer) {
|
|
120
136
|
case "oxc":
|
|
@@ -125,14 +141,34 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
125
141
|
break;
|
|
126
142
|
case "typescript": result = await require_transformer.tsTransform(id, code, options.transformOptions);
|
|
127
143
|
}
|
|
128
|
-
|
|
144
|
+
let { code: dts, errors } = result;
|
|
145
|
+
debug(label, "transformed", id, errors.length ? "with errors" : "successfully");
|
|
129
146
|
if (errors.length) if (options.ignoreErrors) context.warn(errors[0]);
|
|
130
147
|
else {
|
|
131
148
|
context.error(errors[0]);
|
|
132
149
|
return;
|
|
133
150
|
}
|
|
134
|
-
|
|
135
|
-
|
|
151
|
+
const { program } = await parseAsync(dts, { sourceFilename: id });
|
|
152
|
+
const imports = filterImports(program);
|
|
153
|
+
let s;
|
|
154
|
+
if (options.autoAddExts) {
|
|
155
|
+
s = new MagicString(dts);
|
|
156
|
+
for (const i of imports) {
|
|
157
|
+
if (path.basename(i.source.value).includes(".")) continue;
|
|
158
|
+
const resolved = await resolve(context, i.source.value, id);
|
|
159
|
+
if (!resolved || resolved.external) continue;
|
|
160
|
+
if (resolved.id.endsWith(".ts") || resolved.id.endsWith(".tsx")) {
|
|
161
|
+
i.source.value = `${i.source.originalValue = i.source.value}.js`;
|
|
162
|
+
s.overwrite(i.source.start + 1, i.source.end - 1, i.source.value);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
dts = s.toString();
|
|
166
|
+
}
|
|
167
|
+
addOutput(id, {
|
|
168
|
+
source: dts,
|
|
169
|
+
s,
|
|
170
|
+
imports
|
|
171
|
+
});
|
|
136
172
|
const typeImports = program.body.filter((node) => {
|
|
137
173
|
if (node.type === "ImportDeclaration") {
|
|
138
174
|
if (node.importKind === "type") return true;
|
|
@@ -146,7 +182,7 @@ else {
|
|
|
146
182
|
});
|
|
147
183
|
for (const i of typeImports) {
|
|
148
184
|
if (!i.source) continue;
|
|
149
|
-
const resolved = (await resolve(context, i.source.value, id))?.id;
|
|
185
|
+
const resolved = (await resolve(context, i.source.originalValue || i.source.value, id))?.id;
|
|
150
186
|
if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
|
|
151
187
|
let source;
|
|
152
188
|
try {
|
|
@@ -154,22 +190,28 @@ else {
|
|
|
154
190
|
} catch {
|
|
155
191
|
continue;
|
|
156
192
|
}
|
|
193
|
+
debug("transform type import:", resolved);
|
|
157
194
|
await transform(context, source, resolved);
|
|
158
195
|
}
|
|
159
196
|
}
|
|
160
197
|
}
|
|
161
198
|
function rollupRenderStart(outputOptions, inputOptions) {
|
|
162
199
|
const { input } = inputOptions;
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
const outBase = lowestCommonAncestor(...normalizedInput);
|
|
200
|
+
const { outBase, map } = resolveEntry(input);
|
|
201
|
+
debug("[rollup] out base:", outBase);
|
|
166
202
|
if (typeof outputOptions.entryFileNames !== "string") return this.error("entryFileNames must be a string");
|
|
167
203
|
let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
|
|
168
204
|
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
169
|
-
for (let [outname, source] of Object.entries(outputFiles)) {
|
|
170
|
-
const
|
|
171
|
-
const
|
|
205
|
+
for (let [outname, { source, s, imports }] of Object.entries(outputFiles)) {
|
|
206
|
+
const entryAlias = map?.[outname];
|
|
207
|
+
const relativeName = path.relative(outBase, outname);
|
|
208
|
+
const fileName = entryFileNames.replace("[name]", entryAlias || relativeName);
|
|
209
|
+
if (entryAlias && entryAlias !== relativeName) {
|
|
210
|
+
const offset = path.relative(path.dirname(path.resolve(outBase, fileName)), path.dirname(outname));
|
|
211
|
+
source = patchEntryAlias(source, s, imports, outname, offset);
|
|
212
|
+
}
|
|
172
213
|
if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
214
|
+
debug("[rollup] emit dts file:", fileName);
|
|
173
215
|
this.emitFile({
|
|
174
216
|
type: "asset",
|
|
175
217
|
fileName,
|
|
@@ -179,9 +221,8 @@ else {
|
|
|
179
221
|
}
|
|
180
222
|
function farmRenderStart(config) {
|
|
181
223
|
const { input = {}, output = {} } = config;
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
const outBase = lowestCommonAncestor(...normalizedInput);
|
|
224
|
+
const { outBase, map } = resolveEntry(input);
|
|
225
|
+
debug("[farm] out base:", outBase);
|
|
185
226
|
if (output && typeof output.entryFilename !== "string") return console.error("entryFileName must be a string");
|
|
186
227
|
const extFormatMap = new Map([
|
|
187
228
|
["cjs", "cjs"],
|
|
@@ -192,10 +233,16 @@ else {
|
|
|
192
233
|
output.entryFilename = output.entryFilename.replace("[ext]", extFormatMap.get(output.format || "esm") || "js");
|
|
193
234
|
let entryFileNames = output.entryFilename.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
|
|
194
235
|
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
195
|
-
for (let [outname, source] of Object.entries(outputFiles)) {
|
|
196
|
-
const
|
|
197
|
-
const
|
|
236
|
+
for (let [outname, { source, s, imports }] of Object.entries(outputFiles)) {
|
|
237
|
+
const entryAlias = map?.[outname];
|
|
238
|
+
const relativeName = path.relative(outBase, outname);
|
|
239
|
+
const fileName = entryFileNames.replace("[name]", entryAlias || relativeName);
|
|
240
|
+
if (entryAlias && entryAlias !== relativeName) {
|
|
241
|
+
const offset = path.relative(path.dirname(path.resolve(outBase, fileName)), path.dirname(outname));
|
|
242
|
+
source = patchEntryAlias(source, s, imports, outname, offset);
|
|
243
|
+
}
|
|
198
244
|
if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
245
|
+
debug("[farm] emit dts file:", fileName);
|
|
199
246
|
farmPluginContext.emitFile({
|
|
200
247
|
type: "asset",
|
|
201
248
|
fileName,
|
|
@@ -209,6 +256,7 @@ else {
|
|
|
209
256
|
const entries = esbuildOptions.entryPoints;
|
|
210
257
|
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
|
|
211
258
|
const outBase = lowestCommonAncestor(...entries);
|
|
259
|
+
debug("[esbuild] out base:", outBase);
|
|
212
260
|
const jsExt = esbuildOptions.outExtension?.[".js"];
|
|
213
261
|
let outExt;
|
|
214
262
|
switch (jsExt) {
|
|
@@ -227,7 +275,7 @@ else {
|
|
|
227
275
|
if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
|
|
228
276
|
} else result.outputFiles ||= [];
|
|
229
277
|
const textEncoder = new TextEncoder();
|
|
230
|
-
for (let [filename, source] of Object.entries(outputFiles)) {
|
|
278
|
+
for (let [filename, { source }] of Object.entries(outputFiles)) {
|
|
231
279
|
const outDir = build.initialOptions.outdir;
|
|
232
280
|
let outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
233
281
|
if (options.extraOutdir) outFile = path.join(options.extraOutdir, outFile);
|
|
@@ -236,12 +284,16 @@ else {
|
|
|
236
284
|
if (write) {
|
|
237
285
|
await mkdir(path.dirname(filePath), { recursive: true });
|
|
238
286
|
await writeFile(filePath, source);
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
287
|
+
debug("[esbuild] write dts file:", filePath);
|
|
288
|
+
} else {
|
|
289
|
+
debug("[esbuild] emit dts file:", filePath);
|
|
290
|
+
result.outputFiles.push({
|
|
291
|
+
path: filePath,
|
|
292
|
+
contents: textEncoder.encode(source),
|
|
293
|
+
hash: "",
|
|
294
|
+
text: source
|
|
295
|
+
});
|
|
296
|
+
}
|
|
245
297
|
}
|
|
246
298
|
});
|
|
247
299
|
}
|
|
@@ -1,30 +1,17 @@
|
|
|
1
1
|
import { oxcTransform, swcTransform, tsTransform } from "./transformer-DDte86lZ.js";
|
|
2
2
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
-
import path, { default as path$1 } from "node:path";
|
|
3
|
+
import path, { default as path$1, default as path$2 } from "node:path";
|
|
4
4
|
import { createFilter } from "@rollup/pluginutils";
|
|
5
|
-
import MagicString from "magic-string";
|
|
5
|
+
import MagicString, { default as MagicString$1 } from "magic-string";
|
|
6
6
|
import { parseAsync } from "oxc-parser";
|
|
7
7
|
import { createUnplugin } from "unplugin";
|
|
8
|
+
import Debug from "debug";
|
|
8
9
|
|
|
9
|
-
//#region src/core/options.ts
|
|
10
|
-
function resolveOptions(options) {
|
|
11
|
-
return {
|
|
12
|
-
include: options.include || [/\.[cm]?tsx?$/],
|
|
13
|
-
exclude: options.exclude || [/node_modules/],
|
|
14
|
-
enforce: "enforce" in options ? options.enforce : "pre",
|
|
15
|
-
transformer: options.transformer || "typescript",
|
|
16
|
-
ignoreErrors: options.ignoreErrors || false,
|
|
17
|
-
extraOutdir: options.extraOutdir,
|
|
18
|
-
autoAddExts: options.autoAddExts || false,
|
|
19
|
-
patchCjsDefaultExport: options.patchCjsDefaultExport || false
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
10
|
//#region src/core/utils.ts
|
|
11
|
+
const debug = Debug("unplugin-isolated-decl");
|
|
25
12
|
function lowestCommonAncestor(...filepaths) {
|
|
26
13
|
if (filepaths.length === 0) return "";
|
|
27
|
-
if (filepaths.length === 1) return path$
|
|
14
|
+
if (filepaths.length === 1) return path$2.dirname(filepaths[0]);
|
|
28
15
|
filepaths = filepaths.map((p) => p.replaceAll("\\", "/"));
|
|
29
16
|
const [first, ...rest] = filepaths;
|
|
30
17
|
let ancestor = first.split("/");
|
|
@@ -43,6 +30,46 @@ else {
|
|
|
43
30
|
function stripExt(filename) {
|
|
44
31
|
return filename.replace(/\.(.?)[jt]sx?$/, "");
|
|
45
32
|
}
|
|
33
|
+
function resolveEntry(input) {
|
|
34
|
+
const map = !Array.isArray(input) ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path$2.resolve(stripExt(v)), k])) : undefined;
|
|
35
|
+
const arr = Array.isArray(input) && input ? input : Object.values(input);
|
|
36
|
+
const outBase = lowestCommonAncestor(...arr);
|
|
37
|
+
return {
|
|
38
|
+
map,
|
|
39
|
+
outBase
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/core/ast.ts
|
|
45
|
+
function filterImports(program) {
|
|
46
|
+
return program.body.filter((node) => (node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration") && !!node.source);
|
|
47
|
+
}
|
|
48
|
+
function patchEntryAlias(source, s, imports, outname, offset) {
|
|
49
|
+
debug("Patching entry alias:", outname, "offset:", offset);
|
|
50
|
+
s ||= new MagicString$1(source);
|
|
51
|
+
for (const i of imports) if (i.source.value[0] === ".") s.overwrite(i.source.start + 1, i.source.end - 1, `./${path$1.join(offset, i.source.value).replaceAll("\\", "/")}`);
|
|
52
|
+
if (s.hasChanged()) {
|
|
53
|
+
debug("Patched entry alias:", outname);
|
|
54
|
+
return s.toString();
|
|
55
|
+
}
|
|
56
|
+
return source;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/core/options.ts
|
|
61
|
+
function resolveOptions(options) {
|
|
62
|
+
return {
|
|
63
|
+
include: options.include || [/\.[cm]?tsx?$/],
|
|
64
|
+
exclude: options.exclude || [/node_modules/],
|
|
65
|
+
enforce: "enforce" in options ? options.enforce : "pre",
|
|
66
|
+
transformer: options.transformer || "typescript",
|
|
67
|
+
ignoreErrors: options.ignoreErrors || false,
|
|
68
|
+
extraOutdir: options.extraOutdir,
|
|
69
|
+
autoAddExts: options.autoAddExts || false,
|
|
70
|
+
patchCjsDefaultExport: options.patchCjsDefaultExport || false
|
|
71
|
+
};
|
|
72
|
+
}
|
|
46
73
|
|
|
47
74
|
//#endregion
|
|
48
75
|
//#region src/index.ts
|
|
@@ -51,8 +78,10 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
51
78
|
const filter = createFilter(options.include, options.exclude);
|
|
52
79
|
let farmPluginContext;
|
|
53
80
|
const outputFiles = {};
|
|
54
|
-
function addOutput(filename,
|
|
55
|
-
|
|
81
|
+
function addOutput(filename, output) {
|
|
82
|
+
const name = stripExt(filename);
|
|
83
|
+
debug("Add output:", name);
|
|
84
|
+
outputFiles[name] = output;
|
|
56
85
|
}
|
|
57
86
|
const rollup = { renderStart: rollupRenderStart };
|
|
58
87
|
const farm = { renderStart: { executor: farmRenderStart } };
|
|
@@ -76,21 +105,8 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
76
105
|
farm
|
|
77
106
|
};
|
|
78
107
|
async function transform(context, code, id) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
program = (await parseAsync(code, { sourceFilename: id })).program;
|
|
82
|
-
} catch {}
|
|
83
|
-
if (options.autoAddExts && program) {
|
|
84
|
-
const imports = program.body.filter((node) => node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration");
|
|
85
|
-
const s = new MagicString(code);
|
|
86
|
-
for (const i of imports) {
|
|
87
|
-
if (!i.source || path.basename(i.source.value).includes(".")) continue;
|
|
88
|
-
const resolved = await resolve(context, i.source.value, id);
|
|
89
|
-
if (!resolved || resolved.external) continue;
|
|
90
|
-
if (resolved.id.endsWith(".ts") || resolved.id.endsWith(".tsx")) s.overwrite(i.source.start, i.source.end, JSON.stringify(`${i.source.value}.js`));
|
|
91
|
-
}
|
|
92
|
-
code = s.toString();
|
|
93
|
-
}
|
|
108
|
+
const label = debug.enabled && `[${options.transformer}]`;
|
|
109
|
+
debug(label, "transform", id);
|
|
94
110
|
let result;
|
|
95
111
|
switch (options.transformer) {
|
|
96
112
|
case "oxc":
|
|
@@ -101,14 +117,34 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
101
117
|
break;
|
|
102
118
|
case "typescript": result = await tsTransform(id, code, options.transformOptions);
|
|
103
119
|
}
|
|
104
|
-
|
|
120
|
+
let { code: dts, errors } = result;
|
|
121
|
+
debug(label, "transformed", id, errors.length ? "with errors" : "successfully");
|
|
105
122
|
if (errors.length) if (options.ignoreErrors) context.warn(errors[0]);
|
|
106
123
|
else {
|
|
107
124
|
context.error(errors[0]);
|
|
108
125
|
return;
|
|
109
126
|
}
|
|
110
|
-
|
|
111
|
-
|
|
127
|
+
const { program } = await parseAsync(dts, { sourceFilename: id });
|
|
128
|
+
const imports = filterImports(program);
|
|
129
|
+
let s;
|
|
130
|
+
if (options.autoAddExts) {
|
|
131
|
+
s = new MagicString(dts);
|
|
132
|
+
for (const i of imports) {
|
|
133
|
+
if (path.basename(i.source.value).includes(".")) continue;
|
|
134
|
+
const resolved = await resolve(context, i.source.value, id);
|
|
135
|
+
if (!resolved || resolved.external) continue;
|
|
136
|
+
if (resolved.id.endsWith(".ts") || resolved.id.endsWith(".tsx")) {
|
|
137
|
+
i.source.value = `${i.source.originalValue = i.source.value}.js`;
|
|
138
|
+
s.overwrite(i.source.start + 1, i.source.end - 1, i.source.value);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
dts = s.toString();
|
|
142
|
+
}
|
|
143
|
+
addOutput(id, {
|
|
144
|
+
source: dts,
|
|
145
|
+
s,
|
|
146
|
+
imports
|
|
147
|
+
});
|
|
112
148
|
const typeImports = program.body.filter((node) => {
|
|
113
149
|
if (node.type === "ImportDeclaration") {
|
|
114
150
|
if (node.importKind === "type") return true;
|
|
@@ -122,7 +158,7 @@ else {
|
|
|
122
158
|
});
|
|
123
159
|
for (const i of typeImports) {
|
|
124
160
|
if (!i.source) continue;
|
|
125
|
-
const resolved = (await resolve(context, i.source.value, id))?.id;
|
|
161
|
+
const resolved = (await resolve(context, i.source.originalValue || i.source.value, id))?.id;
|
|
126
162
|
if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
|
|
127
163
|
let source;
|
|
128
164
|
try {
|
|
@@ -130,22 +166,28 @@ else {
|
|
|
130
166
|
} catch {
|
|
131
167
|
continue;
|
|
132
168
|
}
|
|
169
|
+
debug("transform type import:", resolved);
|
|
133
170
|
await transform(context, source, resolved);
|
|
134
171
|
}
|
|
135
172
|
}
|
|
136
173
|
}
|
|
137
174
|
function rollupRenderStart(outputOptions, inputOptions) {
|
|
138
175
|
const { input } = inputOptions;
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
const outBase = lowestCommonAncestor(...normalizedInput);
|
|
176
|
+
const { outBase, map } = resolveEntry(input);
|
|
177
|
+
debug("[rollup] out base:", outBase);
|
|
142
178
|
if (typeof outputOptions.entryFileNames !== "string") return this.error("entryFileNames must be a string");
|
|
143
179
|
let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
|
|
144
180
|
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
145
|
-
for (let [outname, source] of Object.entries(outputFiles)) {
|
|
146
|
-
const
|
|
147
|
-
const
|
|
181
|
+
for (let [outname, { source, s, imports }] of Object.entries(outputFiles)) {
|
|
182
|
+
const entryAlias = map?.[outname];
|
|
183
|
+
const relativeName = path.relative(outBase, outname);
|
|
184
|
+
const fileName = entryFileNames.replace("[name]", entryAlias || relativeName);
|
|
185
|
+
if (entryAlias && entryAlias !== relativeName) {
|
|
186
|
+
const offset = path.relative(path.dirname(path.resolve(outBase, fileName)), path.dirname(outname));
|
|
187
|
+
source = patchEntryAlias(source, s, imports, outname, offset);
|
|
188
|
+
}
|
|
148
189
|
if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
190
|
+
debug("[rollup] emit dts file:", fileName);
|
|
149
191
|
this.emitFile({
|
|
150
192
|
type: "asset",
|
|
151
193
|
fileName,
|
|
@@ -155,9 +197,8 @@ else {
|
|
|
155
197
|
}
|
|
156
198
|
function farmRenderStart(config) {
|
|
157
199
|
const { input = {}, output = {} } = config;
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
const outBase = lowestCommonAncestor(...normalizedInput);
|
|
200
|
+
const { outBase, map } = resolveEntry(input);
|
|
201
|
+
debug("[farm] out base:", outBase);
|
|
161
202
|
if (output && typeof output.entryFilename !== "string") return console.error("entryFileName must be a string");
|
|
162
203
|
const extFormatMap = new Map([
|
|
163
204
|
["cjs", "cjs"],
|
|
@@ -168,10 +209,16 @@ else {
|
|
|
168
209
|
output.entryFilename = output.entryFilename.replace("[ext]", extFormatMap.get(output.format || "esm") || "js");
|
|
169
210
|
let entryFileNames = output.entryFilename.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
|
|
170
211
|
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
171
|
-
for (let [outname, source] of Object.entries(outputFiles)) {
|
|
172
|
-
const
|
|
173
|
-
const
|
|
212
|
+
for (let [outname, { source, s, imports }] of Object.entries(outputFiles)) {
|
|
213
|
+
const entryAlias = map?.[outname];
|
|
214
|
+
const relativeName = path.relative(outBase, outname);
|
|
215
|
+
const fileName = entryFileNames.replace("[name]", entryAlias || relativeName);
|
|
216
|
+
if (entryAlias && entryAlias !== relativeName) {
|
|
217
|
+
const offset = path.relative(path.dirname(path.resolve(outBase, fileName)), path.dirname(outname));
|
|
218
|
+
source = patchEntryAlias(source, s, imports, outname, offset);
|
|
219
|
+
}
|
|
174
220
|
if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
221
|
+
debug("[farm] emit dts file:", fileName);
|
|
175
222
|
farmPluginContext.emitFile({
|
|
176
223
|
type: "asset",
|
|
177
224
|
fileName,
|
|
@@ -185,6 +232,7 @@ else {
|
|
|
185
232
|
const entries = esbuildOptions.entryPoints;
|
|
186
233
|
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
|
|
187
234
|
const outBase = lowestCommonAncestor(...entries);
|
|
235
|
+
debug("[esbuild] out base:", outBase);
|
|
188
236
|
const jsExt = esbuildOptions.outExtension?.[".js"];
|
|
189
237
|
let outExt;
|
|
190
238
|
switch (jsExt) {
|
|
@@ -203,7 +251,7 @@ else {
|
|
|
203
251
|
if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
|
|
204
252
|
} else result.outputFiles ||= [];
|
|
205
253
|
const textEncoder = new TextEncoder();
|
|
206
|
-
for (let [filename, source] of Object.entries(outputFiles)) {
|
|
254
|
+
for (let [filename, { source }] of Object.entries(outputFiles)) {
|
|
207
255
|
const outDir = build.initialOptions.outdir;
|
|
208
256
|
let outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
209
257
|
if (options.extraOutdir) outFile = path.join(options.extraOutdir, outFile);
|
|
@@ -212,12 +260,16 @@ else {
|
|
|
212
260
|
if (write) {
|
|
213
261
|
await mkdir(path.dirname(filePath), { recursive: true });
|
|
214
262
|
await writeFile(filePath, source);
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
263
|
+
debug("[esbuild] write dts file:", filePath);
|
|
264
|
+
} else {
|
|
265
|
+
debug("[esbuild] emit dts file:", filePath);
|
|
266
|
+
result.outputFiles.push({
|
|
267
|
+
path: filePath,
|
|
268
|
+
contents: textEncoder.encode(source),
|
|
269
|
+
hash: "",
|
|
270
|
+
text: source
|
|
271
|
+
});
|
|
272
|
+
}
|
|
221
273
|
}
|
|
222
274
|
});
|
|
223
275
|
}
|
package/dist/vite.cjs
CHANGED
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.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "A blazing-fast tool for generating isolated declarations.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -89,6 +89,7 @@
|
|
|
89
89
|
},
|
|
90
90
|
"dependencies": {
|
|
91
91
|
"@rollup/pluginutils": "^5.1.3",
|
|
92
|
+
"debug": "^4.3.7",
|
|
92
93
|
"magic-string": "^0.30.12",
|
|
93
94
|
"oxc-parser": "^0.36.0",
|
|
94
95
|
"unplugin": "^1.16.0"
|
|
@@ -99,6 +100,8 @@
|
|
|
99
100
|
"@swc/core": "^1.9.2",
|
|
100
101
|
"@sxzz/eslint-config": "^4.4.1",
|
|
101
102
|
"@sxzz/prettier-config": "^2.0.2",
|
|
103
|
+
"@sxzz/test-utils": "^0.3.7",
|
|
104
|
+
"@types/debug": "^4.1.12",
|
|
102
105
|
"@types/node": "^22.9.0",
|
|
103
106
|
"bumpp": "^9.8.1",
|
|
104
107
|
"esbuild": "^0.24.0",
|