dtsroll 1.7.3 → 1.7.5

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
@@ -1,7 +1,10 @@
1
- <p align="center">
2
- <img width="200" src="./.github/logo.webp">
3
- </p>
4
- <h2 align="center">dtsroll</h2>
1
+ <h2 align="center">
2
+ <img width="180" src="./.github/logo.webp">
3
+ <br>
4
+ dtsroll
5
+ <br><br>
6
+ <a href="https://npm.im/dtsroll"><img src="https://badgen.net/npm/v/dtsroll"></a> <a href="https://npm.im/dtsroll"><img src="https://badgen.net/npm/dm/dtsroll"></a>
7
+ </h2>
5
8
 
6
9
  Are you publishing a TypeScript project where consumers encounter type-checking errors like:
7
10
 
package/dist/cli.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { cli } from 'cleye';
3
- import { b as bgYellow, a as black, d as dtsroll, l as logOutput, D as DtsrollBuildError } from './index-J5sfIUfU.mjs';
3
+ import { b as bgYellow, a as black, d as dtsroll, l as logOutput } from './index-CcbG5cCp.mjs';
4
+ import { patchErrorWithTrace } from 'rollup-plugin-import-trace';
4
5
  import 'node:path';
5
6
  import 'node:fs/promises';
6
7
  import 'rollup';
@@ -13,7 +14,7 @@ import 'resolve-pkg-maps';
13
14
  import 'byte-size';
14
15
 
15
16
  var name = "dtsroll";
16
- var version = "1.7.3";
17
+ var version = "1.7.5";
17
18
  var description = "Bundle dts files";
18
19
 
