unplugin-isolated-decl 0.6.0 → 0.6.1
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 +22 -0
- package/dist/api.d.cts +1 -1
- package/dist/api.d.ts +1 -1
- package/dist/core/options.d.cts +2 -0
- package/dist/core/options.d.ts +2 -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/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_index--m5BPU5W.cjs → src_index-Q6qrrUBW.cjs} +48 -16
- package/dist/{src_index-pbl3XXPp.js → src_index-g0Uyr2cn.js} +48 -16
- 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 +3 -1
package/README.md
CHANGED
|
@@ -93,9 +93,27 @@ export interface Options {
|
|
|
93
93
|
|
|
94
94
|
/** An extra directory layer for output files. */
|
|
95
95
|
extraOutdir?: string
|
|
96
|
+
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
|
|
97
|
+
autoAddExts?: boolean
|
|
96
98
|
}
|
|
97
99
|
```
|
|
98
100
|
|
|
101
|
+
### `autoAddExts`
|
|
102
|
+
|
|
103
|
+
Automatically add `.js` extension to resolve in Node 16+ ESM mode.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// index.d.ts
|
|
107
|
+
import {} from './foo'
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
With `autoAddExts`, it will be transformed to:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// index.d.ts
|
|
114
|
+
import {} from './foo.js'
|
|
115
|
+
```
|
|
116
|
+
|
|
99
117
|
## Sponsors
|
|
100
118
|
|
|
101
119
|
<p align="center">
|
|
@@ -107,3 +125,7 @@ export interface Options {
|
|
|
107
125
|
## License
|
|
108
126
|
|
|
109
127
|
[MIT](./LICENSE) License © 2024-PRESENT [三咲智子](https://github.com/sxzz)
|
|
128
|
+
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```
|
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";
|
package/dist/core/options.d.cts
CHANGED
|
@@ -7,6 +7,8 @@ export type Options = {
|
|
|
7
7
|
ignoreErrors?: boolean;
|
|
8
8
|
/** An extra directory layer for output files. */
|
|
9
9
|
extraOutdir?: string;
|
|
10
|
+
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
|
|
11
|
+
autoAddExts?: boolean;
|
|
10
12
|
} & ({
|
|
11
13
|
/**
|
|
12
14
|
* `oxc-transform` or `@swc/core` should be installed yourself
|
package/dist/core/options.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export type Options = {
|
|
|
7
7
|
ignoreErrors?: boolean;
|
|
8
8
|
/** An extra directory layer for output files. */
|
|
9
9
|
extraOutdir?: string;
|
|
10
|
+
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
|
|
11
|
+
autoAddExts?: boolean;
|
|
10
12
|
} & ({
|
|
11
13
|
/**
|
|
12
14
|
* `oxc-transform` or `@swc/core` should be installed yourself
|
package/dist/esbuild.cjs
CHANGED
package/dist/esbuild.d.cts
CHANGED
package/dist/esbuild.d.ts
CHANGED
package/dist/esbuild.js
CHANGED
package/dist/index.cjs
CHANGED
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type UnpluginInstance } from 'unplugin';
|
|
2
|
-
import { type Options } from
|
|
2
|
+
import { type Options } from "./core/options.js";
|
|
3
3
|
export type { Options };
|
|
4
4
|
export declare const IsolatedDecl: UnpluginInstance<Options | undefined, false>;
|
|
5
5
|
export declare function lowestCommonAncestor(...filepaths: string[]): string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type UnpluginInstance } from 'unplugin';
|
|
2
|
-
import { type Options } from
|
|
2
|
+
import { type Options } from "./core/options.js";
|
|
3
3
|
export type { Options };
|
|
4
4
|
export declare const IsolatedDecl: UnpluginInstance<Options | undefined, false>;
|
|
5
5
|
export declare function lowestCommonAncestor(...filepaths: string[]): string;
|
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
|
@@ -26,6 +26,7 @@ const require_transformer = require('./transformer-ZHFQTLaY.cjs');
|
|
|
26
26
|
const { mkdir, readFile, writeFile } = __toESM(require("node:fs/promises"));
|
|
27
27
|
const { default: path } = __toESM(require("node:path"));
|
|
28
28
|
const { createFilter } = __toESM(require("@rollup/pluginutils"));
|
|
29
|
+
const { default: MagicString } = __toESM(require("magic-string"));
|
|
29
30
|
const { parseAsync } = __toESM(require("oxc-parser"));
|
|
30
31
|
const { createUnplugin } = __toESM(require("unplugin"));
|
|
31
32
|
|
|
@@ -37,7 +38,8 @@ function resolveOptions(options) {
|
|
|
37
38
|
enforce: "enforce" in options ? options.enforce : "pre",
|
|
38
39
|
transformer: options.transformer || "typescript",
|
|
39
40
|
ignoreErrors: options.ignoreErrors || false,
|
|
40
|
-
extraOutdir: options.extraOutdir
|
|
41
|
+
extraOutdir: options.extraOutdir,
|
|
42
|
+
autoAddExts: options.autoAddExts || false
|
|
41
43
|
};
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -90,6 +92,25 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
90
92
|
}
|
|
91
93
|
};
|
|
92
94
|
async function transform(context, code, id) {
|
|
95
|
+
let program;
|
|
96
|
+
try {
|
|
97
|
+
program = JSON.parse((await parseAsync(code, { sourceFilename: id })).program);
|
|
98
|
+
} catch {}
|
|
99
|
+
if (options.autoAddExts && program) {
|
|
100
|
+
const imports = program.body.filter((node) => node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration");
|
|
101
|
+
const s = new MagicString(code);
|
|
102
|
+
for (const i of imports) {
|
|
103
|
+
if (!i.source || path.basename(i.source.value).includes(".")) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const resolved = await resolve(context, i.source.value, id);
|
|
107
|
+
if (!resolved || resolved.external) continue;
|
|
108
|
+
if (resolved.id.endsWith(".ts")) {
|
|
109
|
+
s.overwrite(i.source.start, i.source.end, JSON.stringify(`${i.source.value}.js`));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
code = s.toString();
|
|
113
|
+
}
|
|
93
114
|
let result;
|
|
94
115
|
switch (options.transformer) {
|
|
95
116
|
case "oxc":
|
|
@@ -110,19 +131,21 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
110
131
|
}
|
|
111
132
|
}
|
|
112
133
|
addOutput(id, sourceText);
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
program = JSON.parse((await parseAsync(code, { sourceFilename: id })).program);
|
|
116
|
-
} catch {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
134
|
+
if (!program) return;
|
|
119
135
|
const typeImports = program.body.filter((node) => {
|
|
120
|
-
if (node.type
|
|
121
|
-
|
|
122
|
-
|
|
136
|
+
if (node.type === "ImportDeclaration") {
|
|
137
|
+
if (node.importKind === "type") return true;
|
|
138
|
+
return node.specifiers && node.specifiers.every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
|
|
139
|
+
}
|
|
140
|
+
if (node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") {
|
|
141
|
+
if (node.exportKind === "type") return true;
|
|
142
|
+
return node.type === "ExportNamedDeclaration" && node.specifiers && node.specifiers.every((spec) => spec.type === "ExportSpecifier" && spec.exportKind === "type");
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
123
145
|
});
|
|
124
146
|
for (const i of typeImports) {
|
|
125
|
-
|
|
147
|
+
if (!i.source) continue;
|
|
148
|
+
const resolved = (await resolve(context, i.source.value, id))?.id;
|
|
126
149
|
if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
|
|
127
150
|
let source;
|
|
128
151
|
try {
|
|
@@ -182,17 +205,26 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
182
205
|
});
|
|
183
206
|
}
|
|
184
207
|
});
|
|
185
|
-
|
|
208
|
+
async function resolve(context, id, importer) {
|
|
186
209
|
const nativeContext = context.getNativeBuildContext?.();
|
|
187
210
|
if (nativeContext?.framework === "esbuild") {
|
|
188
|
-
|
|
211
|
+
const resolved$1 = await nativeContext.build.resolve(id, {
|
|
189
212
|
importer,
|
|
190
213
|
resolveDir: path.dirname(importer),
|
|
191
214
|
kind: "import-statement"
|
|
192
|
-
})
|
|
215
|
+
});
|
|
216
|
+
return {
|
|
217
|
+
id: resolved$1.path,
|
|
218
|
+
external: resolved$1.external
|
|
219
|
+
};
|
|
193
220
|
}
|
|
194
|
-
|
|
195
|
-
|
|
221
|
+
const resolved = await context.resolve(id, importer);
|
|
222
|
+
if (!resolved) return;
|
|
223
|
+
return {
|
|
224
|
+
id: resolved.id,
|
|
225
|
+
external: !!resolved.external
|
|
226
|
+
};
|
|
227
|
+
}
|
|
196
228
|
function stripExt(filename) {
|
|
197
229
|
return filename.replace(/\.(.?)[jt]s$/, "");
|
|
198
230
|
}
|
|
@@ -2,6 +2,7 @@ import { oxcTransform, swcTransform, tsTransform } from "./transformer-YxBC7dLI.
|
|
|
2
2
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
3
|
import { default as path } from "node:path";
|
|
4
4
|
import { createFilter } from "@rollup/pluginutils";
|
|
5
|
+
import { default as MagicString } from "magic-string";
|
|
5
6
|
import { parseAsync } from "oxc-parser";
|
|
6
7
|
import { createUnplugin } from "unplugin";
|
|
7
8
|
|
|
@@ -13,7 +14,8 @@ function resolveOptions(options) {
|
|
|
13
14
|
enforce: "enforce" in options ? options.enforce : "pre",
|
|
14
15
|
transformer: options.transformer || "typescript",
|
|
15
16
|
ignoreErrors: options.ignoreErrors || false,
|
|
16
|
-
extraOutdir: options.extraOutdir
|
|
17
|
+
extraOutdir: options.extraOutdir,
|
|
18
|
+
autoAddExts: options.autoAddExts || false
|
|
17
19
|
};
|
|
18
20
|
}
|
|
19
21
|
|
|
@@ -66,6 +68,25 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
66
68
|
}
|
|
67
69
|
};
|
|
68
70
|
async function transform(context, code, id) {
|
|
71
|
+
let program;
|
|
72
|
+
try {
|
|
73
|
+
program = JSON.parse((await parseAsync(code, { sourceFilename: id })).program);
|
|
74
|
+
} catch {}
|
|
75
|
+
if (options.autoAddExts && program) {
|
|
76
|
+
const imports = program.body.filter((node) => node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration");
|
|
77
|
+
const s = new MagicString(code);
|
|
78
|
+
for (const i of imports) {
|
|
79
|
+
if (!i.source || path.basename(i.source.value).includes(".")) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const resolved = await resolve(context, i.source.value, id);
|
|
83
|
+
if (!resolved || resolved.external) continue;
|
|
84
|
+
if (resolved.id.endsWith(".ts")) {
|
|
85
|
+
s.overwrite(i.source.start, i.source.end, JSON.stringify(`${i.source.value}.js`));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
code = s.toString();
|
|
89
|
+
}
|
|
69
90
|
let result;
|
|
70
91
|
switch (options.transformer) {
|
|
71
92
|
case "oxc":
|
|
@@ -86,19 +107,21 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
86
107
|
}
|
|
87
108
|
}
|
|
88
109
|
addOutput(id, sourceText);
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
program = JSON.parse((await parseAsync(code, { sourceFilename: id })).program);
|
|
92
|
-
} catch {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
110
|
+
if (!program) return;
|
|
95
111
|
const typeImports = program.body.filter((node) => {
|
|
96
|
-
if (node.type
|
|
97
|
-
|
|
98
|
-
|
|
112
|
+
if (node.type === "ImportDeclaration") {
|
|
113
|
+
if (node.importKind === "type") return true;
|
|
114
|
+
return node.specifiers && node.specifiers.every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
|
|
115
|
+
}
|
|
116
|
+
if (node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") {
|
|
117
|
+
if (node.exportKind === "type") return true;
|
|
118
|
+
return node.type === "ExportNamedDeclaration" && node.specifiers && node.specifiers.every((spec) => spec.type === "ExportSpecifier" && spec.exportKind === "type");
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
99
121
|
});
|
|
100
122
|
for (const i of typeImports) {
|
|
101
|
-
|
|
123
|
+
if (!i.source) continue;
|
|
124
|
+
const resolved = (await resolve(context, i.source.value, id))?.id;
|
|
102
125
|
if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
|
|
103
126
|
let source;
|
|
104
127
|
try {
|
|
@@ -158,17 +181,26 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
158
181
|
});
|
|
159
182
|
}
|
|
160
183
|
});
|
|
161
|
-
|
|
184
|
+
async function resolve(context, id, importer) {
|
|
162
185
|
const nativeContext = context.getNativeBuildContext?.();
|
|
163
186
|
if (nativeContext?.framework === "esbuild") {
|
|
164
|
-
|
|
187
|
+
const resolved$1 = await nativeContext.build.resolve(id, {
|
|
165
188
|
importer,
|
|
166
189
|
resolveDir: path.dirname(importer),
|
|
167
190
|
kind: "import-statement"
|
|
168
|
-
})
|
|
191
|
+
});
|
|
192
|
+
return {
|
|
193
|
+
id: resolved$1.path,
|
|
194
|
+
external: resolved$1.external
|
|
195
|
+
};
|
|
169
196
|
}
|
|
170
|
-
|
|
171
|
-
|
|
197
|
+
const resolved = await context.resolve(id, importer);
|
|
198
|
+
if (!resolved) return;
|
|
199
|
+
return {
|
|
200
|
+
id: resolved.id,
|
|
201
|
+
external: !!resolved.external
|
|
202
|
+
};
|
|
203
|
+
}
|
|
172
204
|
function stripExt(filename) {
|
|
173
205
|
return filename.replace(/\.(.?)[jt]s$/, "");
|
|
174
206
|
}
|
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.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "A blazing-fast tool for generating isolated declarations, powered by Oxc.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -85,6 +85,7 @@
|
|
|
85
85
|
},
|
|
86
86
|
"dependencies": {
|
|
87
87
|
"@rollup/pluginutils": "^5.1.0",
|
|
88
|
+
"magic-string": "^0.30.11",
|
|
88
89
|
"oxc-parser": "^0.27.0",
|
|
89
90
|
"unplugin": "^1.14.0"
|
|
90
91
|
},
|
|
@@ -93,6 +94,7 @@
|
|
|
93
94
|
"@sxzz/eslint-config": "^4.2.0",
|
|
94
95
|
"@sxzz/prettier-config": "^2.0.2",
|
|
95
96
|
"@types/node": "^22.5.4",
|
|
97
|
+
"@typescript-eslint/typescript-estree": "^8.5.0",
|
|
96
98
|
"bumpp": "^9.5.2",
|
|
97
99
|
"esbuild": "^0.23.1",
|
|
98
100
|
"eslint": "^9.10.0",
|