diff --git a/package.json b/package.json index f1ad39b..8ed39a7 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,9 @@ "pretest": "npm run build", "test": "mocha", "pretest-coverage": "npm run build", - "test-coverage": "istanbul cover --report json node_modules/.bin/_mocha -- -u exports -R spec test/test.js", - "posttest-coverage": "remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.json -b dist", - "ci": "npm run test-coverage && codecov < coverage/coverage-remapped.json", + "test-coverage": "rm -rf coverage/* && istanbul cover --report json node_modules/.bin/_mocha -- -u exports -R spec test/test.js", + "posttest-coverage": "remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.json -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped.lcov -t lcovonly -b dist && remap-istanbul -i coverage/coverage-final.json -o coverage/coverage-remapped -t html -b dist", + "ci": "npm run test-coverage && codecov < coverage/coverage-remapped.lcov", "build": "gobble build -f dist", "prepublish": "npm test", "lint": "eslint src" diff --git a/src/Module.js b/src/Module.js index 8f0c212..c59c3fd 100644 --- a/src/Module.js +++ b/src/Module.js @@ -599,19 +599,6 @@ export default class Module { magicString.remove( statement.start, statement.next ); return; } - - // skip `export var foo;` if foo is exported - if ( isEmptyExportedVarDeclaration( statement.node.declaration, this.exports, toExport ) ) { - magicString.remove( statement.start, statement.next ); - return; - } - } - - // skip empty var declarations for exported bindings - // (otherwise we're left with `exports.foo;`, which is useless) - if ( isEmptyExportedVarDeclaration( statement.node, this.exports, toExport ) ) { - magicString.remove( statement.start, statement.next ); - return; } // split up/remove var declarations as necessary diff --git a/src/Statement.js b/src/Statement.js index 93b2a03..5b365f0 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -474,24 +474,6 @@ export default class Statement { magicString.overwrite( node.start, id.end, bundleExports[ name ], true ); id._skip = true; } - - // otherwise, we insert the `exports.foo = foo` after the declaration - else { - const exportInitialisers = node.declarations - .map( declarator => declarator.id.name ) - .filter( name => !!bundleExports[ name ] ) - .map( name => `\n${bundleExports[name]} = ${name};` ) - .join( '' ); - - if ( exportInitialisers ) { - // TODO clean this up - try { - magicString.insert( node.end, exportInitialisers ); - } catch ( err ) { - magicString.append( exportInitialisers ); - } - } - } } } diff --git a/test/function/cannot-import-self/_config.js b/test/function/cannot-import-self/_config.js new file mode 100644 index 0000000..2413d03 --- /dev/null +++ b/test/function/cannot-import-self/_config.js @@ -0,0 +1,8 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'prevents a module importing itself', + error: function ( err ) { + assert.ok( /A module cannot import itself/.test( err.message ) ); + } +}; diff --git a/test/function/cannot-import-self/main.js b/test/function/cannot-import-self/main.js new file mode 100644 index 0000000..48e08e6 --- /dev/null +++ b/test/function/cannot-import-self/main.js @@ -0,0 +1 @@ +import me from './main'; diff --git a/test/function/export-type-mismatch-b/_config.js b/test/function/export-type-mismatch-b/_config.js new file mode 100644 index 0000000..25a7220 --- /dev/null +++ b/test/function/export-type-mismatch-b/_config.js @@ -0,0 +1,11 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'export type must be auto, default, named or none', + bundleOptions: { + exports: 'blah' + }, + generateError: function ( err ) { + assert.ok( /options\.exports must be 'default', 'named', 'none', 'auto', or left unspecified/.test( err.message ) ); + } +}; diff --git a/test/function/export-type-mismatch-b/main.js b/test/function/export-type-mismatch-b/main.js new file mode 100644 index 0000000..7a4e8a7 --- /dev/null +++ b/test/function/export-type-mismatch-b/main.js @@ -0,0 +1 @@ +export default 42; diff --git a/test/function/export-type-mismatch-c/_config.js b/test/function/export-type-mismatch-c/_config.js new file mode 100644 index 0000000..8df0d58 --- /dev/null +++ b/test/function/export-type-mismatch-c/_config.js @@ -0,0 +1,11 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'cannot have named exports if explicit export type is default', + bundleOptions: { + exports: 'none' + }, + generateError: function ( err ) { + assert.ok( /'none' was specified for options\.exports/.test( err.message ) ); + } +}; diff --git a/test/function/export-type-mismatch-c/main.js b/test/function/export-type-mismatch-c/main.js new file mode 100644 index 0000000..7a4e8a7 --- /dev/null +++ b/test/function/export-type-mismatch-c/main.js @@ -0,0 +1 @@ +export default 42; diff --git a/test/function/export-type-mismatch/_config.js b/test/function/export-type-mismatch/_config.js new file mode 100644 index 0000000..5b321d6 --- /dev/null +++ b/test/function/export-type-mismatch/_config.js @@ -0,0 +1,11 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'cannot have named exports if explicit export type is default', + bundleOptions: { + exports: 'default' + }, + generateError: function ( err ) { + assert.ok( /'default' was specified for options\.exports/.test( err.message ) ); + } +}; diff --git a/test/function/export-type-mismatch/main.js b/test/function/export-type-mismatch/main.js new file mode 100644 index 0000000..3b8dc9f --- /dev/null +++ b/test/function/export-type-mismatch/main.js @@ -0,0 +1 @@ +export var foo = 42; diff --git a/test/function/property-keys-not-renamed/_config.js b/test/function/property-keys-not-renamed/_config.js new file mode 100644 index 0000000..d4e1f82 --- /dev/null +++ b/test/function/property-keys-not-renamed/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'does not rename property keys' +}; diff --git a/test/function/property-keys-not-renamed/main.js b/test/function/property-keys-not-renamed/main.js new file mode 100644 index 0000000..9134b8e --- /dev/null +++ b/test/function/property-keys-not-renamed/main.js @@ -0,0 +1,7 @@ +import one from './one'; +import two from './two'; +import three from './three'; + +assert.equal( one(), 'one' ); +assert.equal( two(), 'two' ); +assert.equal( three(), 'three' ); diff --git a/test/function/property-keys-not-renamed/one.js b/test/function/property-keys-not-renamed/one.js new file mode 100644 index 0000000..ab4a0f9 --- /dev/null +++ b/test/function/property-keys-not-renamed/one.js @@ -0,0 +1,11 @@ +var obj = { + foo: foo +}; + +function foo () { + return 'one'; +} + +export default function () { + return obj.foo(); +} diff --git a/test/function/property-keys-not-renamed/three.js b/test/function/property-keys-not-renamed/three.js new file mode 100644 index 0000000..a78e2e5 --- /dev/null +++ b/test/function/property-keys-not-renamed/three.js @@ -0,0 +1,11 @@ +var obj = { + foo: foo +}; + +function foo () { + return 'three'; +} + +export default function () { + return obj.foo(); +} diff --git a/test/function/property-keys-not-renamed/two.js b/test/function/property-keys-not-renamed/two.js new file mode 100644 index 0000000..70e142b --- /dev/null +++ b/test/function/property-keys-not-renamed/two.js @@ -0,0 +1,11 @@ +var obj = { + foo: foo +}; + +function foo () { + return 'two'; +} + +export default function () { + return obj.foo(); +} diff --git a/test/test.js b/test/test.js index a871ccd..a69d215 100644 --- a/test/test.js +++ b/test/test.js @@ -45,6 +45,36 @@ describe( 'rollup', function () { it( 'has a rollup method', function () { assert.equal( typeof rollup.rollup, 'function' ); }); + + it( 'fails without options or options.entry', function () { + assert.throws( function () { + rollup.rollup(); + }, /must supply options\.entry/ ); + + assert.throws( function () { + rollup.rollup({}); + }, /must supply options\.entry/ ); + }); + }); + + describe( 'bundle.write()', function () { + it( 'fails without options or options.dest', function () { + return rollup.rollup({ + entry: 'x', + resolveId: function () { return 'test'; }, + load: function () { + return '// empty'; + } + }).then( function ( bundle ) { + assert.throws( function () { + bundle.write(); + }, /must supply options\.dest/ ); + + assert.throws( function () { + bundle.write({}); + }, /must supply options\.dest/ ); + }); + }); }); describe( 'function', function () { @@ -80,64 +110,57 @@ describe( 'rollup', function () { format: 'cjs' })); - if ( config.error ) { + if ( config.generateError ) { unintendedError = new Error( 'Expected an error while generating output' ); } } catch ( err ) { - if ( config.error ) { - config.error( err ); + if ( config.generateError ) { + config.generateError( err ); } else { unintendedError = err; } } if ( unintendedError ) throw unintendedError; + if ( config.error || config.generateError ) return; var code; - try { - if ( config.babel ) { - code = babel.transform( result.code, { - blacklist: [ 'es6.modules' ], - loose: [ 'es6.classes' ] - }).code; - } else { - code = result.code; - } - - var module = { - exports: {} - }; + if ( config.babel ) { + code = babel.transform( result.code, { + blacklist: [ 'es6.modules' ], + loose: [ 'es6.classes' ] + }).code; + } else { + code = result.code; + } - var context = extend({ - require: require, - module: module, - exports: module.exports, - assert: assert - }, config.context || {} ); + var module = { + exports: {} + }; - var contextKeys = Object.keys( context ); - var contextValues = contextKeys.map( function ( key ) { - return context[ key ]; - }); + var context = extend({ + require: require, + module: module, + exports: module.exports, + assert: assert + }, config.context || {} ); - var fn = new Function( contextKeys, code ); - fn.apply( {}, contextValues ); + var contextKeys = Object.keys( context ); + var contextValues = contextKeys.map( function ( key ) { + return context[ key ]; + }); - if ( config.error ) { - unintendedError = new Error( 'Expected an error while executing output' ); - } + var fn = new Function( contextKeys, code ); + fn.apply( {}, contextValues ); - if ( config.exports ) config.exports( module.exports ); - if ( config.bundle ) config.bundle( bundle ); - } catch ( err ) { - if ( config.error ) { - config.error( err ); - } else { - unintendedError = err; - } + if ( config.error ) { + unintendedError = new Error( 'Expected an error while executing output' ); } + if ( config.exports ) config.exports( module.exports ); + if ( config.bundle ) config.bundle( bundle ); + if ( config.show || unintendedError ) { console.log( code + '\n\n\n' ); }