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

Fix in-memory sourcemap filename #2161

Merged
merged 2 commits into from
Apr 28, 2018
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
6 changes: 4 additions & 2 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,10 @@ export default class Chunk {
if (options.sourcemap) {
timeStart('sourcemap', 3);

let file = options.file ? options.sourcemapFile || options.file : this.id;
if (file) file = resolve(typeof process !== 'undefined' ? process.cwd() : '', file);
let file: string;
if (options.file) file = resolve(options.sourcemapFile || options.file);
else if (options.dir) file = resolve(options.dir, this.id);
else file = resolve(this.id);

if (
this.graph.hasLoaders ||
Expand Down
12 changes: 11 additions & 1 deletion src/rollup/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getTimings, initialiseTimers, timeEnd, timeStart } from '../utils/timers';
import { basename } from '../utils/path';
import { basename, resolve, dirname, relative } from '../utils/path';
import { writeFile } from '../utils/fs';
import { mapSequence } from '../utils/promise';
import error from '../utils/error';
Expand Down Expand Up @@ -169,6 +169,10 @@ export default function rollup(
.then(addons => {
chunk.generateInternalExports(outputOptions);
chunk.preRender(outputOptions);
chunk.id =
typeof process !== 'undefined'
? relative(process.cwd(), inputOptions.input)
: inputOptions.input;
return chunk.render(outputOptions, addons);
})
.then(rendered => {
Expand Down Expand Up @@ -312,6 +316,12 @@ export default function rollup(
});
}

if (outputOptions.sourcemapFile)
error({
code: 'INVALID_OPTION',
message: '"sourcemapFile" is only supported for single-file builds.'
});

timeStart('GENERATE', 1);

const generated: { [chunkName: string]: OutputChunk } = {};
Expand Down

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.

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.

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.

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.

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.

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.

30 changes: 30 additions & 0 deletions test/misc/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const assert = require('assert');
const path = require('path');
const rollup = require('../../dist/rollup');
const { executeBundle, loader } = require('../utils.js');
const { SourceMapConsumer } = require( 'source-map' );
const { getLocator } = require( 'locate-character' );

describe('sanity checks', () => {
it('exists', () => {
Expand Down Expand Up @@ -158,6 +161,33 @@ describe('sanity checks', () => {
});
});

describe('in-memory sourcemaps', () => {
it( 'generates an in-memory sourcemap', () => {
return rollup.rollup({
input: 'main',
plugins: [loader({ main: `console.log( 42 );` })],
})
.then(bundle => {
return bundle.generate({
format: 'cjs',
sourcemap: true,
sourcemapFile: path.resolve( 'bundle.js' )
});
})
.then(generated => {
const smc = new SourceMapConsumer( generated.map );
const locator = getLocator( generated.code, { offsetLine: 1 });

let generatedLoc = locator( '42' );
let loc = smc.originalPositionFor( generatedLoc ); // 42
assert.equal( loc.source, 'main' );
assert.equal( loc.line, 1 );
assert.equal( loc.column, 13 );
});
});

});

describe('deprecations', () => {
it('warns on options.entry, but handles', () => {
const warnings = [];
Expand Down