Skip to content

Commit 80c0425

Browse files
committed
Change to use incremental builds API
Add debug and time logging Minor re-factoring Update unit tests Urigo/angular2-meteor#102
1 parent 4cac0d7 commit 80c0425

File tree

7 files changed

+124
-75
lines changed

7 files changed

+124
-75
lines changed

debug.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const util = Npm.require('util');
2+
3+
DebugLog = class DebugLog {
4+
constructor(event) {
5+
this.debug = process.env.TYPESCRIPT_DEBUG;
6+
this.event = event;
7+
this.start();
8+
}
9+
10+
start() {
11+
if (this.debug) {
12+
console.log('%s started', this.event);
13+
console.time(util.format('%s time', this.event));
14+
}
15+
}
16+
17+
18+
log(msg, ...args) {
19+
if (this.debug) {
20+
console.log.apply(null, [msg].concat(args));
21+
}
22+
}
23+
24+
end() {
25+
if (this.debug) {
26+
console.timeEnd(util.format('%s time', this.event));
27+
}
28+
}
29+
};

file-mixin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FileMixin = {
2+
warn(error) {
3+
console.log(`${error.sourcePath} (${error.line}, ${error.column}): ${error.message}`);
4+
},
5+
6+
isBare() {
7+
let fileOptions = this.getFileOptions();
8+
return fileOptions.bare;
9+
}
10+
};

package.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Package.describe({
22
name: 'barbatus:typescript-compiler',
3-
version: '0.5.0-beta.5',
3+
version: '0.5.0-beta.6',
44
summary: 'TypeScript Compiler for Meteor',
55
git: 'https://github.com/barbatus/ts-compilers',
66
documentation: 'README.md'
77
});
88

99
Npm.depends({
10-
'meteor-typescript': '0.6.0-beta.2',
10+
'meteor-typescript': 'https://github.com/barbatus/meteor-typescript/tarball/52fc6c7f3b5b5df9483a927bc96e7a901b61c4ab',
1111
'async': '1.4.0'
1212
});
1313

@@ -19,6 +19,8 @@ Package.onUse(function(api) {
1919
], 'server');
2020

2121
api.addFiles([
22+
'debug.js',
23+
'file-mixin.js',
2224
'typescript-compiler.js',
2325
'typescript.js'
2426
], 'server');

