unplugin-isolated-decl 0.8.1 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import type * as OxcTypes from '@oxc-project/types';
2
2
  import type MagicString from 'magic-string';
3
3
  export type OxcImport = (OxcTypes.ImportDeclaration | OxcTypes.ExportAllDeclaration | OxcTypes.ExportNamedDeclaration) & {
4
4
  source: OxcTypes.StringLiteral;
5
- suffix?: string;
5
+ ext?: string;
6
6
  };
7
7
  export declare function filterImports(program: OxcTypes.Program): OxcImport[];
8
8
  export declare function rewriteImports(s: MagicString, imports: OxcImport[], entryMap: Record<string, string> | undefined, inputBase: string, entryFileNames: string, srcFilename: string): string;
@@ -2,7 +2,7 @@ import type * as OxcTypes from '@oxc-project/types';
2
2
  import type MagicString from 'magic-string';
3
3
  export type OxcImport = (OxcTypes.ImportDeclaration | OxcTypes.ExportAllDeclaration | OxcTypes.ExportNamedDeclaration) & {
4
4
  source: OxcTypes.StringLiteral;
5
- suffix?: string;
5
+ ext?: string;
6
6
  };
7
7
  export declare function filterImports(program: OxcTypes.Program): OxcImport[];
8
8
  export declare function rewriteImports(s: MagicString, imports: OxcImport[], entryMap: Record<string, string> | undefined, inputBase: string, entryFileNames: string, srcFilename: string): string;
