Skip to content

Commit

Permalink
Merge pull request #684 from rollup/gh-653
Browse files Browse the repository at this point in the history
options.preferConst to use const instead of var when creating statements
  • Loading branch information
Rich-Harris committed Jun 5, 2016
2 parents 5cff69c + e85626e commit 83324f9
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Bundle.js
Expand Up @@ -73,6 +73,8 @@ export default class Bundle {

// TODO strictly speaking, this only applies with non-ES6, non-default-only bundles
[ 'module', 'exports', '_interopDefault' ].forEach( global => this.assumedGlobals[ global ] = true );

this.varOrConst = options.preferConst ? 'const' : 'var';
}

build () {
Expand Down
2 changes: 1 addition & 1 deletion src/Declaration.js
Expand Up @@ -241,7 +241,7 @@ export class SyntheticNamespaceDeclaration {
return `${indentString}${name}: ${original.render()}`;
});

return `var ${this.render()} = Object.freeze({\n${members.join( ',\n' )}\n});\n\n`;
return `${this.module.bundle.varOrConst} ${this.render()} = Object.freeze({\n${members.join( ',\n' )}\n});\n\n`;
}

render () {
Expand Down
2 changes: 1 addition & 1 deletion src/Module.js
Expand Up @@ -592,7 +592,7 @@ export default class Module {
if ( statement.node.declaration.type === 'FunctionExpression' ) {
magicString.overwrite( statement.node.start, statement.node.declaration.start + 8, `function ${defaultName}` );
} else {
magicString.overwrite( statement.node.start, statement.node.declaration.start, `var ${defaultName} = ` );
magicString.overwrite( statement.node.start, statement.node.declaration.start, `${this.bundle.varOrConst} ${defaultName} = ` );
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/finalisers/cjs.js
Expand Up @@ -9,18 +9,20 @@ export default function cjs ( bundle, magicString, { exportMode }, options ) {
intro += `function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }\n\n`;
}

const varOrConst = bundle.varOrConst;

// TODO handle empty imports, once they're supported
const importBlock = bundle.externalModules
.map( module => {
if ( module.declarations.default ) {
if (module.exportsNames) {
return `var ${module.name} = require('${module.id}');` +
`\nvar ${module.name}__default = _interopDefault(${module.name});`;
return `${varOrConst} ${module.name} = require('${module.id}');` +
`\n${varOrConst} ${module.name}__default = _interopDefault(${module.name});`;
} else {
return `var ${module.name} = _interopDefault(require('${module.id}'));`;
return `${varOrConst} ${module.name} = _interopDefault(require('${module.id}'));`;
}
} else {
return `var ${module.name} = require('${module.id}');`;
return `${varOrConst} ${module.name} = require('${module.id}');`;
}
})
.join( '\n' );
Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/iife.js
Expand Up @@ -41,7 +41,7 @@ export default function iife ( bundle, magicString, { exportMode, indentString }
let outro = `\n\n}(${dependencies}));`;

if ( exportMode === 'default' ) {
intro = ( isNamespaced ? `this.` : `var ` ) + `${name} = ${intro}`;
intro = ( isNamespaced ? `this.` : `${bundle.varOrConst} ` ) + `${name} = ${intro}`;
}

if ( isNamespaced ) {
Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/shared/getInteropBlock.js
Expand Up @@ -3,7 +3,7 @@ export default function getInteropBlock ( bundle ) {
.map( module => {
return module.declarations.default ?
( module.exportsNames ?
`var ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` :
`${bundle.varOrConst} ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` :
`${module.name} = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` ) :
null;
})
Expand Down
1 change: 1 addition & 0 deletions src/rollup.js
Expand Up @@ -25,6 +25,7 @@ const ALLOWED_KEYS = [
'onwarn',
'outro',
'plugins',
'preferConst',
'sourceMap',
'treeshake',
'useStrict'
Expand Down
7 changes: 7 additions & 0 deletions test/form/prefer-const/_config.js
@@ -0,0 +1,7 @@
module.exports = {
description: 'uses const instead of var if specified (#653)',
options: {
preferConst: true,
moduleName: 'myBundle'
}
};
18 changes: 18 additions & 0 deletions test/form/prefer-const/_expected/amd.js
@@ -0,0 +1,18 @@
define(['external', 'other', 'another'], function (external, other, another) { 'use strict';

const a = 1;
const b = 2;


const namespace = Object.freeze({
a: a,
b: b
});

console.log( Object.keys( namespace ) );

const main = 42;

return main;

});
20 changes: 20 additions & 0 deletions test/form/prefer-const/_expected/cjs.js
@@ -0,0 +1,20 @@
'use strict';

const external = require('external');
const other = require('other');
const another = require('another');

const a = 1;
const b = 2;


const namespace = Object.freeze({
a: a,
b: b
});

console.log( Object.keys( namespace ) );

const main = 42;

module.exports = main;
18 changes: 18 additions & 0 deletions test/form/prefer-const/_expected/es6.js
@@ -0,0 +1,18 @@
import 'external';
import 'other';
import 'another';

const a = 1;
const b = 2;


const namespace = Object.freeze({
a: a,
b: b
});

console.log( Object.keys( namespace ) );

const main = 42;

export default main;
19 changes: 19 additions & 0 deletions test/form/prefer-const/_expected/iife.js
@@ -0,0 +1,19 @@
const myBundle = (function (external,other,another) {
'use strict';

const a = 1;
const b = 2;


const namespace = Object.freeze({
a: a,
b: b
});

console.log( Object.keys( namespace ) );

const main = 42;

return main;

}(external,other,another));
22 changes: 22 additions & 0 deletions test/form/prefer-const/_expected/umd.js
@@ -0,0 +1,22 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('external'), require('other'), require('another')) :
typeof define === 'function' && define.amd ? define(['external', 'other', 'another'], factory) :
(global.myBundle = factory(global.external,global.other,global.another));
}(this, function (external,other,another) { 'use strict';

const a = 1;
const b = 2;


const namespace = Object.freeze({
a: a,
b: b
});

console.log( Object.keys( namespace ) );

const main = 42;

return main;

}));
9 changes: 9 additions & 0 deletions test/form/prefer-const/main.js
@@ -0,0 +1,9 @@
import external from 'external';
import a from 'other';
import { b } from 'other';
import { another } from 'another';
import * as namespace from './namespace.js';

console.log( Object.keys( namespace ) );

export default 42;
2 changes: 2 additions & 0 deletions test/form/prefer-const/namespace.js
@@ -0,0 +1,2 @@
export const a = 1;
export const b = 2;
2 changes: 1 addition & 1 deletion test/test.js
Expand Up @@ -76,7 +76,7 @@ describe( 'rollup', function () {
return rollup.rollup({ entry: 'x', plUgins: [] }).then( function () {
throw new Error( 'Missing expected error' );
}, function ( err ) {
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: banner, dest, entry, exports, external, footer, format, globals, indent, intro, moduleId, moduleName, noConflict, onwarn, outro, plugins, sourceMap, treeshake, useStrict' );
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: banner, dest, entry, exports, external, footer, format, globals, indent, intro, moduleId, moduleName, noConflict, onwarn, outro, plugins, preferConst, sourceMap, treeshake, useStrict' );
});
});
});
Expand Down

0 comments on commit 83324f9

Please sign in to comment.