@travetto/compiler 3.0.0-rc.2 → 3.0.0-rc.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -5
- package/__index__.ts +6 -0
- package/bin/trv.js +71 -0
- package/package.json +32 -15
- package/src/compiler.ts +106 -147
- package/src/log.ts +18 -0
- package/src/state.ts +213 -0
- package/src/types.ts +14 -0
- package/src/util.ts +147 -0
- package/src/watch.ts +142 -0
- package/support/compiler-entry.ts +2 -0
- package/support/launcher.ts +139 -0
- package/support/lock-pinger.ts +25 -0
- package/support/lock.ts +237 -0
- package/support/log.ts +31 -0
- package/support/transpile.ts +202 -0
- package/tsconfig.trv.json +23 -0
- package/LICENSE +0 -21
- package/index.ts +0 -3
- package/src/host.ts +0 -142
- package/src/transformer.ts +0 -75
- package/support/dynamic.compiler.ts +0 -72
- package/support/phase.init.ts +0 -13
- package/support/phase.reset.ts +0 -10
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<!-- This file was generated by @travetto/doc and should not be modified directly -->
|
|
2
|
-
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/compiler/
|
|
2
|
+
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/compiler/DOC.ts and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# Compiler
|
|
4
|
-
##
|
|
4
|
+
## Compiler
|
|
5
5
|
|
|
6
6
|
**Install: @travetto/compiler**
|
|
7
7
|
```bash
|
|
@@ -17,9 +17,12 @@ This module expands upon [Typescript](https://typescriptlang.org), with suppleme
|
|
|
17
17
|
* Support for detecting changes in sources files at runtime
|
|
18
18
|
* Allows for hot-reloading of classes during development
|
|
19
19
|
* Utilizes `es2015` `Proxy`s to allow for swapping out implementation at runtime
|
|
20
|
-
|
|
21
20
|
Additionally, there is support for common AST transformations via [Transformation](https://github.com/travetto/travetto/tree/main/module/transformer#readme "Functionality for AST transformations, with transformer registration, and general utils")
|
|
22
|
-
|
|
23
21
|
## Debugging
|
|
24
|
-
|
|
25
22
|
When dealing with transformers, logging is somewhat tricky as the compiler executes before the code is loaded. To that end, the file `compiler.log` is created in the cache directory during the compilation process. This is a location that transformers should be free to log to, for debugging, and any additional feedback.
|
|
23
|
+
|
|
24
|
+
## CLI
|
|
25
|
+
|
|
26
|
+
The module provides the ability to clear the compilation cache to handle any inconsistencies that may arise.
|
|
27
|
+
|
|
28
|
+
TODO: Describe cli behavior
|
package/__index__.ts
ADDED
package/bin/trv.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// @ts-check
|
|
4
|
+
import fs from 'fs/promises';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { createRequire } from 'module';
|
|
7
|
+
|
|
8
|
+
import { getManifestContext } from '@travetto/manifest/bin/context.js';
|
|
9
|
+
|
|
10
|
+
const VALID_OPS = { watch: 'watch', build: 'build', clean: 'clean', manifest: 'manifest' };
|
|
11
|
+
|
|
12
|
+
const COMPILER_FILES = [...['launcher', 'transpile', 'lock', 'log', 'lock-pinger'].map(x => `support/${x}.js`), 'package.json'];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {import('@travetto/manifest').ManifestContext} ctx
|
|
16
|
+
* @return {Promise<import('@travetto/compiler/support/launcher').launch>}
|
|
17
|
+
*/
|
|
18
|
+
const $getLauncher = async (ctx) => {
|
|
19
|
+
const compPkg = createRequire(path.resolve('node_modules')).resolve('@travetto/compiler/package.json');
|
|
20
|
+
const files = [];
|
|
21
|
+
|
|
22
|
+
for (const file of COMPILER_FILES) {
|
|
23
|
+
const target = path.resolve(ctx.workspacePath, ctx.compilerFolder, 'node_modules', '@travetto/compiler', file);
|
|
24
|
+
const src = compPkg.replace('package.json', file.replace(/[.]js$/, '.ts'));
|
|
25
|
+
|
|
26
|
+
const targetTime = await fs.stat(target).then(s => Math.max(s.mtimeMs, s.ctimeMs)).catch(() => 0);
|
|
27
|
+
const srcTime = await fs.stat(src).then(s => Math.max(s.mtimeMs, s.ctimeMs));
|
|
28
|
+
// If stale
|
|
29
|
+
if (srcTime > targetTime) {
|
|
30
|
+
const ts = (await import('typescript')).default;
|
|
31
|
+
const module = ctx.moduleType === 'module' ? ts.ModuleKind.ESNext : ts.ModuleKind.CommonJS;
|
|
32
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
33
|
+
const text = await fs.readFile(src, 'utf8');
|
|
34
|
+
if (file.endsWith('.js')) {
|
|
35
|
+
const content = ts.transpile(
|
|
36
|
+
text,
|
|
37
|
+
{ target: ts.ScriptTarget.ES2020, module, esModuleInterop: true, allowSyntheticDefaultImports: true }
|
|
38
|
+
)
|
|
39
|
+
.replace(/^((?:im|ex)port .*from '[.][^']+)(')/mg, (_, a, b) => `${a}.js${b}`)
|
|
40
|
+
.replace(/^(import [^\n]*from '[^.][^\n/]+[/][^\n/]+[/][^\n']+)(')/mg, (_, a, b) => `${a}.js${b}`);
|
|
41
|
+
await fs.writeFile(target, content, 'utf8');
|
|
42
|
+
} else {
|
|
43
|
+
const pkg = JSON.parse(text);
|
|
44
|
+
pkg.type = ctx.moduleType;
|
|
45
|
+
await fs.writeFile(target, JSON.stringify(pkg, null, 2), 'utf8');
|
|
46
|
+
}
|
|
47
|
+
// Compile
|
|
48
|
+
}
|
|
49
|
+
files.push(target);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
try { return require(files[0]).launch; }
|
|
53
|
+
catch { return import(files[0]).then(x => x.launch); }
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
(async () => {
|
|
57
|
+
const ctx = await getManifestContext(process.env.TRV_COMPILER_CWD);
|
|
58
|
+
const [op, args] = [VALID_OPS[process.argv[2]], process.argv.slice(3)];
|
|
59
|
+
|
|
60
|
+
if (op === 'clean') {
|
|
61
|
+
const folders = process.argv.find(x => x === '--all' || x === '-a') ? [ctx.outputFolder, ctx.compilerFolder] : [ctx.outputFolder];
|
|
62
|
+
for (const f of folders) {
|
|
63
|
+
await fs.rm(path.resolve(ctx.workspacePath, f), { force: true, recursive: true });
|
|
64
|
+
}
|
|
65
|
+
return console.log(`Cleaned ${ctx.workspacePath}: [${folders.join(', ')}]`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const rootCtx = ctx.monoRepo ? await getManifestContext(ctx.workspacePath) : ctx;
|
|
69
|
+
|
|
70
|
+
return (await $getLauncher(ctx))(ctx, rootCtx, op, args);
|
|
71
|
+
})();
|
package/package.json
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/compiler",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "Node-integration of Typescript Compiler with advanced functionality for detecting changes in classes and methods.",
|
|
3
|
+
"version": "3.0.0-rc.20",
|
|
4
|
+
"description": "Compiler",
|
|
6
5
|
"keywords": [
|
|
7
|
-
"tsc",
|
|
8
|
-
"typescript",
|
|
9
6
|
"compiler",
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"real-time",
|
|
13
|
-
"travetto"
|
|
7
|
+
"travetto",
|
|
8
|
+
"typescript"
|
|
14
9
|
],
|
|
15
10
|
"homepage": "https://travetto.io",
|
|
16
11
|
"license": "MIT",
|
|
@@ -18,21 +13,43 @@
|
|
|
18
13
|
"email": "travetto.framework@gmail.com",
|
|
19
14
|
"name": "Travetto Framework"
|
|
20
15
|
},
|
|
16
|
+
"type": "module",
|
|
21
17
|
"files": [
|
|
22
|
-
"
|
|
18
|
+
"__index__.ts",
|
|
23
19
|
"src",
|
|
24
|
-
"
|
|
20
|
+
"bin",
|
|
21
|
+
"support",
|
|
22
|
+
"tsconfig.trv.json"
|
|
25
23
|
],
|
|
26
|
-
"main": "
|
|
24
|
+
"main": "__index__.ts",
|
|
25
|
+
"bin": {
|
|
26
|
+
"trv": "bin/trv.js"
|
|
27
|
+
},
|
|
27
28
|
"repository": {
|
|
28
29
|
"url": "https://github.com/travetto/travetto.git",
|
|
29
30
|
"directory": "module/compiler"
|
|
30
31
|
},
|
|
31
32
|
"dependencies": {
|
|
32
|
-
"@
|
|
33
|
-
"@travetto/
|
|
34
|
-
"@travetto/
|
|
33
|
+
"@parcel/watcher": "^2.1.0",
|
|
34
|
+
"@travetto/manifest": "^3.0.0-rc.13",
|
|
35
|
+
"@travetto/terminal": "^3.0.0-rc.7",
|
|
36
|
+
"@travetto/transformer": "^3.0.0-rc.16"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@travetto/cli": "^3.0.0-rc.15"
|
|
40
|
+
},
|
|
41
|
+
"peerDependenciesMeta": {
|
|
42
|
+
"@travetto/cli": {
|
|
43
|
+
"optional": true
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"travetto": {
|
|
47
|
+
"displayName": "Compiler",
|
|
48
|
+
"profiles": [
|
|
49
|
+
"compile"
|
|
50
|
+
]
|
|
35
51
|
},
|
|
52
|
+
"private": false,
|
|
36
53
|
"publishConfig": {
|
|
37
54
|
"access": "public"
|
|
38
55
|
}
|
package/src/compiler.ts
CHANGED
|
@@ -1,187 +1,146 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { install } from 'source-map-support';
|
|
2
|
+
import timers from 'timers/promises';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
import fs from 'fs/promises';
|
|
4
5
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { ModuleManager } from '@travetto/boot/src/internal/module';
|
|
8
|
-
import { Dynamic } from '@travetto/base/src/internal/dynamic';
|
|
9
|
-
import { TranspileUtil } from '@travetto/boot/src/internal/transpile-util';
|
|
6
|
+
import { GlobalTerminal, TerminalProgressEvent } from '@travetto/terminal';
|
|
7
|
+
import { RootIndex } from '@travetto/manifest';
|
|
10
8
|
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
9
|
+
import { CompilerUtil } from './util';
|
|
10
|
+
import { CompilerState } from './state';
|
|
11
|
+
import { CompilerWatcher } from './watch';
|
|
12
|
+
import { Log } from './log';
|
|
13
|
+
import { CompileEmitError, CompileEmitEvent, CompileEmitter } from './types';
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
type EventType = 'added' | 'removed' | 'changed';
|
|
15
|
+
const PING_THRESHOLD = 1000;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* Compilation
|
|
18
|
+
* Compilation support
|
|
19
19
|
*/
|
|
20
|
-
|
|
21
|
-
class $Compiler {
|
|
22
|
-
|
|
23
|
-
#program: ts.Program | undefined;
|
|
24
|
-
#transformerManager = new TransformerManager();
|
|
25
|
-
#emitter = new EventEmitter();
|
|
26
|
-
#host = new SourceHost();
|
|
27
|
-
|
|
28
|
-
active = false;
|
|
20
|
+
export class Compiler {
|
|
29
21
|
|
|
30
22
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* @param forFile If this file is new, force a recompilation
|
|
23
|
+
* Run compiler as a main entry point
|
|
34
24
|
*/
|
|
35
|
-
|
|
25
|
+
static async main(): Promise<void> {
|
|
26
|
+
const [dirty, watch] = process.argv.slice(2);
|
|
27
|
+
install();
|
|
28
|
+
const dirtyFiles = (await fs.readFile(dirty, 'utf8')).split(/\n/).filter(x => !!x);
|
|
29
|
+
return new Compiler().init(dirtyFiles).then(c => c.run(watch === 'true'));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
#state: CompilerState;
|
|
33
|
+
#dirtyFiles: string[];
|
|
36
34
|
|
|
37
|
-
|
|
35
|
+
async init(dirtyFiles: string[]): Promise<this> {
|
|
36
|
+
this.#state = await CompilerState.get(RootIndex);
|
|
37
|
+
this.#dirtyFiles = dirtyFiles[0] === '*' ?
|
|
38
|
+
this.#state.getAllFiles() :
|
|
39
|
+
dirtyFiles.map(f => this.#state.getBySource(f)!.input);
|
|
40
|
+
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
38
43
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Watches local modules
|
|
46
|
+
*/
|
|
47
|
+
#watchLocalModules(emit: CompileEmitter): Promise<() => Promise<void>> {
|
|
48
|
+
return new CompilerWatcher(this.#state).watchFiles(async file => {
|
|
49
|
+
const err = await emit(file, true);
|
|
50
|
+
if (err) {
|
|
51
|
+
Log.info('Compilation Error', CompilerUtil.buildTranspileError(file, err));
|
|
52
|
+
} else {
|
|
53
|
+
Log.info(`Compiled ${file.split('node_modules/')[1]}`);
|
|
43
54
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
options: TranspileUtil.compilerOptions,
|
|
47
|
-
host: this.#host,
|
|
48
|
-
oldProgram: this.#program
|
|
49
|
-
});
|
|
50
|
-
this.#transformerManager.build(this.#program.getTypeChecker());
|
|
51
|
-
}
|
|
52
|
-
return this.#program;
|
|
55
|
+
return err;
|
|
56
|
+
});
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
/**
|
|
56
|
-
*
|
|
60
|
+
* Compile in a single pass, only emitting dirty files
|
|
57
61
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
console.debug('Emitting', { filename: filename.replace(PathUtil.cwd, '.') });
|
|
62
|
+
async getCompiler(): Promise<CompileEmitter> {
|
|
63
|
+
let program: ts.Program;
|
|
61
64
|
|
|
65
|
+
const emit = async (inputFile: string, needsNewProgram = program === undefined): Promise<CompileEmitError | undefined> => {
|
|
62
66
|
try {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
this.#transformerManager.getTransformers()
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
TranspileUtil.checkTranspileErrors(filename, result.diagnostics);
|
|
67
|
+
if (needsNewProgram) {
|
|
68
|
+
program = this.#state.createProgram(program);
|
|
69
|
+
}
|
|
70
|
+
const result = this.#state.writeInputFile(program, inputFile);
|
|
71
|
+
if (result?.diagnostics?.length) {
|
|
72
|
+
return result.diagnostics;
|
|
73
|
+
}
|
|
74
74
|
} catch (err) {
|
|
75
|
-
if (
|
|
75
|
+
if (err instanceof Error) {
|
|
76
|
+
return err;
|
|
77
|
+
} else {
|
|
76
78
|
throw err;
|
|
77
79
|
}
|
|
78
|
-
const errContent = TranspileUtil.transpileError(filename, err);
|
|
79
|
-
this.#host.contents.set(filename, errContent);
|
|
80
80
|
}
|
|
81
|
-
|
|
82
|
-
} else {
|
|
83
|
-
this.#host.fetchFile(filename);
|
|
84
|
-
}
|
|
81
|
+
};
|
|
85
82
|
|
|
86
|
-
return
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Get program
|
|
91
|
-
* @private
|
|
92
|
-
*/
|
|
93
|
-
getProgram(): ts.Program {
|
|
94
|
-
return this.#getProgram();
|
|
83
|
+
return emit;
|
|
95
84
|
}
|
|
96
85
|
|
|
97
86
|
/**
|
|
98
|
-
*
|
|
87
|
+
* Emit all files as a stream
|
|
99
88
|
*/
|
|
100
|
-
async
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
this.active = true;
|
|
107
|
-
|
|
108
|
-
if (!EnvUtil.isReadonly()) {
|
|
109
|
-
await this.#transformerManager.init();
|
|
110
|
-
// Enhance transpilation, with custom transformations
|
|
111
|
-
ModuleManager.setTranspiler(tsf => this.#transpile(tsf));
|
|
89
|
+
async * emit(files: string[], emitter: CompileEmitter): AsyncIterable<CompileEmitEvent> {
|
|
90
|
+
let i = 0;
|
|
91
|
+
for (const file of files) {
|
|
92
|
+
const err = await emitter(file);
|
|
93
|
+
const imp = file.replace(/.*node_modules\//, '');
|
|
94
|
+
yield { file: imp, i: i += 1, err, total: files.length };
|
|
112
95
|
}
|
|
113
|
-
|
|
114
|
-
ModuleManager.onUnload((f, unlink) => this.#host.unload(f, unlink)); // Remove source
|
|
115
|
-
|
|
116
|
-
// Update source map support to read from transpiler cache
|
|
117
|
-
sourceMapSupport.install({
|
|
118
|
-
retrieveFile: p => this.#host.contents.get(PathUtil.toUnixTs(p))!
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
console.debug('Initialized', { duration: (Date.now() - start) / 1000 });
|
|
96
|
+
Log.debug(`Compiled ${i} files`);
|
|
122
97
|
}
|
|
123
98
|
|
|
124
99
|
/**
|
|
125
|
-
*
|
|
100
|
+
* Run the compiler
|
|
126
101
|
*/
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
this.#transformerManager.reset();
|
|
130
|
-
this.#host.reset();
|
|
131
|
-
this.#program = undefined;
|
|
132
|
-
}
|
|
133
|
-
ModuleManager.clearUnloadHandlers();
|
|
134
|
-
SourceIndex.reset();
|
|
135
|
-
this.active = false;
|
|
136
|
-
}
|
|
102
|
+
async run(watch?: boolean): Promise<void> {
|
|
103
|
+
Log.debug('Compilation started');
|
|
137
104
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
*/
|
|
141
|
-
notify(type: EventType, filename: string): void {
|
|
142
|
-
console.debug('File Event', { type, filename: filename.replace(PathUtil.cwd, '.') });
|
|
143
|
-
this.#emitter.emit(type, filename);
|
|
144
|
-
}
|
|
105
|
+
const emitter = await this.getCompiler();
|
|
106
|
+
let failed = false;
|
|
145
107
|
|
|
146
|
-
|
|
147
|
-
* Listen for events
|
|
148
|
-
*/
|
|
149
|
-
on(type: EventType, handler: FileListener): this {
|
|
150
|
-
this.#emitter.on(type, handler);
|
|
151
|
-
return this;
|
|
152
|
-
}
|
|
108
|
+
Log.debug('Compiler loaded');
|
|
153
109
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
110
|
+
const resolveEmittedFile = ({ file, total, i, err }: CompileEmitEvent): TerminalProgressEvent => {
|
|
111
|
+
if (err) {
|
|
112
|
+
failed = true;
|
|
113
|
+
console.error(CompilerUtil.buildTranspileError(file, err));
|
|
114
|
+
}
|
|
115
|
+
return { idx: i, total, text: `Compiling [%idx/%total] -- ${file}` };
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
if (this.#dirtyFiles.length) {
|
|
119
|
+
await GlobalTerminal.trackProgress(this.emit(this.#dirtyFiles, emitter), resolveEmittedFile, { position: 'bottom' });
|
|
120
|
+
if (failed) {
|
|
121
|
+
Log.debug('Compilation failed');
|
|
122
|
+
process.exit(1);
|
|
123
|
+
} else {
|
|
124
|
+
Log.debug('Compilation succeeded');
|
|
125
|
+
}
|
|
126
|
+
} else if (watch) {
|
|
127
|
+
// Prime compiler before complete
|
|
128
|
+
const resolved = this.#state.getArbitraryInputFile();
|
|
129
|
+
await emitter(resolved, true);
|
|
160
130
|
}
|
|
161
|
-
// Load Synchronously
|
|
162
|
-
require(filename);
|
|
163
|
-
this.notify('added', filename);
|
|
164
|
-
}
|
|
165
131
|
|
|
166
|
-
|
|
167
|
-
* Handle when a file is removed during watch
|
|
168
|
-
*/
|
|
169
|
-
removed(filename: string): void {
|
|
170
|
-
ModuleManager.unload(filename, true);
|
|
171
|
-
this.notify('removed', filename);
|
|
172
|
-
}
|
|
132
|
+
process.send?.('build-complete');
|
|
173
133
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
134
|
+
if (watch) {
|
|
135
|
+
Log.info('Watch is ready');
|
|
136
|
+
await this.#watchLocalModules(emitter);
|
|
137
|
+
for await (const _ of timers.setInterval(PING_THRESHOLD)) {
|
|
138
|
+
if (!await fs.stat(this.#state.resolveOutputFile('.')).catch(() => false)) { // Output removed
|
|
139
|
+
process.send?.('restart');
|
|
140
|
+
} else {
|
|
141
|
+
process.send?.('ping');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
183
144
|
}
|
|
184
145
|
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
export const Compiler = new $Compiler();
|
|
146
|
+
}
|
package/src/log.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import util from 'util';
|
|
2
|
+
|
|
3
|
+
import type { CompilerLogEvent } from '../support/log';
|
|
4
|
+
|
|
5
|
+
function log(level: 'info' | 'debug', message: string, ...args: unknown[]): void {
|
|
6
|
+
if (process.send) {
|
|
7
|
+
const ev: CompilerLogEvent = [level, util.format(message, ...args)];
|
|
8
|
+
process.send(ev);
|
|
9
|
+
} else {
|
|
10
|
+
// eslint-disable-next-line no-console
|
|
11
|
+
console[level](message, ...args);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const Log = {
|
|
16
|
+
debug: log.bind(null, 'debug'),
|
|
17
|
+
info: log.bind(null, 'info')
|
|
18
|
+
};
|