@travetto/compiler 3.0.0-rc.3 → 3.0.0-rc.30

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/src/host.ts DELETED
@@ -1,142 +0,0 @@
1
- import * as ts from 'typescript';
2
- import { readFileSync } from 'fs';
3
- import * as path from 'path';
4
-
5
- import { PathUtil, AppCache } from '@travetto/boot';
6
- import { SourceUtil } from '@travetto/boot/src/internal/source-util';
7
- import { SystemUtil } from '@travetto/boot/src/internal/system';
8
- import { TranspileUtil } from '@travetto/boot/src/internal/transpile-util';
9
- import { SourceIndex } from '@travetto/boot/src/internal/source';
10
- import { AppManifest } from '@travetto/base';
11
-
12
- /**
13
- * Manages the source code and typescript relationship.
14
- */
15
- export class SourceHost implements ts.CompilerHost {
16
-
17
- #rootFiles = new Set<string>();
18
- #hashes = new Map<string, number>();
19
- #sources = new Map<string, ts.SourceFile>();
20
- readonly contents = new Map<string, string>();
21
-
22
- #trackFile(filename: string, content: string): void {
23
- this.contents.set(filename, content);
24
- this.#hashes.set(filename, SystemUtil.naiveHash(readFileSync(filename, 'utf8'))); // Get og content for hashing
25
- }
26
-
27
- getCanonicalFileName: (file: string) => string = (f: string) => f;
28
- getCurrentDirectory: () => string = () => PathUtil.cwd;
29
- getDefaultLibFileName: (opts: ts.CompilerOptions) => string = (opts: ts.CompilerOptions) => ts.getDefaultLibFileName(opts);
30
- getNewLine: () => string = () => ts.sys.newLine;
31
- useCaseSensitiveFileNames: () => boolean = () => ts.sys.useCaseSensitiveFileNames;
32
- getDefaultLibLocation(): string {
33
- return path.dirname(ts.getDefaultLibFilePath(TranspileUtil.compilerOptions));
34
- }
35
-
36
- /**
37
- * Get root files
38
- */
39
- getRootFiles(): Set<string> {
40
- if (!this.#rootFiles.size) {
41
- // Only needed for compilation
42
- this.#rootFiles = new Set(SourceIndex.findByFolders(AppManifest.source, 'required').map(x => x.file));
43
- }
44
- return this.#rootFiles;
45
- }
46
-
47
- /**
48
- * Read file from disk, using the transpile pre-processor on .ts files
49
- */
50
- readFile(filename: string): string {
51
- filename = PathUtil.toUnixTs(filename);
52
- let content = ts.sys.readFile(filename);
53
- if (content === undefined) {
54
- throw new Error(`Unable to read file ${filename}`);
55
- }
56
- if (filename.endsWith(SourceUtil.EXT) && !filename.endsWith('.d.ts')) {
57
- content = SourceUtil.preProcess(filename, content);
58
- }
59
- return content;
60
- }
61
-
62
- /**
63
- * Write file to disk, and set value in cache as well
64
- */
65
- writeFile(filename: string, content: string): void {
66
- filename = PathUtil.toUnixTs(filename);
67
- this.#trackFile(filename, content);
68
- AppCache.writeEntry(filename, content);
69
- }
70
-
71
- /**
72
- * Fetch file
73
- */
74
- fetchFile(filename: string): void {
75
- filename = PathUtil.toUnixTs(filename);
76
- const cached = AppCache.readEntry(filename);
77
- this.#trackFile(filename, cached);
78
- }
79
-
80
- /**
81
- * Get a source file on demand
82
- * @returns
83
- */
84
- getSourceFile(filename: string, __tgt: unknown, __onErr: unknown, force?: boolean): ts.SourceFile {
85
- if (!this.#sources.has(filename) || force) {
86
- const content = this.readFile(filename)!;
87
- this.#sources.set(filename, ts.createSourceFile(filename, content ?? '',
88
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
89
- (TranspileUtil.compilerOptions as ts.CompilerOptions).target!
90
- ));
91
- }
92
- return this.#sources.get(filename)!;
93
- }
94
-
95
- /**
96
- * See if a file exists
97
- */
98
- fileExists(filename: string): boolean {
99
- filename = PathUtil.toUnixTs(filename);
100
- return this.contents.has(filename) || ts.sys.fileExists(filename);
101
- }
102
-
103
- /**
104
- * See if a file's hash code has changed
105
- */
106
- hashChanged(filename: string, content?: string): boolean {
107
- content ??= readFileSync(filename, 'utf8');
108
- // Let's see if they are really different
109
- const hash = SystemUtil.naiveHash(content);
110
- if (hash === this.#hashes.get(filename)) {
111
- console.debug('Contents Unchanged', { filename });
112
- return false;
113
- }
114
- return true;
115
- }
116
-
117
- /**
118
- * Unload a file from the transpiler
119
- */
120
- unload(filename: string, unlink = true): void {
121
- if (this.contents.has(filename)) {
122
- AppCache.removeExpiredEntry(filename, unlink);
123
-
124
- if (unlink && this.#hashes.has(filename)) {
125
- this.#hashes.delete(filename);
126
- }
127
- this.#rootFiles.delete(filename);
128
- this.contents.delete(filename);
129
- this.#sources.delete(filename);
130
- }
131
- }
132
-
133
- /**
134
- * Reset the transpiler
135
- */
136
- reset(): void {
137
- this.contents.clear();
138
- this.#rootFiles.clear();
139
- this.#hashes.clear();
140
- this.#sources.clear();
141
- }
142
- }
@@ -1,75 +0,0 @@
1
- import * as ts from 'typescript';
2
-
3
- import { SourceIndex } from '@travetto/boot/src/internal/source';
4
-
5
- import {
6
- NodeTransformer, VisitorFactory, TransformerState, getAllTransformers
7
- } from '@travetto/transformer'; // Narrow import to minimize scope
8
-
9
- type TransformerList = { before: ts.TransformerFactory<ts.SourceFile>[] };
10
-
11
-
12
- /**
13
- * Manages the typescript transformers
14
- */
15
- export class TransformerManager {
16
-
17
- #cached: TransformerList | undefined;
18
- #transformers: NodeTransformer<TransformerState>[] = [];
19
-
20
- /**
21
- * Read all transformers from disk under the pattern support/transformer.*
22
- */
23
- async init(): Promise<void> {
24
- if (this.#cached) {
25
- return;
26
- }
27
-
28
- // Modules
29
- const found = SourceIndex.find({ folder: 'support', filter: /\/transformer.*[.]ts/ });
30
-
31
- for (const entry of found) { // Exclude based on blacklist
32
- this.#transformers.push(...getAllTransformers(await import(entry.file)));
33
- }
34
-
35
- console.debug('Transformers', {
36
- order: this.#transformers.map(x => {
37
- const flags = [
38
- ...(x.target ? [] : ['all']),
39
- ...(x.before ? ['before'] : []),
40
- ...(x.after ? ['after'] : [])
41
- ];
42
- return { type: x.type, key: x.key, flags: flags.join(' ') };
43
- })
44
- });
45
-
46
- // Prepare a new visitor factory with a given type checker
47
- }
48
-
49
- build(checker: ts.TypeChecker): void {
50
- const visitor = new VisitorFactory(
51
- (ctx, src) => new TransformerState(src, ctx.factory, checker),
52
- this.#transformers
53
- );
54
-
55
- // Define transformers for the compiler
56
- this.#cached = {
57
- before: [visitor.visitor()]
58
- };
59
- }
60
-
61
- /**
62
- * Get typescript transformer object
63
- */
64
- getTransformers(): TransformerList | undefined {
65
- return this.#cached!;
66
- }
67
-
68
- /**
69
- * Reset state
70
- */
71
- reset(): void {
72
- this.#transformers = [];
73
- this.#cached = undefined;
74
- }
75
- }
@@ -1,72 +0,0 @@
1
- import { AppManifest, Class, ShutdownManager } from '@travetto/base';
2
- import { RetargettingProxy } from '@travetto/base/src/internal/proxy';
3
- import { FsUtil, PathUtil } from '@travetto/boot';
4
- import { ModuleUtil } from '@travetto/boot/src/internal/module-util';
5
- import { ModuleManager } from '@travetto/boot/src/internal/module';
6
-
7
- import { FilePresenceManager } from '@travetto/watch';
8
-
9
- import { Compiler } from '../src/compiler';
10
-
11
- /**
12
- * Wraps the compiler supporting real-time changes to files
13
- */
14
- export function init($Compiler: Class<typeof Compiler>): typeof $Compiler {
15
- /**
16
- * Extending the $Compiler class to add some functionality
17
- */
18
- const Cls = class extends $Compiler {
19
- #modules = new Map<string, RetargettingProxy<unknown>>();
20
-
21
- constructor(...args: unknown[]) {
22
- super(...args);
23
-
24
- ShutdownManager.onUnhandled(err => {
25
- if (err && (err.message ?? '').includes('Cannot find module')) { // Handle module reloading
26
- console.error('Cannot find module', { error: err });
27
- return true;
28
- }
29
- }, 0);
30
-
31
- // Proxy all file loads
32
- ModuleUtil.addHandler((name, mod) => {
33
- if (name.includes(PathUtil.cwd) && !name.includes('node_modules') && /src\//.test(name)) {
34
- if (!this.#modules.has(name)) {
35
- this.#modules.set(name, new RetargettingProxy(mod));
36
- } else {
37
- this.#modules.get(name)!.setTarget(mod);
38
- }
39
- return this.#modules.get(name)!.get();
40
- } else {
41
- return mod;
42
- }
43
- });
44
-
45
- // Clear target on unload
46
- ModuleManager.onUnload(f => this.#modules.get(f)?.setTarget(null));
47
-
48
- new FilePresenceManager(
49
- [...AppManifest.source.local, ...AppManifest.source.common]
50
- .map(x => `./${x}`)
51
- .filter(x => FsUtil.existsSync(x)),
52
- {
53
- ignoreInitial: true,
54
- validFile: x => x.endsWith('.ts') && !x.endsWith('.d.ts')
55
- }
56
- ).on('all', ({ event, entry }) => {
57
- switch (event) {
58
- case 'added': this.added(entry.file); break;
59
- case 'removed': this.removed(entry.file); break;
60
- case 'changed': this.changed(entry.file); break;
61
- }
62
- });
63
- }
64
-
65
- reset(): void {
66
- super.reset();
67
- this.#modules.clear();
68
- }
69
- };
70
-
71
- return Cls;
72
- }
@@ -1,13 +0,0 @@
1
- /**
2
- * Responsible for initializing the compiler
3
- */
4
- export const init = {
5
- key: '@trv:compiler/init',
6
- after: ['@trv:base/init'],
7
- before: ['@trv:base/transpile'],
8
- action: async (): Promise<void> => {
9
- // Overrides the require behavior
10
- const { Compiler } = await import('../src/compiler');
11
- await Compiler.init();
12
- }
13
- };
@@ -1,10 +0,0 @@
1
- /**
2
- * Responsible for resetting the compiler
3
- */
4
- export const init = {
5
- key: '@trv:compiler/reset',
6
- action: async (): Promise<void> => {
7
- const { Compiler } = await import('../src/compiler');
8
- Compiler.reset();
9
- }
10
- };