diff --git a/CHANGELOG.md b/CHANGELOG.md index dacf108..f0a031a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # rollup-plugin-commonjs changelog +## 8.3.0 + +* Handle multiple entry points ([#283](https://github.com/rollup/rollup-plugin-commonjs/issues/283)) +* Extract named exports from exported object literals ([#272](https://github.com/rollup/rollup-plugin-commonjs/issues/272)) +* Fix when `options.external` is modified by other plugins ([#264](https://github.com/rollup/rollup-plugin-commonjs/issues/264)) +* Recognize static template strings in require statements ([#271](https://github.com/rollup/rollup-plugin-commonjs/issues/271)) + ## 8.2.4 * Don't import default from ES modules that don't export default ([#206](https://github.com/rollup/rollup-plugin-commonjs/issues/206)) diff --git a/package-lock.json b/package-lock.json index 8c520ba..99ed478 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-commonjs", - "version": "8.2.6", + "version": "8.4.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -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": { @@ -1595,6 +1595,15 @@ "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", "dev": true }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "dev": true, + "requires": { + "safe-buffer": "5.1.1" + } + }, "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", @@ -1605,15 +1614,6 @@ "strip-ansi": "4.0.0" } }, - "string_decoder": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", - "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", - "dev": true, - "requires": { - "safe-buffer": "5.1.1" - } - }, "strip-ansi": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz", diff --git a/package.json b/package.json index 2ba2ea2..2723664 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-commonjs", - "version": "8.2.6", + "version": "8.4.1", "description": "Convert CommonJS modules to ES2015", "main": "dist/rollup-plugin-commonjs.cjs.js", "module": "dist/rollup-plugin-commonjs.es.js", @@ -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/rollup.config.js b/rollup.config.js index 95c6aef..c9bd226 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -9,15 +9,16 @@ export default { }) ], external: Object.keys( pkg.dependencies ).concat([ 'fs', 'path' ]), - sourcemap: true, output: [ { format: 'es', - file: pkg.module + file: pkg.module, + sourcemap: true }, { format: 'cjs', - file: pkg.main + file: pkg.main, + sourcemap: true } ] }; diff --git a/src/index.js b/src/index.js index 5c265f8..f9c4f60 100644 --- a/src/index.js +++ b/src/index.js @@ -67,8 +67,7 @@ export default function commonjs ( options = {} ) { Array.isArray( options.ignore ) ? id => ~options.ignore.indexOf( id ) : () => false; - let entryModuleIdPromise = null; - let entryModuleId = null; + let entryModuleIdsPromise = null; function resolveId ( importee, importer ) { if ( importee === HELPERS_ID ) return importee; @@ -124,18 +123,19 @@ export default function commonjs ( options = {} ) { }) .filter( Boolean ); - const isExternal = options.external ? - Array.isArray( options.external ) ? id => ~options.external.indexOf( id ) : - options.external : - () => false; + const isExternal = id => options.external ? + Array.isArray( options.external ) ? ~options.external.indexOf( id ) : + options.external(id) : + false; resolvers.unshift( id => isExternal( id ) ? false : null ); 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 )) + ); }, resolveId, @@ -168,7 +168,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( (entryModuleIds) => { const {isEsModule, hasDefaultExport, ast} = checkEsModule( code, id ); if ( isEsModule ) { if ( !hasDefaultExport ) @@ -182,7 +182,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/src/transform.js b/src/transform.js index eb12e45..817a163 100644 --- a/src/transform.js +++ b/src/transform.js @@ -91,14 +91,14 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi if ( !node ) return; if ( node.type !== 'CallExpression' ) return; if ( node.callee.name !== 'require' || scope.contains( 'require' ) ) return; - if ( node.arguments.length !== 1 || node.arguments[0].type !== 'Literal' ) return; // TODO handle these weird cases? + if ( node.arguments.length !== 1 || (node.arguments[0].type !== 'Literal' && (node.arguments[0].type !== 'TemplateLiteral' || node.arguments[0].expressions.length > 0) ) ) return; // TODO handle these weird cases? if ( ignoreRequire( node.arguments[0].value ) ) return; return true; } function getRequired ( node, name ) { - const source = node.arguments[0].value; + const source = node.arguments[0].type === 'Literal' ? node.arguments[0].value : node.arguments[0].quasis[0].value.cooked; const existing = required[ source ]; if ( existing === undefined ) { @@ -359,10 +359,6 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi wrapperStart = `var ${moduleName} = ${HELPERS_NAME}.createCommonjsModule(function (${args}) {\n`; wrapperEnd = `\n});`; - - Object.keys( namedExports ) - .filter( key => !blacklist[ key ] ) - .forEach( addExport ); } else { const names = []; @@ -396,6 +392,7 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi str: declaration, name }); + delete namedExports[name]; } defaultExportPropertyAssignments.push( `${moduleName}.${name} = ${deconflicted};` ); @@ -409,6 +406,9 @@ export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequi }\n};`; } } + Object.keys( namedExports ) + .filter( key => !blacklist[ key ] ) + .forEach( addExport ); const defaultExport = /__esModule/.test( code ) ? `export default ${HELPERS_NAME}.unwrapExports(${moduleName});` : diff --git a/test/form/constant-template-literal/input.js b/test/form/constant-template-literal/input.js new file mode 100644 index 0000000..2a43283 --- /dev/null +++ b/test/form/constant-template-literal/input.js @@ -0,0 +1,2 @@ +var foo = require(`tape`); +console.log(foo); diff --git a/test/form/constant-template-literal/output.js b/test/form/constant-template-literal/output.js new file mode 100644 index 0000000..781cf36 --- /dev/null +++ b/test/form/constant-template-literal/output.js @@ -0,0 +1,11 @@ +import 'tape'; +import foo from 'commonjs-proxy:tape'; + +console.log(foo); + +var input = { + +}; + +export default input; +export { input as __moduleExports }; diff --git a/test/form/dynamic-template-literal/input.js b/test/form/dynamic-template-literal/input.js new file mode 100644 index 0000000..12767b6 --- /dev/null +++ b/test/form/dynamic-template-literal/input.js @@ -0,0 +1,3 @@ +var pe = 'pe'; +var foo = require(`ta${pe}`); +console.log(foo); diff --git a/test/form/dynamic-template-literal/output.js b/test/form/dynamic-template-literal/output.js new file mode 100644 index 0000000..514eb55 --- /dev/null +++ b/test/form/dynamic-template-literal/output.js @@ -0,0 +1,12 @@ +import * as commonjsHelpers from 'commonjsHelpers'; + +var pe = 'pe'; +var foo = commonjsHelpers.commonjsRequire(`ta${pe}`); +console.log(foo); + +var input = { + +}; + +export default input; +export { input as __moduleExports }; 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 37655c2..f7f8723 100644 --- a/test/test.js +++ b/test/test.js @@ -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', @@ -252,6 +271,20 @@ describe( 'rollup-plugin-commonjs', () => { assert.equal( (await executeBundle( bundle )).exports, 42 ); }); + it( 'identifies named exports from object literals', async () => { + const bundle = await rollup({ + input: 'samples/named-exports-from-object-literal/main.js', + plugins: [ commonjs() ] + }); + + const { code } = await bundle.generate({ + format: 'cjs' + }); + + const fn = new Function ( 'module', 'assert', code ); + fn( {}, assert ); + }); + it( 'can ignore references to `global`', async () => { const bundle = await rollup({ input: 'samples/ignore-global/main.js', @@ -450,4 +483,4 @@ describe( 'rollup-plugin-commonjs', () => { assert.equal( warns.length, 0 ); }); }); -}); \ No newline at end of file +});