Skip to content
Rich Harris edited this page Jan 22, 2015 · 2 revisions

Esperanto can create sourcemaps as it converts and bundles your modules, for easier debugging. If you pass sourceMap: true when creating a module/bundle, the returned value will have a map property in addition to the code property.

The value of map is a version 3 sourcemap, with an additional (non-enumerable) toString() method which is the equivalent of calling JSON.stringify(map).

If you pass sourceMap: 'inline', the sourcemap will instead be appended to code as a data URI.

The following examples demonstrate the pattern:

To create a separate .map file

var source = fs.readFileSync( 'source.js', 'utf-8' );

var cjs = esperanto.toCjs( source, {
  sourceMap: true,
  sourceMapSource: 'source.js'
  sourceMapFile: 'dest.js.map'
});

fs.writeFileSync( 'dest.js', cjs.code );
fs.writeFileSync( 'dest.js.map', cjs.map.toString() );

To append an inline sourcemap

var source = fs.readFileSync( 'source.js', 'utf-8' );

var cjs = esperanto.toCjs( source, {
  sourceMap: 'inline',
  sourceMapSource: 'source.js'
  sourceMapFile: 'dest.js.map'
});

fs.writeFileSync( 'dest.js', cjs.code );

To create a bundle with a separate .map file

esperanto.bundle({
  base: 'src',
  entry: 'main.js'
}).then( function ( bundle ) {
  var cjs = bundle.toCjs({
    sourceMap: true,
    sourceMapFile: 'bundle.js.map'
    // note: `sourceMapSource` is unnecessary here
  });

  fs.writeFileSync( 'bundle.js', cjs.code );
  fs.writeFileSync( 'bundle.js.map', cjs.map.toString() );
});

To create a bundle with an inline sourcemap

esperanto.bundle({
  base: 'src',
  entry: 'main.js'
}).then( function ( bundle ) {
  var cjs = bundle.toCjs({
    sourceMap:'inline',
    sourceMapFile: 'bundle.js.map'
  });

  fs.writeFileSync( 'bundle.js', cjs.code );
});