Skip to content

Commit

Permalink
Update sourcemapPathTransform to also take the path to the sourcemap … (
Browse files Browse the repository at this point in the history
#3617)

* Update sourcemapPathTransform to also take the path to the sourcemap file as a second argument

* update docs

* make test example make a little more sense

* Fix test and types, polish documentation

* Fix types again

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 Jun 8, 2020
1 parent ef978c1 commit 37b876c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
10 changes: 5 additions & 5 deletions docs/999-big-list-of-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,19 +670,19 @@ 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.sourcemapPathTransform
Type: `(sourcePath: string) => string`
Type: `(relativeSourcePath: string, sourcemapPath: string) => string`

A transformation to apply to each path in a sourcemap. For instance the following will change all paths to be relative to the `src` directory.
A transformation to apply to each path in a sourcemap. `relativeSourcePath` is a relative path from the generated `.map` file to the corresponding source file while `sourcemapPath` is the fully resolved path of the generated sourcemap file.

```js
import path from 'path';
export default ({
input: 'src/main',
output: [{
file: 'bundle.js',
sourcemapPathTransform: relativePath => {
// will transform e.g. "src/main.js" -> "main.js"
return path.relative('src', relativePath)
sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => {
// will replace relative paths with absolute paths
return path.resolve(path.dirname(sourcemapPath), relativeSourcePath)
},
format: 'es',
sourcemap: true
Expand Down
2 changes: 1 addition & 1 deletion src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ export default class Chunk {
const { sourcemapPathTransform } = options;

if (sourcemapPathTransform) {
const newSourcePath = sourcemapPathTransform(sourcePath);
const newSourcePath = sourcemapPathTransform(sourcePath, `${file}.map`);

if (typeof newSourcePath !== 'string') {
error(errFailedValidation(`sourcemapPathTransform function must return a string.`));
Expand Down
8 changes: 6 additions & 2 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ export type InputOption = string | string[] | { [entryAlias: string]: string };
export type ManualChunksOption = { [chunkAlias: string]: string[] } | GetManualChunk;
export type ModuleSideEffectsOption = boolean | 'no-external' | string[] | HasModuleSideEffects;
export type PreserveEntrySignaturesOption = false | 'strict' | 'allow-extension';
export type SourcemapPathTransformOption = (
relativeSourcePath: string,
sourcemapPath: string
) => string;

export interface InputOptions {
acorn?: Object;
Expand Down Expand Up @@ -569,7 +573,7 @@ export interface OutputOptions {
sourcemap?: boolean | 'inline' | 'hidden';
sourcemapExcludeSources?: boolean;
sourcemapFile?: string;
sourcemapPathTransform?: (sourcePath: string) => string;
sourcemapPathTransform?: SourcemapPathTransformOption;
strict?: boolean;
systemNullSetters?: boolean;
}
Expand Down Expand Up @@ -611,7 +615,7 @@ export interface NormalizedOutputOptions {
sourcemap: boolean | 'inline' | 'hidden';
sourcemapExcludeSources: boolean;
sourcemapFile: string | undefined;
sourcemapPathTransform: ((sourcePath: string) => string) | undefined;
sourcemapPathTransform: SourcemapPathTransformOption | undefined;
strict: boolean;
systemNullSetters: boolean;
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils/options/normalizeOutputOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
NormalizedInputOptions,
NormalizedOutputOptions,
OptionsPaths,
OutputOptions
OutputOptions,
SourcemapPathTransformOption
} from '../../rollup/types';
import { ensureArray } from '../ensureArray';
import { errInvalidExportOptionValue, error, warnDeprecation } from '../error';
Expand Down Expand Up @@ -57,7 +58,7 @@ export function normalizeOutputOptions(
sourcemapExcludeSources: (config.sourcemapExcludeSources as boolean | undefined) || false,
sourcemapFile: config.sourcemapFile as string | undefined,
sourcemapPathTransform: config.sourcemapPathTransform as
| ((sourcePath: string) => string)
| SourcemapPathTransformOption
| undefined,
strict: (config.strict as boolean | undefined) ?? true,
systemNullSetters: (config.systemNullSetters as boolean | undefined) || false
Expand Down
17 changes: 17 additions & 0 deletions test/sourcemaps/samples/transform-full-source-paths/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('path');
const assert = require('assert');

module.exports = {
description: 'provides the full source map path when transforming source maps',
options: {
output: {
name: 'myModule',
file: path.join(__dirname, '_actual/bundle.js'),
sourcemapPathTransform: (relativeSourcePath, sourcemapPath) =>
path.resolve(path.dirname(sourcemapPath), relativeSourcePath)
}
},
test(code, map) {
assert.deepEqual(map.sources, [path.resolve(__dirname, 'main.js')]);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 42;

0 comments on commit 37b876c

Please sign in to comment.