-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcompile.js
75 lines (62 loc) · 2.19 KB
/
compile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* eslint-env node */
'use strict';
const chokidar = require('chokidar');
const fs = require('fs-extra');
const escapeRegex = require('escape-string-regexp');
const debug = require('debug')('ember-cli-typescript:tsc:trace');
module.exports = function compile(project, tsOptions, callbacks) {
// Ensure the output directory is created even if no files are generated
fs.mkdirsSync(tsOptions.outDir);
let fullOptions = Object.assign({
rootDir: project.root,
allowJs: false,
noEmit: false,
diagnostics: debug.enabled,
extendedDiagnostics: debug.enabled
}, tsOptions);
let ts = project.require('typescript');
let configPath = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json');
let createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;
let host = ts.createWatchCompilerHost(
configPath,
fullOptions,
buildWatchHooks(project, ts.sys, callbacks),
createProgram,
diagnosticCallback(callbacks.reportDiagnostic),
diagnosticCallback(callbacks.reportWatchStatus)
);
if (debug.enabled) {
host.trace = str => debug(str.trim());
}
return ts.createWatchProgram(host);
};
function diagnosticCallback(callback) {
if (callback) {
// The initial callbacks may be synchronously invoked during instantiation of the
// WatchProgram, which is annoying if those callbacks want to _reference_ it, so
// we always force invocation to be asynchronous for consistency.
return (diagnostic) => {
process.nextTick(() => callback(diagnostic));
};
}
}
function buildWatchHooks(project, sys, callbacks) {
let root = escapeRegex(project.root);
let sep = `[/\\\\]`;
let patterns = ['\\..*?', 'dist', 'node_modules', 'tmp'];
let ignored = new RegExp(`^${root}${sep}(${patterns.join('|')})${sep}`);
return Object.assign({}, sys, {
watchFile: null,
watchDirectory(dir, callback) {
if (!fs.existsSync(dir)) return;
let watcher = chokidar.watch(dir, { ignored, ignoreInitial: true });
watcher.on('all', (type, path) => {
callback(path);
if (path.endsWith('.ts') && callbacks.watchedFileChanged) {
callbacks.watchedFileChanged();
}
});
return watcher;
}
});
}