rolldown-plugin-dts 0.17.2 → 0.17.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.
package/README.md CHANGED
@@ -107,6 +107,15 @@ Content to be added at the top of each generated `.d.ts` file.
107
107
 
108
108
  Content to be added at the bottom of each generated `.d.ts` file.
109
109
 
110
+ #### `sideEffects`
111
+
112
+ Indicates whether the generated `.d.ts` files have side effects.
113
+
114
+ - If set to `true`, Rolldown will treat the `.d.ts` files as having side effects during tree-shaking.
115
+ - If set to `false`, Rolldown may consider the `.d.ts` files as side-effect-free, potentially removing them if they are not imported.
116
+
117
+ **Default:** false
118
+
110
119
  ### `tsc` Options
111
120
 
112
121
  > [!NOTE]
package/dist/index.d.ts CHANGED
@@ -64,6 +64,14 @@ interface GeneralOptions {
64
64
  * This is useful for compatibility with CommonJS.
65
65
  */
66
66
  cjsDefault?: boolean;
67
+ /**
68
+ * Indicates whether the generated `.d.ts` files have side effects.
69
+ * - If set to `true`, Rolldown will treat the `.d.ts` files as having side effects during tree-shaking.
70
+ * - If set to `false`, Rolldown may consider the `.d.ts` files as side-effect-free, potentially removing them if they are not imported.
71
+ *
72
+ * @default false
73
+ */
74
+ sideEffects?: boolean;
67
75
  }
