use MagicString.Bundle for better sourcemap support

pull/31/head
Rich-Harris 8 years ago
parent 04388f7a0e
commit 8704d38503

@ -1,4 +1,4 @@
import MagicString from 'magic-string';
import MagicString, { Bundle } from 'magic-string';
import { walk } from 'estree-walker';
import deindent from './utils/deindent.js';
import isReference from './utils/isReference.js';
@ -365,8 +365,6 @@ export default function generate ( parsed, source, options ) {
return state[ key ];
};
let setting = false;
this.set = function set ( newState ) {
${setStatements.join( '\n\n' )}
};
@ -426,38 +424,26 @@ export default function generate ( parsed, source, options ) {
const parts = result.split( '✂]' );
const finalChunk = parts.pop();
const sortedByResult = parts.map( ( str, index ) => {
const compiled = new Bundle({ separator: '' });
parts.forEach( str => {
const match = pattern.exec( str );
return {
index,
chunk: str.replace( pattern, '' ),
start: +match[1],
end: +match[2]
};
compiled.addSource({
filename: options.filename,
content: new MagicString( str.replace( pattern, '' ) )
});
const sortedBySource = sortedByResult
.slice()
.sort( ( a, b ) => a.start - b.start );
let c = 0;
sortedBySource.forEach( part => {
generator.code.remove( c, part.start );
generator.code.insertRight( part.start, part.chunk );
c = part.end;
compiled.addSource({
filename: options.filename,
content: generator.code.snip( +match[1], +match[2] )
});
generator.code.remove( c, source.length );
generator.code.append( finalChunk );
sortedByResult.forEach( part => {
generator.code.move( part.start, part.end, 0 );
});
compiled.append( finalChunk );
return {
code: generator.code.toString(),
map: generator.code.generateMap()
code: compiled.toString(),
map: compiled.generateMap()
};
}

@ -11,7 +11,7 @@ export default {
generator.addSourcemapLocations( node.expression );
const { dependencies, snippet, string } = generator.contextualise( node.expression );
const { dependencies, snippet } = generator.contextualise( node.expression );
generator.current.initStatements.push( deindent`
var ${name}_anchor = document.createComment( ${JSON.stringify( `#each ${generator.source.slice( node.expression.start, node.expression.end )}` )} );
@ -29,7 +29,7 @@ export default {
` );
generator.current.updateStatements.push( deindent`
var ${name}_value = ${string};
var ${name}_value = ${snippet};
for ( var i = 0; i < ${name}_value.length; i += 1 ) {
if ( !${name}_iterations[i] ) {

@ -11,7 +11,7 @@ export default {
const elseRenderer = `renderElseBlock_${i}`;
generator.addSourcemapLocations( node.expression );
const { snippet, string } = generator.contextualise( node.expression );
const { snippet } = generator.contextualise( node.expression );
generator.current.initStatements.push( deindent`
var ${name}_anchor = document.createComment( ${JSON.stringify( `#if ${generator.source.slice( node.expression.start, node.expression.end )}` )} );
@ -70,7 +70,7 @@ export default {
}
let update = deindent`
if ( ${string} ) {
if ( ${snippet} ) {
${ifTrue.join( '\n\n' )}
}

@ -1,11 +1,10 @@
import deindent from '../utils/deindent.js';
import isReference from '../utils/isReference.js';
export default {
enter ( generator, node ) {
const name = generator.current.counter( 'text' );
const { snippet, string } = generator.contextualise( node.expression );
const { snippet } = generator.contextualise( node.expression );
generator.current.initStatements.push( deindent`
var ${name} = document.createTextNode( ${snippet} );
@ -15,7 +14,7 @@ export default {
generator.addSourcemapLocations( node.expression );
generator.current.updateStatements.push( deindent`
${name}.data = ${string};
${name}.data = ${snippet};
` );
}
};

@ -61,11 +61,11 @@ export default function addElementAttributes ( generator, node, local ) {
dynamic = true;
// dynamic but potentially non-string attributes
const { snippet, string } = generator.contextualise( value.expression );
const { snippet } = generator.contextualise( value.expression );
const updater = propertyName ?
`${local.name}.${propertyName} = ${snippet};` :
`${local.name}.setAttribute( '${attribute.name}', ${string} );`; // TODO use snippet both times see note below
`${local.name}.setAttribute( '${attribute.name}', ${snippet} );`; // TODO use snippet both times see note below
local.init.push( updater );
local.update.push( updater );

@ -1,6 +1,4 @@
export default {
solo: true,
data: {
columns: [ 'a', 'b', 'c' ],
rows: [ 1, 2, 3 ]

Loading…
Cancel
Save