dtsroll 1.7.2 → 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-DaiC_aHH.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.2";
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);
@@ -3937,8 +3945,8 @@ function hydrateSourcemap(sparseMappings, inputMap, outputCode) {
3937
3945
  hydratedMappings.push([]);
3938
3946
  continue;
3939
3947
  }
3940
- const anchor = sparseSegments[0];
3941
- if (!anchor || anchor.length < 4) {
3948
+ const anchor = sparseSegments.find((segment) => segment.length >= 4);
3949
+ if (!anchor) {
3942
3950
  hydratedMappings.push(sparseSegments);
3943
3951
  continue;
3944
3952
  }
@@ -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 };
@@ -4114,13 +4130,34 @@ const transform = () => {
4114
4130
  fileName,
4115
4131
  inputMap: await loadInputSourcemap(info)
4116
4132
  })));
4133
+ const isUrl = (p) => /^[a-z][a-z0-9+.-]*:\/\//i.test(p);
4117
4134
  for (const { fileName, inputMap } of loadedMaps) {
4118
4135
  if (inputMap && inputMap.sources) {
4119
4136
  const inputMapDir = path.dirname(fileName);
4137
+ let sourceRoot;
4138
+ if (inputMap.sourceRoot) {
4139
+ sourceRoot = isUrl(inputMap.sourceRoot) ? inputMap.sourceRoot : path.resolve(inputMapDir, inputMap.sourceRoot);
4140
+ } else {
4141
+ sourceRoot = inputMapDir;
4142
+ }
4143
+ const sourceRootIsUrl = isUrl(sourceRoot);
4120
4144
  inputSourcemaps.set(fileName, {
4121
4145
  version: inputMap.version ?? 3,
4122
- sources: inputMap.sources.map((source) => path.isAbsolute(source) ? source : path.resolve(inputMapDir, source)),
4123
- sourcesContent: inputMap.sourcesContent,
4146
+ sources: inputMap.sources.map((source) => {
4147
+ if (source === null)
4148
+ return null;
4149
+ if (isUrl(source))
4150
+ return source;
4151
+ if (sourceRootIsUrl) {
4152
+ const base = sourceRoot.endsWith("/") ? sourceRoot : sourceRoot + "/";
4153
+ return new URL(source, base).toString();
4154
+ }
4155
+ return path.isAbsolute(source) ? source : path.resolve(sourceRoot, source);
4156
+ }),
4157
+ // Note: sourcesContent intentionally not copied.
4158
+ // TypeScript's declaration maps never include sourcesContent, and tsserver's
4159
+ // getDocumentPositionMapper() rejects maps that have it (returns identity mapper).
4160
+ // https://github.com/microsoft/TypeScript/blob/b19a9da2a3b8f2a720d314d01258dd2bdc110fef/src/services/sourcemaps.ts#L226
4124
4161
  mappings: inputMap.mappings,
4125
4162
  names: inputMap.names
4126
4163
  });
@@ -4132,70 +4169,78 @@ const transform = () => {
4132
4169
  continue;
4133
4170
  const chunkDir = path.join(outputDir, path.dirname(chunk.fileName));
4134
4171
  const toRelativeSourcePath = (source) => {
4172
+ if (isUrl(source))
4173
+ return source;
4135
4174
  const relative = path.isAbsolute(source) ? path.relative(chunkDir, source) : source;
4136
4175
  return relative.replaceAll("\\", "/");
4137
4176
  };
4177
+ const toRelativeSourcePathOrNull = (source) => source === null ? null : toRelativeSourcePath(source);
4138
4178
  const sourcesToRemap = /* @__PURE__ */ new Map();
4139
4179
  for (const source of chunk.map.sources) {
4140
4180
  if (!source)
4141
4181
  continue;
4182
+ if (isUrl(source))
4183
+ continue;
4142
4184
  const absoluteSource = path.resolve(chunkDir, source);
4143
- 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
+ }
4144
4190
  if (inputMap) {
4145
4191
  sourcesToRemap.set(absoluteSource, inputMap);
4146
4192
  }
4147
4193
  }
4148
4194
  if (sourcesToRemap.size === 0) {
4195
+ delete chunk.map.sourcesContent;
4149
4196
  if (chunk.map.sources.length === 0 && chunk.facadeModuleId) {
4150
4197
  const inputMap = inputSourcemaps.get(chunk.facadeModuleId);
4151
4198
  if (inputMap && inputMap.sources.length > 0) {
4152
- const newSources2 = inputMap.sources.map(toRelativeSourcePath);
4153
- const newSourcesContent2 = inputMap.sourcesContent || [];
4199
+ const newSources2 = inputMap.sources.map(toRelativeSourcePathOrNull);
4154
4200
  chunk.map.sources = newSources2;
4155
- chunk.map.sourcesContent = newSourcesContent2;
4156
- updateSourcemapAsset(bundle, chunk.fileName, {
4157
- sources: newSources2,
4158
- sourcesContent: newSourcesContent2,
4159
- mappings: chunk.map.mappings,
4160
- names: chunk.map.names || []
4161
- });
4162
4201
  }
4163
4202
  }
4203
+ updateSourcemapAsset(bundle, chunk.fileName, {
4204
+ sources: chunk.map.sources.map(toRelativeSourcePathOrNull),
4205
+ mappings: chunk.map.mappings,
4206
+ names: chunk.map.names || []
4207
+ });
4164
4208
  continue;
4165
4209
  }
4166
4210
  const isSingleSource = chunk.map.sources.length === 1 && sourcesToRemap.size === 1;
4167
4211
  const singleInputMap = isSingleSource ? Array.from(sourcesToRemap.values())[0] : null;
4168
4212
  const canSimplyReplace = singleInputMap && singleInputMap.sources.length === 1;
4169
4213
  let newSources;
4170
- let newSourcesContent;
4171
4214
  let newMappings;
4172
4215
  let newNames;
4173
4216
  if (canSimplyReplace && singleInputMap) {
4174
- newSources = singleInputMap.sources.map(toRelativeSourcePath);
4175
- newSourcesContent = singleInputMap.sourcesContent || [null];
4217
+ newSources = singleInputMap.sources.map(toRelativeSourcePathOrNull);
4176
4218
  newMappings = hydrateSourcemap(chunk.map.mappings, singleInputMap, chunk.code);
4177
4219
  newNames = singleInputMap.names || [];
4178
4220
  } else {
4221
+ const visitedFiles = /* @__PURE__ */ new Set();
4179
4222
  const remapped = remapping(chunk.map, (file) => {
4180
4223
  const absolutePath = path.resolve(chunkDir, file);
4224
+ if (visitedFiles.has(absolutePath)) {
4225
+ return null;
4226
+ }
4227
+ visitedFiles.add(absolutePath);
4181
4228
  const inputMap = sourcesToRemap.get(absolutePath);
4182
4229
  if (inputMap) {
4183
4230
  return inputMap;
4184
4231
  }
4185
4232
  return null;
4186
4233
  });
4187
- newSources = remapped.sources.filter((s) => s !== null).map(toRelativeSourcePath);
4188
- newSourcesContent = remapped.sourcesContent || [];
4234
+ newSources = remapped.sources.map(toRelativeSourcePathOrNull);
4189
4235
  newMappings = typeof remapped.mappings === "string" ? remapped.mappings : "";
4190
4236
  newNames = remapped.names || [];
4191
4237
  }
4192
4238
  chunk.map.sources = newSources;
4193
- chunk.map.sourcesContent = newSourcesContent;
4239
+ delete chunk.map.sourcesContent;
4194
4240
  chunk.map.mappings = newMappings;
4195
4241
  chunk.map.names = newNames;
4196
4242
  updateSourcemapAsset(bundle, chunk.fileName, {
4197
4243
  sources: newSources,
4198
- sourcesContent: newSourcesContent,
4199
4244
  mappings: newMappings,
4200
4245
  names: newNames
4201
4246
  });
@@ -4253,7 +4298,7 @@ function getModule({ entries, programs, resolvedOptions }, fileName, code) {
4253
4298
  return { code };
4254
4299
  }
4255
4300
  }
4256
- const newProgram = createProgram$1(fileName, compilerOptions, tsconfig);
4301
+ const newProgram = createProgram$1(fileName, compilerOptions, tsconfig, resolvedOptions.sourcemap);
4257
4302
  programs.push(newProgram);
4258
4303
  const source = newProgram.getSourceFile(fileName);
4259
4304
  return {
@@ -4266,8 +4311,8 @@ function getModule({ entries, programs, resolvedOptions }, fileName, code) {
4266
4311
  }
4267
4312
  }
4268
4313
  const plugin = (options = {}) => {
4269
- const transformPlugin = transform();
4270
4314
  const ctx = { entries: [], programs: [], resolvedOptions: resolveDefaultOptions(options) };
4315
+ const transformPlugin = transform(ctx.resolvedOptions.sourcemap);
4271
4316
  return {
4272
4317
  name: "dts",
4273
4318
  // pass outputOptions, renderChunk, and generateBundle hooks to the inner transform plugin
@@ -4290,7 +4335,7 @@ const plugin = (options = {}) => {
4290
4335
  options2.input[name] = filename;
4291
4336
  }
4292
4337
  }
4293
- 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);
4294
4339
  return transformPlugin.options.call(this, options2);
4295
4340
  },
