Browse Source

more fixes

declarations-and-references
Rich-Harris 9 years ago
parent
commit
0334cc86fe
  1. 23
      src/Bundle.js
  2. 8
      src/Module.js

23
src/Bundle.js

@ -68,19 +68,32 @@ export default class Bundle {
this.markAllModifierStatements(); this.markAllModifierStatements();
this.orderedModules = this.sort(); this.orderedModules = this.sort();
this.deconflict();
}); });
} }
// TODO would be better to deconflict once, rather than per-render
deconflict () { deconflict () {
let nameCount = blank(); let used = blank();
// ensure no conflicts with globals // ensure no conflicts with globals
keys( this.assumedGlobals ).forEach( name => nameCount[ name ] = 0 ); keys( this.assumedGlobals ).forEach( name => used[ name ] = 1 );
let allReplacements = blank(); this.externalModules.forEach( module => {
// TODO treat external module names/imports as globals
});
return allReplacements; this.modules.forEach( module => {
keys( module.declarations ).forEach( originalName => {
const declaration = module.declarations[ originalName ];
const name = declaration.name;
if ( used[ name ] ) {
declaration.name = `${name}$${used[name]++}`;
} else {
used[ name ] = 1;
}
});
});
} }
fetchModule ( id ) { fetchModule ( id ) {

8
src/Module.js

@ -20,6 +20,8 @@ class SyntheticDefaultDeclaration {
addReference ( reference ) { addReference ( reference ) {
reference.declaration = this; reference.declaration = this;
this.name = reference.name; this.name = reference.name;
console.log( 'this.name', this.name )
} }
} }
@ -511,11 +513,13 @@ export default class Module {
} }
else if ( statement.node.type === 'ExportDefaultDeclaration' ) { else if ( statement.node.type === 'ExportDefaultDeclaration' ) {
const defaultName = this.declarations.default.name;
// anonymous functions should be converted into declarations // anonymous functions should be converted into declarations
if ( statement.node.declaration.type === 'FunctionExpression' ) { if ( statement.node.declaration.type === 'FunctionExpression' ) {
magicString.overwrite( statement.node.start, statement.node.declaration.start + 8, `function ${this.defaultName()}` ); magicString.overwrite( statement.node.start, statement.node.declaration.start + 8, `function ${defaultName}` );
} else { } else {
magicString.overwrite( statement.node.start, statement.node.declaration.start, `var ${this.defaultName()} = ` ); magicString.overwrite( statement.node.start, statement.node.declaration.start, `var ${defaultName} = ` );
} }
} }

Loading…
Cancel
Save