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

Commit

Permalink
rewrite top-level define as undefined (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 14, 2016
1 parent c4c2827 commit 9549d3f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus
}
}

if ( node.name === 'define' ) {
magicString.overwrite( node.start, node.end, 'undefined', true );
}

globals.add( node.name );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import foo from './foo.js';

assert.equal( foo, 42 );
export default 42;
15 changes: 9 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ describe( 'rollup-plugin-commonjs', () => {

assert.equal( code.indexOf( 'typeof require' ), -1, code );
assert.notEqual( code.indexOf( 'typeof module' ), -1, code );
assert.notEqual( code.indexOf( 'typeof define' ), -1, code );
// assert.notEqual( code.indexOf( 'typeof define' ), -1, code ); // #144 breaks this test
});
});
});
Expand Down Expand Up @@ -352,11 +352,14 @@ describe( 'rollup-plugin-commonjs', () => {
plugins: [ commonjs() ]
})
.then( bundle => {
executeBundle( bundle, {
define () {
throw new Error( 'nope' );
}
});
function define () {
throw new Error( 'nope' );
}

define.amd = true;

const { exports } = executeBundle( bundle, { define });
assert.equal( exports, 42 );
});
});
});
Expand Down

0 comments on commit 9549d3f

Please sign in to comment.