Skip to content

Commit c6aa575

Browse files
Added option to name sourcemap files, i.e. a output.sourcemapFileName… (#5105)
* Added option to name sourcemap files, i.e. a output.sourcemapFileNames property * Added chunkhash placeholder to sourcemap names * Improve coverage --------- Co-authored-by: Lukas Taegert-Atkinson <[email protected]> Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
1 parent 4398d4d commit c6aa575

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+321
-26
lines changed

cli/help.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Basic options:
6969
--sourcemapBaseUrl <url> Emit absolute sourcemap URLs with given base
7070
--sourcemapExcludeSources Do not include source code in source maps
7171
--sourcemapFile <file> Specify bundle position for source maps
72+
--sourcemapFileNames <pattern> Name pattern for emitted sourcemaps
7273
--stdin=ext Specify file extension used for stdin input
7374
--no-stdin Do not read "-" from stdin
7475
--no-strict Don't emit `"use strict";` in the generated modules

docs/command-line-interface/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export default {
106106
sourcemapBaseUrl,
107107
sourcemapExcludeSources,
108108
sourcemapFile,
109+
sourcemapFileNames,
109110
sourcemapIgnoreList,
110111
sourcemapPathTransform,
111112
validate,
@@ -420,6 +421,7 @@ Many options have command line equivalents. In those cases, any arguments passed
420421
--sourcemapBaseUrl <url> Emit absolute sourcemap URLs with given base
421422
--sourcemapExcludeSources Do not include source code in source maps
422423
--sourcemapFile <file> Specify bundle position for source maps
424+
--sourcemapFileNames <pattern> Name pattern for emitted sourcemaps
423425
--stdin=ext Specify file extension used for stdin input
424426
--no-stdin Do not read "-" from stdin
425427
--no-strict Don't emit `"use strict";` in the generated modules

docs/configuration-options/index.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,22 @@ The location of the generated bundle. If this is an absolute path, all the `sour
14701470

14711471
`sourcemapFile` is not required if `output` is specified, in which case an output filename will be inferred by adding ".map" to the output filename for the bundle.
14721472

1473+
### output.sourcemapFileNames
1474+
1475+
| | |
1476+
| ----: | :--------------------------------------------- |
1477+
| Type: | `string \| ((chunkInfo: ChunkInfo) => string)` |
1478+
| CLI: | `--sourcemapFileNames <pattern>` |
1479+
1480+
The pattern to use for sourcemaps, or a function that is called per sourcemap to return such a pattern. Patterns support the following placeholders:
1481+
1482+
- `[format]`: The rendering format defined in the output options, e.g. `es` or `cjs`.
1483+
- `[hash]`: A hash based only on the content of the final generated sourcemap. You can also set a specific hash length via e.g. `[hash:10]`.
1484+
- `[chunkhash]`: The same hash as the one used for the corresponding generated chunk (if any).
1485+
- `[name]`: The file name (without extension) of the entry point, unless the object form of input was used to define a different name.
1486+
1487+
Forward slashes `/` can be used to place files in sub-directories. When using a function, `chunkInfo` is a reduced version of the one in [`generateBundle`](../plugin-development/index.md#generatebundle) without properties that depend on file names and no information about the rendered modules as rendering only happens after file names have been generated. You can however access a list of included `moduleIds`. See also [`output.assetFileNames`](#output-assetfilenames), [`output.chunkFileNames`](#output-chunkfilenames).
1488+
14731489
### output.sourcemapIgnoreList
14741490

14751491
| | |

docs/javascript-api/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const outputOptions = {
181181
sourcemapBaseUrl,
182182
sourcemapExcludeSources,
183183
sourcemapFile,
184+
sourcemapFileNames,
184185
sourcemapIgnoreList,
185186
sourcemapPathTransform,
186187
validate,

docs/repl/stores/options.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,14 @@ export const useOptions = defineStore('options2', () => {
311311
available: optionOutputPreserveModules.value,
312312
name: 'output.preserveModulesRoot'
313313
});
314-
const optionOutputSourcemap = getBoolean({
315-
name: 'output.sourcemap'
316-
});
317314
const optionOutputSanitizeFileName = getBoolean({
318315
available: alwaysTrue,
319316
defaultValue: true,
320317
name: 'output.sanitizeFileName'
321318
});
319+
const optionOutputSourcemap = getBoolean({
320+
name: 'output.sourcemap'
321+
});
322322
const optionOutputSourcemapBaseUrl = getString({
323323
available: optionOutputSourcemap.value,
324324
name: 'output.sourcemapBaseUrl'
@@ -327,12 +327,22 @@ export const useOptions = defineStore('options2', () => {
327327
available: optionOutputSourcemap.value,
328328
name: 'output.sourcemapExcludeSources'
329329
});
330+
const optionOutputSourcemapFileNames = getString({
331+
available: alwaysTrue,
332+
defaultValue: undefined,
333+
name: 'output.sourcemapFileNames'
334+
});
330335
const optionOutputStrict = getBoolean({
331336
available: () =>
332337
optionOutputFormat.value.value !== undefined && optionOutputFormat.value.value !== 'es',
333338
defaultValue: true,
334339
name: 'output.strict'
335340
});
341+
const optionOutputSystemNullSetters = getBoolean({
342+
available: () => optionOutputFormat.value.value === 'system',
343+
defaultValue: true,
344+
name: 'output.systemNullSetters'
345+
});
336346
const optionOutputValidate = getBoolean({
337347
name: 'output.validate'
338348
});
@@ -342,11 +352,6 @@ export const useOptions = defineStore('options2', () => {
342352
name: 'preserveEntrySignatures',
343353
options: () => ['strict', 'allow-extension', 'exports-only', false]
344354
});
345-
const optionOutputSystemNullSetters = getBoolean({
346-
available: () => optionOutputFormat.value.value === 'system',
347-
defaultValue: true,
348-
name: 'output.systemNullSetters'
349-
});
350355
const optionShimMissingExports = getBoolean({
351356
defaultValue: false,
352357
name: 'shimMissingExports'
@@ -436,6 +441,7 @@ export const useOptions = defineStore('options2', () => {
436441
optionOutputPreserveModules,
437442
optionOutputPreserveModulesRoot,
438443
optionOutputSourcemap,
444+
optionOutputSourcemapFileNames,
439445
optionOutputSanitizeFileName,
440446
optionOutputSourcemapBaseUrl,
441447
optionOutputSourcemapExcludeSources,

src/Chunk.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface ChunkRenderResult {
7272
chunk: Chunk;
7373
magicString: MagicStringBundle;
7474
preliminaryFileName: PreliminaryFileName;
75+
preliminarySourcemapFileName: PreliminaryFileName | null;
7576
usedModules: Module[];
7677
}
7778

@@ -187,6 +188,7 @@ export default class Chunk {
187188
private needsExportsShim = false;
188189
private preRenderedChunkInfo: PreRenderedChunk | null = null;
189190
private preliminaryFileName: PreliminaryFileName | null = null;
191+
private preliminarySourcemapFileName: PreliminaryFileName | null = null;
190192
private renderedChunkInfo: RenderedChunk | null = null;
191193
private renderedDependencies: Map<Chunk | ExternalChunk, ChunkDependency> | null = null;
192194
private readonly renderedModules: {
@@ -328,6 +330,7 @@ export default class Chunk {
328330
finalizeChunk(
329331
code: string,
330332
map: SourceMap | null,
333+
sourcemapFileName: string | null,
331334
hashesByPlaceholder: Map<string, string>
332335
): OutputChunk {
333336
const renderedChunkInfo = this.getRenderedChunkInfo();
@@ -349,7 +352,8 @@ export default class Chunk {
349352
imports: renderedChunkInfo.imports.map(finalize),
350353
map,
351354
preliminaryFileName,
352-
referencedFiles: renderedChunkInfo.referencedFiles.map(finalize)
355+
referencedFiles: renderedChunkInfo.referencedFiles.map(finalize),
356+
sourcemapFileName
353357
};
354358
}
355359

@@ -544,6 +548,36 @@ export default class Chunk {
544548
return (this.preliminaryFileName = { fileName, hashPlaceholder });
545549
}
546550

551+
getPreliminarySourcemapFileName(): PreliminaryFileName | null {
552+
if (this.preliminarySourcemapFileName) {
553+
return this.preliminarySourcemapFileName;
554+
}
555+
let sourcemapFileName: string | null = null;
556+
let hashPlaceholder: string | null = null;
557+
const { sourcemapFileNames, format } = this.outputOptions;
558+
if (sourcemapFileNames) {
559+
const [pattern, patternName] = [sourcemapFileNames, 'output.sourcemapFileNames'];
560+
sourcemapFileName = renderNamePattern(
561+
typeof pattern === 'function' ? pattern(this.getPreRenderedChunkInfo()) : pattern,
562+
patternName,
563+
{
564+
chunkhash: () => this.getPreliminaryFileName().hashPlaceholder || '',
565+
format: () => format,
566+
hash: size =>
567+
hashPlaceholder || (hashPlaceholder = this.getPlaceholder(patternName, size)),
568+
name: () => this.getChunkName()
569+
}
570+
);
571+
if (!hashPlaceholder) {
572+
sourcemapFileName = makeUnique(sourcemapFileName, this.bundle);
573+
}
574+
} else {
575+
return null;
576+
}
577+
578+
return (this.preliminarySourcemapFileName = { fileName: sourcemapFileName, hashPlaceholder });
579+
}
580+
547581
public getRenderedChunkInfo(): RenderedChunk {
548582
if (this.renderedChunkInfo) {
549583
return this.renderedChunkInfo;
@@ -606,6 +640,7 @@ export default class Chunk {
606640
}
607641

608642
const preliminaryFileName = this.getPreliminaryFileName();
643+
const preliminarySourcemapFileName = this.getPreliminarySourcemapFileName();
609644
const { accessedGlobals, indent, magicString, renderedSource, usedModules, usesTopLevelAwait } =
610645
this.renderModules(preliminaryFileName.fileName);
611646

@@ -670,6 +705,7 @@ export default class Chunk {
670705
chunk: this,
671706
magicString,
672707
preliminaryFileName,
708+
preliminarySourcemapFileName,
673709
usedModules
674710
};
675711
}

src/rollup/types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export interface EmittedPrebuiltChunk {
164164
exports?: string[];
165165
fileName: string;
166166
map?: SourceMap;
167+
sourcemapFileName?: string;
167168
type: 'prebuilt-chunk';
168169
}
169170

@@ -744,6 +745,7 @@ export interface OutputOptions {
744745
sourcemapBaseUrl?: string;
745746
sourcemapExcludeSources?: boolean;
746747
sourcemapFile?: string;
748+
sourcemapFileNames?: string | ((chunkInfo: PreRenderedChunk) => string);
747749
sourcemapIgnoreList?: boolean | SourcemapIgnoreListOption;
748750
sourcemapPathTransform?: SourcemapPathTransformOption;
749751
strict?: boolean;
@@ -799,6 +801,7 @@ export interface NormalizedOutputOptions {
799801
sourcemapBaseUrl: string | undefined;
800802
sourcemapExcludeSources: boolean;
801803
sourcemapFile: string | undefined;
804+
sourcemapFileNames: string | ((chunkInfo: PreRenderedChunk) => string) | undefined;
802805
sourcemapIgnoreList: SourcemapIgnoreListOption;
803806
sourcemapPathTransform: SourcemapPathTransformOption | undefined;
804807
strict: boolean;
@@ -862,6 +865,7 @@ export interface RenderedChunk extends PreRenderedChunk {
862865
export interface OutputChunk extends RenderedChunk {
863866
code: string;
864867
map: SourceMap | null;
868+
sourcemapFileName: string | null;
865869
preliminaryFileName: string;
866870
}
867871

src/utils/FileEmitter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ export class FileEmitter {
326326
name: prebuiltChunk.fileName,
327327
preliminaryFileName: prebuiltChunk.fileName,
328328
referencedFiles: [],
329+
sourcemapFileName: prebuiltChunk.sourcemapFileName || null,
329330
type: 'chunk'
330331
};
331332
}

src/utils/options/mergeOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ async function mergeOutputOptions(
282282
sourcemapBaseUrl: getOption('sourcemapBaseUrl'),
283283
sourcemapExcludeSources: getOption('sourcemapExcludeSources'),
284284
sourcemapFile: getOption('sourcemapFile'),
285+
sourcemapFileNames: getOption('sourcemapFileNames'),
285286
sourcemapIgnoreList: getOption('sourcemapIgnoreList'),
286287
sourcemapPathTransform: getOption('sourcemapPathTransform'),
287288
strict: getOption('strict'),

src/utils/options/normalizeOutputOptions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export async function normalizeOutputOptions(
104104
sourcemapBaseUrl: getSourcemapBaseUrl(config),
105105
sourcemapExcludeSources: config.sourcemapExcludeSources || false,
106106
sourcemapFile: config.sourcemapFile,
107+
sourcemapFileNames: getSourcemapFileNames(config, unsetOptions),
107108
sourcemapIgnoreList:
108109
typeof config.sourcemapIgnoreList === 'function'
109110
? config.sourcemapIgnoreList
@@ -528,6 +529,17 @@ const getNamespaceToStringTag = (
528529
return generatedCode.symbols || false;
529530
};
530531

532+
const getSourcemapFileNames = (
533+
config: OutputOptions,
534+
unsetOptions: Set<string>
535+
): NormalizedOutputOptions['sourcemapFileNames'] => {
536+
const configSourcemapFileNames = config.sourcemapFileNames;
537+
if (configSourcemapFileNames == null) {
538+
unsetOptions.add('sourcemapFileNames');
539+
}
540+
return configSourcemapFileNames;
541+
};
542+
531543
const getSourcemapBaseUrl = (
532544
config: OutputOptions
533545
): NormalizedOutputOptions['sourcemapBaseUrl'] => {

0 commit comments

Comments
 (0)