19
20
  const argv = cli({
@@ -66,19 +67,9 @@ dtsroll({
66
67
  }
67
68
  ).catch(
68
69
  (error) => {
69
- let errorMessage = "\nFailed to build";
70
- if (error instanceof DtsrollBuildError) {
71
- errorMessage += `
72
- File: ${error.id}`;
73
- if (error.importChain.length > 1) {
74
- errorMessage += "\n\n Import chain:\n ";
75
- errorMessage += error.importChain.join("\n \u2192 ");
76
- }
77
- }
78
- errorMessage += `
79
-
80
- ${error instanceof Error ? error.message : String(error)}`;
81
- console.error(errorMessage);
70
+ console.error("\nFailed to build\n");
71
+ patchErrorWithTrace(error);
72
+ console.error(error instanceof Error ? error.message : String(error));
82
73
  process.exitCode = 1;
83
74
  }
84
75
  );
@@ -6,6 +6,7 @@ import ts from 'typescript';
6
6
  import { createRequire } from 'node:module';
7
7
  import convert$1 from 'convert-source-map';
8
8
  import nodeResolve from '@rollup/plugin-node-resolve';
9
+ import { importTrace } from 'rollup-plugin-import-trace';
9
10
  import { up } from 'empathic/find';
10
11
  import { resolveImports } from 'resolve-pkg-maps';
11
12
  import byteSize from 'byte-size';
@@ -176,17 +177,6 @@ ${moduleIds.sort().map((moduleId, index) => {
176
177
  }
177
178
  };
178
179
 
179
- class DtsrollBuildError extends Error {
180
- id;
181
- importChain;
182
- constructor(message, id, importChain) {
183
- super(message);
184
- this.name = "DtsrollBuildError";
185
- this.id = id;
186
- this.importChain = importChain;
187
- }
188
- }
189
-
190
180
  const dtsExtensions = [".d.ts", ".d.cts", ".d.mts"];
191
181
  const isDts = (fileName) => dtsExtensions.some((extension) => fileName.endsWith(extension));
192
182
 
@@ -2217,7 +2207,8 @@ function resolveDefaultOptions(options) {
2217
2207
  ...options,
2218
2208
  compilerOptions: options.compilerOptions ?? {},
2219
2209
  respectExternal: options.respectExternal ?? false,
2220
- includeExternal: options.includeExternal ?? []
2210
+ includeExternal: options.includeExternal ?? [],
2211
+ sourcemap: options.sourcemap ?? false
2221
2212
  };
2222
2213
  }
2223
2214
  const DTS_EXTENSIONS = /\.d\.(c|m)?tsx?$/;
@@ -2270,8 +2261,15 @@ function cacheConfig([fromPath, toPath], config) {
2270
2261
  configByPath.set(fromPath, config);
2271
2262
  }
2272
2263
  }
2273
- function getCompilerOptions(input, overrideOptions, overrideConfigPath) {
2274
- const compilerOptions = { ...DEFAULT_OPTIONS, ...overrideOptions };
2264
+ function getCompilerOptions(input, overrideOptions, overrideConfigPath, enableDeclarationMap) {
2265
+ const compilerOptions = {
2266
+ ...DEFAULT_OPTIONS,
2267
+ ...overrideOptions,
2268
+ // When plugin's sourcemap option is explicitly true, force declarationMap
2269
+ // regardless of user's compilerOptions setting.
2270
+ // When sourcemap is false/undefined, respect user's compilerOptions.declarationMap.
2271
+ ...enableDeclarationMap === true && { declarationMap: true }
2272
+ };
2275
2273
  let dirName = path.dirname(input);
2276
2274
  let dtsFiles = [];
2277
2275
  const cacheKey = overrideConfigPath || dirName;
@@ -2313,11 +2311,11 @@ function getCompilerOptions(input, overrideOptions, overrideConfigPath) {
2313
2311
  }
2314
2312
  };
2315
2313
  }
2316
- function createProgram$1(fileName, overrideOptions, tsconfig) {
2317
- const { dtsFiles, compilerOptions } = getCompilerOptions(fileName, overrideOptions, tsconfig);
2314
+ function createProgram$1(fileName, overrideOptions, tsconfig, enableDeclarationMap) {
2315
+ const { dtsFiles, compilerOptions } = getCompilerOptions(fileName, overrideOptions, tsconfig, enableDeclarationMap);
2318
2316
  return ts.createProgram([fileName].concat(Array.from(dtsFiles)), compilerOptions, ts.createCompilerHost(compilerOptions, true));
2319
2317
  }
2320
- function createPrograms(input, overrideOptions, tsconfig) {
2318
+ function createPrograms(input, overrideOptions, tsconfig, enableDeclarationMap) {
2321
2319
  const programs = [];
2322
2320
  const dtsFiles = /* @__PURE__ */ new Set();
2323
2321
  let inputs = [];
@@ -2328,7 +2326,7 @@ function createPrograms(input, overrideOptions, tsconfig) {
2328
2326
  continue;
2329
2327
  }
2330
2328
  main = path.resolve(main);
2331
- const options = getCompilerOptions(main, overrideOptions, tsconfig);
2329
+ const options = getCompilerOptions(main, overrideOptions, tsconfig, enableDeclarationMap);
2332
2330
  options.dtsFiles.forEach(dtsFiles.add, dtsFiles);
2333
2331
  if (!inputs.length) {
2334
2332
  inputs.push(main);
@@ -3975,7 +3973,14 @@ function hydrateSourcemap(sparseMappings, inputMap, outputCode) {
3975
3973
  return encode(hydratedMappings);
3976
3974
  }
3977
3975
  async function loadInputSourcemap(info) {
3978
- const { fileName, originalCode } = info;
3976
+ const { fileName, originalCode, inputMapText } = info;
3977
+ if (inputMapText) {
3978
+ try {
3979
+ return JSON.parse(inputMapText);
3980
+ } catch {
3981
+ return null;
3982
+ }
3983
+ }
3979
3984
  const inlineConverter = convert$1.fromSource(originalCode);
3980
3985
  if (inlineConverter) {
3981
3986
  return inlineConverter.toObject();
@@ -3999,7 +4004,7 @@ async function loadInputSourcemap(info) {
3999
4004
  return null;
4000
4005
  }
4001
4006
  }
4002
- const transform = () => {
4007
+ const transform = (enableSourcemap) => {
4003
4008
  const allTypeReferences = /* @__PURE__ */ new Map();
4004
4009
  const allFileReferences = /* @__PURE__ */ new Map();
4005
4010
  const pendingSourcemaps = /* @__PURE__ */ new Map();
@@ -4044,7 +4049,7 @@ const transform = () => {
4044
4049
  strict: false
4045
4050
  };
4046
4051
  },
4047
- transform(code, fileName) {
4052
+ transform(code, fileName, inputMapText) {
4048
4053
  const name = trimExtension(fileName);
4049
4054
  const moduleIds = this.getModuleIds();
4050
4055
  const moduleId = Array.from(moduleIds).find((id) => trimExtension(id) === name);
@@ -4062,11 +4067,15 @@ const transform = () => {
4062
4067
  console.log(code);
4063
4068
  console.log(JSON.stringify(converted.ast.body, void 0, 2));
4064
4069
  }
4070
+ if (!enableSourcemap) {
4071
+ return { code, ast: converted.ast };
4072
+ }
4065
4073
  const map = preprocessed.code.generateMap({ hires: true, source: fileName });
4066
4074
  if (DTS_EXTENSIONS.test(fileName)) {
4067
4075
  pendingSourcemaps.set(fileName, {
4068
4076
  fileName,
4069
- originalCode: code
4077
+ originalCode: code,
4078
+ inputMapText
4070
4079
  });
4071
4080
  }
4072
4081
  return { code, ast: converted.ast, map };
@@ -4166,25 +4175,29 @@ const transform = () => {
4166
4175
  if (isUrl(source))
4167
4176
  continue;
4168
4177
  const absoluteSource = path.resolve(chunkDir, source);
4169
- const inputMap = inputSourcemaps.get(absoluteSource);
4178
+ let inputMap = inputSourcemaps.get(absoluteSource);
4179
+ if (!inputMap && /\.[cm]?[tj]sx?$/.test(absoluteSource) && !absoluteSource.endsWith(".d.ts")) {
4180
+ const dtsPath = absoluteSource.replace(/\.[cm]?[tj]sx?$/, ".d.ts");
4181
+ inputMap = inputSourcemaps.get(dtsPath);
4182
+ }
4170
4183
  if (inputMap) {
4171
4184
  sourcesToRemap.set(absoluteSource, inputMap);
4172
4185
  }
4173
4186
  }
4174
4187
  if (sourcesToRemap.size === 0) {
4188
+ delete chunk.map.sourcesContent;
4175
4189
  if (chunk.map.sources.length === 0 && chunk.facadeModuleId) {
4176
4190
  const inputMap = inputSourcemaps.get(chunk.facadeModuleId);
4177
4191
  if (inputMap && inputMap.sources.length > 0) {
4178
4192
  const newSources2 = inputMap.sources.map(toRelativeSourcePathOrNull);
4179
4193
  chunk.map.sources = newSources2;
4180
- delete chunk.map.sourcesContent;
4181
- updateSourcemapAsset(bundle, chunk.fileName, {
4182
- sources: newSources2,
4183
- mappings: chunk.map.mappings,
4184
- names: chunk.map.names || []
4185
- });
4186
4194
  }
4187
4195
  }
4196
+ updateSourcemapAsset(bundle, chunk.fileName, {
4197
+ sources: chunk.map.sources.map(toRelativeSourcePathOrNull),
4198
+ mappings: chunk.map.mappings,
4199
+ names: chunk.map.names || []
4200
+ });
4188
4201
  continue;
4189
4202
  }
4190
4203
  const isSingleSource = chunk.map.sources.length === 1 && sourcesToRemap.size === 1;
@@ -4198,8 +4211,13 @@ const transform = () => {
4198
4211
  newMappings = hydrateSourcemap(chunk.map.mappings, singleInputMap, chunk.code);
4199
4212
  newNames = singleInputMap.names || [];
4200
4213
  } else {
4214
+ const visitedFiles = /* @__PURE__ */ new Set();
4201
4215
  const remapped = remapping(chunk.map, (file) => {
4202
4216
  const absolutePath = path.resolve(chunkDir, file);
4217
+ if (visitedFiles.has(absolutePath)) {
4218
+ return null;
4219
+ }
4220
+ visitedFiles.add(absolutePath);
4203
4221
  const inputMap = sourcesToRemap.get(absolutePath);
4204
4222
  if (inputMap) {
4205
4223
  return inputMap;
@@ -4273,7 +4291,7 @@ function getModule({ entries, programs, resolvedOptions }, fileName, code) {
4273
4291
  return { code };
4274
4292
  }
4275
4293
  }
4276
- const newProgram = createProgram$1(fileName, compilerOptions, tsconfig);
4294
+ const newProgram = createProgram$1(fileName, compilerOptions, tsconfig, resolvedOptions.sourcemap);
4277
4295
  programs.push(newProgram);
4278
4296
  const source = newProgram.getSourceFile(fileName);
4279
4297
  return {
@@ -4286,8 +4304,8 @@ function getModule({ entries, programs, resolvedOptions }, fileName, code) {
4286
4304
  }
4287
4305
  }
4288
4306
  const plugin = (options = {}) => {
4289
- const transformPlugin = transform();
4290
4307
  const ctx = { entries: [], programs: [], resolvedOptions: resolveDefaultOptions(options) };
4308
+ const transformPlugin = transform(ctx.resolvedOptions.sourcemap);
4291
4309
  return {
4292
4310
  name: "dts",
4293
4311
  // pass outputOptions, renderChunk, and generateBundle hooks to the inner transform plugin
@@ -4310,7 +4328,7 @@ const plugin = (options = {}) => {
4310
4328
  options2.input[name] = filename;
4311
4329
  }
4312
4330
  }
4313
- ctx.programs = createPrograms(Object.values(input), ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig);
4331
+ ctx.programs = createPrograms(Object.values(input), ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig, ctx.resolvedOptions.sourcemap);
4314
4332
  return transformPlugin.options.call(this, options2);
4315
4333
  },
4316
4334
  transform(code, id) {
@@ -4347,11 +4365,16 @@ const plugin = (options = {}) => {
4347
4365
  return null;
4348
4366
  watchFiles(module);
4349
4367
  const declarationId = getDeclarationId(id);
4350
- let generated;
4368
+ let declarationText;
4369
+ let declarationMapText;
4351
4370
  const { emitSkipped, diagnostics } = module.program.emit(
4352
4371
  module.source,
4353
- (_, declarationText) => {
4354
- generated = transformPlugin.transform.call(this, declarationText, declarationId);
4372
+ (emitFileName, text) => {
4373
+ if (emitFileName.endsWith(".map")) {
4374
+ declarationMapText = text;
4375
+ } else {
4376
+ declarationText = text;
4377
+ }
4355
4378
  },
4356
4379
  void 0,
4357
4380
  // cancellationToken
@@ -4369,7 +4392,10 @@ const plugin = (options = {}) => {
4369
4392
  this.error("Failed to compile. Check the logs above.");
4370
4393
  }
4371
4394
  }
4372
- return generated;
4395
+ if (!declarationText)
4396
+ return null;
4397
+ const cleanDeclarationText = declarationText.replace(/\n?\/\/# sourceMappingURL=[^\n]+/, "");
4398
+ return transformPlugin.transform.call(this, cleanDeclarationText, declarationId, declarationMapText);
4373
4399
  };
4374
4400
  if (DTS_EXTENSIONS.test(id))
4375
4401
  return handleDtsFile();
@@ -4386,7 +4412,7 @@ const plugin = (options = {}) => {
4386
4412
  let resolvedCompilerOptions = ctx.resolvedOptions.compilerOptions;
4387
4413
  if (ctx.resolvedOptions.tsconfig) {
4388
4414
  const resolvedSource = source.startsWith(".") ? path.resolve(path.dirname(importer), source) : source;
4389
- resolvedCompilerOptions = getCompilerOptions(resolvedSource, ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig).compilerOptions;
4415
+ resolvedCompilerOptions = getCompilerOptions(resolvedSource, ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig, ctx.resolvedOptions.sourcemap).compilerOptions;
4390
4416
  }
4391
4417
  const { resolvedModule } = ts.resolveModuleName(source, importer, resolvedCompilerOptions, ts.sys);
4392
4418
  if (!resolvedModule) {
@@ -4461,39 +4487,6 @@ const createExternalizePlugin = (configuredExternals) => {
4461
4487
  };
4462
4488
  };
4463
4489
 
4464
- const createImportChainPlugin = () => {
4465
- const importerMap = /* @__PURE__ */ new Map();
4466
- const plugin = {
4467
- name: "import-chain-tracker",
4468
- buildStart: () => {
4469
- importerMap.clear();
4470
- },
4471
- async resolveId(source, importer) {
4472
- if (!importer) {
4473
- return null;
4474
- }
4475
- const resolved = await this.resolve(source, importer, { skipSelf: true });
4476
- if (resolved && !resolved.external && !importerMap.has(resolved.id)) {
4477
- importerMap.set(resolved.id, importer);
4478
- }
4479
- return null;
4480
- }
4481
- };
4482
- const getImportChain = (errorFileId) => {
4483
- const chain = [];
4484
- let current = errorFileId;
4485
- while (current) {
4486
- chain.unshift(current);
4487
- current = importerMap.get(current);
4488
- }
4489
- return chain;
4490
- };
4491
- return {
4492
- plugin,
4493
- getImportChain
4494
- };
4495
- };
4496
-
4497
4490
  const nodeModules = `${path__default.sep}node_modules${path__default.sep}`;
4498
4491
  const removeBundledModulesPlugin = (outputDirectory, sizeRef) => {
4499
4492
  let deleteFiles = [];
@@ -4599,7 +4592,6 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
4599
4592
  externalized,
4600
4593
  getPackageEntryPoint
4601
4594
  } = createExternalizePlugin(externals);
4602
- const { plugin: importChainPlugin, getImportChain } = createImportChainPlugin();
4603
4595
  const sizeRef = {};
4604
4596
  const rollupConfig = {
4605
4597
  input: createInputMap(input, outputDirectory),
@@ -4610,7 +4602,7 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
4610
4602
  chunkFileNames: "_dtsroll-chunks/[hash]-[name].ts"
4611
4603
  },
4612
4604
  plugins: [
4613
- importChainPlugin,
4605
+ importTrace(),
4614
4606
  externalizePlugin,
4615
4607
  removeBundledModulesPlugin(outputDirectory, sizeRef),
4616
4608
  resolveSubpathImportsPlugin(),
@@ -4619,7 +4611,8 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
4619
4611
  exportConditions: conditions
4620
4612
  }),
4621
4613
  plugin({
4622
- respectExternal: true
4614
+ respectExternal: true,
4615
+ sourcemap
4623
4616
  /**
4624
4617
  * Setting a tsconfig or compilerOptions shouldn't be necessary since
4625
4618
  * we're dealing with pre-compiled d.ts files
@@ -4630,26 +4623,15 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
4630
4623
  })
4631
4624
  ]
4632
4625
  };
4633
- try {
4634
- const rollupBuild = await rollup(rollupConfig);
4635
- const built = await rollupBuild[mode](rollupConfig.output);
4636
- await rollupBuild.close();
4637
- return {
4638
- built,
4639
- externalized,
4640
- getPackageEntryPoint,
4641
- sourceSize: sizeRef.value ?? 0
4642
- };
4643
- } catch (error) {
4644
- if (error instanceof Error && "id" in error && typeof error.id === "string") {
4645
- throw new DtsrollBuildError(
4646
- error.message,
4647
- error.id,
4648
- getImportChain(error.id)
4649
- );
4650
- }
4651
- throw error;
4652
- }
4626
+ const rollupBuild = await rollup(rollupConfig);
4627
+ const built = await rollupBuild[mode](rollupConfig.output);
4628
+ await rollupBuild.close();
4629
+ return {
4630
+ built,
4631
+ externalized,
4632
+ getPackageEntryPoint,
4633
+ sourceSize: sizeRef.value ?? 0
4634
+ };
4653
4635
  };
4654
4636
 
4655
4637
  const dtsroll = async ({
@@ -4746,4 +4728,4 @@ const dtsroll = async ({
4746
4728
  };
4747
4729
  };
4748
4730
 
4749
- export { DtsrollBuildError as D, black as a, bgYellow as b, dtsroll as d, logOutput as l };
4731
+ export { black as a, bgYellow as b, dtsroll as d, logOutput as l };
package/dist/index.mjs CHANGED
@@ -1,12 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  import 'node:path';
3
- export { d as dtsroll } from './index-J5sfIUfU.mjs';
3
+ export { d as dtsroll } from './index-CcbG5cCp.mjs';
4
4
  import 'node:fs/promises';
5
5
  import 'rollup';
6
6
  import 'typescript';
7
7
  import 'node:module';
8
8
  import 'convert-source-map';
9
9
  import '@rollup/plugin-node-resolve';
10
+ import 'rollup-plugin-import-trace';
10
11
  import 'empathic/find';
11
12
  import 'resolve-pkg-maps';
12
13
  import 'byte-size';
package/dist/vite.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { d as dtsroll, l as logOutput } from './index-J5sfIUfU.mjs';
2
+ import { d as dtsroll, l as logOutput } from './index-CcbG5cCp.mjs';
3
3
  import 'node:path';
4
4
  import 'node:fs/promises';
5
5
  import 'rollup';
@@ -7,6 +7,7 @@ import 'typescript';
7
7
  import 'node:module';
8
8
  import 'convert-source-map';
9
9
  import '@rollup/plugin-node-resolve';
10
+ import 'rollup-plugin-import-trace';
10
11
  import 'empathic/find';
11
12
  import 'resolve-pkg-maps';
12
13
  import 'byte-size';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dtsroll",
3
- "version": "1.7.3",
3
+ "version": "1.7.5",
4
4
  "description": "Bundle dts files",
5
5
  "keywords": [
6
6
  "bundle",
@@ -43,7 +43,8 @@
43
43
  "convert-source-map": "^2.0.0",
44
44
  "empathic": "^2.0.0",
45
45
  "resolve-pkg-maps": "^1.0.0",
46
- "rollup": "^4.55.3"
46
+ "rollup": "^4.55.3",
47
+ "rollup-plugin-import-trace": "^1.0.0"
47
48
  },
48
49
  "peerDependencies": {
49
50
  "typescript": "^4.5 || ^5.0",