@travetto/compiler 8.0.0-alpha.3 → 8.0.0-alpha.5

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/bin/hook.js CHANGED
@@ -2,25 +2,15 @@ import module from 'node:module';
2
2
  import { readFileSync } from 'node:fs';
3
3
  import { fileURLToPath } from 'node:url';
4
4
 
5
- import '@travetto/runtime/support/polyfill.js';
6
-
7
- process.setSourceMapsEnabled(true); // Ensure source map during compilation/development
8
- process.env.NODE_OPTIONS = `${process.env.NODE_OPTIONS ?? ''} --enable-source-maps`; // Ensure it passes to children
9
- const ogEmitWarning = process.emitWarning;
10
- Error.stackTraceLimit = 50;
5
+ globalThis.devProcessWarningExclusions.push((message) => message.startsWith('stripTypeScriptTypes'));
11
6
 
12
7
  module.registerHooks({
13
8
  load: (url, context, nextLoad) => {
14
9
  if (/[.]tsx?$/.test(url)) {
15
- try {
16
- process.emitWarning = () => { }; // Suppress ts-node experimental warnings
17
- const source = readFileSync(fileURLToPath(url), 'utf8');
18
- return { format: 'module', source: module.stripTypeScriptTypes(source), shortCircuit: true };
19
- } finally {
20
- process.emitWarning = ogEmitWarning;
21
- }
10
+ const source = readFileSync(fileURLToPath(url), 'utf8');
11
+ return { format: 'module', source: module.stripTypeScriptTypes(source), shortCircuit: true };
22
12
  } else {
23
13
  return nextLoad(url, context);
24
14
  }
25
15
  }
26
- });
16
+ });
@@ -1,4 +1,5 @@
1
1
  // @ts-check
2
+ import '@travetto/runtime/support/patch.js';
2
3
  import './hook.js';
3
4
  const { Compiler } = await import('../src/compiler.ts');
4
5
  await Compiler.main();
package/bin/trvc.js CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  // @ts-check
3
+ import '@travetto/runtime/support/patch.js';
3
4
  import './hook.js';
4
5
  const { invoke } = await import('@travetto/compiler/support/invoke.ts');
5
6
  await invoke();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/compiler",
3
- "version": "8.0.0-alpha.3",
3
+ "version": "8.0.0-alpha.5",
4
4
  "type": "module",
5
5
  "description": "The compiler infrastructure for the Travetto framework",
6
6
  "keywords": [
@@ -31,11 +31,11 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@parcel/watcher": "^2.5.6",
34
- "@travetto/manifest": "^8.0.0-alpha.3",
35
- "@travetto/transformer": "^8.0.0-alpha.3"
34
+ "@travetto/manifest": "^8.0.0-alpha.4",
35
+ "@travetto/transformer": "^8.0.0-alpha.4"
36
36
  },
37
37
  "peerDependencies": {
38
- "@travetto/cli": "^8.0.0-alpha.4"
38
+ "@travetto/cli": "^8.0.0-alpha.8"
39
39
  },
40
40
  "peerDependenciesMeta": {
41
41
  "@travetto/cli": {
package/src/compiler.ts CHANGED
@@ -164,7 +164,9 @@ export class Compiler {
164
164
  log.error(`ERROR ${event.file}:${error}`);
165
165
  }
166
166
  // Touch file to ensure recompilation later
167
- await fs.utimes(event.file, new Date(), new Date());
167
+ if (await fs.stat(event.file, { throwIfNoEntry: false })) {
168
+ await fs.utimes(event.file, new Date(), new Date());
169
+ }
168
170
  }
169
171
  metrics.push(event);
170
172
  }
package/src/state.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import fs from 'node:fs';
1
2
  import type { CompilerHost, SourceFile, CompilerOptions, Program, ScriptTarget } from 'typescript';
2
3
 
3
4
  import { path, ManifestModuleUtil, type ManifestModule, type ManifestRoot, type ManifestIndex, type ManifestModuleFolderType } from '@travetto/manifest';
@@ -39,10 +40,37 @@ export class CompilerState implements CompilerHost {
39
40
  #program: Program;
40
41
 
41
42
  #readFile(sourceFile: string): string | undefined {
42
- return ts.sys.readFile(this.#sourceToEntry.get(sourceFile)?.sourceFile ?? sourceFile);
43
+ const location = this.#sourceToEntry.get(sourceFile)?.sourceFile ?? sourceFile;
44
+ try {
45
+ return ts.sys.readFile(location, 'utf8');
46
+ } catch {
47
+ try { return fs.readFileSync(location, 'utf8'); } catch { }
48
+ }
49
+ return undefined;
50
+ }
51
+
52
+ #writeFile(location: string, text: string, bom?: boolean): void {
53
+ try {
54
+ ts.sys.writeFile(location, text, bom);
55
+ } catch {
56
+ fs.mkdirSync(path.dirname(location), { recursive: true });
57
+ fs.writeFileSync(location, text, 'utf8');
58
+ }
59
+ }
60
+
61
+ #fileExists(location: string): boolean {
62
+ try {
63
+ return ts.sys.fileExists(location);
64
+ } catch { return fs.existsSync(location); }
65
+ }
66
+
67
+ #directoryExists(location: string): boolean {
68
+ try {
69
+ return ts.sys.directoryExists(location);
70
+ } catch { return fs.existsSync(location); }
43
71
  }
44
72
 
45
- #writeExternalTypings(location: string, text: string, bom: boolean): void {
73
+ #writeExternalTypings(location: string, text: string, bom?: boolean): void {
46
74
  let core = location.replace('.map', '');
