Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use babel-plugin-transform-titanium when transpiling #11400

Merged
merged 5 commits into from Jan 27, 2020

Conversation

sgtcoolguy
Copy link
Contributor

@sgtcoolguy sgtcoolguy commented Dec 17, 2019

JIRA: https://jira.appcelerator.org/browse/TIMOB-27167

Description:
This is a PR to make use of appcelerator/babel-plugin-transform-titanium

The goal here is both during SDK build and app build to pass along options that the babel plugin can use to inline static values or during evaluation of special "defines" or common platform/os sniffing expressions.

Relates to tidev/node-titanium-sdk#131.

During SDK build
This PR makes use of the inlining during our generation of the bundled ti.main.js. You can look into the SDK's dist/tmp/common folders for the bundled ti.main.js to see the effects (though given we run other transpiration and rollup it can be difficult to know what to look for - easiest thing to spot are removed Ti.Platform.name and Ti.Platform.osname sniffing expressions).
This can bake in static constant property values for platforms (think Ti.buildHash, Ti.Platform.osname, Ti.Filesystem.separator), or replace platform sniffing in our common JS code.

During App build
We pass along options through to node-titanium-sdk to specify the target platform, distribution target, deploy type, etc. So at app build time it can replace even more than simple platform sniffing (think Ti.App.* properties).

@build
Copy link
Contributor

build commented Dec 17, 2019

Messages
📖

💾 Here's the generated SDK zipfile.

📖 ✊ The commits in this PR match our conventions! Feel free to Rebase and Merge this PR when ready.
📖

✅ All tests are passing
Nice one! All 6549 tests are passing.
(There are 701 skipped tests not included in that total)

New dependencies added: babel-plugin-transform-titanium.

babel-plugin-transform-titanium

Author: cwilliams

Description: Replace known values in Titanium apps to allow for further simplification via dead code/evaluation in babel minify

Homepage: http://npmjs.com/package/babel-plugin-transform-titanium

Createdabout 1 year ago
Last Updated7 months ago
LicenseNO LICENSE FOUND
Maintainers1
Releases3
Keywordsbabel-plugin
README

Greenkeeper badge

This is a babel plugin intended to optimize Titanium SDK applications by inlining various static values. Once inlined, Other babel plugins may performa additional optimizations to help strip away "dead code".

Information about the current Titanium application build should be passed to the plugin's options object.

Plugin Options

const options = {
	deploytype: 'development', // one of 'development', 'test', 'production'
	platform: undefined, // one of 'android', 'ios', 'windows'
	target: undefined, // one of 'dist-playstore', 'dist-appstore', 'dist-adhoc', null/undefined
	Ti: {
		version: undefined, // string, SDK version
		App: {
			id: undefined, // string, application id from tiapp.xml
			name: undefined, // string, application name from tiapp.xml
			version: undefined, // string, application version from tiapp.xml
		}
		Platform: {
			osname: undefined, // one of 'android', 'ipad', 'iphone', 'windowsphone', 'windowsstore'
		}
	}
};

Special "defines"

It can:

  • replace special OS_* references with boolean values
    • OS_IOS - true iff pluginOptions.platform === 'ios'
    • OS_ANDROID - true iff pluginOptions.platform === 'android'
    • OS_WINDOWS - true iff pluginOptions.platform === 'windows'
    • Useful for if/else guards for platform specific code. Once booleans are inlined the false blocks can be removed.
  • replace special ENV_* references with boolean values
    • ENV_DEV and ENV_DEVELOPMENT - true iff pluginOptions.deploytype === 'development' (typically simulator builds)
    • ENV_TEST - true iff pluginOptions.deploytype === 'test' (typically device builds)
    • ENV_PROD and ENV_PRODUCTION - true iff pluginOptions.deploytype === 'production' (typically app store/ad hoc builds)
  • replace special DIST_* references with boolean values
    • DIST_ADHOC - true iff pluginOptions.target === 'dist-adhoc'
    • DIST_STORE - true iff pluginOptions.target === 'dist-appstore' || pluginOptions.target === 'dist-playstore'

OS "sniffing" via Ti.Platform.osname

  • replace typical "sniff" expressions for iOS/Windows using Ti.Platform.osname with static boolean values
    i.e.
if (Ti.Platform.osname === 'iphone' || Ti.Platform.osname === 'ipad') {
	Ti.API.info('on ios!');
}
if (Ti.Platform.osname === 'windowsstore' || Ti.Platform.osname === 'windowsphone') {
	Ti.API.info('on windows!');
}

Generated by 🚫 dangerJS against 9a11c94

Copy link
Collaborator

@ewanharris ewanharris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Just one minor nit on the timestamp in prebuild.js differing to Utils.timestamp.

Did a small FR and am seeing values get inlined as expected 🎉

@@ -81,13 +81,18 @@ async function generateBuildProperties() {
buildGitHash = 'HEAD';
}

let buildTimestamp = process.env.TI_SDK_BUILD_TIMESTAMP;
if (!buildTimestamp) {
buildTimestamp = moment().format('YYYY/MM/DD HH:mm');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timestamp generated here is different to the one generated by Utils.timestamp (2020/01/06 12:10 vs 1/6/2020 12:10). Does that matter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may! We should be using the same format in the manifest.json in the SDK as we use in the Ti.buildDate property at runtime (and that value should be the same on either platform). I think that Utils.timestamp basically is what will get shoved into the iOS Ti.buildDate in Obj-c, and will be used for the babel pre-filling. And I think is set to the env var we'll use and prefer above here.

So I think this really only matters for local dev builds, but yeah we should be consistent in our formatting.

I think our unit test doesn't actually verify the format of the value so it's possible we've had iOS/Android differing here for a very long (forever?) time.

@lokeshchdhry
Copy link
Contributor

FR Passed.

Studio Ver: 5.1.4.201909061933
SDK Ver: 9.0.0 local build
OS Ver: 10.14.5
Xcode Ver: Xcode 11.3
Appc NPM: 4.2.15
Appc CLI: 7.1.2
Daemon Ver: 1.1.3
Ti CLI Ver: 5.2.2
Alloy Ver: 1.14.4
Node Ver: 12.13.1
NPM Ver: 6.12.1
Java Ver: 11.0.1
Android Devices: ⇨ google Pixel (Android 10)
IOS Sim: Iphone 11 pro IOS 13.3

@lokeshchdhry lokeshchdhry merged commit c21f77c into tidev:master Jan 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants