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

Keep the order of execution for require calls #43

Merged
merged 2 commits into from
Jul 20, 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
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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` );
Expand Down
3 changes: 3 additions & 0 deletions test/samples/ordering/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var shared = require('./shared');

module.exports = shared.fooLoaded
4 changes: 4 additions & 0 deletions test/samples/ordering/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var shared = require('./shared');

// Mutate the shared module
shared.fooLoaded = true;
5 changes: 5 additions & 0 deletions test/samples/ordering/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('./foo');

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

assert.ok( fooLoaded );
3 changes: 3 additions & 0 deletions test/samples/ordering/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
fooLoaded: false
};
9 changes: 8 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down