Skip to content

Enable transpilers support in NativeScript projects #493

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

Closed
4 tasks done
ligaz opened this issue May 21, 2015 · 14 comments
Closed
4 tasks done

Enable transpilers support in NativeScript projects #493

ligaz opened this issue May 21, 2015 · 14 comments
Labels
Milestone

Comments

@ligaz
Copy link

ligaz commented May 21, 2015

A common request we have received in the last couple of months is the ability to enable (or start with) JavaScript transpilers for a given NativeScript project. The usual suspects are TypeScript and Babel which recently became quite popular with the approaching ratification of ES6.

Guiding principles

There a couple of guiding principles we want to follow when we enable this functionality.

Making it generic

We want to design this feature so we can accommodate any transpiler, not just ones for JavaScript. What if you want to use LESS or Sass instead of CSS? Fan of Jade instead of XML we should have you covered.

Per project configuration

This feature should allow usage of different version of the transpiler in different projects. For example you can use TypeScript 1.4 for your production app but switch to the cutting edge TypeScript 1.5 in your new project.

Ability to to start with transpiler or enable it on the go

We believe that the barrier to entry for NativeScript should be as low as the one of the Web. We shouldn't force the users to learn all the cool ES6 features or TypeScript's types system. Users should be able to enable a transpiler for an already existing project or start a new project with given transpiler if this is what they want.

The Command

The support will boil down to a single command that users will have to execute:

$ tns install typescript

or

$ tns install babel

As you can see we have not settled on the command name 😄 We did a little brainstorming sessions internally and we come up with enable (ex. $ tns enable babel) however this sounds too broad and we are not totally sold on it. If you have ideas how we can better name this command please file them in the comments.

Implementation details

Here are some internal and more technical implementation details:

  • We plan to add the transpilation code itself in a form of project level hooks. The given transpiler hooks will come in the form of a separate npm package that will be "hooked" in the current NativeScript project.
  • All transpilation tools will be added as devDependencies to the project's package.json.

Work breakdown

v1.5

TypeScript support

  • tns install typescript command. The compiler should be added as devDependency.
  • Consume TypeScript declarations from the tns-core-modules package.
  • Generate tsconfig.json if not present.
  • Add a project level hook that will do the actual transpilation.

Babel support

Future ideas

TypeScript support for NativeScript plugins

@ligaz ligaz added this to the 1.2.0 milestone May 21, 2015
@ligaz ligaz mentioned this issue May 21, 2015
@tjvantoll
Copy link
Contributor

Hey @ligaz,

I like what you have here. I agree that each transpiler should be opt in and as generic as possible. I have two questions:

    1. Where would tns enable xyz store your preference? package.json presumably? I ask because if that's the case, I'm wondering if the tns enable command is needed at all. Couldn't you just add something to your package.json to enable the transpiler during prepare?
    1. As a user where would you include transpiler-specific configuration—version, flags, etc? I'm assuming this would be in the package.json as well, but it'd be nice to see a specific example of what that might look like.

@ligaz
Copy link
Author

ligaz commented May 21, 2015

Straight to the points:

  1. We need a dedicated command because we want to add the actual code that will do the transpilation as project level hook. This means that the files will reside in the /hooks folder of your project. This cannot (easily and in a maintainable way) happen with pure npm install invocation.
  2. This is transpiler specific. For example TypeScript have tsconfig.json file. Babel does not have something like this but we can introduce one if needed.

@emiloberg
Copy link

Babel does indeed, it has the babelrc.

@atanasovg
Copy link
Contributor

I really love the generic approach - this is the architectural way to go. Having specific implementations for specific transpilers was my major concern when we discussed this internally.

@jamlfy
Copy link

jamlfy commented May 28, 2015

I think it's not such a good idea, to have a compiler, or a command such @ligaz says. I think it's best to use the hooks of NPM, which are

  • pre-acction
  • acction
  • post-acction

This way you can make use of any javascript compiler, since only one command is required. And Hooks are being used, which serve to make many more features, such use Jade-Lang to build XML

@moll
Copy link

moll commented Aug 1, 2015

Is there a point in spending resources on reinventing the build pipeline? I've set mine up with Browserify and Jade's CLI through a no-frills Makefile outside of tns. Others might want to go the JS route, Webpack route or pick any of the other build systems.

I'd rather ask why is tns necessary and if possible, make its use seldom. Fix things at the right level, delegate to existing solutions and focus on the unique bits. And say no a lot. ;-)

@ligaz
Copy link
Author

ligaz commented Aug 1, 2015

Hey @moll,

I completely agree that there are multiple way you can accomplish the same using whatever tools make sense for your scenario. However many newcomers are "afraid" of the learning curve of setting up those asset pipelines and would prefer something to get them started easily and without much hassle.

Nevertheless whatever we do here will not be re-inventing the tools that are already there. We would just try to provide sane defaults that work great out of the box.

@jamiebuilds
Copy link

Babel contrib here, if you need anything from us, feel free to let me know. Happy to help!

@teobugslayer teobugslayer removed this from the 1.3.0 milestone Aug 13, 2015
@teobugslayer teobugslayer added this to the 1.4.0 milestone Sep 8, 2015
@ligaz ligaz modified the milestones: 1.4.0 (Under review), 1.5.0 Sep 17, 2015
@tailsu
Copy link
Contributor

tailsu commented Nov 3, 2015

TypeScript and Babel support is now available through hooks:

$ tns install typescript

and

$ tns install babel

The above commands will install nativescript-dev-typescript and nativescript-dev-babel which in turn install the necessary hooks into your project.

Check it out!

@vchimev
Copy link

vchimev commented Nov 11, 2015

@tailsu,

I just checked out the TypeScript support and noticed that the .ts and .d.ts files are copied into the native app and respectively included into the produced .apk/.ipa.

@tailsu
Copy link
Contributor

tailsu commented Nov 11, 2015

Yes, that's not yet implemented. The relevant issue is #887 .

@vchimev
Copy link

vchimev commented Nov 11, 2015

For any issues and further discussions please refer to #1117.

@toddanglin
Copy link

The original issue specifically discussed support for SASS and LESS, too.

Are there still plans to make those two additional CSS transpilers available "out of the box" similar to Babel/TypeScript today?

EDIT:
Seems a transpiler for LESS is already available: https://github.com/NativeScript/nativescript-dev-less
Works with the expected command:

$ tns install less

EDIT 2:
Using the LESS transpiler as a start, I threw together a plugin for SASS: https://github.com/toddanglin/nativescript-dev-sass. Works with the same command:

$ tns install sass

There are also now compiler plugins for Jade, CoffeeScript and XML-XSD. For future reference, growing list of available compiler plugins that work with $ tns install [NAME] can be seen here: https://www.npmjs.com/search?q=nativescript-dev

@pjbonestroo
Copy link

For writing complex Javascript applications, where code readability is important, I use Python as main language, compiling to Javascript using Transcrypt (https://www.transcrypt.org/). Generally this means development of single page applications.

It would be great if NativeScript can use such generated Javascript (single file), so that one can develop native apps using Python.
Is there any chance something like this is possible in the future?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests