rollup-plugin-commonjs
Advanced tools
Comparing version 1.2.0 to 1.3.0
# rollup-plugin-commonjs changelog | ||
## 1.3.0 | ||
* Handle references to `global` ([#6](https://github.com/rollup/rollup-plugin-commonjs/issues/6)) | ||
## 1.2.0 | ||
@@ -4,0 +8,0 @@ |
@@ -46,3 +46,3 @@ 'use strict'; | ||
var firstpass = /\b(?:require|module|exports)\b/; | ||
var firstpass = /\b(?:require|module|exports|global)\b/; | ||
var exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/; | ||
@@ -60,2 +60,3 @@ | ||
var filter = rollupPluginutils.createFilter(options.include, options.exclude); | ||
var bundleUsesGlobal = false; | ||
@@ -102,2 +103,3 @@ return { | ||
var usesModuleOrExports = undefined; | ||
var usesGlobal = undefined; | ||
@@ -131,3 +133,4 @@ estreeWalker.walk(ast, { | ||
if (node.type === 'Identifier') { | ||
if ((node.name === 'module' || node.name === 'exports') && isReference(node, parent)) usesModuleOrExports = true; | ||
if ((node.name === 'module' || node.name === 'exports') && isReference(node, parent) && !scope.contains(node.name)) usesModuleOrExports = true; | ||
if (node.name === 'global' && isReference(node, parent) && !scope.contains('global')) usesGlobal = true; | ||
return; | ||
@@ -162,3 +165,3 @@ } | ||
if (!sources.length && !usesModuleOrExports) return null; // not a CommonJS module | ||
if (!sources.length && !usesModuleOrExports && !usesGlobal) return null; // not a CommonJS module | ||
@@ -171,4 +174,4 @@ var name = getName(id); | ||
var intro = '\n\nvar ' + name + ' = (function (module) {\nvar exports = module.exports;\n'; | ||
var outro = '\nreturn module.exports;\n})({exports:{}});\n\nexport default ' + name + ';\n'; | ||
var intro = '\n\nvar ' + name + ' = (function (module' + (usesGlobal ? ', global' : '') + ') {\nvar exports = module.exports;\n'; | ||
var outro = '\nreturn module.exports;\n})({exports:{}}' + (usesGlobal ? ', __commonjs_global' : '') + ');\n\nexport default ' + name + ';\n'; | ||
@@ -184,3 +187,9 @@ outro += Object.keys(namedExports).map(function (x) { | ||
if (usesGlobal) bundleUsesGlobal = true; | ||
return { code: code, map: map }; | ||
}, | ||
intro: function intro() { | ||
return bundleUsesGlobal ? 'var __commonjs_global = typeof window !== \'undefined\' ? window : typeof global !== \'undefined\' ? global : this;' : ''; | ||
} | ||
@@ -187,0 +196,0 @@ }; |
@@ -42,3 +42,3 @@ import { statSync } from 'fs'; | ||
var firstpass = /\b(?:require|module|exports)\b/; | ||
var firstpass = /\b(?:require|module|exports|global)\b/; | ||
var exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/; | ||
@@ -56,2 +56,3 @@ | ||
var filter = createFilter(options.include, options.exclude); | ||
var bundleUsesGlobal = false; | ||
@@ -98,2 +99,3 @@ return { | ||
var usesModuleOrExports = undefined; | ||
var usesGlobal = undefined; | ||
@@ -127,3 +129,4 @@ walk(ast, { | ||
if (node.type === 'Identifier') { | ||
if ((node.name === 'module' || node.name === 'exports') && isReference(node, parent)) usesModuleOrExports = true; | ||
if ((node.name === 'module' || node.name === 'exports') && isReference(node, parent) && !scope.contains(node.name)) usesModuleOrExports = true; | ||
if (node.name === 'global' && isReference(node, parent) && !scope.contains('global')) usesGlobal = true; | ||
return; | ||
@@ -158,3 +161,3 @@ } | ||
if (!sources.length && !usesModuleOrExports) return null; // not a CommonJS module | ||
if (!sources.length && !usesModuleOrExports && !usesGlobal) return null; // not a CommonJS module | ||
@@ -167,4 +170,4 @@ var name = getName(id); | ||
var intro = '\n\nvar ' + name + ' = (function (module) {\nvar exports = module.exports;\n'; | ||
var outro = '\nreturn module.exports;\n})({exports:{}});\n\nexport default ' + name + ';\n'; | ||
var intro = '\n\nvar ' + name + ' = (function (module' + (usesGlobal ? ', global' : '') + ') {\nvar exports = module.exports;\n'; | ||
var outro = '\nreturn module.exports;\n})({exports:{}}' + (usesGlobal ? ', __commonjs_global' : '') + ');\n\nexport default ' + name + ';\n'; | ||
@@ -180,3 +183,9 @@ outro += Object.keys(namedExports).map(function (x) { | ||
if (usesGlobal) bundleUsesGlobal = true; | ||
return { code: code, map: map }; | ||
}, | ||
intro: function intro() { | ||
return bundleUsesGlobal ? 'var __commonjs_global = typeof window !== \'undefined\' ? window : typeof global !== \'undefined\' ? global : this;' : ''; | ||
} | ||
@@ -183,0 +192,0 @@ }; |
{ | ||
"name": "rollup-plugin-commonjs", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Convert CommonJS modules to ES2015", | ||
"devDependencies": { | ||
"eslint": "^1.7.3", | ||
"gobble": "^0.10.2", | ||
"gobble-cli": "^0.6.0", | ||
"gobble-rollup": "^0.11.0", | ||
"mocha": "^2.3.3", | ||
@@ -19,3 +17,3 @@ "rollup": "^0.20.3", | ||
"pretest": "npm run build", | ||
"build": "gobble build -f dist", | ||
"build": "rm -rf dist/* && rollup -c -f cjs -o dist/rollup-plugin-commonjs.cjs.js && rollup -c -f es6 -o dist/rollup-plugin-commonjs.es6.js", | ||
"prepublish": "npm test" | ||
@@ -30,6 +28,16 @@ }, | ||
"acorn": "^2.4.0", | ||
"estree-walker": "^0.1.3", | ||
"magic-string": "^0.7.0", | ||
"estree-walker": "^0.2.0", | ||
"magic-string": "^0.8.0", | ||
"rollup-pluginutils": "^1.2.0" | ||
} | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/rollup/rollup-plugin-commonjs.git" | ||
}, | ||
"author": "Rich Harris", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/rollup/rollup-plugin-commonjs/issues" | ||
}, | ||
"homepage": "https://github.com/rollup/rollup-plugin-commonjs" | ||
} |
@@ -10,3 +10,3 @@ import { statSync } from 'fs'; | ||
var firstpass = /\b(?:require|module|exports)\b/; | ||
var firstpass = /\b(?:require|module|exports|global)\b/; | ||
var exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/; | ||
@@ -22,3 +22,4 @@ | ||
export default function commonjs ( options = {} ) { | ||
var filter = createFilter( options.include, options.exclude ); | ||
const filter = createFilter( options.include, options.exclude ); | ||
let bundleUsesGlobal = false; | ||
@@ -69,2 +70,3 @@ return { | ||
let usesModuleOrExports; | ||
let usesGlobal; | ||
@@ -98,3 +100,4 @@ walk( ast, { | ||
if ( node.type === 'Identifier' ) { | ||
if ( ( node.name === 'module' || node.name === 'exports' ) && isReference( node, parent ) ) usesModuleOrExports = true; | ||
if ( ( node.name === 'module' || node.name === 'exports' ) && isReference( node, parent ) && !scope.contains( node.name ) ) usesModuleOrExports = true; | ||
if ( node.name === 'global' && isReference( node, parent ) && !scope.contains( 'global' ) ) usesGlobal = true; | ||
return; | ||
@@ -129,3 +132,3 @@ } | ||
if ( !sources.length && !usesModuleOrExports ) return null; // not a CommonJS module | ||
if ( !sources.length && !usesModuleOrExports && !usesGlobal ) return null; // not a CommonJS module | ||
@@ -138,4 +141,4 @@ const name = getName( id ); | ||
const intro = `\n\nvar ${name} = (function (module) {\nvar exports = module.exports;\n`; | ||
let outro = `\nreturn module.exports;\n})({exports:{}});\n\nexport default ${name};\n`; | ||
const intro = `\n\nvar ${name} = (function (module${usesGlobal ? ', global' : ''}) {\nvar exports = module.exports;\n`; | ||
let outro = `\nreturn module.exports;\n})({exports:{}}${usesGlobal ? ', __commonjs_global' : ''});\n\nexport default ${name};\n`; | ||
@@ -152,5 +155,13 @@ outro += Object.keys( namedExports ).map( x => `export var ${x} = ${name}.${x};` ).join( '\n' ); | ||
if ( usesGlobal ) bundleUsesGlobal = true; | ||
return { code, map }; | ||
}, | ||
intro () { | ||
return bundleUsesGlobal ? | ||
`var __commonjs_global = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : this;` : | ||
''; | ||
} | ||
}; | ||
} |
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
19955
5
0
418
1
0
3
+ Addedmagic-string@0.8.0(transitive)
- Removedestree-walker@0.1.3(transitive)
- Removedmagic-string@0.7.0(transitive)
Updatedestree-walker@^0.2.0
Updatedmagic-string@^0.8.0