Skip to content

Commit

Permalink
Support excluding sources from sourcemaps (#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored and lukastaegert committed Oct 30, 2018
1 parent a1cc69c commit 24fe07f
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/Chunk.ts
Expand Up @@ -1147,9 +1147,16 @@ export default class Chunk {

if (this.graph.pluginDriver.hasLoadersOrTransforms) {
const decodedMap = magicString.generateDecodedMap({});
map = collapseSourcemaps(this, file, decodedMap, this.usedModules, chunkSourcemapChain);
map = collapseSourcemaps(
this,
file,
decodedMap,
this.usedModules,
chunkSourcemapChain,
options.sourcemapExcludeSources
);
} else {
map = magicString.generateMap({ file, includeContent: true });
map = magicString.generateMap({ file, includeContent: !options.sourcemapExcludeSources });
}

map.sources = map.sources.map(sourcePath =>
Expand Down
1 change: 1 addition & 0 deletions src/rollup/types.d.ts
Expand Up @@ -283,6 +283,7 @@ export interface OutputOptions {
intro?: string | (() => string | Promise<string>);
outro?: string | (() => string | Promise<string>);
sourcemap?: boolean | 'inline';
sourcemapExcludeSources?: boolean;
sourcemapFile?: string;
sourcemapPathTransform?: (sourcePath: string) => string;
interop?: boolean;
Expand Down
5 changes: 4 additions & 1 deletion src/utils/collapseSourcemaps.ts
Expand Up @@ -140,7 +140,8 @@ export default function collapseSourcemaps(
file: string,
map: DecodedSourceMap,
modules: Module[],
bundleSourcemapChain: RawSourceMap[]
bundleSourcemapChain: RawSourceMap[],
excludeContent: boolean
) {
const moduleSources = modules.filter(module => !module.excludeFromSourcemap).map(module => {
let sourcemapChain = module.sourcemapChain;
Expand Down Expand Up @@ -206,5 +207,7 @@ export default function collapseSourcemaps(
file = basename(file);
}

sourcesContent = excludeContent ? null : sourcesContent;

return new SourceMap({ file, sources, sourcesContent, names, mappings });
}
1 change: 1 addition & 0 deletions src/utils/mergeOptions.ts
Expand Up @@ -271,6 +271,7 @@ function getOutputOptions(
outro: getOption('outro'),
paths: getOption('paths'),
sourcemap: getOption('sourcemap'),
sourcemapExcludeSources: getOption('sourcemapExcludeSources'),
sourcemapFile: getOption('sourcemapFile'),
sourcemapPathTransform: getOption('sourcemapPathTransform'),
strict: getOption('strict', true)
Expand Down
8 changes: 6 additions & 2 deletions test/form/index.js
Expand Up @@ -78,14 +78,18 @@ function generateAndTestBundle(bundle, outputOptions, expectedFile, { show }) {

try {
actualMap = JSON.parse(sander.readFileSync(outputOptions.file + '.map').toString());
actualMap.sourcesContent = actualMap.sourcesContent.map(normaliseOutput);
actualMap.sourcesContent = actualMap.sourcesContent
? actualMap.sourcesContent.map(normaliseOutput)
: null;
} catch (err) {
assert.equal(err.code, 'ENOENT');
}

try {
expectedMap = JSON.parse(sander.readFileSync(expectedFile + '.map').toString());
expectedMap.sourcesContent = expectedMap.sourcesContent.map(normaliseOutput);
expectedMap.sourcesContent = actualMap.sourcesContent
? expectedMap.sourcesContent.map(normaliseOutput)
: null;
} catch (err) {
assert.equal(err.code, 'ENOENT');
}
Expand Down
7 changes: 7 additions & 0 deletions test/form/samples/sourcemaps-excludesources/_config.js
@@ -0,0 +1,7 @@
module.exports = {
description: 'correct sourcemaps are written (excluding sourceContent)',
skipIfWindows: true,
options: {
output: { sourcemap: true, sourcemapExcludeSources: true }
}
};
17 changes: 17 additions & 0 deletions test/form/samples/sourcemaps-excludesources/_expected/amd.js

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

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

15 changes: 15 additions & 0 deletions test/form/samples/sourcemaps-excludesources/_expected/cjs.js

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

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

13 changes: 13 additions & 0 deletions test/form/samples/sourcemaps-excludesources/_expected/es.js

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

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

18 changes: 18 additions & 0 deletions test/form/samples/sourcemaps-excludesources/_expected/iife.js

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

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

22 changes: 22 additions & 0 deletions test/form/samples/sourcemaps-excludesources/_expected/system.js

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

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

21 changes: 21 additions & 0 deletions test/form/samples/sourcemaps-excludesources/_expected/umd.js

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

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

3 changes: 3 additions & 0 deletions test/form/samples/sourcemaps-excludesources/bar.js
@@ -0,0 +1,3 @@
export default function bar () {
console.log( 'hello from bar.js' );
}
3 changes: 3 additions & 0 deletions test/form/samples/sourcemaps-excludesources/foo.js
@@ -0,0 +1,3 @@
export default function foo () {
console.log( 'hello from foo.js' );
}
7 changes: 7 additions & 0 deletions test/form/samples/sourcemaps-excludesources/main.js
@@ -0,0 +1,7 @@
import foo from './foo';
import bar from './bar';

console.log( 'hello from main.js' );

foo();
bar();
4 changes: 2 additions & 2 deletions test/misc/optionList.js

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

0 comments on commit 24fe07f

Please sign in to comment.