Browse Source

Add sourcemaps test

gh-438-b
Bogdan Chadkin 9 years ago
parent
commit
10c4933ed6
  1. 3
      package.json
  2. 13
      src/utils/transformBundle.js
  3. 8
      test/form/transform-bundle-plugin/_config.js
  4. 47
      test/sourcemaps/transform-bundle/_config.js
  5. 1
      test/sourcemaps/transform-bundle/main.js

3
package.json

@ -57,7 +57,8 @@
"rollup-plugin-replace": "^1.0.1",
"sander": "^0.4.0",
"source-map": "^0.5.3",
"sourcemap-codec": "^1.2.1"
"sourcemap-codec": "^1.2.1",
"uglify-js": "^2.6.1"
},
"dependencies": {
"chalk": "^1.1.1",

13
src/utils/transformBundle.js

@ -1,3 +1,5 @@
import MagicString from 'magic-string';
export default function transformBundle ( source, transformers ) {
if ( typeof source === 'string' ) {
source = {
@ -22,7 +24,16 @@ export default function transformBundle ( source, transformers ) {
result.map = JSON.parse( result.map );
}
return result;
if (result.map != null) {
let map = new MagicString.Bundle().generateMap({});
map.file = result.map.file;
map.sources = result.map.sources;
map.sourcesContent = result.map.sourcesContent;
map.names = result.map.names;
map.mappings = result.map.mappings;
result.map = map;
}
return result;
}, source );
}

8
test/form/transform-bundle-plugin/_config.js

@ -4,16 +4,12 @@ module.exports = {
plugins: [
{
transformBundle: function (result) {
return {
code: '/* first plugin */'
};
return '/* first plugin */';
}
},
{
transformBundle: function (result) {
return {
code: result.code + '\n/* second plugin */'
};
return result.code + '\n/* second plugin */';
}
}
]

47
test/sourcemaps/transform-bundle/_config.js

@ -0,0 +1,47 @@
var uglify = require( 'uglify-js' );
var MagicString = require( 'magic-string' );
var assert = require( 'assert' );
var getLocation = require( '../../utils/getLocation' );
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;
module.exports = {
description: 'preserves sourcemap chains when transforming',
options: {
plugins: [
{
transformBundle: function ( source ) {
var options = { fromString: true };
if ( source.map != null ) {
options.inSourceMap = source.map;
options.outSourceMap = "out";
}
var result = uglify.minify( source.code, options );
if (source.map != null) {
result.code = result.code.slice( 0, -25 );
}
return result;
}
}
]
},
test: function ( code, map ) {
var smc = new SourceMapConsumer( map );
var generatedLoc = getLocation( code, code.indexOf( '42' ) );
var originalLoc = smc.originalPositionFor( generatedLoc );
assert.ok( /main/.test( originalLoc.source ) );
assert.equal( originalLoc.line, 1 );
assert.equal( originalLoc.column, 13 );
generatedLoc = getLocation( code, code.indexOf( 'log' ) );
originalLoc = smc.originalPositionFor( generatedLoc );
assert.equal( originalLoc.line, 1 );
assert.equal( originalLoc.column, 8 );
}
};

1
test/sourcemaps/transform-bundle/main.js

@ -0,0 +1 @@
console.log( 42 );
Loading…
Cancel
Save