unplugin-isolated-decl 0.6.8 → 0.7.0

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/dist/api.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_transformer = require('./transformer-uKy0L3Za.cjs');
2
+ const require_transformer = require('./transformer-C4rLQvdl.cjs');
3
3
 
4
4
  exports.oxcTransform = require_transformer.oxcTransform
5
5
  exports.swcTransform = require_transformer.swcTransform
package/dist/api.js CHANGED
@@ -1,3 +1,3 @@
1
- import { oxcTransform, swcTransform, tsTransform } from "./transformer-w7XvOpWe.js";
1
+ import { oxcTransform, swcTransform, tsTransform } from "./transformer-DDte86lZ.js";
2
2
 
3
3
  export { oxcTransform, swcTransform, tsTransform };
@@ -1,5 +1,8 @@
1
1
  import type { FilterPattern } from '@rollup/pluginutils';
2
2
  import type { TranspileOptions } from 'typescript';
3
+ /**
4
+ * Represents the options for the plugin.
5
+ */
3
6
  export type Options = {
4
7
  include?: FilterPattern;
5
8
  exclude?: FilterPattern;
@@ -1,5 +1,8 @@
1
1
  import type { FilterPattern } from '@rollup/pluginutils';
2
2
  import type { TranspileOptions } from 'typescript';
3
+ /**
4
+ * Represents the options for the plugin.
5
+ */
3
6
  export type Options = {
4
7
  include?: FilterPattern;
5
8
  exclude?: FilterPattern;
@@ -3,6 +3,15 @@ export interface TransformResult {
3
3
  code: string;
4
4
  errors: Array<string>;
5
5
  }
6
+ /**
7
+ * Transform isolated declarations with `oxc-transform`.
8
+ */
6
9
  export declare function oxcTransform(id: string, code: string): Promise<TransformResult>;
10
+ /**
11
+ * Transform isolated declarations with `@swc/core`.
12
+ */
7
13
  export declare function swcTransform(id: string, code: string): Promise<TransformResult>;
14
+ /**
15
+ * Transform isolated declarations with `typescript`.
16
+ */
8
17
  export declare function tsTransform(id: string, code: string, transformOptions?: TranspileOptions): Promise<TransformResult>;
@@ -3,6 +3,15 @@ export interface TransformResult {
3
3
  code: string;
4
4
  errors: Array<string>;
5
5
  }
6
+ /**
7
+ * Transform isolated declarations with `oxc-transform`.
8
+ */
6
9
  export declare function oxcTransform(id: string, code: string): Promise<TransformResult>;
10
+ /**
11
+ * Transform isolated declarations with `@swc/core`.
12
+ */
7
13
  export declare function swcTransform(id: string, code: string): Promise<TransformResult>;
14
+ /**
15
+ * Transform isolated declarations with `typescript`.
16
+ */
8
17
  export declare function tsTransform(id: string, code: string, transformOptions?: TranspileOptions): Promise<TransformResult>;
@@ -0,0 +1,2 @@
1
+ export declare function lowestCommonAncestor(...filepaths: string[]): string;
2
+ export declare function stripExt(filename: string): string;
@@ -0,0 +1,2 @@
1
+ export declare function lowestCommonAncestor(...filepaths: string[]): string;
2
+ export declare function stripExt(filename: string): string;
package/dist/esbuild.cjs CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index-NKcZyc90.cjs');
3
- require('./transformer-uKy0L3Za.cjs');
2
+ const require_src = require('./src-C3bhcfQ9.cjs');
3
+ require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/esbuild.ts
6
- var esbuild_default = require_src_index.IsolatedDecl.esbuild;
6
+ var esbuild_default = require_src.IsolatedDecl.esbuild;
7
7
 
8
8
  //#endregion
9
9
  module.exports = esbuild_default;
@@ -1,3 +1,20 @@
1
+ /**
2
+ * This entry file is for esbuild plugin.
3
+ *
4
+ * @module
5
+ */
1
6
  import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Esbuild plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { build } from 'esbuild'
13
+ *
14
+ * build({
15
+ * plugins: [require('unplugin-isolated-decl/esbuild')()],
16
+ * })
17
+ * ```
18
+ */
2
19
  declare const _default: typeof IsolatedDecl.esbuild;
3
20
  export = _default;
package/dist/esbuild.d.ts CHANGED
@@ -1,3 +1,20 @@
1
+ /**
2
+ * This entry file is for esbuild plugin.
3
+ *
4
+ * @module
5
+ */
1
6
  import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Esbuild plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { build } from 'esbuild'
13
+ *
14
+ * build({
15
+ * plugins: [require('unplugin-isolated-decl/esbuild')()],
16
+ * })
17
+ * ```
18
+ */
2
19
  declare const _default: typeof IsolatedDecl.esbuild;
3
20
  export default _default;
package/dist/esbuild.js CHANGED
@@ -1,5 +1,5 @@
1
- import "./transformer-w7XvOpWe.js";
2
- import { IsolatedDecl } from "./src_index-2WEAOr05.js";
1
+ import "./transformer-DDte86lZ.js";
2
+ import { IsolatedDecl } from "./src-B4aBGZxL.js";
3
3
 
4
4
  //#region src/esbuild.ts
5
5
  var esbuild_default = IsolatedDecl.esbuild;
package/dist/farm.cjs ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ const require_src = require('./src-C3bhcfQ9.cjs');
3
+ require('./transformer-C4rLQvdl.cjs');
4
+
5
+ //#region src/farm.ts
6
+ var farm_default = require_src.IsolatedDecl.farm;
7
+
8
+ //#endregion
9
+ module.exports = farm_default;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * This entry file is for Farm plugin.
3
+ *
4
+ * @module
5
+ */
6
+ import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Farm plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // farm.config.js
13
+ * import IsolatedDecl from 'unplugin-isolated-decl/farm'
14
+ *
15
+ * export default {
16
+ * plugins: [IsolatedDecl()],
17
+ * }
18
+ * ```
19
+ */
20
+ declare const _default: typeof IsolatedDecl.farm;
21
+ export = _default;
package/dist/farm.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * This entry file is for Farm plugin.
3
+ *
4
+ * @module
5
+ */
6
+ import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Farm plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // farm.config.js
13
+ * import IsolatedDecl from 'unplugin-isolated-decl/farm'
14
+ *
15
+ * export default {
16
+ * plugins: [IsolatedDecl()],
17
+ * }
18
+ * ```
19
+ */
20
+ declare const _default: typeof IsolatedDecl.farm;
21
+ export default _default;
package/dist/farm.js ADDED
@@ -0,0 +1,8 @@
1
+ import "./transformer-DDte86lZ.js";
2
+ import { IsolatedDecl } from "./src-B4aBGZxL.js";
3
+
4
+ //#region src/farm.ts
5
+ var farm_default = IsolatedDecl.farm;
6
+
7
+ //#endregion
8
+ export { farm_default as default };
package/dist/index.cjs CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index-NKcZyc90.cjs');
3
- require('./transformer-uKy0L3Za.cjs');
2
+ const require_src = require('./src-C3bhcfQ9.cjs');
3
+ require('./transformer-C4rLQvdl.cjs');
4
4
 
5
- exports.IsolatedDecl = require_src_index.IsolatedDecl
6
- exports.lowestCommonAncestor = require_src_index.lowestCommonAncestor
5
+ exports.IsolatedDecl = require_src.IsolatedDecl
package/dist/index.d.cts CHANGED
@@ -1,5 +1,11 @@
1
+ /**
2
+ * This entry file is for main unplugin.
3
+ * @module
4
+ */
1
5
  import { type UnpluginInstance } from 'unplugin';
2
6
  import { type Options } from "./core/options.js";
3
7
  export type { Options };
8
+ /**
9
+ * The main unplugin instance.
10
+ */
4
11
  export declare const IsolatedDecl: UnpluginInstance<Options | undefined, false>;
5
- export declare function lowestCommonAncestor(...filepaths: string[]): string;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,11 @@
1
+ /**
2
+ * This entry file is for main unplugin.
3
+ * @module
4
+ */
1
5
  import { type UnpluginInstance } from 'unplugin';
2
6
  import { type Options } from "./core/options.js";
3
7
  export type { Options };
8
+ /**
9
+ * The main unplugin instance.
10
+ */
4
11
  export declare const IsolatedDecl: UnpluginInstance<Options | undefined, false>;
5
- export declare function lowestCommonAncestor(...filepaths: string[]): string;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import "./transformer-w7XvOpWe.js";
2
- import { IsolatedDecl, lowestCommonAncestor } from "./src_index-2WEAOr05.js";
1
+ import "./transformer-DDte86lZ.js";
2
+ import { IsolatedDecl } from "./src-B4aBGZxL.js";
3
3
 
4
- export { IsolatedDecl, lowestCommonAncestor };
4
+ export { IsolatedDecl };
package/dist/rolldown.cjs CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index-NKcZyc90.cjs');
3
- require('./transformer-uKy0L3Za.cjs');
2
+ const require_src = require('./src-C3bhcfQ9.cjs');
3
+ require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/rolldown.ts
6
- var rolldown_default = require_src_index.IsolatedDecl.rolldown;
6
+ var rolldown_default = require_src.IsolatedDecl.rolldown;
7
7
 
8
8
  //#endregion
9
9
  module.exports = rolldown_default;
@@ -1,3 +1,21 @@
1
+ /**
2
+ * This entry file is for Rolldown plugin.
3
+ *
4
+ * @module
5
+ */
1
6
  import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Rolldown plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // rolldown.config.js
13
+ * import IsolatedDecl from 'unplugin-isolated-decl/rolldown'
14
+ *
15
+ * export default {
16
+ * plugins: [IsolatedDecl()],
17
+ * }
18
+ * ```
19
+ */
2
20
  declare const _default: typeof IsolatedDecl.rolldown;
3
21
  export = _default;
@@ -1,3 +1,21 @@
1
+ /**
2
+ * This entry file is for Rolldown plugin.
3
+ *
4
+ * @module
5
+ */
1
6
  import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Rolldown plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // rolldown.config.js
13
+ * import IsolatedDecl from 'unplugin-isolated-decl/rolldown'
14
+ *
15
+ * export default {
16
+ * plugins: [IsolatedDecl()],
17
+ * }
18
+ * ```
19
+ */
2
20
  declare const _default: typeof IsolatedDecl.rolldown;
3
21
  export default _default;
package/dist/rolldown.js CHANGED
@@ -1,5 +1,5 @@
1
- import "./transformer-w7XvOpWe.js";
2
- import { IsolatedDecl } from "./src_index-2WEAOr05.js";
1
+ import "./transformer-DDte86lZ.js";
2
+ import { IsolatedDecl } from "./src-B4aBGZxL.js";
3
3
 
4
4
  //#region src/rolldown.ts
5
5
  var rolldown_default = IsolatedDecl.rolldown;
package/dist/rollup.cjs CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index-NKcZyc90.cjs');
3
- require('./transformer-uKy0L3Za.cjs');
2
+ const require_src = require('./src-C3bhcfQ9.cjs');
3
+ require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/rollup.ts
6
- var rollup_default = require_src_index.IsolatedDecl.rollup;
6
+ var rollup_default = require_src.IsolatedDecl.rollup;
7
7
 
8
8
  //#endregion
9
9
  module.exports = rollup_default;
package/dist/rollup.d.cts CHANGED
@@ -1,3 +1,21 @@
1
+ /**
2
+ * This entry file is for Rollup plugin.
3
+ *
4
+ * @module
5
+ */
1
6
  import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Rollup plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // rollup.config.js
13
+ * import IsolatedDecl from 'unplugin-isolated-decl/rollup'
14
+ *
15
+ * export default {
16
+ * plugins: [IsolatedDecl()],
17
+ * }
18
+ * ```
19
+ */
2
20
  declare const _default: typeof IsolatedDecl.rollup;
3
21
  export = _default;
package/dist/rollup.d.ts CHANGED
@@ -1,3 +1,21 @@
1
+ /**
2
+ * This entry file is for Rollup plugin.
3
+ *
4
+ * @module
5
+ */
1
6
  import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Rollup plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // rollup.config.js
13
+ * import IsolatedDecl from 'unplugin-isolated-decl/rollup'
14
+ *
15
+ * export default {
16
+ * plugins: [IsolatedDecl()],
17
+ * }
18
+ * ```
19
+ */
2
20
  declare const _default: typeof IsolatedDecl.rollup;
3
21
  export default _default;
package/dist/rollup.js CHANGED
@@ -1,5 +1,5 @@
1
- import "./transformer-w7XvOpWe.js";
2
- import { IsolatedDecl } from "./src_index-2WEAOr05.js";
1
+ import "./transformer-DDte86lZ.js";
2
+ import { IsolatedDecl } from "./src-B4aBGZxL.js";
3
3
 
4
4
  //#region src/rollup.ts
5
5
  var rollup_default = IsolatedDecl.rollup;
@@ -1,6 +1,6 @@
1
- import { oxcTransform, swcTransform, tsTransform } from "./transformer-w7XvOpWe.js";
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 } from "node:path";
4
4
  import { createFilter } from "@rollup/pluginutils";
5
5
  import MagicString from "magic-string";
6
6
  import { parseAsync } from "oxc-parser";
@@ -20,39 +20,48 @@ function resolveOptions(options) {
20
20
  };
21
21
  }
