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

Commit

Permalink
Merge pull request #147 from rollup/gh-144
Browse files Browse the repository at this point in the history
Rewrite top-level define as `undefined`
  • Loading branch information
Rich-Harris committed Dec 14, 2016
2 parents 5e140bc + b726a59 commit 3f39ecf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 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
9 changes: 9 additions & 0 deletions test/samples/define-is-undefined/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function (global, factory) {
typeof define === 'function' && define.amd ? define(factory) :
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
(global.foo = factory());
}(this, (function () { 'use strict';

return 42;

})));
3 changes: 3 additions & 0 deletions test/samples/define-is-undefined/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from './foo.js';

export default 42;
21 changes: 19 additions & 2 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 @@ -327,7 +327,7 @@ describe( 'rollup-plugin-commonjs', () => {
})
.then( executeBundle )
.catch( error => {
assert.equal( error.message, `'named' is not exported by samples/reexport/reexport.js (imported by samples/reexport/main.js). For help fixing this error see https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module` );
assert.equal( error.message, `'named' is not exported by samples${path.sep}reexport${path.sep}reexport.js (imported by samples${path.sep}reexport${path.sep}main.js). For help fixing this error see https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module` );
});
});

Expand All @@ -345,5 +345,22 @@ describe( 'rollup-plugin-commonjs', () => {
]
}).then( executeBundle );
});

it( 'rewrites top-level defines', () => {
return rollup({
entry: 'samples/define-is-undefined/main.js',
plugins: [ commonjs() ]
})
.then( bundle => {
function define () {
throw new Error( 'nope' );
}

define.amd = true;

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

0 comments on commit 3f39ecf

Please sign in to comment.