@@ -14,6 +14,8 @@ export type Options = {
14
14
  autoAddExts?: boolean;
15
15
  /** Patch `export default` in `.d.cts` to `export = ` */
16
16
  patchCjsDefaultExport?: boolean;
17
+ /** Base directory for input files. */
18
+ inputBase?: string;
17
19
  rewriteImports?: (id: string, importer: string) => string | void | null | undefined;
18
20
  } & ({
19
21
  /**
@@ -31,6 +33,6 @@ export type Options = {
31
33
  transformOptions?: TranspileOptions;
32
34
  });
33
35
  type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
34
- export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, 'enforce' | 'extraOutdir' | 'rewriteImports'>>;
36
+ export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, 'enforce' | 'extraOutdir' | 'rewriteImports' | 'inputBase'>>;
35
37
  export declare function resolveOptions(options: Options): OptionsResolved;
36
38
  export {};
@@ -14,6 +14,8 @@ export type Options = {
14
14
  autoAddExts?: boolean;
15
15
  /** Patch `export default` in `.d.cts` to `export = ` */
16
16
  patchCjsDefaultExport?: boolean;
17
+ /** Base directory for input files. */
18
+ inputBase?: string;
17
19
  rewriteImports?: (id: string, importer: string) => string | void | null | undefined;
18
20
  } & ({
19
21
  /**
@@ -31,6 +33,6 @@ export type Options = {
31
33
  transformOptions?: TranspileOptions;
32
34
  });
33
35
  type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
34
- export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, 'enforce' | 'extraOutdir' | 'rewriteImports'>>;
36
+ export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, 'enforce' | 'extraOutdir' | 'rewriteImports' | 'inputBase'>>;
35
37
  export declare function resolveOptions(options: Options): OptionsResolved;
36
38
  export {};
@@ -2,7 +2,8 @@ import Debug from 'debug';
2
2
  export declare const debug: Debug.Debugger;
3
3
  export declare function lowestCommonAncestor(...filepaths: string[]): string;
4
4
  export declare function stripExt(filename: string): string;
5
- export declare function resolveEntry(input: string | string[] | Record<string, string>): {
5
+ export declare function resolveEntry(input: string | string[] | Record<string, string>, userInputBase?: string): {
6
6
  entryMap: Record<string, string> | undefined;
7
7
  inputBase: string;
8
8
  };
9
+ export declare function guessExt(filename: string): string;
@@ -2,7 +2,8 @@ import Debug from 'debug';
2
2
  export declare const debug: Debug.Debugger;
3
3
  export declare function lowestCommonAncestor(...filepaths: string[]): string;
4
4
  export declare function stripExt(filename: string): string;
5
- export declare function resolveEntry(input: string | string[] | Record<string, string>): {
5
+ export declare function resolveEntry(input: string | string[] | Record<string, string>, userInputBase?: string): {
6
6
  entryMap: Record<string, string> | undefined;
7
7
  inputBase: string;
8
8
  };
9
+ export declare function guessExt(filename: string): string;
package/dist/esbuild.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src = require('./src-D3Z1XJCx.cjs');
2
+ const require_src = require('./src-wtIilNZo.cjs');
3
3
  require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/esbuild.ts
package/dist/esbuild.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-DDte86lZ.js";
2
- import { IsolatedDecl } from "./src-BiFhTk02.js";
2
+ import { IsolatedDecl } from "./src-CglEH7vG.js";
3
3
 
4
4
  //#region src/esbuild.ts
5
5
  var esbuild_default = IsolatedDecl.esbuild;
package/dist/farm.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src = require('./src-D3Z1XJCx.cjs');
2
+ const require_src = require('./src-wtIilNZo.cjs');
3
3
  require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/farm.ts
package/dist/farm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-DDte86lZ.js";
2
- import { IsolatedDecl } from "./src-BiFhTk02.js";
2
+ import { IsolatedDecl } from "./src-CglEH7vG.js";
3
3
 
4
4
  //#region src/farm.ts
5
5
  var farm_default = IsolatedDecl.farm;
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_src = require('./src-D3Z1XJCx.cjs');
1
+ const require_src = require('./src-wtIilNZo.cjs');
2
2
  require('./transformer-C4rLQvdl.cjs');
3
3
 
4
4
  exports.IsolatedDecl = require_src.IsolatedDecl
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import "./transformer-DDte86lZ.js";
2
- import { IsolatedDecl } from "./src-BiFhTk02.js";
2
+ import { IsolatedDecl } from "./src-CglEH7vG.js";
3
3
 
4
4
  export { IsolatedDecl };
package/dist/rolldown.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src = require('./src-D3Z1XJCx.cjs');
2
+ const require_src = require('./src-wtIilNZo.cjs');
3
3
  require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/rolldown.ts
package/dist/rolldown.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-DDte86lZ.js";
2
- import { IsolatedDecl } from "./src-BiFhTk02.js";
2
+ import { IsolatedDecl } from "./src-CglEH7vG.js";
3
3
 
4
4
  //#region src/rolldown.ts
5
5
  var rolldown_default = IsolatedDecl.rolldown;
package/dist/rollup.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src = require('./src-D3Z1XJCx.cjs');
2
+ const require_src = require('./src-wtIilNZo.cjs');
3
3
  require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/rollup.ts
package/dist/rollup.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-DDte86lZ.js";
2
- import { IsolatedDecl } from "./src-BiFhTk02.js";
2
+ import { IsolatedDecl } from "./src-CglEH7vG.js";
3
3
 
4
4
  //#region src/rollup.ts
5
5
  var rollup_default = IsolatedDecl.rollup;
@@ -30,16 +30,20 @@ else {
30
30
  function stripExt(filename) {
31
31
  return filename.replace(/\.(.?)[jt]sx?$/, "");
32
32
  }
33
- function resolveEntry(input) {
33
+ function resolveEntry(input, userInputBase) {
34
34
  if (typeof input === "string") input = [input];
35
35
  const entryMap = !Array.isArray(input) ? Object.fromEntries(Object.entries(input).map(([k, v]) => [path$2.resolve(stripExt(v)), k])) : undefined;
36
36
  const arr = Array.isArray(input) && input ? input : Object.values(input);
37
- const inputBase = lowestCommonAncestor(...arr);
37
+ const inputBase = userInputBase || lowestCommonAncestor(...arr);
38
38
  return {
39
39
  entryMap,
40
40
  inputBase
41
41
  };
42
42
  }
43
+ function guessExt(filename) {
44
+ const ext = path$2.extname(filename).slice(1);
45
+ return ext.replace(/^([cm]?)ts/, (_, $1) => `${$1}js`);
46
+ }
43
47
 
44
48
  //#endregion
45
49
  //#region src/core/ast.ts
@@ -68,9 +72,10 @@ function rewriteImports(s, imports, entryMap, inputBase, entryFileNames, srcFile
68
72
  const resolved = stripExt(entryFileNames.replaceAll("[name]", importAlias));
69
73
  id = pathRelative(srcDirRel, resolved);
70
74
  } else id = stripExt(entryFileNames.replaceAll("[name]", srcIdRel));
71
- if (i.suffix) id += i.suffix;
75
+ const ext = i.ext || guessExt(source.value);
76
+ if (ext) id += `.${ext}`;
72
77
  let final = path$1.normalize(path$1.join(offset, id));
73
- if (final !== source.value) {
78
+ if (final !== path$1.normalize(source.value)) {
74
79
  debug("Patch import in", srcRel, ":", srcIdRel, "->", final);
75
80
  final = final.replaceAll("\\", "/");
76
81
  if (!/^\.\.?\//.test(final)) final = `./${final}`;
@@ -95,7 +100,8 @@ function resolveOptions(options) {
95
100
  extraOutdir: options.extraOutdir,
96
101
  autoAddExts: options.autoAddExts || false,
97
102
  patchCjsDefaultExport: options.patchCjsDefaultExport || false,
98
- rewriteImports: options.rewriteImports
103
+ rewriteImports: options.rewriteImports,
104
+ inputBase: options.inputBase
99
105
  };
100
106
  }
101
107
 
@@ -168,7 +174,7 @@ else {
168
174
  if (options.autoAddExts && (path.isAbsolute(value) || value[0] === ".")) {
169
175
  const resolved = await resolve(context, value, stripExt(id));
170
176
  if (!resolved || resolved.external) continue;
171
- if (resolved.id.endsWith(".ts") || resolved.id.endsWith(".tsx")) i.suffix = ".js";
177
+ i.ext = guessExt(resolved.id);
172
178
  }
173
179
  }
174
180
  addOutput(id, {
@@ -182,8 +188,7 @@ else {
182
188
  if (node.type === "ImportDeclaration") return !!node.specifiers && node.specifiers.every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
183
189
  return node.type === "ExportNamedDeclaration" && node.specifiers && node.specifiers.every((spec) => spec.exportKind === "type");
184
190
  });
185
- for (const i of typeImports) {
186
- const { source } = i;
191
+ for (const { source } of typeImports) {
187
192
  const resolved = (await resolve(context, source.value, id))?.id;
188
193
  if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
189
194
  let source$1;
@@ -199,7 +204,7 @@ else {
199
204
  }
200
205
  function rollupRenderStart(outputOptions, inputOptions) {
201
206
  const { input } = inputOptions;
202
- const { inputBase, entryMap } = resolveEntry(input);
207
+ const { inputBase, entryMap } = resolveEntry(input, options.inputBase);
203
208
  debug("[rollup] input base:", inputBase);
204
209
  let { entryFileNames = "[name].js" } = outputOptions;
205
210
  if (typeof entryFileNames !== "string") return this.error("entryFileNames must be a string");
@@ -218,8 +223,8 @@ else {
218
223
  }
219
224
  function farmRenderStart(config) {
220
225
  const { input = {}, output = {} } = config;
221
- const { inputBase, entryMap } = resolveEntry(input);
222
- debug("[farm] out base:", inputBase);
226
+ const { inputBase, entryMap } = resolveEntry(input, options.inputBase);
227
+ debug("[farm] input base:", inputBase);
223
228
  if (output && typeof output.entryFilename !== "string") return console.error("entryFileName must be a string");
224
229
  const extFormatMap = new Map([
225
230
  ["cjs", "cjs"],
@@ -247,8 +252,8 @@ else {
247
252
  const esbuildOptions = build.initialOptions;
248
253
  const entries = esbuildOptions.entryPoints;
249
254
  if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
250
- const inputBase = lowestCommonAncestor(...entries);
251
- debug("[esbuild] out base:", inputBase);
255
+ const inputBase = options.inputBase || lowestCommonAncestor(...entries);
256
+ debug("[esbuild] input base:", inputBase);
252
257
  const jsExt = esbuildOptions.outExtension?.[".js"];
253
258
  let outExt;
254
259
  switch (jsExt) {
@@ -54,16 +54,20 @@ else {
54
54
  function stripExt(filename) {
55
55
  return filename.replace(/\.(.?)[jt]sx?$/, "");
56
56
  }
57
- function resolveEntry(input) {
57
+ function resolveEntry(input, userInputBase) {
58
58
  if (typeof input === "string") input = [input];
59
59
  const entryMap = !Array.isArray(input) ? Object.fromEntries(Object.entries(input).map(([k, v]) => [node_path.default.resolve(stripExt(v)), k])) : undefined;
60
60
  const arr = Array.isArray(input) && input ? input : Object.values(input);
61
- const inputBase = lowestCommonAncestor(...arr);
61
+ const inputBase = userInputBase || lowestCommonAncestor(...arr);
62
62
  return {
63
63
  entryMap,
64
64
  inputBase
65
65
  };
66
66
  }
67
+ function guessExt(filename) {
68
+ const ext = node_path.default.extname(filename).slice(1);
69
+ return ext.replace(/^([cm]?)ts/, (_, $1) => `${$1}js`);
70
+ }
67
71
 
68
72
  //#endregion
69
73
  //#region src/core/ast.ts
@@ -92,9 +96,10 @@ function rewriteImports(s, imports, entryMap, inputBase, entryFileNames, srcFile
92
96
  const resolved = stripExt(entryFileNames.replaceAll("[name]", importAlias));
93
97
  id = pathRelative(srcDirRel, resolved);
94
98
  } else id = stripExt(entryFileNames.replaceAll("[name]", srcIdRel));
95
- if (i.suffix) id += i.suffix;
99
+ const ext = i.ext || guessExt(source.value);
100
+ if (ext) id += `.${ext}`;
96
101
  let final = node_path.default.normalize(node_path.default.join(offset, id));
97
- if (final !== source.value) {
102
+ if (final !== node_path.default.normalize(source.value)) {
98
103
  debug$1("Patch import in", srcRel, ":", srcIdRel, "->", final);
99
104
  final = final.replaceAll("\\", "/");
100
105
  if (!/^\.\.?\//.test(final)) final = `./${final}`;
@@ -119,7 +124,8 @@ function resolveOptions(options) {
119
124
  extraOutdir: options.extraOutdir,
120
125
  autoAddExts: options.autoAddExts || false,
121
126
  patchCjsDefaultExport: options.patchCjsDefaultExport || false,
122
- rewriteImports: options.rewriteImports
127
+ rewriteImports: options.rewriteImports,
128
+ inputBase: options.inputBase
123
129
  };
124
130
  }
125
131
 
@@ -192,7 +198,7 @@ else {
192
198
  if (options.autoAddExts && (node_path.default.isAbsolute(value) || value[0] === ".")) {
193
199
  const resolved = await resolve(context, value, stripExt(id));
194
200
  if (!resolved || resolved.external) continue;
195
- if (resolved.id.endsWith(".ts") || resolved.id.endsWith(".tsx")) i.suffix = ".js";
201
+ i.ext = guessExt(resolved.id);
196
202
  }
197
203
  }
198
204
  addOutput(id, {
@@ -206,8 +212,7 @@ else {
206
212
  if (node.type === "ImportDeclaration") return !!node.specifiers && node.specifiers.every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
207
213
  return node.type === "ExportNamedDeclaration" && node.specifiers && node.specifiers.every((spec) => spec.exportKind === "type");
208
214
  });
209
- for (const i of typeImports) {
210
- const { source } = i;
215
+ for (const { source } of typeImports) {
211
216
  const resolved = (await resolve(context, source.value, id))?.id;
212
217
  if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
213
218
  let source$1;
@@ -223,7 +228,7 @@ else {
223
228
  }
224
229
  function rollupRenderStart(outputOptions, inputOptions) {
225
230
  const { input } = inputOptions;
226
- const { inputBase, entryMap } = resolveEntry(input);
231
+ const { inputBase, entryMap } = resolveEntry(input, options.inputBase);
227
232
  debug$1("[rollup] input base:", inputBase);
228
233
  let { entryFileNames = "[name].js" } = outputOptions;
229
234
  if (typeof entryFileNames !== "string") return this.error("entryFileNames must be a string");
@@ -242,8 +247,8 @@ else {
242
247
  }
243
248
  function farmRenderStart(config) {
244
249
  const { input = {}, output = {} } = config;
245
- const { inputBase, entryMap } = resolveEntry(input);
246
- debug$1("[farm] out base:", inputBase);
250
+ const { inputBase, entryMap } = resolveEntry(input, options.inputBase);
251
+ debug$1("[farm] input base:", inputBase);
247
252
  if (output && typeof output.entryFilename !== "string") return console.error("entryFileName must be a string");
248
253
  const extFormatMap = new Map([
249
254
  ["cjs", "cjs"],
@@ -271,8 +276,8 @@ else {
271
276
  const esbuildOptions = build.initialOptions;
272
277
  const entries = esbuildOptions.entryPoints;
273
278
  if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
274
- const inputBase = lowestCommonAncestor(...entries);
275
- debug$1("[esbuild] out base:", inputBase);
279
+ const inputBase = options.inputBase || lowestCommonAncestor(...entries);
280
+ debug$1("[esbuild] input base:", inputBase);
276
281
  const jsExt = esbuildOptions.outExtension?.[".js"];
277
282
  let outExt;
278
283
  switch (jsExt) {
package/dist/vite.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src = require('./src-D3Z1XJCx.cjs');
2
+ const require_src = require('./src-wtIilNZo.cjs');
3
3
  require('./transformer-C4rLQvdl.cjs');
4
4
 
5
5
  //#region src/vite.ts
package/dist/vite.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-DDte86lZ.js";
2
- import { IsolatedDecl } from "./src-BiFhTk02.js";
2
+ import { IsolatedDecl } from "./src-CglEH7vG.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.8.1",
3
+ "version": "0.8.3",
4
4
  "description": "A blazing-fast tool for generating isolated declarations.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -95,11 +95,11 @@
95
95
  "unplugin": "^1.16.0"
96
96
  },
97
97
  "devDependencies": {
98
- "@farmfe/core": "^1.4.3",
98
+ "@farmfe/core": "^1.4.4",
99
99
  "@oxc-project/types": "^0.37.0",
100
100
  "@rollup/plugin-alias": "^5.1.1",
101
101
  "@swc/core": "^1.9.3",
102
- "@sxzz/eslint-config": "^4.4.1",
102
+ "@sxzz/eslint-config": "^4.5.0",
103
103
  "@sxzz/prettier-config": "^2.0.2",
104
104
  "@sxzz/test-utils": "^0.3.7",
105
105
  "@types/debug": "^4.1.12",