Skip to content

Commit

Permalink
leave the expression inside 'export default' in the source, evaluate …
Browse files Browse the repository at this point in the history
…it even if we never access the default export
  • Loading branch information
Swatinem committed Aug 5, 2014
1 parent 124cd57 commit d7014e2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
38 changes: 22 additions & 16 deletions lib/exports.js
Expand Up @@ -17,45 +17,46 @@ function isExportDeclaration(node) {
}

Module.prototype.buildExports = function () {
// [expression, exportedVariable]
// [localVariable, exportedVariable]
var exports = []

this.exports.forEach(function (node) {
var declaration = node.declaration

if (node.default) {
exports.push([declaration, 'default'])
var id = node._varname = this.sourceToVariableName('default')
exports.push([id, 'default'])
return
}

if (node.specifiers) {
return node.specifiers.forEach(function (specifier) {
var id = specifier.id
exports.push([id, id.name])
var id = specifier.id.name
exports.push([id, id])
})
}

if (n.FunctionDeclaration.check(declaration)) {
var id = declaration.id
if (!id.name) throw new Error('unnamed function declaration')
return exports.push([id, id.name])
var id = declaration.id.name
if (!id) throw new Error('unnamed function declaration')
return exports.push([id, id])
}

if (n.VariableDeclaration.check(declaration)) {
return declaration.declarations.forEach(function (declaration) {
var id = declaration.id
exports.push([id, id.name])
var id = declaration.id.name
exports.push([id, id])
})
}

if (n.ClassDeclaration.check(declaration)) {
var id = declaration.id
if (!id.name) throw new Error('unnamed class')
return exports.push([id, id.name])
var id = declaration.id.name
if (!id) throw new Error('unnamed class')
return exports.push([id, id])
}

throw new Error('wtf')
})
}, this)

if (!exports.length) return // nothing to export

Expand All @@ -71,7 +72,12 @@ Module.prototype.removeExports = function () {
visitExportDeclaration: function (path) {
if (path.node.default) {
// remove `export default`s
path.replace()
path.replace(b.variableDeclaration('var', [
b.variableDeclarator(
b.identifier(path.node._varname),
path.node.declaration
)
]))
} else if (path.node.specifiers) {
// remove `export { x, y }`s
path.replace()
Expand All @@ -96,7 +102,7 @@ Module.prototype.removeExports = function () {
* Returns a property
*/

function buildGetter(expression, as) {
function buildGetter(name, as) {
return b.property(
'init',
b.identifier(as),
Expand All @@ -108,7 +114,7 @@ function buildGetter(expression, as) {
null,
[],
b.blockStatement(
[b.returnStatement(expression)]
[b.returnStatement(b.identifier(name))]
)
)
),
Expand Down
4 changes: 3 additions & 1 deletion test/unit.js
Expand Up @@ -128,10 +128,12 @@ describe('Build Exports', function () {
var context = vm.createContext()
vm.runInThisContext(vmExports, context)
vm.runInThisContext(result.code, context)
vm.runInThisContext('if (exports.default() !== "a") throw new Error()', context)
// these should be evaluated before we access the default
vm.runInThisContext('if (a() !== "a") throw new Error()', context)
vm.runInThisContext('if (o.prop1() !== "a") throw new Error()', context)
vm.runInThisContext('if (o.prop2() !== "a") throw new Error()', context)
// and the default itself
vm.runInThisContext('if (exports.default() !== "a") throw new Error()', context)
})

it('export-multiple', function () {
Expand Down

0 comments on commit d7014e2

Please sign in to comment.