dtsroll 1.7.3 → 1.7.4

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/cli.mjs CHANGED
@@ -1,6 +1,6 @@
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, D as DtsrollBuildError } from './index-DJ_g9wqZ.mjs';
4
4
  import 'node:path';
5
5
  import 'node:fs/promises';
6
6
  import 'rollup';
@@ -13,7 +13,7 @@ import 'resolve-pkg-maps';
13
13
  import 'byte-size';
14
14
 
15
15
  var name = "dtsroll";
16
- var version = "1.7.3";
16
+ var version = "1.7.4";
17
17
  var description = "Bundle dts files";
18
18
 
19
19
  const argv = cli({
@@ -2217,7 +2217,8 @@ function resolveDefaultOptions(options) {
2217
2217
  ...options,
2218
2218
  compilerOptions: options.compilerOptions ?? {},
2219
2219
  respectExternal: options.respectExternal ?? false,
2220
- includeExternal: options.includeExternal ?? []
2220
+ includeExternal: options.includeExternal ?? [],
2221
+ sourcemap: options.sourcemap ?? false
2221
2222
  };
2222
2223
  }
2223
2224
  const DTS_EXTENSIONS = /\.d\.(c|m)?tsx?$/;
@@ -2270,8 +2271,15 @@ function cacheConfig([fromPath, toPath], config) {
2270
2271
  configByPath.set(fromPath, config);
2271
2272
  }
2272
2273
  }
2273
- function getCompilerOptions(input, overrideOptions, overrideConfigPath) {
2274
- const compilerOptions = { ...DEFAULT_OPTIONS, ...overrideOptions };
2274
+ function getCompilerOptions(input, overrideOptions, overrideConfigPath, enableDeclarationMap) {
2275
+ const compilerOptions = {
2276
+ ...DEFAULT_OPTIONS,
2277
+ ...overrideOptions,
2278
+ // When plugin's sourcemap option is explicitly true, force declarationMap
2279
+ // regardless of user's compilerOptions setting.
2280
+ // When sourcemap is false/undefined, respect user's compilerOptions.declarationMap.
2281
+ ...enableDeclarationMap === true && { declarationMap: true }
2282
+ };
2275
2283
  let dirName = path.dirname(input);
2276
2284
  let dtsFiles = [];
2277
2285
  const cacheKey = overrideConfigPath || dirName;
@@ -2313,11 +2321,11 @@ function getCompilerOptions(input, overrideOptions, overrideConfigPath) {
2313
2321
  }
2314
2322
  };
2315
2323
  }
