unplugin-isolated-decl 0.6.8 → 0.7.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 +20 -2
- package/dist/api.cjs +1 -1
- package/dist/api.d.cts +1 -1
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/core/ast.d.cts +7 -0
- package/dist/core/ast.d.ts +7 -0
- package/dist/core/options.d.cts +3 -0
- package/dist/core/options.d.ts +3 -0
- package/dist/core/transformer.d.cts +9 -0
- package/dist/core/transformer.d.ts +9 -0
- package/dist/core/utils.d.cts +8 -0
- package/dist/core/utils.d.ts +8 -0
- package/dist/esbuild.cjs +3 -3
- package/dist/esbuild.d.cts +18 -1
- package/dist/esbuild.d.ts +18 -1
- package/dist/esbuild.js +2 -2
- package/dist/farm.cjs +9 -0
- package/dist/farm.d.cts +21 -0
- package/dist/farm.d.ts +21 -0
- package/dist/farm.js +8 -0
- package/dist/index.cjs +3 -4
- package/dist/index.d.cts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +3 -3
- package/dist/rolldown.cjs +3 -3
- package/dist/rolldown.d.cts +19 -1
- package/dist/rolldown.d.ts +19 -1
- package/dist/rolldown.js +2 -2
- package/dist/rollup.cjs +3 -3
- package/dist/rollup.d.cts +19 -1
- package/dist/rollup.d.ts +19 -1
- package/dist/rollup.js +2 -2
- package/dist/{src_index-2WEAOr05.js → src-BatylriB.js} +194 -90
- package/dist/{src_index-NKcZyc90.cjs → src-lcwrBdQD.cjs} +193 -94
- package/dist/vite.cjs +3 -3
- package/dist/vite.d.cts +19 -1
- package/dist/vite.d.ts +19 -1
- package/dist/vite.js +2 -2
- package/package.json +21 -14
- /package/dist/{transformer-uKy0L3Za.cjs → transformer-C4rLQvdl.cjs} +0 -0
- /package/dist/{transformer-w7XvOpWe.js → transformer-DDte86lZ.js} +0 -0
|
@@ -1,11 +1,62 @@
|
|
|
1
|
-
import { oxcTransform, swcTransform, tsTransform } from "./transformer-
|
|
1
|
+
import { oxcTransform, swcTransform, tsTransform } from "./transformer-DDte86lZ.js";
|
|
2
2
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
-
import path 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
|
|
|
10
|
+
//#region src/core/utils.ts
|
|
11
|
+
const debug = Debug("unplugin-isolated-decl");
|
|
12
|
+
function lowestCommonAncestor(...filepaths) {
|
|
13
|
+
if (filepaths.length === 0) return "";
|
|
14
|
+
if (filepaths.length === 1) return path$2.dirname(filepaths[0]);
|
|
15
|
+
filepaths = filepaths.map((p) => p.replaceAll("\\", "/"));
|
|
16
|
+
const [first, ...rest] = filepaths;
|
|
17
|
+
let ancestor = first.split("/");
|
|
18
|
+
for (const filepath of rest) {
|
|
19
|
+
const directories = filepath.split("/", ancestor.length);
|
|
20
|
+
let index = 0;
|
|
21
|
+
for (const directory of directories) if (directory === ancestor[index]) index += 1;
|
|
22
|
+
else {
|
|
23
|
+
ancestor = ancestor.slice(0, index);
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
ancestor = ancestor.slice(0, index);
|
|
27
|
+
}
|
|
28
|
+
return ancestor.length <= 1 && ancestor[0] === "" ? `/${ancestor[0]}` : ancestor.join("/");
|
|
29
|
+
}
|
|
30
|
+
function stripExt(filename) {
|
|
31
|
+
return filename.replace(/\.(.?)[jt]sx?$/, "");
|
|
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
|
|
9
60
|
//#region src/core/options.ts
|
|
10
61
|
function resolveOptions(options) {
|
|
11
62
|
return {
|
|
@@ -25,34 +76,21 @@ function resolveOptions(options) {
|
|
|
25
76
|
const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
26
77
|
const options = resolveOptions(rawOptions);
|
|
27
78
|
const filter = createFilter(options.include, options.exclude);
|
|
79
|
+
let farmPluginContext;
|
|
28
80
|
const outputFiles = {};
|
|
29
|
-
function addOutput(filename,
|
|
30
|
-
|
|
81
|
+
function addOutput(filename, output) {
|
|
82
|
+
const name = stripExt(filename);
|
|
83
|
+
debug("Add output:", name);
|
|
84
|
+
outputFiles[name] = output;
|
|
31
85
|
}
|
|
32
|
-
const rollup = { renderStart
|
|
33
|
-
|
|
34
|
-
const inputMap = !Array.isArray(input) ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path.resolve(stripExt(v)), k])) : undefined;
|
|
35
|
-
const normalizedInput = Array.isArray(input) ? input : Object.values(input);
|
|
36
|
-
const outBase = lowestCommonAncestor(...normalizedInput);
|
|
37
|
-
if (typeof outputOptions.entryFileNames !== "string") return this.error("entryFileNames must be a string");
|
|
38
|
-
let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
|
|
39
|
-
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
40
|
-
for (let [outname, source] of Object.entries(outputFiles)) {
|
|
41
|
-
const name = inputMap?.[outname] || path.relative(outBase, outname);
|
|
42
|
-
const fileName = entryFileNames.replace("[name]", name);
|
|
43
|
-
if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
44
|
-
this.emitFile({
|
|
45
|
-
type: "asset",
|
|
46
|
-
fileName,
|
|
47
|
-
source
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
} };
|
|
86
|
+
const rollup = { renderStart: rollupRenderStart };
|
|
87
|
+
const farm = { renderStart: { executor: farmRenderStart } };
|
|
51
88
|
return {
|
|
52
89
|
name: "unplugin-isolated-decl",
|
|
53
|
-
|
|
54
|
-
|
|
90
|
+
buildStart() {
|
|
91
|
+
farmPluginContext = this;
|
|
55
92
|
},
|
|
93
|
+
transformInclude: (id) => filter(id),
|
|
56
94
|
transform(code, id) {
|
|
57
95
|
return transform(this, code, id);
|
|
58
96
|
},
|
|
@@ -63,24 +101,12 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
63
101
|
apply: "build",
|
|
64
102
|
enforce: "pre",
|
|
65
103
|
...rollup
|
|
66
|
-
}
|
|
104
|
+
},
|
|
105
|
+
farm
|
|
67
106
|
};
|
|
68
107
|
async function transform(context, code, id) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
program = (await parseAsync(code, { sourceFilename: id })).program;
|
|
72
|
-
} catch {}
|
|
73
|
-
if (options.autoAddExts && program) {
|
|
74
|
-
const imports = program.body.filter((node) => node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration");
|
|
75
|
-
const s = new MagicString(code);
|
|
76
|
-
for (const i of imports) {
|
|
77
|
-
if (!i.source || path.basename(i.source.value).includes(".")) continue;
|
|
78
|
-
const resolved = await resolve(context, i.source.value, id);
|
|
79
|
-
if (!resolved || resolved.external) continue;
|
|
80
|
-
if (resolved.id.endsWith(".ts") || resolved.id.endsWith(".tsx")) s.overwrite(i.source.start, i.source.end, JSON.stringify(`${i.source.value}.js`));
|
|
81
|
-
}
|
|
82
|
-
code = s.toString();
|
|
83
|
-
}
|
|
108
|
+
const label = debug.enabled && `[${options.transformer}]`;
|
|
109
|
+
debug(label, "transform", id);
|
|
84
110
|
let result;
|
|
85
111
|
switch (options.transformer) {
|
|
86
112
|
case "oxc":
|
|
@@ -91,14 +117,34 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
|
|
|
91
117
|
break;
|
|
92
118
|
case "typescript": result = await tsTransform(id, code, options.transformOptions);
|
|
93
119
|
}
|
|
94
|
-
|
|
120
|
+
let { code: dts, errors } = result;
|
|
121
|
+
debug(label, "transformed", id, errors.length ? "with errors" : "successfully");
|
|
95
122
|
if (errors.length) if (options.ignoreErrors) context.warn(errors[0]);
|
|
96
123
|
else {
|
|
97
124
|
context.error(errors[0]);
|
|
98
125
|
return;
|
|
99
126
|
}
|
|
100
|
-
|
|
101
|
-
|
|
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.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
|
+
});
|
|
102
148
|
const typeImports = program.body.filter((node) => {
|
|
103
149
|
if (node.type === "ImportDeclaration") {
|
|
104
150
|
if (node.importKind === "type") return true;
|
|
@@ -120,16 +166,73 @@ else {
|
|
|
120
166
|
} catch {
|
|
121
167
|
continue;
|
|
122
168
|
}
|
|
169
|
+
debug("transform type import:", resolved);
|
|
123
170
|
await transform(context, source, resolved);
|
|
124
171
|
}
|
|
125
172
|
}
|
|
126
173
|
}
|
|
174
|
+
function rollupRenderStart(outputOptions, inputOptions) {
|
|
175
|
+
const { input } = inputOptions;
|
|
176
|
+
const { outBase, map } = resolveEntry(input);
|
|
177
|
+
debug("[rollup] out base:", outBase);
|
|
178
|
+
if (typeof outputOptions.entryFileNames !== "string") return this.error("entryFileNames must be a string");
|
|
179
|
+
let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
|
|
180
|
+
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
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
|
+
}
|
|
189
|
+
if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
190
|
+
debug("[rollup] emit dts file:", fileName);
|
|
191
|
+
this.emitFile({
|
|
192
|
+
type: "asset",
|
|
193
|
+
fileName,
|
|
194
|
+
source
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function farmRenderStart(config) {
|
|
199
|
+
const { input = {}, output = {} } = config;
|
|
200
|
+
const { outBase, map } = resolveEntry(input);
|
|
201
|
+
debug("[farm] out base:", outBase);
|
|
202
|
+
if (output && typeof output.entryFilename !== "string") return console.error("entryFileName must be a string");
|
|
203
|
+
const extFormatMap = new Map([
|
|
204
|
+
["cjs", "cjs"],
|
|
205
|
+
["esm", "js"],
|
|
206
|
+
["mjs", "js"]
|
|
207
|
+
]);
|
|
208
|
+
output.entryFilename = "[entryName].[ext]";
|
|
209
|
+
output.entryFilename = output.entryFilename.replace("[ext]", extFormatMap.get(output.format || "esm") || "js");
|
|
210
|
+
let entryFileNames = output.entryFilename.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
|
|
211
|
+
if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
|
|
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
|
+
}
|
|
220
|
+
if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
|
|
221
|
+
debug("[farm] emit dts file:", fileName);
|
|
222
|
+
farmPluginContext.emitFile({
|
|
223
|
+
type: "asset",
|
|
224
|
+
fileName,
|
|
225
|
+
source
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
127
229
|
function esbuildSetup(build) {
|
|
128
230
|
build.onEnd(async (result) => {
|
|
129
231
|
const esbuildOptions = build.initialOptions;
|
|
130
232
|
const entries = esbuildOptions.entryPoints;
|
|
131
233
|
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
|
|
132
234
|
const outBase = lowestCommonAncestor(...entries);
|
|
235
|
+
debug("[esbuild] out base:", outBase);
|
|
133
236
|
const jsExt = esbuildOptions.outExtension?.[".js"];
|
|
134
237
|
let outExt;
|
|
135
238
|
switch (jsExt) {
|
|
@@ -148,7 +251,7 @@ else {
|
|
|
148
251
|
if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
|
|
149
252
|
} else result.outputFiles ||= [];
|
|
150
253
|
const textEncoder = new TextEncoder();
|
|
151
|
-
for (let [filename, source] of Object.entries(outputFiles)) {
|
|
254
|
+
for (let [filename, { source }] of Object.entries(outputFiles)) {
|
|
152
255
|
const outDir = build.initialOptions.outdir;
|
|
153
256
|
let outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
154
257
|
if (options.extraOutdir) outFile = path.join(options.extraOutdir, outFile);
|
|
@@ -157,60 +260,61 @@ else {
|
|
|
157
260
|
if (write) {
|
|
158
261
|
await mkdir(path.dirname(filePath), { recursive: true });
|
|
159
262
|
await writeFile(filePath, source);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
+
}
|
|
166
273
|
}
|
|
167
274
|
});
|
|
168
275
|
}
|
|
169
276
|
});
|
|
170
277
|
async function resolve(context, id, importer) {
|
|
171
278
|
const nativeContext = context.getNativeBuildContext?.();
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
279
|
+
switch (nativeContext?.framework) {
|
|
280
|
+
case "esbuild": {
|
|
281
|
+
const resolved = await nativeContext?.build.resolve(id, {
|
|
282
|
+
importer,
|
|
283
|
+
resolveDir: path.dirname(importer),
|
|
284
|
+
kind: "import-statement"
|
|
285
|
+
});
|
|
286
|
+
return {
|
|
287
|
+
id: resolved?.path,
|
|
288
|
+
external: resolved?.external
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
case "farm": {
|
|
292
|
+
const resolved = await nativeContext?.context.resolve({
|
|
293
|
+
source: id,
|
|
294
|
+
importer,
|
|
295
|
+
kind: "import"
|
|
296
|
+
}, {
|
|
297
|
+
meta: {},
|
|
298
|
+
caller: "unplugin-isolated-decl"
|
|
299
|
+
});
|
|
300
|
+
return {
|
|
301
|
+
id: resolved.resolvedPath,
|
|
302
|
+
external: !!resolved.external
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
default: {
|
|
306
|
+
const resolved = await context.resolve(id, importer);
|
|
307
|
+
if (!resolved) return;
|
|
308
|
+
return {
|
|
309
|
+
id: resolved.id,
|
|
310
|
+
external: !!resolved.external
|
|
311
|
+
};
|
|
312
|
+
}
|
|
182
313
|
}
|
|
183
|
-
const resolved = await context.resolve(id, importer);
|
|
184
|
-
if (!resolved) return;
|
|
185
|
-
return {
|
|
186
|
-
id: resolved.id,
|
|
187
|
-
external: !!resolved.external
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
function stripExt(filename) {
|
|
191
|
-
return filename.replace(/\.(.?)[jt]sx?$/, "");
|
|
192
314
|
}
|
|
193
315
|
function patchCjsDefaultExport(source) {
|
|
194
316
|
return source.replace(/(?<=(?:[;}]|^)\s*export\s*)(?:\{\s*([\w$]+)\s*as\s+default\s*\}|default\s+([\w$]+))/, (_, s1, s2) => `= ${s1 || s2}`);
|
|
195
317
|
}
|
|
196
|
-
function lowestCommonAncestor(...filepaths) {
|
|
197
|
-
if (filepaths.length === 0) return "";
|
|
198
|
-
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
199
|
-
filepaths = filepaths.map((p) => p.replaceAll("\\", "/"));
|
|
200
|
-
const [first, ...rest] = filepaths;
|
|
201
|
-
let ancestor = first.split("/");
|
|
202
|
-
for (const filepath of rest) {
|
|
203
|
-
const directories = filepath.split("/", ancestor.length);
|
|
204
|
-
let index = 0;
|
|
205
|
-
for (const directory of directories) if (directory === ancestor[index]) index += 1;
|
|
206
|
-
else {
|
|
207
|
-
ancestor = ancestor.slice(0, index);
|
|
208
|
-
break;
|
|
209
|
-
}
|
|
210
|
-
ancestor = ancestor.slice(0, index);
|
|
211
|
-
}
|
|
212
|
-
return ancestor.length <= 1 && ancestor[0] === "" ? `/${ancestor[0]}` : ancestor.join("/");
|
|
213
|
-
}
|
|
214
318
|
|
|
215
319
|
//#endregion
|
|
216
|
-
export { IsolatedDecl
|
|
320
|
+
export { IsolatedDecl };
|