68
76
  interface TscOptions {
69
77
  /**
@@ -190,6 +198,7 @@ declare function resolveOptions({
190
198
  cjsDefault,
191
199
  banner,
192
200
  footer,
201
+ sideEffects,
193
202
  build,
194
203
  incremental,
195
204
  vue,
@@ -205,8 +214,9 @@ declare function resolveOptions({
205
214
  //#region src/fake-js.d.ts
206
215
  declare function createFakeJsPlugin({
207
216
  sourcemap,
208
- cjsDefault
209
- }: Pick<OptionsResolved, "sourcemap" | "cjsDefault">): Plugin;
217
+ cjsDefault,
218
+ sideEffects
219
+ }: Pick<OptionsResolved, "sourcemap" | "cjsDefault" | "sideEffects">): Plugin;
210
220
  //#endregion
211
221
  //#region src/generate.d.ts
212
222
  declare function createGeneratePlugin({
package/dist/index.js CHANGED
@@ -63,10 +63,10 @@ function createBannerPlugin({ banner, footer }) {
63
63
 
64
64
  //#endregion
65
65
  //#region src/dts-input.ts
66
- function createDtsInputPlugin() {
66
+ function createDtsInputPlugin({ sideEffects }) {
67
67
  return {
68
68
  name: "rolldown-plugin-dts:dts-input",
69
- options(options) {
69
+ options: sideEffects === false ? (options) => {
70
70
  return {
71
71
  treeshake: options.treeshake === false ? false : {
72
72
  ...options.treeshake === true ? {} : options.treeshake,
@@ -74,7 +74,7 @@ function createDtsInputPlugin() {
74
74
  },
75
75
  ...options
76
76
  };
77
- },
77
+ } : void 0,
78
78
  outputOptions(options) {
79
79
  return {
80
80
  ...options,
@@ -284,7 +284,7 @@ function walk(ast, { enter, leave }) {
284
284
 
285
285
  //#endregion
286
286
  //#region src/fake-js.ts
287
- function createFakeJsPlugin({ sourcemap, cjsDefault }) {
287
+ function createFakeJsPlugin({ sourcemap, cjsDefault, sideEffects }) {
288
288
  let symbolIdx = 0;
289
289
  const identifierMap = Object.create(null);
290
290
  const symbolMap = /* @__PURE__ */ new Map();
@@ -325,7 +325,8 @@ function createFakeJsPlugin({ sourcemap, cjsDefault }) {
325
325
  function transform(code, id) {
326
326
  const file = parse(code, {
327
327
  plugins: [["typescript", { dts: true }]],
328
- sourceType: "module"
328
+ sourceType: "module",
329
+ errorRecovery: true
329
330
  });
330
331
  const { program, comments } = file;
331
332
  const typeOnlyIds = [];
@@ -404,6 +405,7 @@ function createFakeJsPlugin({ sourcemap, cjsDefault }) {
404
405
  setStmt(runtimeAssignment);
405
406
  } else setDecl(runtimeAssignment);
406
407
  }
408
+ if (sideEffects) appendStmts.push(t.expressionStatement(t.callExpression(t.identifier("sideEffect"), [])));
407
409
  program.body = [
408
410
  ...Array.from(namespaceStmts.values()).map(({ stmt }) => stmt),
409
411
  ...program.body,
@@ -964,7 +966,6 @@ export { __json_default_export as default }`;
964
966
  }
965
967
  return {
966
968
  code: dtsCode || "",
967
- moduleSideEffects: false,
968
969
  map
969
970
  };
970
971
  }
@@ -1010,11 +1011,14 @@ function collectJsonExportMap(code) {
1010
1011
  const exportMap = /* @__PURE__ */ new Map();
1011
1012
  const { program } = parse(code, {
1012
1013
  sourceType: "module",
1013
- plugins: [["typescript", { dts: true }]]
1014
+ plugins: [["typescript", { dts: true }]],
1015
+ errorRecovery: true
1014
1016
  });
1015
1017
  for (const decl of program.body) if (decl.type === "ExportNamedDeclaration") {
1016
- if (decl.declaration && decl.declaration.type === "VariableDeclaration") {
1017
- for (const vdecl of decl.declaration.declarations) if (vdecl.id.type === "Identifier") exportMap.set(vdecl.id.name, vdecl.id.name);
1018
+ if (decl.declaration) {
1019
+ if (decl.declaration.type === "VariableDeclaration") {
1020
+ for (const vdecl of decl.declaration.declarations) if (vdecl.id.type === "Identifier") exportMap.set(vdecl.id.name, vdecl.id.name);
1021
+ } else if (decl.declaration.type === "TSModuleDeclaration" && decl.declaration.id.type === "Identifier") exportMap.set(decl.declaration.id.name, decl.declaration.id.name);
1018
1022
  } else if (decl.specifiers.length) {
1019
1023
  for (const spec of decl.specifiers) if (spec.type === "ExportSpecifier" && spec.exported.type === "Identifier") exportMap.set(spec.exported.name, spec.local.type === "Identifier" ? spec.local.name : spec.exported.name);
1020
1024
  }
@@ -1037,7 +1041,7 @@ function collectJsonExports(code) {
1037
1041
  //#endregion
1038
1042
  //#region src/options.ts
1039
1043
  let warnedTsgo = false;
1040
- function resolveOptions({ cwd = process.cwd(), dtsInput = false, emitDtsOnly = false, tsconfig, tsconfigRaw: overriddenTsconfigRaw = {}, compilerOptions = {}, sourcemap, resolve = false, cjsDefault = false, banner, footer, build = false, incremental = false, vue = false, tsMacro = false, parallel = false, eager = false, newContext = false, emitJs, oxc, tsgo = false }) {
1044
+ function resolveOptions({ cwd = process.cwd(), dtsInput = false, emitDtsOnly = false, tsconfig, tsconfigRaw: overriddenTsconfigRaw = {}, compilerOptions = {}, sourcemap, resolve = false, cjsDefault = false, banner, footer, sideEffects = false, build = false, incremental = false, vue = false, tsMacro = false, parallel = false, eager = false, newContext = false, emitJs, oxc, tsgo = false }) {
1041
1045
  let resolvedTsconfig;
1042
1046
  if (tsconfig === true || tsconfig == null) {
1043
1047
  const { config, path: path$1 } = getTsconfig(cwd) || {};
@@ -1088,6 +1092,7 @@ function resolveOptions({ cwd = process.cwd(), dtsInput = false, emitDtsOnly = f
1088
1092
  cjsDefault,
1089
1093
  banner,
1090
1094
  footer,
1095
+ sideEffects,
1091
1096
  build,
1092
1097
  incremental,
1093
1098
  vue,
@@ -1106,12 +1111,13 @@ function resolveOptions({ cwd = process.cwd(), dtsInput = false, emitDtsOnly = f
1106
1111
  function isSourceFile(id) {
1107
1112
  return RE_TS.test(id) || RE_VUE.test(id) || RE_JSON.test(id);
1108
1113
  }
1109
- function createDtsResolvePlugin({ tsconfig, resolve }) {
1114
+ function createDtsResolvePlugin({ tsconfig, resolve, sideEffects }) {
1110
1115
  const baseDtsResolver = createResolver({
1111
1116
  tsconfig,
1112
1117
  resolveNodeModules: !!resolve,
1113
1118
  ResolverFactory
1114
1119
  });
1120
+ const moduleSideEffects = sideEffects ? true : null;
1115
1121
  return {
1116
1122
  name: "rolldown-plugin-dts:resolver",
1117
1123
  resolveId: {
@@ -1121,17 +1127,23 @@ function createDtsResolvePlugin({ tsconfig, resolve }) {
1121
1127
  const external = {
1122
1128
  id,
1123
1129
  external: true,
1124
- moduleSideEffects: false
1130
+ moduleSideEffects: sideEffects
1125
1131
  };
1126
1132
  if (RE_CSS.test(id)) return external;
1127
1133
  const rolldownResolution = await this.resolve(id, importer, options);
1128
1134
  const dtsResolution = resolveDtsPath(id, importer, rolldownResolution);
1129
1135
  if (!dtsResolution) return isFilePath(id) ? null : external;
1130
1136
  if (RE_NODE_MODULES.test(dtsResolution) && !shouldBundleNodeModule(id) && (!RE_NODE_MODULES.test(importer) || rolldownResolution?.external)) return external;
1131
- if (RE_DTS.test(dtsResolution)) return dtsResolution;
1137
+ if (RE_DTS.test(dtsResolution)) return {
1138
+ id: dtsResolution,
1139
+ moduleSideEffects
1140
+ };
1132
1141
  if (isSourceFile(dtsResolution)) {
1133
1142
  await this.load({ id: dtsResolution });
1134
- return filename_to_dts(dtsResolution);
1143
+ return {
1144
+ id: filename_to_dts(dtsResolution),
1145
+ moduleSideEffects
1146
+ };
1135
1147
  }
1136
1148
  }
1137
1149
  }
@@ -1162,7 +1174,7 @@ function dts(options = {}) {
1162
1174
  const resolved = resolveOptions(options);
1163
1175
  debug("resolved dts options %o", resolved);
1164
1176
  const plugins = [];
1165
- if (options.dtsInput) plugins.push(createDtsInputPlugin());
1177
+ if (options.dtsInput) plugins.push(createDtsInputPlugin(resolved));
1166
1178
  else plugins.push(createGeneratePlugin(resolved));
1167
1179
  plugins.push(createDtsResolvePlugin(resolved), createFakeJsPlugin(resolved));
1168
1180
  if (options.banner || options.footer) plugins.push(createBannerPlugin(resolved));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rolldown-plugin-dts",
3
- "version": "0.17.2",
3
+ "version": "0.17.3",
4
4
  "description": "A Rolldown plugin to generate and bundle dts files.",
5
5
  "type": "module",
6
6
  "keywords": [