2316
- function createProgram$1(fileName, overrideOptions, tsconfig) {
2317
- const { dtsFiles, compilerOptions } = getCompilerOptions(fileName, overrideOptions, tsconfig);
2324
+ function createProgram$1(fileName, overrideOptions, tsconfig, enableDeclarationMap) {
2325
+ const { dtsFiles, compilerOptions } = getCompilerOptions(fileName, overrideOptions, tsconfig, enableDeclarationMap);
2318
2326
  return ts.createProgram([fileName].concat(Array.from(dtsFiles)), compilerOptions, ts.createCompilerHost(compilerOptions, true));
2319
2327
  }
2320
- function createPrograms(input, overrideOptions, tsconfig) {
2328
+ function createPrograms(input, overrideOptions, tsconfig, enableDeclarationMap) {
2321
2329
  const programs = [];
2322
2330
  const dtsFiles = /* @__PURE__ */ new Set();
2323
2331
  let inputs = [];
@@ -2328,7 +2336,7 @@ function createPrograms(input, overrideOptions, tsconfig) {
2328
2336
  continue;
2329
2337
  }
2330
2338
  main = path.resolve(main);
2331
- const options = getCompilerOptions(main, overrideOptions, tsconfig);
2339
+ const options = getCompilerOptions(main, overrideOptions, tsconfig, enableDeclarationMap);
2332
2340
  options.dtsFiles.forEach(dtsFiles.add, dtsFiles);
2333
2341
  if (!inputs.length) {
2334
2342
  inputs.push(main);
@@ -3975,7 +3983,14 @@ function hydrateSourcemap(sparseMappings, inputMap, outputCode) {
3975
3983
  return encode(hydratedMappings);
3976
3984
  }
3977
3985
  async function loadInputSourcemap(info) {
3978
- const { fileName, originalCode } = info;
3986
+ const { fileName, originalCode, inputMapText } = info;
3987
+ if (inputMapText) {
3988
+ try {
3989
+ return JSON.parse(inputMapText);
3990
+ } catch {
3991
+ return null;
3992
+ }
3993
+ }
3979
3994
  const inlineConverter = convert$1.fromSource(originalCode);
3980
3995
  if (inlineConverter) {
3981
3996
  return inlineConverter.toObject();
@@ -3999,7 +4014,7 @@ async function loadInputSourcemap(info) {
3999
4014
  return null;
4000
4015
  }
4001
4016
  }
4002
- const transform = () => {
4017
+ const transform = (enableSourcemap) => {
4003
4018
  const allTypeReferences = /* @__PURE__ */ new Map();
4004
4019
  const allFileReferences = /* @__PURE__ */ new Map();
4005
4020
  const pendingSourcemaps = /* @__PURE__ */ new Map();
@@ -4044,7 +4059,7 @@ const transform = () => {
4044
4059
  strict: false
4045
4060
  };
4046
4061
  },
4047
- transform(code, fileName) {
4062
+ transform(code, fileName, inputMapText) {
4048
4063
  const name = trimExtension(fileName);
4049
4064
  const moduleIds = this.getModuleIds();
4050
4065
  const moduleId = Array.from(moduleIds).find((id) => trimExtension(id) === name);
@@ -4063,10 +4078,11 @@ const transform = () => {
4063
4078
  console.log(JSON.stringify(converted.ast.body, void 0, 2));
4064
4079
  }
4065
4080
  const map = preprocessed.code.generateMap({ hires: true, source: fileName });
4066
- if (DTS_EXTENSIONS.test(fileName)) {
4081
+ if (enableSourcemap && DTS_EXTENSIONS.test(fileName)) {
4067
4082
  pendingSourcemaps.set(fileName, {
4068
4083
  fileName,
4069
- originalCode: code
4084
+ originalCode: code,
4085
+ inputMapText
4070
4086
  });
4071
4087
  }
4072
4088
  return { code, ast: converted.ast, map };
@@ -4166,25 +4182,29 @@ const transform = () => {
4166
4182
  if (isUrl(source))
4167
4183
  continue;
4168
4184
  const absoluteSource = path.resolve(chunkDir, source);
4169
- const inputMap = inputSourcemaps.get(absoluteSource);
4185
+ let inputMap = inputSourcemaps.get(absoluteSource);
4186
+ if (!inputMap && /\.[cm]?[tj]sx?$/.test(absoluteSource) && !absoluteSource.endsWith(".d.ts")) {
4187
+ const dtsPath = absoluteSource.replace(/\.[cm]?[tj]sx?$/, ".d.ts");
4188
+ inputMap = inputSourcemaps.get(dtsPath);
4189
+ }
4170
4190
  if (inputMap) {
4171
4191
  sourcesToRemap.set(absoluteSource, inputMap);
4172
4192
  }
4173
4193
  }
4174
4194
  if (sourcesToRemap.size === 0) {
4195
+ delete chunk.map.sourcesContent;
4175
4196
  if (chunk.map.sources.length === 0 && chunk.facadeModuleId) {
4176
4197
  const inputMap = inputSourcemaps.get(chunk.facadeModuleId);
4177
4198
  if (inputMap && inputMap.sources.length > 0) {
4178
4199
  const newSources2 = inputMap.sources.map(toRelativeSourcePathOrNull);
4179
4200
  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
4201
  }
4187
4202
  }
4203
+ updateSourcemapAsset(bundle, chunk.fileName, {
4204
+ sources: chunk.map.sources.map(toRelativeSourcePathOrNull),
4205
+ mappings: chunk.map.mappings,
4206
+ names: chunk.map.names || []
4207
+ });
4188
4208
  continue;
4189
4209
  }
4190
4210
  const isSingleSource = chunk.map.sources.length === 1 && sourcesToRemap.size === 1;
@@ -4198,8 +4218,13 @@ const transform = () => {
4198
4218
  newMappings = hydrateSourcemap(chunk.map.mappings, singleInputMap, chunk.code);
4199
4219
  newNames = singleInputMap.names || [];
4200
4220
  } else {
4221
+ const visitedFiles = /* @__PURE__ */ new Set();
4201
4222
  const remapped = remapping(chunk.map, (file) => {
4202
4223
  const absolutePath = path.resolve(chunkDir, file);
4224
+ if (visitedFiles.has(absolutePath)) {
4225
+ return null;
4226
+ }
4227
+ visitedFiles.add(absolutePath);
4203
4228
  const inputMap = sourcesToRemap.get(absolutePath);
4204
4229
  if (inputMap) {
4205
4230
  return inputMap;
@@ -4273,7 +4298,7 @@ function getModule({ entries, programs, resolvedOptions }, fileName, code) {
4273
4298
  return { code };
4274
4299
  }
4275
4300
  }
4276
- const newProgram = createProgram$1(fileName, compilerOptions, tsconfig);
4301
+ const newProgram = createProgram$1(fileName, compilerOptions, tsconfig, resolvedOptions.sourcemap);
4277
4302
  programs.push(newProgram);
4278
4303
  const source = newProgram.getSourceFile(fileName);
4279
4304
  return {
@@ -4286,8 +4311,8 @@ function getModule({ entries, programs, resolvedOptions }, fileName, code) {
4286
4311
  }
4287
4312
  }
4288
4313
  const plugin = (options = {}) => {
4289
- const transformPlugin = transform();
4290
4314
  const ctx = { entries: [], programs: [], resolvedOptions: resolveDefaultOptions(options) };
4315
+ const transformPlugin = transform(ctx.resolvedOptions.sourcemap);
4291
4316
  return {
4292
4317
  name: "dts",
4293
4318
  // pass outputOptions, renderChunk, and generateBundle hooks to the inner transform plugin
@@ -4310,7 +4335,7 @@ const plugin = (options = {}) => {
4310
4335
  options2.input[name] = filename;
4311
4336
  }
4312
4337
  }
4313
- ctx.programs = createPrograms(Object.values(input), ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig);
4338
+ ctx.programs = createPrograms(Object.values(input), ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig, ctx.resolvedOptions.sourcemap);
4314
4339
  return transformPlugin.options.call(this, options2);
4315
4340
  },
4316
4341
  transform(code, id) {
@@ -4347,11 +4372,16 @@ const plugin = (options = {}) => {
4347
4372
  return null;
4348
4373
  watchFiles(module);
4349
4374
  const declarationId = getDeclarationId(id);
4350
- let generated;
4375
+ let declarationText;
4376
+ let declarationMapText;
4351
4377
  const { emitSkipped, diagnostics } = module.program.emit(
4352
4378
  module.source,
4353
- (_, declarationText) => {
4354
- generated = transformPlugin.transform.call(this, declarationText, declarationId);
4379
+ (emitFileName, text) => {
4380
+ if (emitFileName.endsWith(".map")) {
4381
+ declarationMapText = text;
4382
+ } else {
4383
+ declarationText = text;
4384
+ }
4355
4385
  },
4356
4386
  void 0,
4357
4387
  // cancellationToken
@@ -4369,7 +4399,10 @@ const plugin = (options = {}) => {
4369
4399
  this.error("Failed to compile. Check the logs above.");
4370
4400
  }
4371
4401
  }
4372
- return generated;
4402
+ if (!declarationText)
4403
+ return null;
4404
+ const cleanDeclarationText = declarationText.replace(/\n?\/\/# sourceMappingURL=[^\n]+/, "");
4405
+ return transformPlugin.transform.call(this, cleanDeclarationText, declarationId, declarationMapText);
4373
4406
  };
4374
4407
  if (DTS_EXTENSIONS.test(id))
4375
4408
  return handleDtsFile();
@@ -4386,7 +4419,7 @@ const plugin = (options = {}) => {
4386
4419
  let resolvedCompilerOptions = ctx.resolvedOptions.compilerOptions;
4387
4420
  if (ctx.resolvedOptions.tsconfig) {
4388
4421
  const resolvedSource = source.startsWith(".") ? path.resolve(path.dirname(importer), source) : source;
4389
- resolvedCompilerOptions = getCompilerOptions(resolvedSource, ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig).compilerOptions;
4422
+ resolvedCompilerOptions = getCompilerOptions(resolvedSource, ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig, ctx.resolvedOptions.sourcemap).compilerOptions;
4390
4423
  }
4391
4424
  const { resolvedModule } = ts.resolveModuleName(source, importer, resolvedCompilerOptions, ts.sys);
4392
4425
  if (!resolvedModule) {
@@ -4619,7 +4652,8 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
4619
4652
  exportConditions: conditions
4620
4653
  }),
4621
4654
  plugin({
4622
- respectExternal: true
4655
+ respectExternal: true,
4656
+ sourcemap
4623
4657
  /**
4624
4658
  * Setting a tsconfig or compilerOptions shouldn't be necessary since
4625
4659
  * we're dealing with pre-compiled d.ts files
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
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-DJ_g9wqZ.mjs';
4
4
  import 'node:fs/promises';
5
5
  import 'rollup';
6
6
  import 'typescript';
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-DJ_g9wqZ.mjs';
3
3
  import 'node:path';
4
4
  import 'node:fs/promises';
5
5
  import 'rollup';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dtsroll",
3
- "version": "1.7.3",
3
+ "version": "1.7.4",
4
4
  "description": "Bundle dts files",
5
5
  "keywords": [
6
6
  "bundle",