@travetto/compiler 3.0.0-rc.32 → 3.0.0-rc.34

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 CHANGED
@@ -1,28 +1,82 @@
1
1
  <!-- This file was generated by @travetto/doc and should not be modified directly -->
2
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
- ## Compiler
4
+ ## The compiler infrastructure for the Travetto framework
5
5
 
6
6
  **Install: @travetto/compiler**
7
7
  ```bash
8
8
  npm install @travetto/compiler
9
+
10
+ # or
11
+
12
+ yarn add @travetto/compiler
9
13
  ```
10
14
 
11
- This module expands upon [Typescript](https://typescriptlang.org), with supplemental functionality:
15
+ This module expands upon the [Typescript](https://typescriptlang.org) compiler, with the additional features:
12
16
 
13
- * Read `tsconfig.json` from the project directory to provide
14
- * Supports on-the-fly compilation, nothing needs to be compiled ahead of time
15
- * Enhanced AST transformations, and transformer registration [object Object]
16
- * Intelligent caching of source files to minimize recompilation
17
- * Support for detecting changes in sources files at runtime
18
- * Allows for hot-reloading of classes during development
19
- * Utilizes `es2015` `Proxy`s to allow for swapping out implementation at runtime
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")
21
- ## Debugging
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.
17
+ * Integration with the [Transformation](https://github.com/travetto/travetto/tree/main/module/transformer#readme "Functionality for AST transformations, with transformer registration, and general utils") module, allowing for rich, type-aware transformations
18
+ * Automatic conversion to either [Ecmascript Module](https://nodejs.org/api/esm.html) or [CommonJS](https://nodejs.org/api/modules.html) based on the [Package JSON](https://docs.npmjs.com/cli/v9/configuring-npm/package-json) `type` value
19
+ * Removal of type only imports which can break [Ecmascript Module](https://nodejs.org/api/esm.html)-style output
20
+ * Automatic addition of `.js` extension to imports to also support [Ecmascript Module](https://nodejs.org/api/esm.html)-style output
21
+
22
+ Beyond the [Typescript](https://typescriptlang.org) compiler functionality, the module provides the primary entry point into the development process.
23
23
 
24
24
  ## CLI
25
25
 
26
- The module provides the ability to clear the compilation cache to handle any inconsistencies that may arise.
26
+ The cli, [trv](https://github.com/travetto/travetto/tree/main/module/compiler/bin/trv.js#L60) is a compilation aware entry point, that has the ability to check for active builds, and ongoing watch operations to ensure only one process is building at a time. Within the framework, regardless of mono-repo or not, always builds the entire project. With the efficient caching behavior, this leads to generally a minimal overhead but allows for centralization of all operations.
27
+
28
+ The CLI supports the following operations:
29
+
30
+
31
+ * `clean` - Removes the output folder, and if `-a` is also passed, will also clean out the compiler folder
32
+ * `build` - Will attempt to build the project. If the project is already built, will return immediately. If the project is being built somewhere else, will wait until a build is completed.
33
+ * `watch` - If nothing else is watching, will start the watch operation. Otherwise will return immediately.
34
+ * `manifest` - Will produce a manifest. If no file is passed in the command line arguments, will output to stdout
35
+ * `<other>` - Will be delegated to the [Command Line Interface](https://github.com/travetto/travetto/tree/main/module/cli#readme "CLI infrastructure for Travetto framework") entry point after a successful build.
36
+
37
+ In addition to the normal output, the compiler supports an environment variable `TRV_BUILD` that supports the following values: `debug`, `info`, `warn` or `none`. This provides different level of logging during the build process which is helpful to diagnose any odd behaviors. When invoking an unknown command (e.g. `<other>` from above), the default level is `warn`. Otherwise the default logging level is `info`.
38
+
39
+ **Terminal: Sample trv output with debug logging**
40
+ ```bash
41
+ $ TRV_BUILD=debug trv build
42
+
43
+ 2029-03-14T04:00:00.618Z [lock ] watch pid=000000 Started
44
+ 2029-03-14T04:00:00.837Z [lock ] watch pid=000000 Already running, and has built
45
+ 2029-03-14T04:00:01.510Z [lock ] watch pid=000000 Completed
46
+ 2029-03-14T04:00:02.450Z [build ] Successfully built
47
+ ```
48
+
49
+ **Terminal: Sample trv output with default log level**
50
+ ```bash
51
+ $ trv build
52
+ ```
53
+
54
+ ## Compilation Architecture
55
+
56
+ The compiler will move through the following phases on a given compilation execution:
57
+
58
+ * `Bootstrapping` - Initial compilation of [Compiler](https://github.com/travetto/travetto/tree/main/module/compiler#readme "The compiler infrastructure for the Travetto framework")'s `support/*.ts` files
59
+ * `Lock Management` - Manages cross-process interaction to ensure single compiler
60
+ * `Build Compiler` - Leverages [Typescript](https://typescriptlang.org) to build files needed to execute compiler
61
+ * `Build Manifest` - Produces the manifest for the given execution
62
+ * `Build Transformers` - Leverages [Typescript](https://typescriptlang.org) to compile all transformers defined in the manifest
63
+ * `Produce Manifest Delta` - Compare the output file system with the manifest to determine what needs to be compiled
64
+ * `Clear all output if needed` - When the compiler source or transformers change, invalidate the entire output
65
+ * `Persist Manifest(s)` - Ensure the manifest is available for the compiler to leverage. Multiple will be written if in a monorepo
66
+ * `Invoke Compiler` - Run [Typescript](https://typescriptlang.org) compiler with the aforementioned enhancements
67
+
68
+ ### Bootstrapping
69
+
70
+ Given that the framework is distributed as [Typescript](https://typescriptlang.org) only files, there is a bootstrapping problem that needs to be mitigated. The [trv](https://github.com/travetto/travetto/tree/main/module/compiler/bin/trv.js#L60) entrypoint, along with a small context utility in [Manifest](https://github.com/travetto/travetto/tree/main/module/manifest#readme "Support for project indexing, manifesting, along with file watching") are the only [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) files needed to run the project. The [trv](https://github.com/travetto/travetto/tree/main/module/compiler/bin/trv.js#L60) entry point will compile `@travetto/compiler/support/*` files as the set that is used at startup. These files are also accessible to the compiler as they get re-compiled after the fact.
71
+
72
+ ### Lock Management
73
+
74
+ The compiler supports invocation from multiple locations at the same time, and provides a layer of orchestration to ensure a single process is building at a time. For a given project, there are four main states:
75
+
76
+
77
+ * No Watch - Building
78
+ * Watch - No Build
79
+ * Watch - Building
80
+ * Inactive / Stale
27
81
 
28
- TODO: Describe cli behavior
82
+ Depending on what state the project is in (depending on various processes), will influence what the supporting tooling should do. [LockManager](https://github.com/travetto/travetto/tree/main/module/compiler/support/lock.ts#L25) represents the majority of the logic for tracking various states, and informing what action should happen when in the above states.
package/bin/trv.js CHANGED
@@ -16,6 +16,10 @@ const COMPILER_FILES = [...['launcher', 'transpile', 'lock', 'log', 'lock-pinger
16
16
  * @return {Promise<import('@travetto/compiler/support/launcher').launch>}
17
17
  */
18
18
  const $getLauncher = async (ctx) => {
19
+ const tsconfigFile = path.resolve(ctx.workspacePath, 'tsconfig.json');
20
+ if (!(await fs.stat(tsconfigFile).catch(() => undefined))) {
21
+ await fs.writeFile(tsconfigFile, JSON.stringify({ extends: '@travetto/compiler/tsconfig.trv.json' }), 'utf8');
22
+ }
19
23
  const compPkg = createRequire(path.resolve(ctx.workspacePath, 'node_modules')).resolve('@travetto/compiler/package.json');
20
24
  const files = [];
21
25
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@travetto/compiler",
3
- "version": "3.0.0-rc.32",
4
- "description": "Compiler",
3
+ "version": "3.0.0-rc.34",
4
+ "description": "The compiler infrastructure for the Travetto framework",
5
5
  "keywords": [
6
6
  "compiler",
7
7
  "travetto",
@@ -31,12 +31,12 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@parcel/watcher": "^2.1.0",
34
- "@travetto/manifest": "^3.0.0-rc.17",
35
- "@travetto/terminal": "^3.0.0-rc.10",
36
- "@travetto/transformer": "^3.0.0-rc.21"
34
+ "@travetto/manifest": "^3.0.0-rc.19",
35
+ "@travetto/terminal": "^3.0.0-rc.11",
36
+ "@travetto/transformer": "^3.0.0-rc.23"
37
37
  },
38
38
  "peerDependencies": {
39
- "@travetto/cli": "^3.0.0-rc.22"
39
+ "@travetto/cli": "^3.0.0-rc.25"
40
40
  },
41
41
  "peerDependenciesMeta": {
42
42
  "@travetto/cli": {
package/src/watch.ts CHANGED
@@ -44,10 +44,8 @@ export class CompilerWatcher {
44
44
  for (const ctx of [...contexts, this.#state.manifest]) {
45
45
  const newManifest = await ManifestUtil.buildManifest(ctx);
46
46
  for (const file of files) {
47
- if (
48
- file.folderKey && file.moduleFile && file.type &&
49
- file.mod in newManifest.modules && file.folderKey in newManifest.modules[file.mod].files
50
- ) {
47
+ if (file.folderKey && file.moduleFile && file.type && file.mod in newManifest.modules) {
48
+ newManifest.modules[file.mod].files[file.folderKey] ??= [];
51
49
  newManifest.modules[file.mod].files[file.folderKey]!.push([file.moduleFile, file.type, Date.now()]);
52
50
  }
53
51
  }
@@ -74,7 +72,9 @@ export class CompilerWatcher {
74
72
 
75
73
  return async ({ file: sourceFile, action }: WatchEvent, folder: string): Promise<void> => {
76
74
  const mod = mods[folder];
77
- const moduleFile = sourceFile.includes(mod.sourceFolder) ? sourceFile.split(`${mod.sourceFolder}/`)[1] : sourceFile;
75
+ const moduleFile = mod.sourceFolder ?
76
+ (sourceFile.includes(mod.sourceFolder) ? sourceFile.split(`${mod.sourceFolder}/`)[1] : sourceFile) :
77
+ sourceFile.replace(`${this.#state.manifest.workspacePath}/`, '');
78
78
  switch (action) {
79
79
  case 'create': {
80
80
  const fileType = ManifestModuleUtil.getFileType(moduleFile);
@@ -82,8 +82,8 @@ export class CompilerWatcher {
82
82
  mod: mod.name,
83
83
  modFolder: folder,
84
84
  moduleFile,
85
- folderKey: ManifestModuleUtil.getFolderKey(sourceFile),
86
- type: ManifestModuleUtil.getFileType(sourceFile)
85
+ folderKey: ManifestModuleUtil.getFolderKey(moduleFile),
86
+ type: ManifestModuleUtil.getFileType(moduleFile)
87
87
  });
88
88
  if (CompilerUtil.validFile(fileType)) {
89
89
  await this.#rebuildManifestsIfNeeded();