Browse Source

saner deconflicting

contingency-plan
Rich Harris 10 years ago
parent
commit
4317485330
  1. 35
      src/Bundle.js
  2. 3
      test/form/internal-conflict-resolution/_config.js
  3. 15
      test/form/internal-conflict-resolution/_expected/amd.js
  4. 13
      test/form/internal-conflict-resolution/_expected/cjs.js
  5. 11
      test/form/internal-conflict-resolution/_expected/es6.js
  6. 15
      test/form/internal-conflict-resolution/_expected/iife.js
  7. 19
      test/form/internal-conflict-resolution/_expected/umd.js
  8. 2
      test/form/internal-conflict-resolution/bar.js
  9. 7
      test/form/internal-conflict-resolution/foo.js
  10. 7
      test/form/internal-conflict-resolution/main.js

35
src/Bundle.js

@ -107,10 +107,13 @@ export default class Bundle {
this.externalModules.forEach( module => {
let name = makeLegalIdentifier( module.id );
while ( has( definers, name ) ) {
name = `_${name}`;
if ( has( definers, name ) ) {
conflicts[ name ] = true;
} else {
definers[ name ] = [];
}
definers[ name ].push( module );
module.name = name;
});
@ -121,9 +124,19 @@ export default class Bundle {
modules.pop(); // the module closest to the entryModule gets away with keeping things as they are
modules.forEach( module => {
module.rename( name, name + '$' + ~~( Math.random() * 100000 ) ); // TODO proper deconfliction mechanism
const replacement = getSafeName( name );
module.rename( name, replacement );
});
});
function getSafeName ( name ) {
while ( has( conflicts, name ) ) {
name = `_${name}`;
}
conflicts[ name ] = true;
return name;
}
}
generate ( options = {} ) {
@ -168,12 +181,20 @@ export default class Bundle {
source.remove( statement.start, statement.declaration.start );
}
// declare variables for expressions
else {
const name = statement.type === 'ExportDefaultDeclaration' ? 'default' : 'TODO';
const canonicalName = statement._module.getCanonicalName( name );
else if ( statement.type === 'ExportDefaultDeclaration' ) {
const module = statement._module;
const canonicalName = module.getCanonicalName( 'default' );
if ( statement.declaration.type === 'Identifier' && canonicalName === module.getCanonicalName( statement.declaration.name ) ) {
return;
}
source.overwrite( statement.start, statement.declaration.start, `var ${canonicalName} = ` );
}
else {
throw new Error( 'Unhandled export' );
}
}
replaceIdentifiers( statement, source, replacements );

3
test/form/internal-conflict-resolution/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'internal name conflicts are resolved sanely'
};

15
test/form/internal-conflict-resolution/_expected/amd.js

@ -0,0 +1,15 @@
define(function () { 'use strict';
var _bar = 42;
function foo () {
return _bar;
}
function bar () {
alert( foo() );
}
bar();
});

13
test/form/internal-conflict-resolution/_expected/cjs.js

@ -0,0 +1,13 @@
'use strict';
var _bar = 42;
function foo () {
return _bar;
}
function bar () {
alert( foo() );
}
bar();

11
test/form/internal-conflict-resolution/_expected/es6.js

@ -0,0 +1,11 @@
var _bar = 42;
function foo () {
return _bar;
}
function bar () {
alert( foo() );
}
bar();

15
test/form/internal-conflict-resolution/_expected/iife.js

@ -0,0 +1,15 @@
(function () { 'use strict';
var _bar = 42;
function foo () {
return _bar;
}
function bar () {
alert( foo() );
}
bar();
})();

19
test/form/internal-conflict-resolution/_expected/umd.js

@ -0,0 +1,19 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(this, function () { 'use strict';
var _bar = 42;
function foo () {
return _bar;
}
function bar () {
alert( foo() );
}
bar();
}));

2
test/form/internal-conflict-resolution/bar.js

@ -0,0 +1,2 @@
var bar = 42;
export default bar;

7
test/form/internal-conflict-resolution/foo.js

@ -0,0 +1,7 @@
import bar from './bar';
function foo () {
return bar;
}
export default foo;

7
test/form/internal-conflict-resolution/main.js

@ -0,0 +1,7 @@
import foo from './foo';
function bar () {
alert( foo() );
}
bar();
Loading…
Cancel
Save