rolldown-plugin-dts 0.2.0 → 0.3.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/README.md CHANGED
@@ -51,14 +51,8 @@ interface Options {
51
51
  */
52
52
  inputAlias?: Record<string, string>
53
53
 
54
- /**
55
- * Determines whether the module imported by `.d.ts` files should be treated as external or not.
56
- */
57
- external?: (
58
- id: string,
59
- importer: string,
60
- extraOptions: ResolveIdExtraOptions,
61
- ) => boolean | void
54
+ /** Resolve external types used in dts files from `node_modules` */
55
+ resolve?: boolean | (string | RegExp)[]
62
56
  }
63
57
  ````
64
58
 
package/dist/index.d.ts CHANGED
@@ -1,16 +1,15 @@
1
1
  import { IsolatedDeclarationsOptions } from "oxc-transform";
2
- import { FunctionPluginHooks, Plugin } from "rolldown";
2
+ import { Plugin } from "rolldown";
3
3
 
4
4
  //#region src/fake-js.d.ts
5
5
  declare function createFakeJsPlugin({ dtsInput }: Pick<Options, "dtsInput">): Plugin;
6
6
 
7
7
  //#endregion
8
8
  //#region src/generate.d.ts
9
- declare function createGeneratePlugin({ isolatedDeclaration, inputAlias, external }: Pick<Options, "external" | "isolatedDeclaration" | "inputAlias">): Plugin;
9
+ declare function createGeneratePlugin({ isolatedDeclaration, inputAlias, resolve }: Pick<Options, "isolatedDeclaration" | "inputAlias" | "resolve">): Plugin;
10
10
 
11
11
  //#endregion
12
12
  //#region src/index.d.ts
13
- type ResolveIdExtraOptions = Parameters<FunctionPluginHooks["resolveId"]>[2];
14
13
  interface Options {
15
14
  /**
16
15
  * When entries are `.d.ts` files (instead of `.ts` files), this option should be set to `true`.
@@ -29,10 +28,8 @@ interface Options {
29
28
  * }
30
29
  */
31
30
  inputAlias?: Record<string, string>;
32
- /**
33
- * Determines whether the module imported by `.d.ts` files should be treated as external or not.
34
- */
35
- external?: (id: string, importer: string, extraOptions: ResolveIdExtraOptions) => boolean | void;
31
+ /** Resolve external types used in dts files from `node_modules` */
32
+ resolve?: boolean | (string | RegExp)[];
36
33
  }
37
34
  declare function dts(options?: Options): Plugin[];
38
35
 
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { MagicStringAST } from "magic-string-ast";
2
2
  import { parseAsync } from "oxc-parser";
3
3
  import path, { basename } from "node:path";
4
+ import { createResolver } from "dts-resolver";
4
5
  import { isolatedDeclaration } from "oxc-transform";
5
6
 
6
7
  //#region node_modules/.pnpm/estree-walker@3.0.3/node_modules/estree-walker/src/walker.js
@@ -234,6 +235,7 @@ function createFakeJsPlugin({ dtsInput }) {
234
235
  };
235
236
  } : void 0,
