Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sourcemapPathTransform to also take the path to the sourcemap … #3617

Merged
merged 6 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;