22
22
 
23
+ //#endregion
24
+ //#region src/core/utils.ts
25
+ function lowestCommonAncestor(...filepaths) {
26
+ if (filepaths.length === 0) return "";
27
+ if (filepaths.length === 1) return path$1.dirname(filepaths[0]);
28
+ filepaths = filepaths.map((p) => p.replaceAll("\\", "/"));
29
+ const [first, ...rest] = filepaths;
30
+ let ancestor = first.split("/");
31
+ for (const filepath of rest) {
32
+ const directories = filepath.split("/", ancestor.length);
33
+ let index = 0;
34
+ for (const directory of directories) if (directory === ancestor[index]) index += 1;
35
+ else {
36
+ ancestor = ancestor.slice(0, index);
37
+ break;
38
+ }
39
+ ancestor = ancestor.slice(0, index);
40
+ }
41
+ return ancestor.length <= 1 && ancestor[0] === "" ? `/${ancestor[0]}` : ancestor.join("/");
42
+ }
43
+ function stripExt(filename) {
44
+ return filename.replace(/\.(.?)[jt]sx?$/, "");
45
+ }
46
+
23
47
  //#endregion
24
48
  //#region src/index.ts
25
49
  const IsolatedDecl = createUnplugin((rawOptions = {}) => {
26
50
  const options = resolveOptions(rawOptions);
27
51
  const filter = createFilter(options.include, options.exclude);
52
+ let farmPluginContext;
28
53
  const outputFiles = {};
29
54
  function addOutput(filename, source) {
30
55
  outputFiles[stripExt(filename)] = source;
31
56
  }
32
- const rollup = { renderStart(outputOptions, inputOptions) {
33
- const { input } = inputOptions;
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
- } };
57
+ const rollup = { renderStart: rollupRenderStart };
58
+ const farm = { renderStart: { executor: farmRenderStart } };
51
59
  return {
52
60
  name: "unplugin-isolated-decl",
53
- transformInclude(id) {
54
- return filter(id);
61
+ buildStart() {
62
+ farmPluginContext = this;
55
63
  },
64
+ transformInclude: (id) => filter(id),
56
65
  transform(code, id) {
57
66
  return transform(this, code, id);
58
67
  },
@@ -63,7 +72,8 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
63
72
  apply: "build",
64
73
  enforce: "pre",
65
74
  ...rollup
66
- }
75
+ },
76
+ farm
67
77
  };
68
78
  async function transform(context, code, id) {
69
79
  let program;
@@ -124,6 +134,51 @@ else {
124
134
  }
125
135
  }
126
136
  }
