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

Rewrite top-level define as undefined #147

Merged
merged 3 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
});
});
});
});