Skip to content
Permalink
Browse files

Merge pull request #43 from dralletje/master

Keep the order of execution for require calls
  • Loading branch information...
TrySound committed Jul 20, 2016
2 parents 4d94701 + bc924ba commit 8c9d77f351cef9291f1c9da6042b7af64cf11ff4
@@ -122,6 +122,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' );
@@ -202,6 +206,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 ) {
@@ -226,8 +233,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 @@
var shared = require('./shared');

module.exports = shared.fooLoaded
@@ -0,0 +1,4 @@
var 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
};
@@ -277,6 +277,13 @@ describe( 'rollup-plugin-commonjs', () => {
}).then( executeBundle );
});

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

it( 'can ignore references to `global`', () => {
return rollup({
entry: 'samples/ignore-global/main.js',
@@ -333,7 +340,7 @@ describe( 'rollup-plugin-commonjs', () => {
assert.notEqual( module.exports, 'nope' );
});
});

it( 'does not process the entry file when it has a leading "." (issue #63)', () => {
return rollup({
entry: './samples/basic/main.js',

0 comments on commit 8c9d77f

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