From cd78e4753957f5bc056e7615ed57d8c75bf92d88 Mon Sep 17 00:00:00 2001 From: Josh Thomas Date: Thu, 25 Jan 2018 08:24:31 -0600 Subject: [PATCH 1/3] chore(): update dependency on rollup to 0.55.0 and correct tests based on correct sourcemap output. --- package-lock.json | 6 +++--- package.json | 2 +- test/test.js | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8c520ba..a97e383 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1435,9 +1435,9 @@ } }, "rollup": { - "version": "0.50.0", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.50.0.tgz", - "integrity": "sha512-7RqCBQ9iwsOBPkjYgoIaeUij606mSkDMExP0NT7QDI3bqkHYQHrQ83uoNIXwPcQm/vP2VbsUz3kiyZZ1qPlLTQ==", + "version": "0.55.0", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.55.0.tgz", + "integrity": "sha512-uCwDXz2qHQ0XsPekrLIeIEORSF32Zfk1H057ENgb+sj84m10pWaG2YGQSvF8kvyf0WLcrzk2TzYuC/+iZP4hyA==", "dev": true }, "rollup-plugin-buble": { diff --git a/package.json b/package.json index 2ba2ea2..d4b7fba 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/test.js b/test/test.js index 37655c2..0902a96 100644 --- a/test/test.js +++ b/test/test.js @@ -130,13 +130,14 @@ describe( 'rollup-plugin-commonjs', () => { let generatedLoc = locator( '42' ); let loc = smc.originalPositionFor( generatedLoc ); // 42 - assert.equal( loc.source, 'samples/sourcemap/foo.js' ); + console.log(JSON.stringify(generated, null, 2)); + 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 ); }); From d8879a82237f8a4ee68bfaffc6a6f830f6c59d3d Mon Sep 17 00:00:00 2001 From: Josh Thomas Date: Thu, 25 Jan 2018 08:42:38 -0600 Subject: [PATCH 2/3] fix(): allow for rollup to use multiple entry points with experimentalCodeSplitting. --- src/index.js | 19 ++++++++++++------- test/samples/multiple-entry-points/2.js | 4 ++++ test/samples/multiple-entry-points/3.js | 5 +++++ test/samples/multiple-entry-points/4.js | 3 +++ test/samples/multiple-entry-points/b.js | 5 +++++ test/samples/multiple-entry-points/c.js | 7 +++++++ test/test.js | 20 +++++++++++++++++++- 7 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 test/samples/multiple-entry-points/2.js create mode 100644 test/samples/multiple-entry-points/3.js create mode 100644 test/samples/multiple-entry-points/4.js create mode 100644 test/samples/multiple-entry-points/b.js create mode 100644 test/samples/multiple-entry-points/c.js diff --git a/src/index.js b/src/index.js index 5c265f8..a847a2c 100644 --- a/src/index.js +++ b/src/index.js @@ -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; @@ -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 ); + }) + ) + ); }, resolveId, @@ -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 ) @@ -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 ); diff --git a/test/samples/multiple-entry-points/2.js b/test/samples/multiple-entry-points/2.js new file mode 100644 index 0000000..0875d7d --- /dev/null +++ b/test/samples/multiple-entry-points/2.js @@ -0,0 +1,4 @@ +function second () { + console.log('second'); +} +exports.second = second; \ No newline at end of file diff --git a/test/samples/multiple-entry-points/3.js b/test/samples/multiple-entry-points/3.js new file mode 100644 index 0000000..340f55e --- /dev/null +++ b/test/samples/multiple-entry-points/3.js @@ -0,0 +1,5 @@ +function third () { + console.log('third'); +} + +exports.third = third; diff --git a/test/samples/multiple-entry-points/4.js b/test/samples/multiple-entry-points/4.js new file mode 100644 index 0000000..04404fe --- /dev/null +++ b/test/samples/multiple-entry-points/4.js @@ -0,0 +1,3 @@ +export function fourth () { + console.log('fourth'); +} diff --git a/test/samples/multiple-entry-points/b.js b/test/samples/multiple-entry-points/b.js new file mode 100644 index 0000000..d348811 --- /dev/null +++ b/test/samples/multiple-entry-points/b.js @@ -0,0 +1,5 @@ +import { second } from './2'; +import { third } from './3'; + +second(); +third(); diff --git a/test/samples/multiple-entry-points/c.js b/test/samples/multiple-entry-points/c.js new file mode 100644 index 0000000..3025ef7 --- /dev/null +++ b/test/samples/multiple-entry-points/c.js @@ -0,0 +1,7 @@ +import { second } from './2'; +import { third } from './3'; +import { fourth } from './4'; + +second(); +third(); +fourth(); diff --git a/test/test.js b/test/test.js index 0902a96..0f59ce0 100644 --- a/test/test.js +++ b/test/test.js @@ -130,7 +130,6 @@ describe( 'rollup-plugin-commonjs', () => { let generatedLoc = locator( '42' ); let loc = smc.originalPositionFor( generatedLoc ); // 42 - console.log(JSON.stringify(generated, null, 2)); assert.equal( loc.source, 'foo.js' ); assert.equal( loc.line, 1 ); assert.equal( loc.column, 15 ); @@ -142,6 +141,25 @@ describe( 'rollup-plugin-commonjs', () => { 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', From 370bf07c26ef670883f253a7246e597a92273137 Mon Sep 17 00:00:00 2001 From: Josh Thomas Date: Fri, 26 Jan 2018 11:01:43 -0600 Subject: [PATCH 3/3] chore(): ensure the order of entryModuleIds does not get changed. --- src/index.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index a847a2c..9faf80b 100644 --- a/src/index.js +++ b/src/index.js @@ -68,7 +68,6 @@ export default function commonjs ( options = {} ) { () => false; let entryModuleIdsPromise = null; - const entryModuleIds = []; function resolveId ( importee, importer ) { if ( importee === HELPERS_ID ) return importee; @@ -135,11 +134,7 @@ export default function commonjs ( options = {} ) { const entryModules = [].concat( options.input || options.entry ); entryModuleIdsPromise = Promise.all( - entryModules.map( entry => - resolveId( entry ).then( resolved => { - entryModuleIds.push( resolved ); - }) - ) + entryModules.map( entry => resolveId( entry )) ); }, @@ -173,7 +168,7 @@ export default function commonjs ( options = {} ) { if ( !filter( id ) ) return null; if ( extensions.indexOf( extname( id ) ) === -1 ) return null; - return entryModuleIdsPromise.then( () => { + return entryModuleIdsPromise.then( (entryModuleIds) => { const {isEsModule, hasDefaultExport, ast} = checkEsModule( code, id ); if ( isEsModule ) { if ( !hasDefaultExport )