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

Commit

Permalink
Merge branch 'cjs-transform-ordering-fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 29, 2018
2 parents 796c53c + 71fb4a7 commit f2d792c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# rollup-plugin-commonjs changelog

## 9.1.1
*2018-04-30*
* Fix ordering of modules when using rollup 0.58 ([#302](https://github.com/rollup/rollup-plugin-commonjs/issues/302))

## 9.1.0

* Do not automatically wrap modules with return statements in top level arrow functions ([#302](https://github.com/rollup/rollup-plugin-commonjs/issues/302))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"locate-character": "^2.0.5",
"mocha": "^5.0.1",
"require-relative": "^0.8.7",
"rollup": "^0.56.4",
"rollup": "^0.57.0",
"rollup-plugin-buble": "^0.19.2",
"rollup-plugin-node-resolve": "^3.0.3",
"shx": "^0.2.2",
Expand Down
47 changes: 38 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,32 @@ export default function commonjs ( options = {} ) {

const sourceMap = options.sourceMap !== false;

const commonjsModules = new Map();
let resolveUsingOtherResolvers;

const isCjsPromises = Object.create(null);
function getIsCjsPromise ( id ) {
let isCjsPromise = isCjsPromises[id];
if (isCjsPromise)
return isCjsPromise.promise;

let promise = new Promise( resolve => {
isCjsPromises[id] = isCjsPromise = {
resolve: resolve,
promise: undefined
};
});
isCjsPromise.promise = promise;

return promise;
}
function setIsCjsPromise ( id, promise ) {
let isCjsPromise = isCjsPromises[id];
if (isCjsPromise)
isCjsPromise.resolve(promise);
else
isCjsPromises[id] = { promise: promise, resolve: undefined };
}

return {
name: 'commonjs',

Expand Down Expand Up @@ -155,20 +178,23 @@ export default function commonjs ( options = {} ) {
const actualId = id.slice( PREFIX.length );
const name = getName( actualId );

if (commonjsModules.has( actualId ))
return `import { __moduleExports } from ${JSON.stringify( actualId )}; export default __moduleExports;`;
else if (esModulesWithoutDefaultExport.indexOf(actualId) !== -1)
return `import * as ${name} from ${JSON.stringify( actualId )}; export default ${name};`;
else
return `import * as ${name} from ${JSON.stringify( actualId )}; export default ( ${name} && ${name}['default'] ) || ${name};`;
return getIsCjsPromise( actualId )
.then( isCjs => {
if ( isCjs )
return `import { __moduleExports } from ${JSON.stringify( actualId )}; export default __moduleExports;`;
else if (esModulesWithoutDefaultExport.indexOf(actualId) !== -1)
return `import * as ${name} from ${JSON.stringify( actualId )}; export default ${name};`;
else
return `import * as ${name} from ${JSON.stringify( actualId )}; export default ( ${name} && ${name}['default'] ) || ${name};`;
});
}
},

transform ( code, id ) {
if ( !filter( id ) ) return null;
if ( extensions.indexOf( extname( id ) ) === -1 ) return null;

return entryModuleIdsPromise.then( (entryModuleIds) => {
const transformPromise = entryModuleIdsPromise.then( (entryModuleIds) => {
const {isEsModule, hasDefaultExport, ast} = checkEsModule( this.parse, code, id );
if ( isEsModule ) {
if ( !hasDefaultExport )
Expand All @@ -188,11 +214,14 @@ export default function commonjs ( options = {} ) {
return;
}

commonjsModules.set( id, true );
return transformed;
}).catch(err => {
this.error(err, err.loc);
});

setIsCjsPromise(id, transformPromise.then( transformed => transformed ? true : false, err => true ));

return transformPromise;
}
};
}

0 comments on commit f2d792c

Please sign in to comment.