137
+ function rollupRenderStart(outputOptions, inputOptions) {
138
+ const { input } = inputOptions;
139
+ const inputMap = !Array.isArray(input) ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path.resolve(stripExt(v)), k])) : undefined;
140
+ const normalizedInput = Array.isArray(input) ? input : Object.values(input);
141
+ const outBase = lowestCommonAncestor(...normalizedInput);
142
+ if (typeof outputOptions.entryFileNames !== "string") return this.error("entryFileNames must be a string");
143
+ let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
144
+ if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
145
+ for (let [outname, source] of Object.entries(outputFiles)) {
146
+ const name = inputMap?.[outname] || path.relative(outBase, outname);
147
+ const fileName = entryFileNames.replace("[name]", name);
148
+ if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
149
+ this.emitFile({
150
+ type: "asset",
151
+ fileName,
152
+ source
153
+ });
154
+ }
155
+ }
156
+ function farmRenderStart(config) {
157
+ const { input = {}, output = {} } = config;
158
+ const inputMap = !Array.isArray(input) && input ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path.resolve(stripExt(v)), k])) : undefined;
159
+ const normalizedInput = Array.isArray(input) && input ? input : Object.values(input);
160
+ const outBase = lowestCommonAncestor(...normalizedInput);
161
+ if (output && typeof output.entryFilename !== "string") return console.error("entryFileName must be a string");
162
+ const extFormatMap = new Map([
163
+ ["cjs", "cjs"],
164
+ ["esm", "js"],
165
+ ["mjs", "js"]
166
+ ]);
167
+ output.entryFilename = "[entryName].[ext]";
168
+ output.entryFilename = output.entryFilename.replace("[ext]", extFormatMap.get(output.format || "esm") || "js");
169
+ let entryFileNames = output.entryFilename.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
170
+ if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
171
+ for (let [outname, source] of Object.entries(outputFiles)) {
172
+ const name = inputMap?.[outname] || path.relative(outBase, outname);
173
+ const fileName = entryFileNames.replace("[entryName]", name);
174
+ if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
175
+ farmPluginContext.emitFile({
176
+ type: "asset",
177
+ fileName,
178
+ source
179
+ });
180
+ }
181
+ }
127
182
  function esbuildSetup(build) {
128
183
  build.onEnd(async (result) => {
129
184
  const esbuildOptions = build.initialOptions;
@@ -169,48 +224,45 @@ else {
169
224
  });
170
225
  async function resolve(context, id, importer) {
171
226
  const nativeContext = context.getNativeBuildContext?.();
172
- if (nativeContext?.framework === "esbuild") {
173
- const resolved$1 = await nativeContext.build.resolve(id, {
174
- importer,
175
- resolveDir: path.dirname(importer),
176
- kind: "import-statement"
177
- });
178
- return {
179
- id: resolved$1.path,
180
- external: resolved$1.external
181
- };
227
+ switch (nativeContext?.framework) {
228
+ case "esbuild": {
229
+ const resolved = await nativeContext?.build.resolve(id, {
230
+ importer,
231
+ resolveDir: path.dirname(importer),
232
+ kind: "import-statement"
233
+ });
234
+ return {
235
+ id: resolved?.path,
236
+ external: resolved?.external
237
+ };
238
+ }
239
+ case "farm": {
240
+ const resolved = await nativeContext?.context.resolve({
241
+ source: id,
242
+ importer,
243
+ kind: "import"
244
+ }, {
245
+ meta: {},
246
+ caller: "unplugin-isolated-decl"
247
+ });
248
+ return {
249
+ id: resolved.resolvedPath,
250
+ external: !!resolved.external
251
+ };
252
+ }
253
+ default: {
254
+ const resolved = await context.resolve(id, importer);
255
+ if (!resolved) return;
256
+ return {
257
+ id: resolved.id,
258
+ external: !!resolved.external
259
+ };
260
+ }
182
261
  }
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
262
  }
193
263
  function patchCjsDefaultExport(source) {
194
264
  return source.replace(/(?<=(?:[;}]|^)\s*export\s*)(?:\{\s*([\w$]+)\s*as\s+default\s*\}|default\s+([\w$]+))/, (_, s1, s2) => `= ${s1 || s2}`);
195
265
  }
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
266
 
215
267
  //#endregion
216
- export { IsolatedDecl, lowestCommonAncestor };
268
+ export { IsolatedDecl };
@@ -22,9 +22,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
  }) : target, mod));
