Skip to content

Instantly share code, notes, and snippets.

@zo0m
Last active February 13, 2024 17:41
Show Gist options
  • Save zo0m/2e81102fc177fc4b4897581549b1fc85 to your computer and use it in GitHub Desktop.
Save zo0m/2e81102fc177fc4b4897581549b1fc85 to your computer and use it in GitHub Desktop.
replace Ti.UI.createWindow with (require("/app/ui/kit").createWindow || Ti.UI.createWindow)
var sourceMapper = require('./sourceMapper');
var logger;
function log(message) {
if (logger) {
logger.info(`[fc][alloy.jmk]: ${message}`);
}
}
task('pre:load', (event, alloyLogger) => {
logger = alloyLogger;
projectDirectory = event.dir.project;
sourceMapper.OPTIONS_OUTPUT.presets = sourceMapper.OPTIONS_OUTPUT.presets || [];
sourceMapper.OPTIONS_OUTPUT.presets.push({
plugins: [
[addDefaultModule],
],
});
});
function addDefaultModule(_ref) {
log(`addDefaultModule()`)
var types = _ref.types;
const visitor = {
CallExpression(path) {
const {callee, arguments: args} = path.node;
const customTagsCreateNames = [
'div', 'FancyButton', 'Row', 'Form'
].map(tag => `create${tag}`);
if (
types.isMemberExpression(callee) &&
callee.object &&
callee.object.object &&
callee.object.property &&
types.isIdentifier(callee.object.object, {name: 'Ti'}) &&
types.isIdentifier(callee.object.property, {name: 'UI'}) &&
callee.property &&
callee.property.name &&
`${callee.property.name}`.startsWith('create') &&
customTagsCreateNames.indexOf(`${callee.property.name}`) >= 0
) {
const createFunctionName = callee.property.name;
// log(`[CallExpression] replace Ti.UI.${createFunctionName} with (require("/app/ui/kit").${createFunctionName} || Ti.UI.${createFunctionName})`);
// replace Ti.UI.createWindow with (require("/app/ui/kit").createWindow || Ti.UI.createWindow)
path.replaceWith(
types.callExpression(
types.logicalExpression(
'||',
types.memberExpression(
types.callExpression(
types.identifier('require'),
[types.stringLiteral('/app/ui/kit')]
),
types.identifier(createFunctionName)
),
types.memberExpression(
types.memberExpression(
types.identifier('Ti'),
types.identifier('UI')
),
types.identifier(createFunctionName)
)
),
args
)
);
}
}
};
return {
pre: function (state) {
},
visitor
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment