rollup-plugin-commonjs
Advanced tools
Comparing version 8.2.3 to 8.2.4
# rollup-plugin-commonjs changelog | ||
## 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)) | ||
## 8.2.3 | ||
* Prevent duplicate default exports ([#230](https://github.com/rollup/rollup-plugin-commonjs/pull/230)) | ||
* Only include default export when it exists ([#226](https://github.com/rollup/rollup-plugin-commonjs/pull/226)) | ||
* Deconflict `require` aliases ([#232](https://github.com/rollup/rollup-plugin-commonjs/issues/232)) | ||
## 8.2.1 | ||
@@ -4,0 +14,0 @@ |
@@ -205,15 +205,28 @@ 'use strict'; | ||
function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire ) { | ||
function checkFirstpass (code, ignoreGlobal) { | ||
var firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal; | ||
if ( !firstpass.test( code ) ) { return null; } | ||
return firstpass.test(code); | ||
} | ||
var ast = tryParse( code, id ); | ||
function checkEsModule (code, id) { | ||
var ast = tryParse(code, id); | ||
// if there are top-level import/export declarations, this is ES not CommonJS | ||
var hasDefaultExport = false; | ||
var isEsModule = false; | ||
for ( var i = 0, list = ast.body; i < list.length; i += 1 ) { | ||
var node = list[i]; | ||
if ( importExportDeclaration.test( node.type ) ) { return null; } | ||
if ( node.type === 'ExportDefaultDeclaration' ) | ||
{ hasDefaultExport = true; } | ||
if ( importExportDeclaration.test( node.type ) ) | ||
{ isEsModule = true; } | ||
} | ||
return { isEsModule: isEsModule, hasDefaultExport: hasDefaultExport, ast: ast }; | ||
} | ||
function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire, astCache ) { | ||
var ast = astCache || tryParse( code, id ); | ||
var magicString = new MagicString( code ); | ||
@@ -651,2 +664,4 @@ | ||
var esModulesWithoutDefaultExport = []; | ||
var allowDynamicRequire = !!options.ignore; // TODO maybe this should be configurable? | ||
@@ -747,5 +762,8 @@ | ||
return commonjsModules.has( actualId$1 ) ? | ||
("import { __moduleExports } from " + (JSON.stringify( actualId$1 )) + "; export default __moduleExports;") : | ||
("import * as " + name$1 + " from " + (JSON.stringify( actualId$1 )) + "; export default ( " + name$1 + " && " + name$1 + "['default'] ) || " + name$1 + ";"); | ||
if (commonjsModules.has( actualId$1 )) | ||
{ return ("import { __moduleExports } from " + (JSON.stringify( actualId$1 )) + "; export default __moduleExports;"); } | ||
else if (esModulesWithoutDefaultExport.includes(actualId$1)) | ||
{ return ("import * as " + name$1 + " from " + (JSON.stringify( actualId$1 )) + "; export default " + name$1 + ";"); } | ||
else | ||
{ return ("import * as " + name$1 + " from " + (JSON.stringify( actualId$1 )) + "; export default ( " + name$1 + " && " + name$1 + "['default'] ) || " + name$1 + ";"); } | ||
} | ||
@@ -759,8 +777,23 @@ }, | ||
return entryModuleIdPromise.then( function () { | ||
var transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire ); | ||
var ref = checkEsModule( code, id ); | ||
var isEsModule = ref.isEsModule; | ||
var hasDefaultExport = ref.hasDefaultExport; | ||
var ast = ref.ast; | ||
if ( isEsModule ) { | ||
if ( !hasDefaultExport ) | ||
{ esModulesWithoutDefaultExport.push( id ); } | ||
return; | ||
} | ||
if ( transformed ) { | ||
commonjsModules.set( id, true ); | ||
return transformed; | ||
// it is not an ES module but not a commonjs module, too. | ||
if ( !checkFirstpass( code, ignoreGlobal ) ) { | ||
esModulesWithoutDefaultExport.push( id ); | ||
return; | ||
} | ||
var transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire, ast ); | ||
if ( !transformed ) { return; } | ||
commonjsModules.set( id, true ); | ||
return transformed; | ||
}); | ||
@@ -767,0 +800,0 @@ } |
@@ -201,15 +201,28 @@ import { statSync } from 'fs'; | ||
function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire ) { | ||
function checkFirstpass (code, ignoreGlobal) { | ||
var firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal; | ||
if ( !firstpass.test( code ) ) { return null; } | ||
return firstpass.test(code); | ||
} | ||
var ast = tryParse( code, id ); | ||
function checkEsModule (code, id) { | ||
var ast = tryParse(code, id); | ||
// if there are top-level import/export declarations, this is ES not CommonJS | ||
var hasDefaultExport = false; | ||
var isEsModule = false; | ||
for ( var i = 0, list = ast.body; i < list.length; i += 1 ) { | ||
var node = list[i]; | ||
if ( importExportDeclaration.test( node.type ) ) { return null; } | ||
if ( node.type === 'ExportDefaultDeclaration' ) | ||
{ hasDefaultExport = true; } | ||
if ( importExportDeclaration.test( node.type ) ) | ||
{ isEsModule = true; } | ||
} | ||
return { isEsModule: isEsModule, hasDefaultExport: hasDefaultExport, ast: ast }; | ||
} | ||
function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire, astCache ) { | ||
var ast = astCache || tryParse( code, id ); | ||
var magicString = new MagicString( code ); | ||
@@ -647,2 +660,4 @@ | ||
var esModulesWithoutDefaultExport = []; | ||
var allowDynamicRequire = !!options.ignore; // TODO maybe this should be configurable? | ||
@@ -743,5 +758,8 @@ | ||
return commonjsModules.has( actualId$1 ) ? | ||
("import { __moduleExports } from " + (JSON.stringify( actualId$1 )) + "; export default __moduleExports;") : | ||
("import * as " + name$1 + " from " + (JSON.stringify( actualId$1 )) + "; export default ( " + name$1 + " && " + name$1 + "['default'] ) || " + name$1 + ";"); | ||
if (commonjsModules.has( actualId$1 )) | ||
{ return ("import { __moduleExports } from " + (JSON.stringify( actualId$1 )) + "; export default __moduleExports;"); } | ||
else if (esModulesWithoutDefaultExport.includes(actualId$1)) | ||
{ return ("import * as " + name$1 + " from " + (JSON.stringify( actualId$1 )) + "; export default " + name$1 + ";"); } | ||
else | ||
{ return ("import * as " + name$1 + " from " + (JSON.stringify( actualId$1 )) + "; export default ( " + name$1 + " && " + name$1 + "['default'] ) || " + name$1 + ";"); } | ||
} | ||
@@ -755,8 +773,23 @@ }, | ||
return entryModuleIdPromise.then( function () { | ||
var transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire ); | ||
var ref = checkEsModule( code, id ); | ||
var isEsModule = ref.isEsModule; | ||
var hasDefaultExport = ref.hasDefaultExport; | ||
var ast = ref.ast; | ||
if ( isEsModule ) { | ||
if ( !hasDefaultExport ) | ||
{ esModulesWithoutDefaultExport.push( id ); } | ||
return; | ||
} | ||
if ( transformed ) { | ||
commonjsModules.set( id, true ); | ||
return transformed; | ||
// it is not an ES module but not a commonjs module, too. | ||
if ( !checkFirstpass( code, ignoreGlobal ) ) { | ||
esModulesWithoutDefaultExport.push( id ); | ||
return; | ||
} | ||
var transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire, ast ); | ||
if ( !transformed ) { return; } | ||
commonjsModules.set( id, true ); | ||
return transformed; | ||
}); | ||
@@ -763,0 +796,0 @@ } |
{ | ||
"name": "rollup-plugin-commonjs", | ||
"version": "8.2.3", | ||
"version": "8.2.4", | ||
"description": "Convert CommonJS modules to ES2015", | ||
@@ -5,0 +5,0 @@ "main": "dist/rollup-plugin-commonjs.cjs.js", |
@@ -7,3 +7,3 @@ import { statSync } from 'fs'; | ||
import defaultResolver from './defaultResolver.js'; | ||
import transformCommonjs from './transform.js'; | ||
import { checkFirstpass, checkEsModule, transformCommonjs } from './transform.js'; | ||
import { getName } from './utils.js'; | ||
@@ -62,2 +62,4 @@ | ||
const esModulesWithoutDefaultExport = []; | ||
const allowDynamicRequire = !!options.ignore; // TODO maybe this should be configurable? | ||
@@ -158,5 +160,8 @@ | ||
return commonjsModules.has( actualId ) ? | ||
`import { __moduleExports } from ${JSON.stringify( actualId )}; export default __moduleExports;` : | ||
`import * as ${name} from ${JSON.stringify( actualId )}; export default ( ${name} && ${name}['default'] ) || ${name};`; | ||
if (commonjsModules.has( actualId )) | ||
return `import { __moduleExports } from ${JSON.stringify( actualId )}; export default __moduleExports;`; | ||
else if (esModulesWithoutDefaultExport.includes(actualId)) | ||
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};`; | ||
} | ||
@@ -170,8 +175,20 @@ }, | ||
return entryModuleIdPromise.then( () => { | ||
const transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire ); | ||
const {isEsModule, hasDefaultExport, ast} = checkEsModule( code, id ); | ||
if ( isEsModule ) { | ||
if ( !hasDefaultExport ) | ||
esModulesWithoutDefaultExport.push( id ); | ||
return; | ||
} | ||
if ( transformed ) { | ||
commonjsModules.set( id, true ); | ||
return transformed; | ||
// it is not an ES module but not a commonjs module, too. | ||
if ( !checkFirstpass( code, ignoreGlobal ) ) { | ||
esModulesWithoutDefaultExport.push( id ); | ||
return; | ||
} | ||
const transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire, ast ); | ||
if ( !transformed ) return; | ||
commonjsModules.set( id, true ); | ||
return transformed; | ||
}); | ||
@@ -178,0 +195,0 @@ } |
@@ -42,13 +42,26 @@ import acorn from 'acorn'; | ||
export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire ) { | ||
export function checkFirstpass (code, ignoreGlobal) { | ||
const firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal; | ||
if ( !firstpass.test( code ) ) return null; | ||
return firstpass.test(code); | ||
} | ||
const ast = tryParse( code, id ); | ||
export function checkEsModule (code, id) { | ||
const ast = tryParse(code, id); | ||
// if there are top-level import/export declarations, this is ES not CommonJS | ||
let hasDefaultExport = false; | ||
let isEsModule = false; | ||
for ( const node of ast.body ) { | ||
if ( importExportDeclaration.test( node.type ) ) return null; | ||
if ( node.type === 'ExportDefaultDeclaration' ) | ||
hasDefaultExport = true; | ||
if ( importExportDeclaration.test( node.type ) ) | ||
isEsModule = true; | ||
} | ||
return { isEsModule, hasDefaultExport, ast }; | ||
} | ||
export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire, astCache ) { | ||
const ast = astCache || tryParse( code, id ); | ||
const magicString = new MagicString( code ); | ||
@@ -55,0 +68,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
191047
1842
0