23
23
 
24
24
  //#endregion
25
- const require_transformer = require('./transformer-uKy0L3Za.cjs');
25
+ const require_transformer = require('./transformer-C4rLQvdl.cjs');
26
26
  const { mkdir, readFile, writeFile } = __toESM(require("node:fs/promises"));
27
- const { default: path } = __toESM(require("node:path"));
27
+ const { default: path, default: path$1 } = __toESM(require("node:path"));
28
28
  const { createFilter } = __toESM(require("@rollup/pluginutils"));
29
29
  const { default: MagicString } = __toESM(require("magic-string"));
30
30
  const { parseAsync } = __toESM(require("oxc-parser"));
@@ -44,39 +44,48 @@ function resolveOptions(options) {
44
44
  };
45
45
  }
46
46
 
47
+ //#endregion
48
+ //#region src/core/utils.ts
49
+ function lowestCommonAncestor(...filepaths) {
50
+ if (filepaths.length === 0) return "";
51
+ if (filepaths.length === 1) return path$1.dirname(filepaths[0]);
52
+ filepaths = filepaths.map((p) => p.replaceAll("\\", "/"));
53
+ const [first, ...rest] = filepaths;
54
+ let ancestor = first.split("/");
55
+ for (const filepath of rest) {
56
+ const directories = filepath.split("/", ancestor.length);
57
+ let index = 0;
58
+ for (const directory of directories) if (directory === ancestor[index]) index += 1;
59
+ else {
60
+ ancestor = ancestor.slice(0, index);
61
+ break;
62
+ }
63
+ ancestor = ancestor.slice(0, index);
64
+ }
65
+ return ancestor.length <= 1 && ancestor[0] === "" ? `/${ancestor[0]}` : ancestor.join("/");
66
+ }
67
+ function stripExt(filename) {
68
+ return filename.replace(/\.(.?)[jt]sx?$/, "");
69
+ }
70
+
47
71
  //#endregion
