Skip to content

Commit

Permalink
fix in-memory and chunk sourcemap naming
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Apr 26, 2018
1 parent e5df762 commit c98f72b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,9 @@ 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);
const file = options.file
? options.sourcemapFile || options.file
: resolve(options.dir, this.id);

if (
this.graph.hasLoaders ||
Expand Down
11 changes: 10 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 } from '../utils/path';
import { writeFile } from '../utils/fs';
import { mapSequence } from '../utils/promise';
import error from '../utils/error';
Expand Down Expand Up @@ -153,6 +153,8 @@ export default function rollup(
function generate(rawOutputOptions: GenericConfigObject) {
const outputOptions = normalizeOutputOptions(inputOptions, rawOutputOptions);

outputOptions.dir = dirname(resolve(inputOptions.input));

if (outputOptions.entryNames || outputOptions.chunkNames)
error({
code: 'INVALID_OPTION',
Expand All @@ -169,6 +171,7 @@ export default function rollup(
.then(addons => {
chunk.generateInternalExports(outputOptions);
chunk.preRender(outputOptions);
chunk.id = basename(inputOptions.input);
return chunk.render(outputOptions, addons);
})
.then(rendered => {
Expand Down Expand Up @@ -312,6 +315,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
28 changes: 28 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,31 @@ describe('sanity checks', () => {
});
});

describe('in-memory sourcemaps', () => {
it( 'generates an in-memory sourcemap', async () => {
const bundle = await rollup.rollup({
input: 'main',
plugins: [loader({ main: `console.log( 42 );` })],
});

const generated = await bundle.generate({
format: 'cjs',
sourcemap: true,
sourcemapFile: path.resolve( 'bundle.js' )
});

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

0 comments on commit c98f72b

Please sign in to comment.