mirror of https://github.com/lukechilds/rollup.git
12 changed files with 123 additions and 2 deletions
@ -0,0 +1,39 @@ |
|||||
|
import MagicString from 'magic-string'; |
||||
|
|
||||
|
export default function transformBundle ( source, transformers ) { |
||||
|
if ( typeof source === 'string' ) { |
||||
|
source = { |
||||
|
code: source, |
||||
|
map: null |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
return transformers.reduce( ( previous, transformer ) => { |
||||
|
let result = transformer( previous.code, previous.map ); |
||||
|
|
||||
|
if ( result == null ) return previous; |
||||
|
|
||||
|
if ( typeof result === 'string' ) { |
||||
|
result = { |
||||
|
code: result, |
||||
|
map: null |
||||
|
}; |
||||
|
} |
||||
|
// `result.map` can only be a string if `result` isn't
|
||||
|
else if ( typeof result.map === 'string' ) { |
||||
|
result.map = JSON.parse( result.map ); |
||||
|
} |
||||
|
|
||||
|
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 ); |
||||
|
} |
@ -0,0 +1,17 @@ |
|||||
|
module.exports = { |
||||
|
description: 'allows plugins to transform bundle', |
||||
|
options: { |
||||
|
plugins: [ |
||||
|
{ |
||||
|
transformBundle: function (code) { |
||||
|
return '/* first plugin */'; |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
transformBundle: function (code) { |
||||
|
return code + '\n/* second plugin */'; |
||||
|
} |
||||
|
} |
||||
|
] |
||||
|
} |
||||
|
} |
@ -0,0 +1,2 @@ |
|||||
|
/* first plugin */ |
||||
|
/* second plugin */ |
@ -0,0 +1,2 @@ |
|||||
|
/* first plugin */ |
||||
|
/* second plugin */ |
@ -0,0 +1,2 @@ |
|||||
|
/* first plugin */ |
||||
|
/* second plugin */ |
@ -0,0 +1,2 @@ |
|||||
|
/* first plugin */ |
||||
|
/* second plugin */ |
@ -0,0 +1,2 @@ |
|||||
|
/* first plugin */ |
||||
|
/* second plugin */ |
@ -0,0 +1 @@ |
|||||
|
console.log( 1 + 1 ); |
@ -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 ( code, map ) { |
||||
|
var options = { fromString: true }; |
||||
|
|
||||
|
if ( map != null ) { |
||||
|
options.inSourceMap = map; |
||||
|
options.outSourceMap = "out"; |
||||
|
} |
||||
|
|
||||
|
var result = uglify.minify( code, options ); |
||||
|
|
||||
|
if ( 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 ); |
||||
|
} |
||||
|
}; |
@ -0,0 +1 @@ |
|||||
|
console.log( 42 ); |
Loading…
Reference in new issue