Skip to content
Permalink
Browse files

Keep the order of execution for require calls

  • Loading branch information...
dralletje committed Feb 22, 2016
1 parent 3b22781 commit 37a40323ccd20b03acdb27a9a2d8f0ebfa21e34a
@@ -94,6 +94,10 @@ export default function commonjs ( options = {} ) {
const magicString = new MagicString( code );

let required = {};
// Because objects have no guaranteed ordering, yet we need it,
// we need to keep track of the order in a array
let sources = [];

let uid = 0;

let scope = attachScopes( ast, 'scope' );
@@ -159,6 +163,9 @@ export default function commonjs ( options = {} ) {
const source = node.arguments[0].value;

let existing = required[ source ];
if ( existing === undefined ) {
sources.unshift(source);
}
let name;

if ( !existing ) {
@@ -183,8 +190,6 @@ export default function commonjs ( options = {} ) {
}
});

const sources = Object.keys( required );

if ( !sources.length && !uses.module && !uses.exports && !uses.global ) {
if ( Object.keys( namedExports ).length ) {
throw new Error( `Custom named exports were specified for ${id} but it does not appear to be a CommonJS module` );
@@ -0,0 +1,3 @@
let shared = require('./shared');

module.exports = shared.fooLoaded
@@ -0,0 +1,4 @@
let shared = require('./shared');

// Mutate the shared module
shared.fooLoaded = true;
@@ -0,0 +1,5 @@
require('./foo');

var fooLoaded = require('./bar');

assert.ok( fooLoaded );
@@ -0,0 +1,3 @@
module.exports = {
fooLoaded: false
};
@@ -222,4 +222,11 @@ describe( 'rollup-plugin-commonjs', () => {
plugins: [ commonjs() ]
}).then( executeBundle );
});

it( 'obeys order of require expressions', () => {
return rollup({
entry: 'samples/ordering/main.js',
plugins: [ commonjs() ]
}).then( executeBundle );
});
});

0 comments on commit 37a4032

Please sign in to comment.
You can’t perform that action at this time.