Skip to content

Commit

Permalink
Added option to transform the paths within a sourcemap. (#2430)
Browse files Browse the repository at this point in the history
Resolves #2168.
  • Loading branch information
sebinsua authored and lukastaegert committed Sep 13, 2018
1 parent 007f42b commit b67bf5b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Chunk.ts
Expand Up @@ -1137,7 +1137,11 @@ export default class Chunk {
map = magicString.generateMap({ file, includeContent: true });
}

map.sources = map.sources.map(normalize);
map.sources = map.sources.map(sourcePath =>
normalize(
options.sourcemapPathTransform ? options.sourcemapPathTransform(sourcePath) : sourcePath
)
);

timeEnd('sourcemap', 3);
}
Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Expand Up @@ -284,6 +284,7 @@ export interface OutputOptions {
outro?: string | (() => string | Promise<string>);
sourcemap?: boolean | 'inline';
sourcemapFile?: string;
sourcemapPathTransform?: (sourcePath: string) => string;
interop?: boolean;
extend?: boolean;

Expand Down
6 changes: 5 additions & 1 deletion src/utils/mergeOptions.ts
Expand Up @@ -128,11 +128,14 @@ export default function mergeOptions({
'output option'
);

const validCliOutputOptions = validOutputOptions.filter(
option => option !== 'sourcemapPathTransform'
);
addUnknownOptionErrors(
unknownOptionErrors,
Object.keys(command),
validInputOptions.concat(
validOutputOptions,
validCliOutputOptions,
Object.keys(commandAliases),
'config',
'environment',
Expand Down Expand Up @@ -269,6 +272,7 @@ function getOutputOptions(
paths: getOption('paths'),
sourcemap: getOption('sourcemap'),
sourcemapFile: getOption('sourcemapFile'),
sourcemapPathTransform: getOption('sourcemapPathTransform'),
strict: getOption('strict', true)
};
}
Expand Down
2 changes: 1 addition & 1 deletion test/misc/optionList.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions test/sourcemaps/samples/transform-source-paths/_config.js
@@ -0,0 +1,19 @@
const path = require('path');
const assert = require('assert');

module.exports = {
description: 'transform sourcemap paths (#2168)',
options: {
output: {
name: 'myModule',
file: path.resolve(__dirname, '_actual/bundle.js'),
sourcemapPathTransform: sourcePath => sourcePath.replace(
`..${path.sep}`,
'~/pkg-name/'
)
}
},
test(code, map) {
assert.deepEqual(map.sources, ['~/pkg-name/main.js']);
}
};
1 change: 1 addition & 0 deletions test/sourcemaps/samples/transform-source-paths/main.js
@@ -0,0 +1 @@
export default 42;

0 comments on commit b67bf5b

Please sign in to comment.