diff --git a/Alloy/commands/compile/ast/controller.js b/Alloy/commands/compile/ast/controller.js index e864413fa..5ee7b1ec8 100644 --- a/Alloy/commands/compile/ast/controller.js +++ b/Alloy/commands/compile/ast/controller.js @@ -14,6 +14,7 @@ exports.processController = function(code, file, isProduction = false) { var baseController = '', moduleCodes = '', newCode = '', + preCode = '', exportSpecifiers = []; if (isProduction) { @@ -42,10 +43,20 @@ exports.processController = function(code, file, isProduction = false) { }).setContext(); traverse(ast, { enter: function(path) { - if (types.isAssignmentExpression(path.node) && isBaseControllerExportExpression(path.node.left)) { + var node = path.node; + if (types.isAssignmentExpression(node) && isBaseControllerExportExpression(node.left)) { // what's equivalent of print_to_string()? I replaced with simple value property assuming it's a string literal baseController = '\'' + path.node.right.value + '\''; } + + // find function named __pre + if (node.type === 'FunctionDeclaration') { + if (node.id.name == '__pre') { + // put function code into preCode + preCode += generate(node.body, GENCODE_OPTIONS).code; + path.remove(); + } + } }, ImportDeclaration: function(path) { @@ -95,6 +106,7 @@ exports.processController = function(code, file, isProduction = false) { return { es6mods: moduleCodes, base: baseController, - code: newCode + code: newCode, + pre: preCode.trim() }; }; diff --git a/Alloy/commands/compile/compilerUtils.js b/Alloy/commands/compile/compilerUtils.js index cedc6f04a..abb009ebb 100755 --- a/Alloy/commands/compile/compilerUtils.js +++ b/Alloy/commands/compile/compilerUtils.js @@ -958,6 +958,7 @@ exports.loadController = function(file) { code.controller = controller.code; code.parentControllerName = controller.base; code.es6mods = controller.es6mods; + code.pre = controller.pre; return code; };