236
237
  outputOptions(options) {
238
+ if (options.format === "cjs" || options.format === "commonjs") throw new Error("[rolldown-plugin-dts] Cannot bundle dts files with `cjs` format.");
237
239
  return {
238
240
  ...options,
239
241
  entryFileNames: options.entryFileNames ?? (dtsInput ? "[name].ts" : void 0),
@@ -245,10 +247,7 @@ function createFakeJsPlugin({ dtsInput }) {
245
247
  };
246
248
  },
247
249
  transform: {
248
- filter: { id: {
249
- include: [RE_DTS],
250
- exclude: [RE_NODE_MODULES]
251
- } },
250
+ filter: { id: RE_DTS },
252
251
  async handler(code, id) {
253
252
  const { program, comments } = await parseAsync(id, code);
254
253
  const preserved = collectReferenceDirectives(comments);
@@ -436,10 +435,17 @@ function importNamespace(s, source, imported, getIdentifierIndex) {
436
435
 
437
436
  //#endregion
438
437
  //#region src/generate.ts
439
- function createGeneratePlugin({ isolatedDeclaration: isolatedDeclaration$1, inputAlias, external }) {
438
+ const meta = { dtsFile: true };
439
+ function createGeneratePlugin({ isolatedDeclaration: isolatedDeclaration$1, inputAlias = {}, resolve = false }) {
440
440
  const dtsMap = new Map();
441
+ const inputAliasMap = new Map(Object.entries(inputAlias));
442
+ const resolver = createResolver();
443
+ let inputOption;
441
444
  return {
442
445
  name: "rolldown-plugin-dts:generate",
446
+ options({ input }) {
447
+ if (isPlainObject(input)) inputOption = { ...input };
448
+ },
443
449
  transform: {
444
450
  order: "pre",
445
451
  filter: { id: {
@@ -454,7 +460,8 @@ function createGeneratePlugin({ isolatedDeclaration: isolatedDeclaration$1, inpu
454
460
  const mod = this.getModuleInfo(id);
455
461
  if (mod?.isEntry) {
456
462
  let fileName = basename(dtsId);
457
- if (inputAlias?.[fileName]) fileName = inputAlias[fileName];
463
+ if (inputAliasMap.has(fileName)) fileName = inputAliasMap.get(fileName);
464
+ else if (inputAliasMap.has(dtsId)) fileName = inputAliasMap.get(dtsId);
458
465
  this.emitFile({
459
466
  type: "chunk",
460
467
  id: dtsId,
@@ -466,26 +473,47 @@ function createGeneratePlugin({ isolatedDeclaration: isolatedDeclaration$1, inpu
466
473
  async resolveId(id, importer, extraOptions) {
467
474
  if (dtsMap.has(id)) return {
468
475
  id,
469
- meta: { dtsFile: true }
476
+ meta
470
477
  };
471
- const importerMod = importer ? this.getModuleInfo(importer) : null;
472
- if (importerMod?.meta.dtsFile) {
473
- if (!isRelative(id) || external?.(id, importer, extraOptions) === true) return {
474
- id,
475
- external: true
476
- };
478
+ if (importer && this.getModuleInfo(importer)?.meta.dtsFile) {
479
+ if (!isRelative(id)) {
480
+ let shouldResolve;
481
+ if (typeof resolve === "boolean") shouldResolve = resolve;
482
+ else shouldResolve = resolve.some((pattern) => typeof pattern === "string" ? id === pattern : pattern.test(id));
483
+ if (shouldResolve) {
484
+ const resolution$1 = resolver(id, importer);
485
+ if (resolution$1) return {
486
+ id: resolution$1,
487
+ meta
488
+ };
489
+ } else return {
490
+ id,
491
+ external: true,
492
+ meta
493
+ };
494
+ }
477
495
  const resolution = await this.resolve(id, filename_dts_to(importer, "ts"));
478
496
  if (!resolution || resolution.external) return;
479
497
  const dtsId = filename_ts_to_dts(resolution.id);
480
498
  if (dtsMap.has(dtsId)) return {
481
499
  id: dtsId,
482
- meta: { dtsFile: true }
500
+ meta
483
501
  };
484
502
  await this.load(resolution);
485
503
  if (dtsMap.has(dtsId)) return {
486
504
  id: dtsId,
487
- meta: { dtsFile: true }
505
+ meta
488
506
  };
507
+ } else if (extraOptions.isEntry && inputOption) {
508
+ const resolution = await this.resolve(id, importer, extraOptions);
509
+ if (!resolution) return;
510
+ const dtsId = filename_ts_to_dts(resolution.id);
511
+ if (inputAliasMap.has(dtsId)) return resolution;
512
+ for (const [name, entry] of Object.entries(inputOption)) if (entry === id) {
513
+ inputAliasMap.set(dtsId, `${name}.d.ts`);
514
+ break;
515
+ }
516
+ return resolution;
489
517
  }
490
518
  },
491
519
  load: {
@@ -502,6 +530,11 @@ function createGeneratePlugin({ isolatedDeclaration: isolatedDeclaration$1, inpu
502
530
  }
503
531
  };
504
532
  }
533
+ function isPlainObject(data) {
534
+ if (typeof data !== "object" || data === null) return false;
535
+ const proto = Object.getPrototypeOf(data);
536
+ return proto === null || proto === Object.prototype;
537
+ }
505
538
 
506
539
  //#endregion
507
540
  //#region src/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A Rolldown plugin to bundle dts files",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -27,7 +27,11 @@
27
27
  "publishConfig": {
28
28
  "access": "public"
29
29
  },
30
+ "peerDependencies": {
31
+ "rolldown": "^1.0.0-beta.7"
32
+ },
30
33
  "dependencies": {
34
+ "dts-resolver": "^0.1.0",
31
35
  "magic-string-ast": "^0.9.1",
32
36
  "oxc-parser": "^0.62.0",
33
37
  "oxc-transform": "^0.62.0"
@@ -43,7 +47,7 @@
43
47
  "eslint": "^9.24.0",
44
48
  "estree-walker": "^3.0.3",
45
49
  "prettier": "^3.5.3",
46
- "rolldown": "1.0.0-beta.7",
50
+ "rolldown": "^1.0.0-beta.7",
47
51
  "rollup-plugin-dts": "^6.2.1",
48
52
  "tsdown": "^0.8.0-beta.1",
49
53
  "tsx": "^4.19.3",