Skip to content

Commit

Permalink
fix in-memory chunk and 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 4aba7fe
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 15 deletions.
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.

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 4aba7fe

Please sign in to comment.