48
72
  //#region src/index.ts
49
73
  const IsolatedDecl = createUnplugin((rawOptions = {}) => {
50
74
  const options = resolveOptions(rawOptions);
51
75
  const filter = createFilter(options.include, options.exclude);
76
+ let farmPluginContext;
52
77
  const outputFiles = {};
53
78
  function addOutput(filename, source) {
54
79
  outputFiles[stripExt(filename)] = source;
55
80
  }
56
- const rollup = { renderStart(outputOptions, inputOptions) {
57
- const { input } = inputOptions;
58
- const inputMap = !Array.isArray(input) ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path.resolve(stripExt(v)), k])) : undefined;
59
- const normalizedInput = Array.isArray(input) ? input : Object.values(input);
60
- const outBase = lowestCommonAncestor(...normalizedInput);
61
- if (typeof outputOptions.entryFileNames !== "string") return this.error("entryFileNames must be a string");
62
- let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
63
- if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
64
- for (let [outname, source] of Object.entries(outputFiles)) {
65
- const name = inputMap?.[outname] || path.relative(outBase, outname);
66
- const fileName = entryFileNames.replace("[name]", name);
67
- if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
68
- this.emitFile({
69
- type: "asset",
70
- fileName,
71
- source
72
- });
73
- }
74
- } };
81
+ const rollup = { renderStart: rollupRenderStart };
82
+ const farm = { renderStart: { executor: farmRenderStart } };
75
83
  return {
76
84
  name: "unplugin-isolated-decl",
77
- transformInclude(id) {
78
- return filter(id);
85
+ buildStart() {
86
+ farmPluginContext = this;
79
87
  },
88
+ transformInclude: (id) => filter(id),
80
89
  transform(code, id) {
81
90
  return transform(this, code, id);
82
91
  },
@@ -87,7 +96,8 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
87
96
  apply: "build",
88
97
  enforce: "pre",
89
98
  ...rollup
90
- }
99
+ },
100
+ farm
91
101
  };
