@travetto/compiler 4.0.5 → 4.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/compiler",
3
- "version": "4.0.5",
3
+ "version": "4.0.7",
4
4
  "description": "The compiler infrastructure for the Travetto framework",
5
5
  "keywords": [
6
6
  "compiler",
@@ -36,7 +36,7 @@
36
36
  "@types/node": "^20.11.16"
37
37
  },
38
38
  "peerDependencies": {
39
- "@travetto/cli": "^4.0.4"
39
+ "@travetto/cli": "^4.0.5"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@travetto/cli": {
package/src/compiler.ts CHANGED
@@ -10,7 +10,7 @@ import { CompileEmitEvent, CompileEmitter } from './types';
10
10
  import { EventUtil } from './event';
11
11
 
12
12
  import { IpcLogger } from '../support/log';
13
- import { TimerUtil } from '../support/timer';
13
+ import { CommonUtil } from '../support/util';
14
14
 
15
15
  const log = new IpcLogger({ level: 'debug' });
16
16
 
@@ -82,7 +82,7 @@ export class Compiler {
82
82
  process.removeAllListeners('disconnect');
83
83
  process.removeAllListeners('message');
84
84
  this.#ctrl.abort();
85
- TimerUtil.nonBlockingTimeout(1000).then(() => process.exit()); // Allow upto 1s to shutdown gracefully
85
+ CommonUtil.nonBlockingTimeout(1000).then(() => process.exit()); // Allow upto 1s to shutdown gracefully
86
86
  }
87
87
 
88
88
  /**
@@ -113,7 +113,7 @@ export class Compiler {
113
113
  }
114
114
  EventUtil.sendEvent('progress', { total: files.length, idx: files.length, message: 'Complete', operation: 'compile', complete: true });
115
115
 
116
- await TimerUtil.queueMacroTask();
116
+ await CommonUtil.queueMacroTask();
117
117
 
118
118
  log.debug(`Compiled ${i} files`);
119
119
  }
package/src/state.ts CHANGED
@@ -4,7 +4,7 @@ import timers from 'node:timers/promises';
4
4
  import { path, ManifestModuleUtil, ManifestModule, ManifestRoot, ManifestIndex } from '@travetto/manifest';
5
5
  import { TransformerManager } from '@travetto/transformer';
6
6
 
7
- import { CommonUtil } from '../support/util';
7
+ import { TypescriptUtil } from '../support/ts-util';
8
8
 
9
9
  import { CompilerUtil } from './util';
10
10
  import { CompileEmitError, CompileStateEntry } from './types';
@@ -82,7 +82,7 @@ export class CompilerState implements ts.CompilerHost {
82
82
  this.#transformerManager = await TransformerManager.create(this.#manifestIndex);
83
83
 
84
84
  this.#compilerOptions = {
85
- ...await CommonUtil.getCompilerOptions(this.#manifest),
85
+ ...await TypescriptUtil.getCompilerOptions(this.#manifest),
86
86
  rootDir: this.#rootDir,
87
87
  outDir: this.#outputPath
88
88
  };
@@ -6,7 +6,6 @@ import type { ManifestContext } from '@travetto/manifest';
6
6
 
7
7
  import type { CompilerMode, CompilerServerInfo } from './types';
8
8
  import { Log } from './log';
9
- import { CommonUtil } from './util';
10
9
  import { CompilerSetup } from './setup';
11
10
  import { CompilerServer } from './server/server';
12
11
  import { CompilerRunner } from './server/runner';
@@ -97,7 +96,15 @@ export const main = (ctx: ManifestContext) => {
97
96
  Log.initLevel('error');
98
97
  await compile('build');
99
98
  }
100
- return CommonUtil.moduleLoader(ctx);
99
+
100
+ return (mod, args) => {
101
+ const outputRoot = path.resolve(ctx.workspace.path, ctx.build.outputFolder);
102
+ process.env.TRV_MANIFEST = path.resolve(outputRoot, 'node_modules', ctx.main.name); // Setup for running
103
+ if (args) {
104
+ process.argv = [process.argv0, mod, ...args];
105
+ }
106
+ return import(path.join(outputRoot, 'node_modules', mod)); // Return function to run import on a module
107
+ };
101
108
  },
102
109
 
103
110
  /** Manifest entry point */
@@ -6,7 +6,7 @@ import { ManifestContext } from '@travetto/manifest';
6
6
 
7
7
  import type { CompilerEvent, CompilerEventType, CompilerServerInfo, CompilerStateType } from '../types';
8
8
  import type { LogShape } from '../log';
9
- import { TimerUtil } from '../timer';
9
+ import { CommonUtil } from '../util';
10
10
  import { ProcessHandle } from './process-handle';
11
11
 
12
12
  type FetchEventsConfig<T> = {
@@ -129,7 +129,7 @@ export class CompilerClient {
129
129
  if (line.trim().charAt(0) === '{') {
130
130
  const val = JSON.parse(line);
131
131
  if (cfg.until?.(val)) {
132
- await TimerUtil.queueMacroTask();
132
+ await CommonUtil.queueMacroTask();
133
133
  ctrl.abort();
134
134
  }
135
135
  yield val;
@@ -140,7 +140,7 @@ export class CompilerClient {
140
140
  }
141
141
  signal.removeEventListener('abort', quit);
142
142
 
143
- await TimerUtil.queueMacroTask();
143
+ await CommonUtil.queueMacroTask();
144
144
 
145
145
  info = await this.info();
146
146
 
@@ -3,7 +3,7 @@ import path from 'node:path';
3
3
 
4
4
  import type { ManifestContext } from '@travetto/manifest';
5
5
  import { Log, Logger } from '../log';
6
- import { TimerUtil } from '../timer';
6
+ import { CommonUtil } from '../util';
7
7
 
8
8
  export class ProcessHandle {
9
9
 
@@ -56,7 +56,7 @@ export class ProcessHandle {
56
56
  if (!await this.isRunning()) {
57
57
  return true;
58
58
  }
59
- await TimerUtil.nonBlockingTimeout(100);
59
+ await CommonUtil.blockingTimeout(100);
60
60
  }
61
61
  try {
62
62
  this.#log.debug('Force Killing', pid);
@@ -8,7 +8,6 @@ import type { ManifestContext } from '@travetto/manifest';
8
8
  import type { CompilerMode, CompilerProgressEvent, CompilerEvent, CompilerEventType, CompilerServerInfo } from '../types';
9
9
  import { Log } from '../log';
10
10
  import { CommonUtil } from '../util';
11
- import { TimerUtil } from '../timer';
12
11
  import { CompilerClient } from './client';
13
12
  import { ProcessHandle } from './process-handle';
14
13
 
@@ -78,7 +77,7 @@ export class CompilerServer {
78
77
  .on('close', () => log.debug('Server close event'));
79
78
 
80
79
  const url = new URL(this.#url);
81
- TimerUtil.queueMacroTask().then(() => this.#server.listen(+url.port, url.hostname)); // Run async
80
+ CommonUtil.queueMacroTask().then(() => this.#server.listen(+url.port, url.hostname)); // Run async
82
81
  });
83
82
 
84
83
  if (output === 'retry') {
@@ -122,7 +121,7 @@ export class CompilerServer {
122
121
  async #disconnectActive(): Promise<void> {
123
122
  log.info('Server disconnect requested');
124
123
  this.info.iteration = Date.now();
125
- await TimerUtil.nonBlockingTimeout(20);
124
+ await CommonUtil.blockingTimeout(20);
126
125
  for (const el of Object.values(this.#listeners)) {
127
126
  try { el.res.end(); } catch { }
128
127
  }
@@ -205,7 +204,7 @@ export class CompilerServer {
205
204
 
206
205
  try {
207
206
  await new Promise((resolve, reject) => {
208
- TimerUtil.nonBlockingTimeout(2000).then(reject); // Wait 2s max
207
+ CommonUtil.nonBlockingTimeout(2000).then(reject); // Wait 2s max
209
208
  this.#server.close(resolve);
210
209
  this.#emitEvent({ type: 'state', payload: { state: 'closed' } });
211
210
  setImmediate(() => {
package/support/setup.ts CHANGED
@@ -6,6 +6,7 @@ import { type DeltaEvent, type ManifestContext, type Package } from '@travetto/m
6
6
 
7
7
  import { Log } from './log';
8
8
  import { CommonUtil } from './util';
9
+ import { TypescriptUtil } from './ts-util';
9
10
 
10
11
  type ModFile = { input: string, output: string, stale: boolean };
11
12
 
@@ -57,7 +58,7 @@ export class CompilerSetup {
57
58
 
58
59
  const ts = (await import('typescript')).default;
59
60
  const content = ts.transpile(text, {
60
- ...await CommonUtil.getCompilerOptions(ctx),
61
+ ...await TypescriptUtil.getCompilerOptions(ctx),
61
62
  sourceMap: false,
62
63
  inlineSourceMap: true,
63
64
  }, inputFile);
@@ -0,0 +1,38 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+
4
+ import { ManifestContext } from '@travetto/manifest';
5
+
6
+ const OPT_CACHE: Record<string, import('typescript').CompilerOptions> = {};
7
+
8
+ export class TypescriptUtil {
9
+ /**
10
+ * Returns the compiler options
11
+ */
12
+ static async getCompilerOptions(ctx: ManifestContext): Promise<{}> {
13
+ if (!(ctx.workspace.path in OPT_CACHE)) {
14
+ let tsconfig = path.resolve(ctx.workspace.path, 'tsconfig.json');
15
+
16
+ if (!await fs.stat(tsconfig).then(_ => true, _ => false)) {
17
+ tsconfig = path.resolve(ctx.workspace.path, ctx.build.compilerModuleFolder, 'tsconfig.trv.json');
18
+ }
19
+
20
+ const ts = (await import('typescript')).default;
21
+
22
+ const { options } = ts.parseJsonSourceFileConfigFileContent(
23
+ ts.readJsonConfigFile(tsconfig, ts.sys.readFile), ts.sys, ctx.workspace.path
24
+ );
25
+
26
+ OPT_CACHE[ctx.workspace.path] = {
27
+ ...options,
28
+ allowJs: true,
29
+ resolveJsonModule: true,
30
+ sourceRoot: ctx.workspace.path,
31
+ rootDir: ctx.workspace.path,
32
+ outDir: path.resolve(ctx.workspace.path),
33
+ module: ctx.workspace.type === 'commonjs' ? ts.ModuleKind.CommonJS : ts.ModuleKind.ESNext,
34
+ };
35
+ }
36
+ return OPT_CACHE[ctx.workspace.path];
37
+ }
38
+ }
package/support/util.ts CHANGED
@@ -1,44 +1,11 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
  import { setMaxListeners } from 'node:events';
4
-
5
- import type { ManifestContext } from '@travetto/manifest';
4
+ import timers from 'node:timers/promises';
6
5
 
7
6
  import { Log } from './log';
8
7
 
9
- const OPT_CACHE: Record<string, import('typescript').CompilerOptions> = {};
10
-
11
8
  export class CommonUtil {
12
- /**
13
- * Returns the compiler options
14
- */
15
- static async getCompilerOptions(ctx: ManifestContext): Promise<{}> {
16
- if (!(ctx.workspace.path in OPT_CACHE)) {
17
- let tsconfig = path.resolve(ctx.workspace.path, 'tsconfig.json');
18
-
19
- if (!await fs.stat(tsconfig).then(_ => true, _ => false)) {
20
- tsconfig = path.resolve(ctx.workspace.path, ctx.build.compilerModuleFolder, 'tsconfig.trv.json');
21
- }
22
-
23
- const ts = (await import('typescript')).default;
24
-
25
- const { options } = ts.parseJsonSourceFileConfigFileContent(
26
- ts.readJsonConfigFile(tsconfig, ts.sys.readFile), ts.sys, ctx.workspace.path
27
- );
28
-
29
- OPT_CACHE[ctx.workspace.path] = {
30
- ...options,
31
- allowJs: true,
32
- resolveJsonModule: true,
33
- sourceRoot: ctx.workspace.path,
34
- rootDir: ctx.workspace.path,
35
- outDir: path.resolve(ctx.workspace.path),
36
- module: ctx.workspace.type === 'commonjs' ? ts.ModuleKind.CommonJS : ts.ModuleKind.ESNext,
37
- };
38
- }
39
- return OPT_CACHE[ctx.workspace.path];
40
- }
41
-
42
9
  /**
43
10
  * Determine file type
44
11
  */
@@ -92,17 +59,23 @@ export class CommonUtil {
92
59
  }
93
60
 
94
61
  /**
95
- * Create a module loader given a context, and assuming build is complete
96
- * @param ctx
62
+ * Non-blocking timeout
97
63
  */
98
- static moduleLoader(ctx: ManifestContext): (mod: string, args?: string[]) => Promise<unknown> {
99
- return (mod, args) => {
100
- const outputRoot = path.resolve(ctx.workspace.path, ctx.build.outputFolder);
101
- process.env.TRV_MANIFEST = path.resolve(outputRoot, 'node_modules', ctx.main.name); // Setup for running
102
- if (args) {
103
- process.argv = [process.argv0, mod, ...args];
104
- }
105
- return import(path.join(outputRoot, 'node_modules', mod)); // Return function to run import on a module
106
- };
64
+ static nonBlockingTimeout(time: number): Promise<void> {
65
+ return timers.setTimeout(time, undefined, { ref: false }).catch(() => { });
66
+ }
67
+
68
+ /**
69
+ * Blocking timeout
70
+ */
71
+ static blockingTimeout(time: number): Promise<void> {
72
+ return timers.setTimeout(time, undefined, { ref: true }).catch(() => { });
73
+ }
74
+
75
+ /**
76
+ * Queue new macrotask
77
+ */
78
+ static queueMacroTask(): Promise<void> {
79
+ return timers.setImmediate(undefined);
107
80
  }
108
81
  }
package/support/timer.ts DELETED
@@ -1,17 +0,0 @@
1
- import timers from 'node:timers/promises';
2
-
3
- export class TimerUtil {
4
- /**
5
- * Non-blocking timeout, that is cancellable
6
- */
7
- static nonBlockingTimeout(time: number): Promise<void> {
8
- return timers.setTimeout(time, undefined, { ref: false }).catch(() => { });
9
- }
10
-
11
- /**
12
- * Queue new macrotask
13
- */
14
- static queueMacroTask(): Promise<void> {
15
- return timers.setImmediate(undefined);
16
- }
17
- }