Skip to content

Commit

Permalink
Added option to name sourcemap files, i.e. a output.sourcemapFileName… (
Browse files Browse the repository at this point in the history
#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 <lukas.taegert-atkinson@tngtech.com>
Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 6, 2023
1 parent 4398d4d commit c6aa575
Show file tree
Hide file tree
Showing 53 changed files with 321 additions and 26 deletions.
1 change: 1 addition & 0 deletions cli/help.md
Expand Up @@ -69,6 +69,7 @@ Basic options:
--sourcemapBaseUrl <url> Emit absolute sourcemap URLs with given base
--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--sourcemapFileNames <pattern> Name pattern for emitted sourcemaps
--stdin=ext Specify file extension used for stdin input
--no-stdin Do not read "-" from stdin
--no-strict Don't emit `"use strict";` in the generated modules
Expand Down
2 changes: 2 additions & 0 deletions docs/command-line-interface/index.md
Expand Up @@ -106,6 +106,7 @@ export default {
sourcemapBaseUrl,
sourcemapExcludeSources,
sourcemapFile,
sourcemapFileNames,
sourcemapIgnoreList,
sourcemapPathTransform,
validate,
Expand Down Expand Up @@ -420,6 +421,7 @@ Many options have command line equivalents. In those cases, any arguments passed
--sourcemapBaseUrl <url> Emit absolute sourcemap URLs with given base
--sourcemapExcludeSources Do not include source code in source maps
--sourcemapFile <file> Specify bundle position for source maps
--sourcemapFileNames <pattern> Name pattern for emitted sourcemaps
--stdin=ext Specify file extension used for stdin input
--no-stdin Do not read "-" from stdin
--no-strict Don't emit `"use strict";` in the generated modules
Expand Down
16 changes: 16 additions & 0 deletions docs/configuration-options/index.md
Expand Up @@ -1470,6 +1470,22 @@ The location of the generated bundle. If this is an absolute path, all the `sour

`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.

### output.sourcemapFileNames

| | |
| ----: | :--------------------------------------------- |
| Type: | `string \| ((chunkInfo: ChunkInfo) => string)` |
| CLI: | `--sourcemapFileNames <pattern>` |

The pattern to use for sourcemaps, or a function that is called per sourcemap to return such a pattern. Patterns support the following placeholders:

- `[format]`: The rendering format defined in the output options, e.g. `es` or `cjs`.
- `[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]`.
- `[chunkhash]`: The same hash as the one used for the corresponding generated chunk (if any).
- `[name]`: The file name (without extension) of the entry point, unless the object form of input was used to define a different name.

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).

### output.sourcemapIgnoreList

| | |
Expand Down
1 change: 1 addition & 0 deletions docs/javascript-api/index.md
Expand Up @@ -181,6 +181,7 @@ const outputOptions = {
sourcemapBaseUrl,
sourcemapExcludeSources,
sourcemapFile,
sourcemapFileNames,
sourcemapIgnoreList,
sourcemapPathTransform,
validate,
Expand Down
22 changes: 14 additions & 8 deletions docs/repl/stores/options.ts
Expand Up @@ -311,14 +311,14 @@ export const useOptions = defineStore('options2', () => {
available: optionOutputPreserveModules.value,
name: 'output.preserveModulesRoot'
});
const optionOutputSourcemap = getBoolean({
name: 'output.sourcemap'
});
const optionOutputSanitizeFileName = getBoolean({
available: alwaysTrue,
defaultValue: true,
name: 'output.sanitizeFileName'
});
const optionOutputSourcemap = getBoolean({
name: 'output.sourcemap'
});
const optionOutputSourcemapBaseUrl = getString({
available: optionOutputSourcemap.value,
name: 'output.sourcemapBaseUrl'
Expand All @@ -327,12 +327,22 @@ export const useOptions = defineStore('options2', () => {
available: optionOutputSourcemap.value,
name: 'output.sourcemapExcludeSources'
});
const optionOutputSourcemapFileNames = getString({
available: alwaysTrue,
defaultValue: undefined,
name: 'output.sourcemapFileNames'
});
const optionOutputStrict = getBoolean({
available: () =>
optionOutputFormat.value.value !== undefined && optionOutputFormat.value.value !== 'es',
defaultValue: true,
name: 'output.strict'
});
const optionOutputSystemNullSetters = getBoolean({
available: () => optionOutputFormat.value.value === 'system',
defaultValue: true,
name: 'output.systemNullSetters'
});
const optionOutputValidate = getBoolean({
name: 'output.validate'
});
Expand All @@ -342,11 +352,6 @@ export const useOptions = defineStore('options2', () => {
name: 'preserveEntrySignatures',
options: () => ['strict', 'allow-extension', 'exports-only', false]
});
const optionOutputSystemNullSetters = getBoolean({
available: () => optionOutputFormat.value.value === 'system',
defaultValue: true,
name: 'output.systemNullSetters'
});
const optionShimMissingExports = getBoolean({
defaultValue: false,
name: 'shimMissingExports'
Expand Down Expand Up @@ -436,6 +441,7 @@ export const useOptions = defineStore('options2', () => {
optionOutputPreserveModules,
optionOutputPreserveModulesRoot,
optionOutputSourcemap,
optionOutputSourcemapFileNames,
optionOutputSanitizeFileName,
optionOutputSourcemapBaseUrl,
optionOutputSourcemapExcludeSources,
Expand Down
38 changes: 37 additions & 1 deletion src/Chunk.ts
Expand Up @@ -72,6 +72,7 @@ export interface ChunkRenderResult {
chunk: Chunk;
magicString: MagicStringBundle;
preliminaryFileName: PreliminaryFileName;
preliminarySourcemapFileName: PreliminaryFileName | null;
usedModules: Module[];
}

Expand Down Expand Up @@ -187,6 +188,7 @@ export default class Chunk {
private needsExportsShim = false;
private preRenderedChunkInfo: PreRenderedChunk | null = null;
private preliminaryFileName: PreliminaryFileName | null = null;
private preliminarySourcemapFileName: PreliminaryFileName | null = null;
private renderedChunkInfo: RenderedChunk | null = null;
private renderedDependencies: Map<Chunk | ExternalChunk, ChunkDependency> | null = null;
private readonly renderedModules: {
Expand Down Expand Up @@ -328,6 +330,7 @@ export default class Chunk {
finalizeChunk(
code: string,
map: SourceMap | null,
sourcemapFileName: string | null,
hashesByPlaceholder: Map<string, string>
): OutputChunk {
const renderedChunkInfo = this.getRenderedChunkInfo();
Expand All @@ -349,7 +352,8 @@ export default class Chunk {
imports: renderedChunkInfo.imports.map(finalize),
map,
preliminaryFileName,
referencedFiles: renderedChunkInfo.referencedFiles.map(finalize)
referencedFiles: renderedChunkInfo.referencedFiles.map(finalize),
sourcemapFileName
};
}

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

getPreliminarySourcemapFileName(): PreliminaryFileName | null {
if (this.preliminarySourcemapFileName) {
return this.preliminarySourcemapFileName;
}
let sourcemapFileName: string | null = null;
let hashPlaceholder: string | null = null;
const { sourcemapFileNames, format } = this.outputOptions;
if (sourcemapFileNames) {
const [pattern, patternName] = [sourcemapFileNames, 'output.sourcemapFileNames'];
sourcemapFileName = renderNamePattern(
typeof pattern === 'function' ? pattern(this.getPreRenderedChunkInfo()) : pattern,
patternName,
{
chunkhash: () => this.getPreliminaryFileName().hashPlaceholder || '',
format: () => format,
hash: size =>
hashPlaceholder || (hashPlaceholder = this.getPlaceholder(patternName, size)),
name: () => this.getChunkName()
}
);
if (!hashPlaceholder) {
sourcemapFileName = makeUnique(sourcemapFileName, this.bundle);
}
} else {
return null;
}

return (this.preliminarySourcemapFileName = { fileName: sourcemapFileName, hashPlaceholder });
}

public getRenderedChunkInfo(): RenderedChunk {
if (this.renderedChunkInfo) {
return this.renderedChunkInfo;
Expand Down Expand Up @@ -606,6 +640,7 @@ export default class Chunk {
}

const preliminaryFileName = this.getPreliminaryFileName();
const preliminarySourcemapFileName = this.getPreliminarySourcemapFileName();
const { accessedGlobals, indent, magicString, renderedSource, usedModules, usesTopLevelAwait } =
this.renderModules(preliminaryFileName.fileName);

Expand Down Expand Up @@ -670,6 +705,7 @@ export default class Chunk {
chunk: this,
magicString,
preliminaryFileName,
preliminarySourcemapFileName,
usedModules
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -164,6 +164,7 @@ export interface EmittedPrebuiltChunk {
exports?: string[];
fileName: string;
map?: SourceMap;
sourcemapFileName?: string;
type: 'prebuilt-chunk';
}

Expand Down Expand Up @@ -744,6 +745,7 @@ export interface OutputOptions {
sourcemapBaseUrl?: string;
sourcemapExcludeSources?: boolean;
sourcemapFile?: string;
sourcemapFileNames?: string | ((chunkInfo: PreRenderedChunk) => string);
sourcemapIgnoreList?: boolean | SourcemapIgnoreListOption;
sourcemapPathTransform?: SourcemapPathTransformOption;
strict?: boolean;
Expand Down Expand Up @@ -799,6 +801,7 @@ export interface NormalizedOutputOptions {
sourcemapBaseUrl: string | undefined;
sourcemapExcludeSources: boolean;
sourcemapFile: string | undefined;
sourcemapFileNames: string | ((chunkInfo: PreRenderedChunk) => string) | undefined;
sourcemapIgnoreList: SourcemapIgnoreListOption;
sourcemapPathTransform: SourcemapPathTransformOption | undefined;
strict: boolean;
Expand Down Expand Up @@ -862,6 +865,7 @@ export interface RenderedChunk extends PreRenderedChunk {
export interface OutputChunk extends RenderedChunk {
code: string;
map: SourceMap | null;
sourcemapFileName: string | null;
preliminaryFileName: string;
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/FileEmitter.ts
Expand Up @@ -326,6 +326,7 @@ export class FileEmitter {
name: prebuiltChunk.fileName,
preliminaryFileName: prebuiltChunk.fileName,
referencedFiles: [],
sourcemapFileName: prebuiltChunk.sourcemapFileName || null,
type: 'chunk'
};
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/options/mergeOptions.ts
Expand Up @@ -282,6 +282,7 @@ async function mergeOutputOptions(
sourcemapBaseUrl: getOption('sourcemapBaseUrl'),
sourcemapExcludeSources: getOption('sourcemapExcludeSources'),
sourcemapFile: getOption('sourcemapFile'),
sourcemapFileNames: getOption('sourcemapFileNames'),
sourcemapIgnoreList: getOption('sourcemapIgnoreList'),
sourcemapPathTransform: getOption('sourcemapPathTransform'),
strict: getOption('strict'),
Expand Down
12 changes: 12 additions & 0 deletions src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -104,6 +104,7 @@ export async function normalizeOutputOptions(
sourcemapBaseUrl: getSourcemapBaseUrl(config),
sourcemapExcludeSources: config.sourcemapExcludeSources || false,
sourcemapFile: config.sourcemapFile,
sourcemapFileNames: getSourcemapFileNames(config, unsetOptions),
sourcemapIgnoreList:
typeof config.sourcemapIgnoreList === 'function'
? config.sourcemapIgnoreList
Expand Down Expand Up @@ -528,6 +529,17 @@ const getNamespaceToStringTag = (
return generatedCode.symbols || false;
};

const getSourcemapFileNames = (
config: OutputOptions,
unsetOptions: Set<string>
): NormalizedOutputOptions['sourcemapFileNames'] => {
const configSourcemapFileNames = config.sourcemapFileNames;
if (configSourcemapFileNames == null) {
unsetOptions.add('sourcemapFileNames');
}
return configSourcemapFileNames;
};

const getSourcemapBaseUrl = (
config: OutputOptions
): NormalizedOutputOptions['sourcemapBaseUrl'] => {
Expand Down

0 comments on commit c6aa575

Please sign in to comment.