92
102
  async function transform(context, code, id) {
93
103
  let program;
@@ -148,6 +158,51 @@ else {
148
158
  }
149
159
  }
150
160
  }
161
+ function rollupRenderStart(outputOptions, inputOptions) {
162
+ const { input } = inputOptions;
163
+ const inputMap = !Array.isArray(input) ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path.resolve(stripExt(v)), k])) : undefined;
164
+ const normalizedInput = Array.isArray(input) ? input : Object.values(input);
165
+ const outBase = lowestCommonAncestor(...normalizedInput);
166
+ if (typeof outputOptions.entryFileNames !== "string") return this.error("entryFileNames must be a string");
167
+ let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
168
+ if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
169
+ for (let [outname, source] of Object.entries(outputFiles)) {
170
+ const name = inputMap?.[outname] || path.relative(outBase, outname);
171
+ const fileName = entryFileNames.replace("[name]", name);
172
+ if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
173
+ this.emitFile({
174
+ type: "asset",
175
+ fileName,
176
+ source
177
+ });
178
+ }
179
+ }
180
+ function farmRenderStart(config) {
181
+ const { input = {}, output = {} } = config;
182
+ const inputMap = !Array.isArray(input) && input ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path.resolve(stripExt(v)), k])) : undefined;
183
+ const normalizedInput = Array.isArray(input) && input ? input : Object.values(input);
184
+ const outBase = lowestCommonAncestor(...normalizedInput);
185
+ if (output && typeof output.entryFilename !== "string") return console.error("entryFileName must be a string");
186
+ const extFormatMap = new Map([
187
+ ["cjs", "cjs"],
188
+ ["esm", "js"],
189
+ ["mjs", "js"]
190
+ ]);
191
+ output.entryFilename = "[entryName].[ext]";
192
+ output.entryFilename = output.entryFilename.replace("[ext]", extFormatMap.get(output.format || "esm") || "js");
193
+ let entryFileNames = output.entryFilename.replace(/\.(.)?[jt]sx?$/, (_, s) => `.d.${s || ""}ts`);
194
+ if (options.extraOutdir) entryFileNames = path.join(options.extraOutdir, entryFileNames);
195
+ for (let [outname, source] of Object.entries(outputFiles)) {
196
+ const name = inputMap?.[outname] || path.relative(outBase, outname);
197
+ const fileName = entryFileNames.replace("[entryName]", name);
198
+ if (options.patchCjsDefaultExport && fileName.endsWith(".d.cts")) source = patchCjsDefaultExport(source);
199
+ farmPluginContext.emitFile({
200
+ type: "asset",
201
+ fileName,
202
+ source
203
+ });
204
+ }
205
+ }
151
206
  function esbuildSetup(build) {
152
207
  build.onEnd(async (result) => {
153
208
  const esbuildOptions = build.initialOptions;
@@ -193,48 +248,45 @@ else {
193
248
  });
194
249
  async function resolve(context, id, importer) {
195
250
  const nativeContext = context.getNativeBuildContext?.();
196
- if (nativeContext?.framework === "esbuild") {
197
- const resolved$1 = await nativeContext.build.resolve(id, {
198
- importer,
199
- resolveDir: path.dirname(importer),
200
- kind: "import-statement"
201
- });
202
- return {
203
- id: resolved$1.path,
204
- external: resolved$1.external
205
- };
251
+ switch (nativeContext?.framework) {
252
+ case "esbuild": {
253
+ const resolved = await nativeContext?.build.resolve(id, {
254
+ importer,
255
+ resolveDir: path.dirname(importer),
256
+ kind: "import-statement"
257
+ });
258
+ return {
259
+ id: resolved?.path,
260
+ external: resolved?.external
261
+ };
262
+ }
263
+ case "farm": {
264
+ const resolved = await nativeContext?.context.resolve({
265
+ source: id,
266
+ importer,
267
+ kind: "import"
268
+ }, {
269
+ meta: {},
270
+ caller: "unplugin-isolated-decl"
271
+ });
272
+ return {
273
+ id: resolved.resolvedPath,
274
+ external: !!resolved.external
275
+ };
276
+ }
277
+ default: {
278
+ const resolved = await context.resolve(id, importer);
279
+ if (!resolved) return;
280
+ return {
281
+ id: resolved.id,
282
+ external: !!resolved.external
283
+ };
284
+ }
206
285
  }
207
- const resolved = await context.resolve(id, importer);
208
- if (!resolved) return;
209
- return {
210
- id: resolved.id,
211
- external: !!resolved.external
212
- };
213
- }
214
- function stripExt(filename) {
215
- return filename.replace(/\.(.?)[jt]sx?$/, "");
216
286
  }
217
287
  function patchCjsDefaultExport(source) {
218
288
  return source.replace(/(?<=(?:[;}]|^)\s*export\s*)(?:\{\s*([\w$]+)\s*as\s+default\s*\}|default\s+([\w$]+))/, (_, s1, s2) => `= ${s1 || s2}`);
219
289
  }
220
- function lowestCommonAncestor(...filepaths) {
221
- if (filepaths.length === 0) return "";
222
- if (filepaths.length === 1) return path.dirname(filepaths[0]);
223
- filepaths = filepaths.map((p) => p.replaceAll("\\", "/"));
224
- const [first, ...rest] = filepaths;
225
- let ancestor = first.split("/");
226
- for (const filepath of rest) {
227
- const directories = filepath.split("/", ancestor.length);
228
- let index = 0;
229
- for (const directory of directories) if (directory === ancestor[index]) index += 1;
230
- else {
231
- ancestor = ancestor.slice(0, index);
232
- break;
233
- }
234
- ancestor = ancestor.slice(0, index);
235
- }
236
- return ancestor.length <= 1 && ancestor[0] === "" ? `/${ancestor[0]}` : ancestor.join("/");
237
- }
238
290
 
239
291
  //#endregion
240
292
  Object.defineProperty(exports, 'IsolatedDecl', {
@@ -242,9 +294,4 @@ Object.defineProperty(exports, 'IsolatedDecl', {
242
294
  get: function () {
243
295
  return IsolatedDecl;
244
296
  }
245
- });Object.defineProperty(exports, 'lowestCommonAncestor', {
246
- enumerable: true,
247
- get: function () {
248
- return lowestCommonAncestor;
249
- }
250
297
  });
package/dist/vite.cjs CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index-NKcZyc90.cjs');
3
- require('./transformer-uKy0L3Za.cjs');
2
+ const require_src = require('./src-C3bhcfQ9.cjs');
3
+ require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/vite.ts
6
- var vite_default = require_src_index.IsolatedDecl.vite;
6
+ var vite_default = require_src.IsolatedDecl.vite;
7
7
 
8
8
  //#endregion
9
9
  module.exports = vite_default;
package/dist/vite.d.cts CHANGED
@@ -1,3 +1,21 @@
1
+ /**
2
+ * This entry file is for Vite plugin.
3
+ *
4
+ * @module
5
+ */
1
6
  import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Vite plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // vite.config.js
13
+ * import IsolatedDecl from 'unplugin-isolated-decl/vite'
14
+ *
15
+ * export default {
16
+ * plugins: [IsolatedDecl()],
17
+ * }
18
+ * ```
19
+ */
2
20
  declare const _default: typeof IsolatedDecl.vite;
3
21
  export = _default;
package/dist/vite.d.ts CHANGED
@@ -1,3 +1,21 @@
1
+ /**
2
+ * This entry file is for Vite plugin.
3
+ *
4
+ * @module
5
+ */
1
6
  import { IsolatedDecl } from "./index.js";
7
+ /**
8
+ * Vite plugin
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * // vite.config.js
13
+ * import IsolatedDecl from 'unplugin-isolated-decl/vite'
14
+ *
15
+ * export default {
16
+ * plugins: [IsolatedDecl()],
17
+ * }
18
+ * ```
19
+ */
2
20
  declare const _default: typeof IsolatedDecl.vite;
3
21
  export default _default;
package/dist/vite.js CHANGED
@@ -1,5 +1,5 @@
1
- import "./transformer-w7XvOpWe.js";
2
- import { IsolatedDecl } from "./src_index-2WEAOr05.js";
1
+ import "./transformer-DDte86lZ.js";
2
+ import { IsolatedDecl } from "./src-B4aBGZxL.js";
3
3
 
4
4
  //#region src/vite.ts
5
5
  var vite_default = IsolatedDecl.vite;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-isolated-decl",
3
- "version": "0.6.8",
3
+ "version": "0.7.0",
4
4
  "description": "A blazing-fast tool for generating isolated declarations.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -47,6 +47,10 @@
47
47
  "require": "./dist/esbuild.cjs",
48
48
  "import": "./dist/esbuild.js"
49
49
  },
50
+ "./farm": {
51
+ "require": "./dist/farm.cjs",
52
+ "import": "./dist/farm.js"
53
+ },
50
54
  "./api": {
51
55
  "require": "./dist/api.cjs",
52
56
  "import": "./dist/api.js"
@@ -86,29 +90,30 @@
86
90
  "dependencies": {
87
91
  "@rollup/pluginutils": "^5.1.3",
88
92
  "magic-string": "^0.30.12",
89
- "oxc-parser": "^0.34.0",
90
- "unplugin": "^1.14.1"
93
+ "oxc-parser": "^0.36.0",
94
+ "unplugin": "^1.16.0"
91
95
  },
92
96
  "devDependencies": {
93
- "@oxc-project/types": "^0.34.0",
94
- "@swc/core": "^1.7.40",
95
- "@sxzz/eslint-config": "^4.4.0",
97
+ "@farmfe/core": "^1.4.1",
98
+ "@oxc-project/types": "^0.36.0",
99
+ "@swc/core": "^1.9.2",
100
+ "@sxzz/eslint-config": "^4.4.1",
96
101
  "@sxzz/prettier-config": "^2.0.2",
97
- "@types/node": "^22.8.1",
98
- "bumpp": "^9.7.1",
102
+ "@types/node": "^22.9.0",
103
+ "bumpp": "^9.8.1",
99
104
  "esbuild": "^0.24.0",
100
- "eslint": "^9.13.0",
105
+ "eslint": "^9.15.0",
101
106
  "fast-glob": "^3.3.2",
102
- "oxc-transform": "^0.34.0",
107
+ "oxc-transform": "^0.36.0",
103
108
  "prettier": "^3.3.3",
104
109
  "rolldown": "nightly",
105
- "rollup": "^4.24.2",
110
+ "rollup": "^4.27.2",
106
111
  "rollup-plugin-esbuild": "^6.1.1",
107
- "tsdown": "^0.2.17",
112
+ "tsdown": "^0.3.0",
108
113
  "tsx": "^4.19.2",
109
114
  "typescript": "^5.6.3",
110
- "vite": "^5.4.10",
111
- "vitest": "^2.1.3"
115
+ "vite": "^5.4.11",
116
+ "vitest": "^2.1.5"
112
117
  },
113
118
  "engines": {
114
119
  "node": ">=18.12.0"