Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
dont overwrite globals (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 22, 2016
1 parent feb9160 commit 3acc5d6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/transform.js
Expand Up @@ -230,14 +230,11 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus
const name = getName( id );

function addExport ( x ) {
let declaration;
const deconflicted = deconflict( scope, globals, name );

if ( x === name ) {
const deconflicted = deconflict( scope, globals, name );
declaration = `var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;
} else {
declaration = `export var ${x} = ${moduleName}.${x};`;
}
const declaration = deconflicted === name ?
`export var ${x} = ${moduleName}.${x};` :
`var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;

namedExportDeclarations.push( declaration );
}
Expand Down
7 changes: 7 additions & 0 deletions test/function/global-not-overwritten/_config.js
@@ -0,0 +1,7 @@
const assert = require( 'assert' );

module.exports = {
exports: function ( exports ) {
assert.equal( exports.encoded, encodeURIComponent( 'test string' ) );
}
};
5 changes: 5 additions & 0 deletions test/function/global-not-overwritten/encode.js
@@ -0,0 +1,5 @@
exports.encodeURIComponent = function () {
return encodeURIComponent( this.str );
};

console.log( exports ); // to ensure module is wrapped
8 changes: 8 additions & 0 deletions test/function/global-not-overwritten/main.js
@@ -0,0 +1,8 @@
import { encodeURIComponent } from './encode.js';

var foo = {
str: 'test string',
encodeURIComponent
};

export var encoded = foo.encodeURIComponent();

0 comments on commit 3acc5d6

Please sign in to comment.