4296
4341
  transform(code, id) {
@@ -4327,11 +4372,16 @@ const plugin = (options = {}) => {
4327
4372
  return null;
4328
4373
  watchFiles(module);
4329
4374
  const declarationId = getDeclarationId(id);
4330
- let generated;
4375
+ let declarationText;
4376
+ let declarationMapText;
4331
4377
  const { emitSkipped, diagnostics } = module.program.emit(
4332
4378
  module.source,
4333
- (_, declarationText) => {
4334
- generated = transformPlugin.transform.call(this, declarationText, declarationId);
4379
+ (emitFileName, text) => {
4380
+ if (emitFileName.endsWith(".map")) {
4381
+ declarationMapText = text;
4382
+ } else {
4383
+ declarationText = text;
4384
+ }
4335
4385
  },
4336
4386
  void 0,
4337
4387
  // cancellationToken
@@ -4349,7 +4399,10 @@ const plugin = (options = {}) => {
4349
4399
  this.error("Failed to compile. Check the logs above.");
4350
4400
  }
4351
4401
  }
4352
- 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);
4353
4406
  };
4354
4407
  if (DTS_EXTENSIONS.test(id))
4355
4408
  return handleDtsFile();
@@ -4366,7 +4419,7 @@ const plugin = (options = {}) => {
4366
4419
  let resolvedCompilerOptions = ctx.resolvedOptions.compilerOptions;
4367
4420
  if (ctx.resolvedOptions.tsconfig) {
4368
4421
  const resolvedSource = source.startsWith(".") ? path.resolve(path.dirname(importer), source) : source;
4369
- resolvedCompilerOptions = getCompilerOptions(resolvedSource, ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig).compilerOptions;
4422
+ resolvedCompilerOptions = getCompilerOptions(resolvedSource, ctx.resolvedOptions.compilerOptions, ctx.resolvedOptions.tsconfig, ctx.resolvedOptions.sourcemap).compilerOptions;
4370
4423
  }
4371
4424
  const { resolvedModule } = ts.resolveModuleName(source, importer, resolvedCompilerOptions, ts.sys);
4372
4425
  if (!resolvedModule) {
@@ -4599,7 +4652,8 @@ const build = async (input, outputDirectory, externals, mode, conditions, source
4599
4652
  exportConditions: conditions
4600
4653
  }),
4601
4654
  plugin({
4602
- respectExternal: true
4655
+ respectExternal: true,
4656
+ sourcemap
4603
4657
  /**
4604
4658
  * Setting a tsconfig or compilerOptions shouldn't be necessary since
4605
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-DaiC_aHH.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-DaiC_aHH.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.2",
3
+ "version": "1.7.4",
4
4
  "description": "Bundle dts files",
5
5
  "keywords": [
6
6
  "bundle",