47
75
  if (!this.#outputToEntry.has(core)) {
48
76
  core = core.replace(ManifestModuleUtil.TYPINGS_EXT_REGEX, ManifestModuleUtil.OUTPUT_EXT);
@@ -51,7 +79,7 @@ export class CompilerState implements CompilerHost {
51
79
  if (entry) {
52
80
  const relative = this.#manifestIndex.getFromSource(entry.sourceFile)?.relativeFile;
53
81
  if (relative && TYPINGS_FOLDER_KEYS.has(ManifestModuleUtil.getFolderKey(relative))) {
54
- ts.sys.writeFile(location.replace(this.#outputPath, this.#typingsPath), text, bom);
82
+ this.#writeFile(location.replace(this.#outputPath, this.#typingsPath), text, bom);
55
83
  }
56
84
  }
57
85
  }
@@ -59,7 +87,7 @@ export class CompilerState implements CompilerHost {
59
87
  async #initCompilerOptions(): Promise<CompilerOptions> {
60
88
  const tsconfigFile = CommonUtil.resolveWorkspace(this.#manifest, 'tsconfig.json');
61
89
  if (!ts.sys.fileExists(tsconfigFile)) {
62
- ts.sys.writeFile(tsconfigFile, JSON.stringify({ extends: '@travetto/compiler/tsconfig.trv.json' }, null, 2));
90
+ this.#writeFile(tsconfigFile, JSON.stringify({ extends: '@travetto/compiler/tsconfig.trv.json' }, null, 2));
63
91
  }
64
92
 
65
93
  const { options } = ts.parseJsonSourceFileConfigFileContent(
@@ -153,16 +181,18 @@ export class CompilerState implements CompilerHost {
153
181
  return;
154
182
  }
155
183
 
156
- const program = await this.getProgram(needsNewProgram);
157
184
  switch (ManifestModuleUtil.getFileType(sourceFile)) {
158
- case 'typings':
159
- case 'package-json':
160
- this.writeFile(output, this.readFile(sourceFile)!, false), undefined;
161
- break;
162
185
  case 'js':
163
- this.writeFile(output, ts.transpile(this.readFile(sourceFile)!, this.#compilerOptions), false);
186
+ case 'typings':
187
+ case 'package-json': {
188
+ const text = this.readFile(sourceFile)!;
189
+ const finalText = sourceFile.endsWith('package.json') ? CompilerUtil.rewritePackageJSON(this.#manifest, text) : text;
190
+ const location = this.#tscOutputFileToOuptut.get(output) ?? output;
191
+ this.#writeFile(location, finalText, false);
164
192
  break;
193
+ }
165
194
  case 'ts': {
195
+ const program = await this.getProgram(needsNewProgram);
166
196
  const tsSourceFile = program.getSourceFile(sourceFile)!;
167
197
  program.emit(
168
198
  tsSourceFile,
@@ -177,7 +207,14 @@ export class CompilerState implements CompilerHost {
177
207
  .filter(d => d.category === ts.DiagnosticCategory.Error)
178
208
  .map(diag => {
179
209
  let message = ts.flattenDiagnosticMessageText(diag.messageText, '\n');
180
- if (message.includes('rootDir') || message.includes('EnvDataCombinedType')) {
210
+ if (
211
+ message.includes("is not under 'rootDir'")
212
+ || message.includes("does not exist on type 'EnvDataCombinedType'")
213
+ || message.startsWith('Could not find a declaration file for module')
214
+ || message.startsWith("Cannot find module '@travetto")
215
+ || message.startsWith("This JSX tag requires the module path '@travetto")
216
+ || message.startsWith("JSX element implicitly has type 'any'")
217
+ ) {
181
218
  return '';
182
219
  }
183
220
  if (diag.file) {
@@ -279,22 +316,14 @@ export class CompilerState implements CompilerHost {
279
316
  getDefaultLibLocation(): string { return path.dirname(ts.getDefaultLibFilePath(this.#compilerOptions)); }
280
317
 
281
318
  fileExists(sourceFile: string): boolean {
282
- return this.#sourceToEntry.has(sourceFile) || ts.sys.fileExists(sourceFile);
319
+ return this.#sourceToEntry.has(sourceFile) || this.#fileExists(sourceFile);
283
320
  }
284
321
 
285
322
  directoryExists(sourceDirectory: string): boolean {
286
- return this.#sourceDirectory.has(sourceDirectory) || ts.sys.directoryExists(sourceDirectory);
323
+ return this.#sourceDirectory.has(sourceDirectory) || this.#directoryExists(sourceDirectory);
287
324
  }
288
325
 
289
- writeFile(
290
- outputFile: string,
291
- text: string,
292
- bom: boolean
293
- ): void {
294
- if (outputFile.endsWith('package.json')) {
295
- text = CompilerUtil.rewritePackageJSON(this.#manifest, text);
296
- }
297
-
326
+ writeFile(outputFile: string, text: string, bom?: boolean): void {
298
327
  // JSX runtime shenanigans
299
328
  text = text.replace(/support\/jsx-runtime"/g, 'support/jsx-runtime.js"');
300
329
 
@@ -304,7 +333,7 @@ export class CompilerState implements CompilerHost {
304
333
  this.#writeExternalTypings(location, text, bom);
305
334
  }
306
335
 
307
- ts.sys.writeFile(location, text, bom);
336
+ return this.#writeFile(location, text, bom);
308
337
  }
309
338
 
310
339
  readFile(sourceFile: string): string | undefined {