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

Add support for experimentalCodeSplitting from Rollup 0.55.0 #283

Merged
merged 3 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"locate-character": "^2.0.1",
"mocha": "^4.0.1",
"require-relative": "^0.8.7",
"rollup": "^0.50.0",
"rollup": "^0.55.0",
"rollup-plugin-buble": "^0.16.0",
"rollup-plugin-node-resolve": "^3.0.0",
"shx": "^0.2.2",
Expand Down
19 changes: 12 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export default function commonjs ( options = {} ) {
Array.isArray( options.ignore ) ? id => ~options.ignore.indexOf( id ) :
() => false;

let entryModuleIdPromise = null;
let entryModuleId = null;
let entryModuleIdsPromise = null;
const entryModuleIds = [];

function resolveId ( importee, importer ) {
if ( importee === HELPERS_ID ) return importee;
Expand Down Expand Up @@ -133,9 +133,14 @@ export default function commonjs ( options = {} ) {

resolveUsingOtherResolvers = first( resolvers );

entryModuleIdPromise = resolveId( options.input || options.entry ).then( resolved => {
entryModuleId = resolved;
});
const entryModules = [].concat( options.input || options.entry );
entryModuleIdsPromise = Promise.all(
entryModules.map( entry =>
resolveId( entry ).then( resolved => {
entryModuleIds.push( resolved );
})
)
);
Copy link
Member

Choose a reason for hiding this comment

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

Minor totally unimportant nit: This looks a little more confusing as it needs to be (and it can possibly shuffle the order of the entry points which may or may not cause someone to stumble over this again in the future). If you Promise.all the mapped entryModules first and then assign the resulting array to entryModuleIds, the order would be preserved and one would not stop for a second to wonder if entryModuleIds really is a permuted map of entryModules 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally fair, I have updated the code to be more succinct and to use the result of the entryModuleIdsPromise.

},

resolveId,
Expand Down Expand Up @@ -168,7 +173,7 @@ export default function commonjs ( options = {} ) {
if ( !filter( id ) ) return null;
if ( extensions.indexOf( extname( id ) ) === -1 ) return null;

return entryModuleIdPromise.then( () => {
return entryModuleIdsPromise.then( () => {
const {isEsModule, hasDefaultExport, ast} = checkEsModule( code, id );
if ( isEsModule ) {
if ( !hasDefaultExport )
Expand All @@ -182,7 +187,7 @@ export default function commonjs ( options = {} ) {
return;
}

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

commonjsModules.set( id, true );
Expand Down
4 changes: 4 additions & 0 deletions test/samples/multiple-entry-points/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function second () {
console.log('second');
}
exports.second = second;
5 changes: 5 additions & 0 deletions test/samples/multiple-entry-points/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function third () {
console.log('third');
}

exports.third = third;
3 changes: 3 additions & 0 deletions test/samples/multiple-entry-points/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function fourth () {
console.log('fourth');
}
5 changes: 5 additions & 0 deletions test/samples/multiple-entry-points/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { second } from './2';
import { third } from './3';

second();
third();
7 changes: 7 additions & 0 deletions test/samples/multiple-entry-points/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { second } from './2';
import { third } from './3';
import { fourth } from './4';

second();
third();
fourth();
23 changes: 21 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,36 @@ describe( 'rollup-plugin-commonjs', () => {

let generatedLoc = locator( '42' );
let loc = smc.originalPositionFor( generatedLoc ); // 42
assert.equal( loc.source, 'samples/sourcemap/foo.js' );
assert.equal( loc.source, 'foo.js' );
assert.equal( loc.line, 1 );
assert.equal( loc.column, 15 );

generatedLoc = locator( 'log' );
loc = smc.originalPositionFor( generatedLoc ); // log
assert.equal( loc.source, 'samples/sourcemap/main.js' );
assert.equal( loc.source, 'main.js' );
assert.equal( loc.line, 2 );
assert.equal( loc.column, 8 );
});

it( 'supports multiple entry points for experimentalCodeSplitting', async () => {
const bundle = await rollup({
input: [
'samples/multiple-entry-points/b.js',
'samples/multiple-entry-points/c.js'
],
experimentalCodeSplitting: true,
plugins: [ commonjs() ]
});

const generated = await bundle.generate({
format: 'cjs',
});

assert.equal(Object.keys(generated).length, 3);
assert.equal(generated.hasOwnProperty('./b.js'), true);
assert.equal(generated.hasOwnProperty('./c.js'), true);
});

it( 'handles references to `global`', async () => {
const bundle = await rollup({
input: 'samples/global/main.js',
Expand Down