From da584d91c5a533e739038536ed19fd5bd3cf7ba1 Mon Sep 17 00:00:00 2001 From: Dral Date: Mon, 22 Feb 2016 01:45:20 -0300 Subject: [PATCH] Keep the order of execution for require calls --- src/index.js | 9 +++++++-- test/samples/ordering/bar.js | 3 +++ test/samples/ordering/foo.js | 4 ++++ test/samples/ordering/main.js | 5 +++++ test/samples/ordering/shared.js | 3 +++ test/test.js | 7 +++++++ 6 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/samples/ordering/bar.js create mode 100644 test/samples/ordering/foo.js create mode 100644 test/samples/ordering/main.js create mode 100644 test/samples/ordering/shared.js diff --git a/src/index.js b/src/index.js index 51a9f19..180af29 100644 --- a/src/index.js +++ b/src/index.js @@ -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` ); diff --git a/test/samples/ordering/bar.js b/test/samples/ordering/bar.js new file mode 100644 index 0000000..b1ab9e8 --- /dev/null +++ b/test/samples/ordering/bar.js @@ -0,0 +1,3 @@ +var shared = require('./shared'); + +module.exports = shared.fooLoaded diff --git a/test/samples/ordering/foo.js b/test/samples/ordering/foo.js new file mode 100644 index 0000000..8d276ad --- /dev/null +++ b/test/samples/ordering/foo.js @@ -0,0 +1,4 @@ +var shared = require('./shared'); + +// Mutate the shared module +shared.fooLoaded = true; diff --git a/test/samples/ordering/main.js b/test/samples/ordering/main.js new file mode 100644 index 0000000..020d915 --- /dev/null +++ b/test/samples/ordering/main.js @@ -0,0 +1,5 @@ +require('./foo'); + +var fooLoaded = require('./bar'); + +assert.ok( fooLoaded ); diff --git a/test/samples/ordering/shared.js b/test/samples/ordering/shared.js new file mode 100644 index 0000000..51ed67b --- /dev/null +++ b/test/samples/ordering/shared.js @@ -0,0 +1,3 @@ +module.exports = { + fooLoaded: false +}; diff --git a/test/test.js b/test/test.js index ef12760..23c3007 100644 --- a/test/test.js +++ b/test/test.js @@ -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 ); + }); });