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

Handle missing default export even if first pass matched #285

Merged
merged 1 commit into from
Mar 5, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ export default function commonjs ( options = {} ) {
}

const transformed = transformCommonjs( code, id, entryModuleIds.indexOf(id) !== -1, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire, ast );
if ( !transformed ) return;
if ( !transformed ) {
esModulesWithoutDefaultExport.push( id );
return;
}

commonjsModules.set( id, true );
return transformed;
Expand Down
2 changes: 2 additions & 0 deletions test/function/bare-import-comment/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Great module
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one! Took me some time to figure out what was going on here so I'll just write it down for future reference:

  • The CJS plugin is first doing a quick regexp check to see if a module could be CJS. This check is just looking for keywords such as module
  • If the check matches, the transform function repeats this test using an actual AST
  • Modules that are not CJS specific were treated differently if the first or if the second check was successful. This is fixed by this PR.

Math.bar = 42;
1 change: 1 addition & 0 deletions test/function/bare-import-comment/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require( './bar.js' );
3 changes: 3 additions & 0 deletions test/function/bare-import-comment/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './foo.js';

assert.equal( Math.bar, 42 );
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,13 @@ describe( 'rollup-plugin-commonjs', () => {
onwarn: (warn) => warns.push( warn )
});
assert.equal( warns.length, 0 );

await rollup({
input: 'function/bare-import-comment/main.js',
plugins: [ commonjs() ],
onwarn: (warn) => warns.push( warn )
});
assert.equal( warns.length, 0 );
});
});
});