-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdev.js
55 lines (48 loc) · 1.42 KB
/
dev.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
#!/usr/bin/env node
const historyFallback = require('connect-history-api-fallback');
const browserSync = require('browser-sync').create();
const ts = require('typescript');
const configPath = ts.findConfigFile("./", ts.sys.fileExists, "tsconfig.json");
if (!configPath) throw new Error("Could not find a valid 'tsconfig.json' file");
browserSync.init({
files: ['src/**/*', '!src/**/*.ts'],
server: {
baseDir: 'src',
middleware: [historyFallback()],
},
});
const tscHost = ts.createWatchCompilerHost(
configPath,
{},
ts.sys,
ts.createEmitAndSemanticDiagnosticsBuilderProgram,
reportDiagnostic,
reportWatchStatusChanged
);
const tsc = ts.createWatchProgram(tscHost);
/**
* @param {ts.Diagnostic} diagnostic
*/
function reportDiagnostic(diagnostic) {
console.error(
"Error",
diagnostic.code,
":",
ts.flattenDiagnosticMessageText(
diagnostic.messageText,
() => ts.sys.newLine
)
);
}
/**
* Prints a diagnostic every time the watch status changes.
* This is mainly for messages like "Starting compilation" or "Compilation completed".
* @param {ts.Diagnostic} diagnostic
*/
function reportWatchStatusChanged(diagnostic) {
console.info(ts.formatDiagnostic(diagnostic, {
getCanonicalFileName: path => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine
}));
}