Titanium JIRA Archive
Alloy (ALOY)

[ALOY-1574] Unable to use ES6 import/exports in alloy controllers due to bad code generation

GitHub Issuen/a
TypeBug
PriorityHigh
StatusResolved
ResolutionFixed
Resolution Date2017-09-21T01:00:31.000+0000
Affected Version/sn/a
Fix Version/sAlloy 1.10.4
ComponentsTooling
Labelsn/a
ReporterChristopher Williams
AssigneeFeon Sua Xin Miao
Created2017-08-02T15:54:56.000+0000
Updated2018-01-29T15:52:57.000+0000

Description

If you add imports or exports to an alloy controller, we can now parse the code and handle it in our use of babel/babylon instead of uglify. BUT, we don't "hoist" the imports/exports to the top of the generated code, so it can become embedded underneath a function which is invalid ES6 code. imports and exports must be "moved" up to the global top-level or else the code is invalid. i.e.
import 'foo' from 'bar';
export const foo = Math.sqrt(2);
On a related note, we don't really "handle" ES6 imports/exports anyways down the line, but that's another ticket we can address in titanium CLI/build to just use babel transpilation to generate requires out of them (until the wider JS community has settled how to handle module vs script difference and imports/exports)

Comments

  1. Feon Sua Xin Miao 2017-09-18

    PR: https://github.com/appcelerator/alloy/pull/850 Alloy compile should be able to parse import/export without error. *Import* 1. Default import, i.e. import test from '/test/hello'; 2. Namespace import, i.e. import * as test from '/test/hello'; 3. Named imports, i.e. import { a, b } from '/test/hello'; 4. Empty import, i.e. import '/test/hellot'; *Export* 1. Inline Named exports , i.e. export const sqrt = Math.sqrt; *Note* Default export, i.e. export default 'abc';, is not supported in controller.

JSON Source