Skip to content

Commit 690ca51

Browse files
committed
feat(all): convert to TypeScript
1 parent 0a2acad commit 690ca51

File tree

9 files changed

+122
-66
lines changed

9 files changed

+122
-66
lines changed

build/babel-options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
"es7.classProperties"
2222
],
2323
plugins: [
24-
"babel-dts-generator"
24+
//"babel-dts-generator"
2525
],
2626
extra: {
2727
dts: {

build/paths.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ var fs = require('fs');
33

44
var appRoot = 'src/';
55
var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
6+
var outputRoot = 'dist/';
67

78
module.exports = {
89
root: appRoot,
10+
tsSource: outputRoot + pkg.name + '.ts',
11+
jspmDefinitions: 'jspm_packages/**/*.d.ts',
12+
typings: 'typings/**/*.d.ts',
913
source: appRoot + '**/*.js',
1014
html: appRoot + '**/*.html',
1115
style: 'styles/**/*.css',
12-
output: 'dist/',
16+
output: outputRoot,
1317
doc:'./doc',
1418
e2eSpecsSrc: 'test/e2e/src/*.js',
1519
e2eSpecsDist: 'test/e2e/dist/',

build/tasks/build.js

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,83 @@
11
var gulp = require('gulp');
22
var runSequence = require('run-sequence');
33
var to5 = require('gulp-babel');
4+
var sourcemaps = require('gulp-sourcemaps');
45
var paths = require('../paths');
56
var compilerOptions = require('../babel-options');
67
var assign = Object.assign || require('object.assign');
78
var rename = require('gulp-rename');
8-
9+
var merge = require('merge2');
910
var jsName = paths.packageName + '.js';
11+
var ts = require('gulp-typescript');
12+
var tsProject = ts.createProject('tsconfig.json', {
13+
typescript: require('typescript')
14+
});
1015

11-
gulp.task('build-index', function(){
12-
return gulp.src(paths.root + 'index.js')
13-
.pipe(rename(jsName))
16+
// copies src/index.ts to dist/<package-name>.ts
17+
gulp.task('build-index', function() {
18+
return gulp.src(paths.root + 'index.ts')
19+
.pipe(rename(function (file) { file.basename = paths.packageName; }))
1420
.pipe(gulp.dest(paths.output));
1521
});
1622

23+
// gulp-typescript compiles TS files into ES6
24+
gulp.task('build-ts', function () {
25+
var tsResult = gulp.src([paths.tsSource, paths.typings, paths.jspmDefinitions])
26+
.pipe(sourcemaps.init())
27+
.pipe(ts(tsProject));
28+
return merge([
29+
tsResult.js
30+
.pipe(sourcemaps.write({includeContent: false, sourceRoot: paths.sourceMapRelativePath}))
31+
.pipe(gulp.dest(paths.output)),
32+
tsResult.dts
33+
.pipe(gulp.dest(paths.output))
34+
]);
35+
});
36+
37+
function buildModule(moduleType, dirName) {
38+
return gulp.src(paths.output + jsName)
39+
.pipe(sourcemaps.init({loadMaps: true}))
40+
.pipe(to5(assign({}, compilerOptions, { modules: moduleType })))
41+
.pipe(sourcemaps.write({includeContent: true}))
42+
.pipe(gulp.dest(paths.output + dirName));
43+
}
44+
45+
// copies js file from ts output for ES6 build
1746
gulp.task('build-es6', function () {
1847
return gulp.src(paths.output + jsName)
1948
.pipe(gulp.dest(paths.output + 'es6'));
2049
});
2150

51+
// builds js file as ES5 with CommonJS module
2252
gulp.task('build-commonjs', function () {
23-
return gulp.src(paths.output + jsName)
24-
.pipe(to5(assign({}, compilerOptions, {modules:'common'})))
25-
.pipe(gulp.dest(paths.output + 'commonjs'));
53+
return buildModule('common', 'commonjs');
2654
});
2755

56+
// builds js file as ES5 with AMD module
2857
gulp.task('build-amd', function () {
29-
return gulp.src(paths.output + jsName)
30-
.pipe(to5(assign({}, compilerOptions, {modules:'amd'})))
31-
.pipe(gulp.dest(paths.output + 'amd'));
58+
return buildModule('amd', 'amd');
3259
});
3360

61+
// builds js file as ES5 with SystemJS module
3462
gulp.task('build-system', function () {
35-
return gulp.src(paths.output + jsName)
36-
.pipe(to5(assign({}, compilerOptions, {modules:'system'})))
37-
.pipe(gulp.dest(paths.output + 'system'));
63+
return buildModule('system', 'system');
3864
});
3965

40-
gulp.task('build-dts', function(){
41-
return gulp.src(paths.output + paths.packageName + '.d.ts')
42-
.pipe(rename(paths.packageName + '.d.ts'))
43-
.pipe(gulp.dest(paths.output + 'es6'))
44-
.pipe(gulp.dest(paths.output + 'commonjs'))
45-
.pipe(gulp.dest(paths.output + 'amd'))
46-
.pipe(gulp.dest(paths.output + 'system'));
66+
// copies the ts declaration file for each module build
67+
gulp.task('build-dts', function() {
68+
return gulp.src([paths.output + paths.packageName + '.d.ts', paths.typings])
69+
.pipe(gulp.dest(paths.output + 'es6'))
70+
.pipe(gulp.dest(paths.output + 'commonjs'))
71+
.pipe(gulp.dest(paths.output + 'amd'))
72+
.pipe(gulp.dest(paths.output + 'system'));
4773
});
4874

75+
// main build task
4976
gulp.task('build', function(callback) {
5077
return runSequence(
5178
'clean',
5279
'build-index',
80+
'build-ts',
5381
['build-es6', 'build-commonjs', 'build-amd', 'build-system'],
5482
'build-dts',
5583
callback

doc/core-js.d.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

jsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"module": "system"
5+
}
6+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,22 @@
4949
"gulp-bump": "^0.3.1",
5050
"gulp-eslint": "^1.0.0",
5151
"gulp-rename": "^1.2.2",
52+
"gulp-sourcemaps": "^1.6.0",
5253
"gulp-typedoc": "^1.2.1",
5354
"gulp-typedoc-extractor": "^0.0.8",
55+
"gulp-typescript": "^2.9.2",
5456
"jasmine-core": "^2.1.3",
5557
"karma": "^0.13.15",
5658
"karma-babel-preprocessor": "^5.2.2",
5759
"karma-chrome-launcher": "^0.1.7",
5860
"karma-coverage": "^0.3.1",
5961
"karma-jasmine": "^0.3.5",
6062
"karma-jspm": "^2.0.1",
63+
"merge2": "^0.3.6",
6164
"object.assign": "^1.0.3",
6265
"require-dir": "^0.1.0",
6366
"run-sequence": "^1.0.2",
67+
"typescript": "^1.6.2",
6468
"vinyl": "^0.5.1",
6569
"vinyl-paths": "^1.0.0",
6670
"yargs": "^2.1.1"

src/index.js renamed to src/index.ts

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import * as LogManager from 'aurelia-logging';
33
const logger = LogManager.getLogger('event-aggregator');
44

55
class Handler {
6-
constructor(messageType, callback) {
7-
this.messageType = messageType;
8-
this.callback = callback;
6+
constructor(public messageType: any, public callback: (message: any) => void) {
97
}
108

11-
handle(message) {
9+
handle(message: any) {
1210
if (message instanceof this.messageType) {
1311
this.callback.call(null, message);
1412
}
@@ -18,7 +16,7 @@ class Handler {
1816
/**
1917
* Represents a disposable subsciption to an EventAggregator event.
2018
*/
21-
interface Subscription {
19+
export interface Subscription {
2220
/**
2321
* Disposes the subscription.
2422
*/
@@ -28,23 +26,24 @@ interface Subscription {
2826
/**
2927
* Enables loosely coupled publish/subscribe messaging.
3028
*/
31-
export class EventAggregator {
29+
export class EventAggregator {
30+
private eventLookup: any = {};
31+
private messageHandlers: Handler[] = [];
32+
3233
/**
33-
* Creates an instance of the EventAggregator class.
34+
* Publishes a message.
35+
* @param event The event data type to publish to.
3436
*/
35-
constructor() {
36-
this.eventLookup = {};
37-
this.messageHandlers = [];
38-
}
39-
37+
publish<T>(event: T): void;
4038
/**
4139
* Publishes a message.
42-
* @param event The event or channel to publish to.
40+
* @param event The message channel to publish to.
4341
* @param data The data to publish on the channel.
4442
*/
45-
publish(event: string | any, data?: any): void {
46-
let subscribers;
47-
let i;
43+
publish(event: string, data?: any): void;
44+
publish(event: any, data?: any): void {
45+
let subscribers: any[];
46+
let i: number;
4847

4948
if (typeof event === 'string') {
5049
subscribers = this.eventLookup[event];
@@ -76,12 +75,19 @@ export class EventAggregator {
7675

7776
/**
7877
* Subscribes to a message channel or message type.
79-
* @param event The event channel or event data type.
80-
* @param callback The callback to be invoked when when the specified message is published.
78+
* @param event The message data Type to subscribe to.
79+
* @param callback The callback to be invoked when the specified message is published.
80+
*/
81+
subscribe<T>(event: Constructor<T>, callback: (message: T) => void): Subscription;
82+
/**
83+
* Subscribes to a message channel or message type.
84+
* @param event The message channel to subscribe to.
85+
* @param callback The callback to be invoked when the specified message is published.
8186
*/
82-
subscribe(event: string | Function, callback: Function): Subscription {
83-
let handler;
84-
let subscribers;
87+
subscribe(event: string, callback: (message: any, event?: string) => void): Subscription;
88+
subscribe(event: string|Constructor<any>, callback: (message: any, event?: string) => void): Subscription {
89+
let handler: Function|Handler;
90+
let subscribers: any[];
8591

8692
if (typeof event === 'string') {
8793
handler = callback;
@@ -104,12 +110,19 @@ export class EventAggregator {
104110
}
105111

106112
/**
107-
* Subscribes to a message channel or message type, then disposes the subscription automatically after the first message is received.
108-
* @param event The event channel or event data type.
113+
* Subscribes to a message type, then disposes the subscription automatically after the first message is received.
114+
* @param event The message data Type to subscribe to.
109115
* @param callback The callback to be invoked when when the specified message is published.
110116
*/
111-
subscribeOnce(event: string | Function, callback: Function): Subscription {
112-
let sub = this.subscribe(event, (a, b) => {
117+
subscribeOnce<T>(event: Constructor<T>, callback: (message: T) => void): Subscription;
118+
/**
119+
* Subscribes to a message channel, then disposes the subscription automatically after the first message is received.
120+
* @param event The message channel to subscribe to.
121+
* @param callback The callback to be invoked when when the specified message is published.
122+
*/
123+
subscribeOnce(event: string, callback: (message: any, event?: string) => void): Subscription;
124+
subscribeOnce(event: string|Constructor<any>, callback: (message: any, event?: string) => void): Subscription {
125+
let sub = this.subscribe(<any>event, (a, b) => {
113126
sub.dispose();
114127
return callback(a, b);
115128
});
@@ -122,28 +135,18 @@ export class EventAggregator {
122135
* Includes EA functionality into an object instance.
123136
* @param obj The object to mix Event Aggregator functionality into.
124137
*/
125-
export function includeEventsIn(obj: Object): EventAggregator {
138+
export function includeEventsIn(obj: any): EventAggregator {
126139
let ea = new EventAggregator();
127-
128-
obj.subscribeOnce = function(event, callback) {
129-
return ea.subscribeOnce(event, callback);
130-
};
131-
132-
obj.subscribe = function(event, callback) {
133-
return ea.subscribe(event, callback);
134-
};
135-
136-
obj.publish = function(event, data) {
137-
ea.publish(event, data);
138-
};
139-
140+
obj.subscribeOnce = (event: any, callback: any) => ea.subscribeOnce(event, callback);
141+
obj.subscribe = (event: any, callback: any) => ea.subscribe(event, callback);
142+
obj.publish = (event: any, data?: any) => ea.publish(event, data);
140143
return ea;
141144
}
142145

143146
/**
144147
* Configures a global EA by merging functionality into the Aurelia instance.
145148
* @param config The Aurelia Framework configuration object used to configure the plugin.
146149
*/
147-
export function configure(config: Object): void {
150+
export function configure(config: any): void {
148151
config.instance(EventAggregator, includeEventsIn(config.aurelia));
149152
}

tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES6",
4+
"declaration": true,
5+
"noImplicitAny": true,
6+
"noEmitOnError": true,
7+
"sourceMap": true
8+
},
9+
"exclude": [
10+
"node_modules"
11+
]
12+
}

typings/Interfaces.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Constructor<T> {
2+
new (...args: any[]): T;
3+
}

0 commit comments

Comments
 (0)