tests/server/unit/compiler-tests_spec.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ describe('typescript-compiler', () => {
4747

4848
describe('testing diagnostics', () => {
4949
it('should log out diagnostics by default', () => {
50-
let logger = jasmine.createSpy();
51-
let compiler = new TypeScriptCompiler(null, null, logger);
50+
let compiler = new TypeScriptCompiler();
5251

5352
let configFile = new ConfigFile({
5453
compilerOptions: {
@@ -57,18 +56,17 @@ describe('typescript-compiler', () => {
5756
});
5857
let wrongImport = 'import {api} from "lib";';
5958
let inputFile = new InputFile(wrongImport, 'foo4.ts');
59+
inputFile.warn = jasmine.createSpy();
6060
compiler.processFilesForTarget([inputFile, configFile]);
6161

62-
expect(logger).toHaveBeenCalled();
63-
expect(logger.calls.first().args[0]).toBeDefined();
62+
expect(inputFile.warn).toHaveBeenCalled();
63+
expect(inputFile.warn.calls.first().args[0]).toBeDefined();
6464
});
6565
});
6666

6767
describe('testing modules', () => {
68-
it('should render bare source code if module is none', () => {
69-
let compiler = new TypeScriptCompiler({
70-
module: 'none'
71-
});
68+
it('should render bare source code if there is no ES6 exports', () => {
69+
let compiler = new TypeScriptCompiler();
7270
let moduleFoo = 'module foo {}';
7371
let inputFile = new InputFile(moduleFoo, 'foo5.ts');
7472
compiler.processFilesForTarget([inputFile]);

tests/server/unit/input-file.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ InputFile = class InputFile {
4444
getArch() {
4545
return 'os';
4646
}
47+
48+
warn(error) {
49+
this.error = error;
50+
}
4751
}
4852

4953
ConfigFile = class ConfigFile extends InputFile {

typescript-compiler.js

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,106 @@
1-
'use strict';
2-
31
const async = Npm.require('async');
42
const Future = Npm.require('fibers/future');
3+
const TSBuild = Npm.require('meteor-typescript').TSBuild;
54

65
TypeScriptCompiler = class TypeScriptCompiler {
7-
constructor(extraOptions, maxParallelism, logFn) {
6+
constructor(extraOptions, maxParallelism) {
87
TypeScript.validateExtraOptions(extraOptions);
98

109
this.extraOptions = extraOptions;
1110
this.maxParallelism = maxParallelism || 10;
12-
this.tsconfig = null;
11+
this.tsconfig = TypeScript.getDefaultOptions();
1312
this.cfgHash = null;
14-
this.logFn = logFn || console.log;
1513
}
1614

1715
processFilesForTarget(inputFiles) {
16+
this.extendFiles(inputFiles);
17+
1818
// If tsconfig.json has changed, create new one.
1919
this.processConfig(inputFiles);
2020

21-
let filesSourceMap = new Map();
21+
let archMap = {};
22+
let filesMap = {};
2223
inputFiles.forEach((inputFile, index) => {
2324
if (this.isConfigFile(inputFile)) return;
2425

25-
filesSourceMap.set(this.getExtendedPath(inputFile), index);
26+
let arch = inputFile.getArch();
27+
let archFiles = archMap[arch];
28+
if (! archFiles) {
29+
archFiles = [];
30+
archMap[arch] = archFiles;
31+
}
32+
archFiles.push(inputFile);
33+
filesMap[this.getExtendedPath(inputFile)] = index;
2634
});
35+
2736
let getFileContent = filePath => {
28-
let index = filesSourceMap.get(filePath);
37+
let index = filesMap[filePath];
2938
return index !== undefined ?
3039
inputFiles[index].getContentsAsString() : null;
3140
};
3241

33-
// Filters out typings and tsconfig.
34-
// Other files should be compiled.
35-
let tsFiles = inputFiles.filter(inputFile =>
36-
!(this.isConfigFile(inputFile) || this.isDeclarationFile(inputFile)));
42+
// Assemble options.
43+
let typings = this.tsconfig.typings;
44+
let compilerOptions = this.tsconfig.compilerOptions;
45+
compilerOptions = TypeScript.getCompilerOptions(
46+
compilerOptions, this.extraOptions);
47+
let buildOptions = { compilerOptions, typings };
3748

49+
let compileDebug = new DebugLog('compilation');
3850
const future = new Future;
39-
async.eachLimit(tsFiles, this.maxParallelism, (inputFile, cb) => {
40-
let compilerOptions = this.tsconfig ?
41-
this.tsconfig.compilerOptions : null;
42-
43-
compilerOptions = TypeScript.getCompilerOptions(
44-
compilerOptions, this.extraOptions);
45-
46-
let source = inputFile.getContentsAsString();
47-
let inputFilePath = inputFile.getPathInPackage();
48-
let outputFilePath = removeTsExt(inputFilePath) + '.js';
49-
let toBeAdded = {
50-
sourcePath: inputFilePath,
51-
path: outputFilePath,
52-
data: source,
53-
hash: inputFile.getSourceHash(),
54-
sourceMap: null,
55-
bare: this.isBareFile(inputFile, compilerOptions)
56-
};
57-
58-
let filePath = this.getExtendedPath(inputFile);
59-
let typings = this.tsconfig ? this.tsconfig.typings : [];
60-
let moduleName = this.getFileModuleName(inputFile, compilerOptions);
61-
let arch = inputFile.getArch();
62-
let tsOptions = {
63-
compilerOptions,
64-
moduleName,
65-
filePath,
66-
typings,
67-
arch
68-
};
69-
70-
let error = null;
71-
try {
72-
let result = TypeScript.compile(getFileContent, tsOptions);
73-
this.processDiagnostics(inputFile,
74-
result.diagnostics, compilerOptions);
51+
async.each(_.keys(archMap), (arch, cb) => {
52+
let archFiles = archMap[arch];
53+
let filePaths = archFiles.map(inputFile => this.getExtendedPath(inputFile));
54+
compileDebug.log('process files: %s', filePaths);
55+
buildOptions.arch = arch;
56+
57+
let buildDebug = new DebugLog('tsBuild');
58+
let tsBuild = new TSBuild(filePaths, getFileContent, buildOptions);
59+
60+
archFiles.forEach(inputFile => {
61+
if (this.isDeclarationFile(inputFile)) return;
62+
63+
let co = compilerOptions;
64+
let source = inputFile.getContentsAsString();
65+
let inputFilePath = inputFile.getPathInPackage();
66+
let outputFilePath = removeTsExt(inputFilePath) + '.js';
67+
let toBeAdded = {
68+
sourcePath: inputFilePath,
69+
path: outputFilePath,
70+
data: source,
71+
hash: inputFile.getSourceHash(),
72+
sourceMap: null,
73+
bare: inputFile.isBare()
74+
};
75+
76+
let filePath = this.getExtendedPath(inputFile);
77+
let moduleName = this.getFileModuleName(inputFile, co);
78+
79+
let emitDebug = new DebugLog('tsEmit');
80+
let result = tsBuild.emit(filePath, moduleName);
81+
this.processDiagnostics(inputFile, result.diagnostics, co);
82+
emitDebug.end();
7583

7684
toBeAdded.data = result.code;
85+
toBeAdded.bare = toBeAdded.bare || ! result.isExternal;
7786
toBeAdded.hash = result.hash;
7887
toBeAdded.sourceMap = result.sourceMap;
7988

8089
inputFile.addJavaScript(toBeAdded);
81-
} catch (e) {
82-
error = e;
83-
} finally {
84-
cb(error);
85-
}
90+
});
91+
92+
cb();
93+
94+
buildDebug.end();
8695
}, future.resolver());
96+
8797
future.wait();
98+
99+
compileDebug.end();
100+
}
101+
102+
extendFiles(inputFiles) {
103+
inputFiles.forEach(inputFile => _.defaults(inputFile, FileMixin));
88104
}
89105

90106
processDiagnostics(inputFile, diagnostics, compilerOptions) {
@@ -104,13 +120,12 @@ TypeScriptCompiler = class TypeScriptCompiler {
104120
// And log out other errors except package files.
105121
if (compilerOptions && compilerOptions.diagnostics) {
106122
diagnostics.semanticErrors.forEach(diagnostic => {
107-
let error = {
123+
inputFile.warn({
108124
message: diagnostic.message,
109125
sourcePath: this.getExtendedPath(inputFile),
110126
line: diagnostic.line,
111127
column: diagnostic.column
112-
};
113-
this.logFn(`${error.sourcePath} (${error.line}, ${error.column}): ${error.message}`);
128+
});
114129
});
115130
}
116131
}
@@ -125,13 +140,6 @@ TypeScriptCompiler = class TypeScriptCompiler {
125140
return noExt ? removeTsExt(filePath) : filePath;
126141
}
127142

128-
isBareFile(inputFile, compilerOptions) {
129-
let fileOptions = inputFile.getFileOptions();
130-
let packageName = inputFile.getPackageName();
131-
return (fileOptions.bare ||
132-
(! packageName && compilerOptions.module === 'none'));
133-
}
134-
135143
getFileModuleName(inputFile, options) {
136144
return options.module !== 'none' ?
137145
this.getExtendedPath(inputFile, true): null;

typescript.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
const meteorTS = Npm.require('meteor-typescript');
42

53
TypeScript = {

0 commit comments

Comments
 (0)