Skip to content
This repository has been archived by the owner on Feb 19, 2020. It is now read-only.

Commit

Permalink
Add typescript package plus some initial runtime tests
Browse files Browse the repository at this point in the history
This is part of the effort to create an official TypeScript compiler:
Urigo/angular2-meteor#89
Urigo/angular2-meteor#90
Urigo/angular2-meteor#102
  • Loading branch information
barbatus committed Feb 15, 2016
1 parent 6f43add commit 6698063
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 486 deletions.
112 changes: 4 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,109 +1,5 @@
## TypeScript packaged for Meteor.
TypeScript API that is adapted to be used in Meteor packages.

### Getting Started
Install package `meteor add barbatus:typescript` and start using TypeScript right away, e.g.:
````js
let result = TypeScript.transpile(fileContent, {
compilerOptions: {module: 'system'},
typings: ['typings/angular2.d.ts'], // typings that will be compiled together with the given content.
filePath: some_path, // file path relative to the app root.
moduleName: some_module // set module name if you want to use ES6 modules.
})
````
Package's API strives to be close to the Babel package's [one](https://atmospherejs.com/meteor/babel-compiler).

### API
#### TypeScript.transpileFiles(files, options, fileReadyCallback)
**`files`** param is expected to be Meteor's file objects in a compiler plugin.
Method eventually uses only `getContentsAsString()` inside,
access to other file properties can be defined in the **`options`**.

**`options`** should have the following structure:
````js
{
compilerOptions: Object,
typings?: Array<String>,
filePath: file => file.getPathInPackage()
moduleName: file => getModuleName(file)
}
````
`compilerOptions` TypeScript compiler options. See the next paragraph for detailed description.

`typings` (optional) is expected be an array of declaration file paths. If provided, these files will be compiled together with .ts-files, thus,
eliminating the need to use `/// <reference path='...' />` syntax.

`filePath` field is expected to be a function that gets in a file object and return its file path.
Field is **required**.

`moduleName` (optional) field. If you want to use modules, you set the `module` field of the `compilerOptions` (e.g., `compilerOptions.module = ts.ModuleKind.System`) and define a `moduleName` function that gets in a file and retuns its module name.

**`fileReadyCallback`** — callback that is being executed each time file transpilation is completed.

Callback's params are defined as follows:
````js
TypeScript.transpileFiles(files, options,
(file, referencedPaths, diagnostics, resultData) => {

})
````

`file` — file that has been compiled.

`referencedPaths` — paths of the TypeScript modules and declaration files that current file uses.
Param is useful for Meteor compilers since allows to watch for changes in the file's dependencies and re-compile file when used methods or classes have been changed.

`diagnostics` — an diagnostics object that provides diagnostics of the file transpilation.
Structure of this object is described below.

`resultData` — result of the transpilation:
````js
{
path: filePath, // normalized relative path of the transpiled file (no ./, ../ and \ inside)
data: content, // transpiled content
sourceMap: sourceMapContent // source map content
}
```
`sourceMap` is not null only if you set `sourceMap: true` in the `compilerOptions`.

#### TypeScript.transpile(fileContent, options);
Same as `transpileFiles` but only for one file's string content. String content can be taken by file API's method
`file.getContentsAsString()`.

Method returns a result object of the following structure:
````js
{
data: content,
sourceMap: sourceMapContent,
referencedPaths: filePaths, // file paths of other modules and declaration files
diagnostics: diagnosticsObject
}
````

### Compiler Options
Compiler options are pretty much the same as described [here](https://github.com/Microsoft/TypeScript/wiki/Compiler-Options) with few exceptions.

Package restricts usage of options that potentially conflics or not supported by the API.

Namely options that are set to false for time being are `declaration`, `project`, `watch` (file changes watch is expected to done via Meteor plugins), `inlineSourceMap`, `inlineSources`, `outDir`, `outFile`, `rootDir`, `sourceRoot`.

Package also extends usage of some of the options for the API. For example, if you set ``noResolve`` to true `referencedFiles` array will be always empty.

### Diagnostics
As now diagnostics object consists of two properties: syntactic and semantic.

Syntactic is an array of all syntax errors. Semantic is an array of basically all type-cheking erros including unresolved modules errors, missing variables etc.
In the future versions semactic errors are expected to be structured into more parts for convenience.

Diagnostics object has two convenient methods for logging out error details to the terminal, e.g.:
````js
diagnostics.logSyntactic(); // prints all syntactic errors
diagnostics.logSemantic(); // prints all semantics errors
// Or you can
diagnostics.semantic.forEach(message => ...); // iterate semantic messages
diagnostics.syntactic.forEach(message => ...); // iterate syntactic messages
````

### Example of Usage
You can check out TS caching [compiler](https://github.com/barbatus/angular2/blob/master/packages/ts-compilers/compilers/ts_caching_compiler.js) where this package is used.
## TypeScript

This package adds TypeScript to Meteor projects.
TypeScript syntax are transpiled into ECMAScript 3 (for older browsers compatibility) and
CommonJS modules by default.
35 changes: 16 additions & 19 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
Package.describe({
name: 'barbatus:typescript',
version: '0.1.5',
summary: 'TypeScript Package for Meteor',
git: 'https://github.com/barbatus/angular2/packages/typescript',
name: 'typescript',
version: '0.1.0',
summary: 'TypeScript for Meteor',
git: 'https://github.com/barbatus/typescript',
documentation: 'README.md'
});

Npm.depends({
'typescript': '1.7.5'
Package.registerBuildPlugin({
name: 'typescript',
use: ['typescript-compiler'],
sources: ['plugin.js']
});

var server = 'server';

Package.onUse(function(api) {
api.versionsFrom('1.2.0.1');
api.use('isobuild:[email protected]');
api.use('typescript-compiler');

api.use([
'[email protected]',
'[email protected]',
'[email protected]'
], server);
api.addFiles(['typescript-helpers.js']);

api.addFiles([
'typescript.js'
], server);
api.export(['__extends', '__decorate', '__metadata', '__param', '__awaiter']);

api.export(['TypeScript'], server);
api.imply('promise');
});

Package.onTest(function(api) {
api.use('tinytest');
api.use('barbatus:typescript');
api.use('typescript');

api.addFiles('runtime-tests.ts');
});
9 changes: 9 additions & 0 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Plugin.registerCompiler({
extensions: ['ts'],
filename: ['tsconfig.json']
}, function () {
return new TypeScriptCompiler({
// We define own helpers.
noEmitHelpers: true
});
});
14 changes: 14 additions & 0 deletions runtime-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Tinytest.add('typescript - runtime - decorators', (test) => {
{
function classDecorator() {
return function(cls) {
cls.prototype.foo = 'foo';
};
}

@classDecorator()
class Foo {}

test.equal((new Foo()).foo, 'foo');
}
});
36 changes: 36 additions & 0 deletions typescript-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// There is no no-global helpers available for TypeScript as now,
// check out this issue https://github.com/Microsoft/TypeScript/issues/3364.
// In order to avoid generating them for each ts-file, they are added here
// while TypeScript always runs with noEmitHelpers = true.
// It might be useful as well if we need to override some of them
// to support old browsers.

__extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};

__decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};

__metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};

__param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};

__awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};
Loading

0 comments on commit 6698063

Please sign in to comment.