@travetto/compiler 3.0.2-rc.1 → 3.0.3
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 +36 -22
- package/bin/trv.js +5 -5
- package/package.json +5 -5
- package/src/compiler.ts +2 -2
- package/src/state.ts +13 -12
- package/src/util.ts +5 -14
- package/src/watch.ts +4 -1
- package/support/transpile.ts +38 -12
- package/tsconfig.trv.json +1 -0
package/README.md
CHANGED
|
@@ -1,6 +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/DOC.
|
|
2
|
+
<!-- Please modify https://github.com/travetto/travetto/tree/main/module/compiler/DOC.tsx and execute "npx trv doc" to rebuild -->
|
|
3
3
|
# Compiler
|
|
4
|
+
|
|
4
5
|
## The compiler infrastructure for the Travetto framework
|
|
5
6
|
|
|
6
7
|
**Install: @travetto/compiler**
|
|
@@ -13,37 +14,57 @@ yarn add @travetto/compiler
|
|
|
13
14
|
```
|
|
14
15
|
|
|
15
16
|
This module expands upon the [Typescript](https://typescriptlang.org) compiler, with the additional features:
|
|
16
|
-
|
|
17
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
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
19
|
* Removal of type only imports which can break [Ecmascript Module](https://nodejs.org/api/esm.html)-style output
|
|
20
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.
|
|
21
|
+
Beyond the [Typescript](https://typescriptlang.org) compiler functionality, the module provides the primary entry point into the development process.
|
|
23
22
|
|
|
24
23
|
## CLI
|
|
25
|
-
|
|
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.
|
|
24
|
+
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
25
|
|
|
28
26
|
The CLI supports the following operations:
|
|
29
|
-
|
|
30
|
-
|
|
31
27
|
* `clean` - Removes the output folder, and if `-a` is also passed, will also clean out the compiler folder
|
|
32
28
|
* `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
29
|
* `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
|
|
30
|
+
* `manifest` - Will produce a manifest. If no file is passed in the command line arguments, will output to stdout.
|
|
35
31
|
* `<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
32
|
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
33
|
|
|
39
34
|
**Terminal: Sample trv output with debug logging**
|
|
40
35
|
```bash
|
|
41
36
|
$ TRV_BUILD=debug trv build
|
|
42
37
|
|
|
43
|
-
2029-03-14T04:00:00.618Z [lock ]
|
|
44
|
-
2029-03-14T04:00:00.837Z [
|
|
45
|
-
2029-03-14T04:00:01.510Z [
|
|
46
|
-
2029-03-14T04:00:02.450Z [
|
|
38
|
+
2029-03-14T04:00:00.618Z [lock ] Acquiring build
|
|
39
|
+
2029-03-14T04:00:00.837Z [precompile ] Started
|
|
40
|
+
2029-03-14T04:00:01.510Z [precompile ] @travetto/terminal Skipped
|
|
41
|
+
2029-03-14T04:00:02.450Z [precompile ] @travetto/manifest Skipped
|
|
42
|
+
2029-03-14T04:00:02.762Z [precompile ] @travetto/transformer Skipped
|
|
43
|
+
2029-03-14T04:00:02.947Z [precompile ] @travetto/compiler Skipped
|
|
44
|
+
2029-03-14T04:00:03.093Z [precompile ] Completed
|
|
45
|
+
2029-03-14T04:00:04.003Z [manifest ] Started
|
|
46
|
+
2029-03-14T04:00:04.495Z [manifest ] Completed
|
|
47
|
+
2029-03-14T04:00:05.066Z [transformers ] Started
|
|
48
|
+
2029-03-14T04:00:05.307Z [transformers ] @travetto-test/transformer Skipped
|
|
49
|
+
2029-03-14T04:00:05.952Z [transformers ] @travetto/app Skipped
|
|
50
|
+
2029-03-14T04:00:06.859Z [transformers ] @travetto/base Skipped
|
|
51
|
+
2029-03-14T04:00:07.720Z [transformers ] @travetto/cache Skipped
|
|
52
|
+
2029-03-14T04:00:08.179Z [transformers ] @travetto/di Skipped
|
|
53
|
+
2029-03-14T04:00:08.588Z [transformers ] @travetto/manifest Skipped
|
|
54
|
+
2029-03-14T04:00:09.493Z [transformers ] @travetto/registry Skipped
|
|
55
|
+
2029-03-14T04:00:10.395Z [transformers ] @travetto/rest Skipped
|
|
56
|
+
2029-03-14T04:00:10.407Z [transformers ] @travetto/schema Skipped
|
|
57
|
+
2029-03-14T04:00:10.799Z [transformers ] @travetto/test Skipped
|
|
58
|
+
2029-03-14T04:00:11.013Z [transformers ] Completed
|
|
59
|
+
2029-03-14T04:00:11.827Z [delta ] Started
|
|
60
|
+
2029-03-14T04:00:11.894Z [delta ] Completed
|
|
61
|
+
2029-03-14T04:00:12.133Z [manifest ] Started
|
|
62
|
+
2029-03-14T04:00:13.123Z [manifest ] Wrote manifest @travetto/mono-repo
|
|
63
|
+
2029-03-14T04:00:14.014Z [manifest ] Completed
|
|
64
|
+
2029-03-14T04:00:14.924Z [compile ] Started action=build changed=
|
|
65
|
+
2029-03-14T04:00:15.690Z [compile ] Skipped
|
|
66
|
+
2029-03-14T04:00:15.865Z [lock ] Releasing build
|
|
67
|
+
2029-03-14T04:00:16.757Z [build ] Successfully built
|
|
47
68
|
```
|
|
48
69
|
|
|
49
70
|
**Terminal: Sample trv output with default log level**
|
|
@@ -52,9 +73,7 @@ $ trv build
|
|
|
52
73
|
```
|
|
53
74
|
|
|
54
75
|
## Compilation Architecture
|
|
55
|
-
|
|
56
76
|
The compiler will move through the following phases on a given compilation execution:
|
|
57
|
-
|
|
58
77
|
* `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
78
|
* `Lock Management` - Manages cross-process interaction to ensure single compiler
|
|
60
79
|
* `Build Compiler` - Leverages [Typescript](https://typescriptlang.org) to build files needed to execute compiler
|
|
@@ -66,17 +85,12 @@ The compiler will move through the following phases on a given compilation execu
|
|
|
66
85
|
* `Invoke Compiler` - Run [Typescript](https://typescriptlang.org) compiler with the aforementioned enhancements
|
|
67
86
|
|
|
68
87
|
### 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.
|
|
88
|
+
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
89
|
|
|
72
90
|
### Lock Management
|
|
73
|
-
|
|
74
91
|
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
92
|
* No Watch - Building
|
|
78
93
|
* Watch - No Build
|
|
79
94
|
* Watch - Building
|
|
80
95
|
* Inactive / Stale
|
|
81
|
-
|
|
82
96
|
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
|
@@ -9,7 +9,7 @@ import { getManifestContext } from '@travetto/manifest/bin/context.js';
|
|
|
9
9
|
|
|
10
10
|
const VALID_OPS = { watch: 'watch', build: 'build', clean: 'clean', manifest: 'manifest' };
|
|
11
11
|
|
|
12
|
-
const COMPILER_FILES = [...['launcher', 'transpile', 'lock', 'log', 'lock-pinger'].map(x => `support/${x}.
|
|
12
|
+
const COMPILER_FILES = [...['launcher', 'transpile', 'lock', 'log', 'lock-pinger'].map(x => `support/${x}.ts`), 'package.json'];
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* @param {import('@travetto/manifest').ManifestContext} ctx
|
|
@@ -20,12 +20,12 @@ const $getLauncher = async (ctx) => {
|
|
|
20
20
|
if (!(await fs.stat(tsconfigFile).catch(() => undefined))) {
|
|
21
21
|
await fs.writeFile(tsconfigFile, JSON.stringify({ extends: '@travetto/compiler/tsconfig.trv.json' }), 'utf8');
|
|
22
22
|
}
|
|
23
|
-
const
|
|
23
|
+
const compMod = path.dirname(createRequire(path.resolve(ctx.workspacePath, 'node_modules')).resolve('@travetto/compiler/package.json'));
|
|
24
24
|
const files = [];
|
|
25
25
|
|
|
26
26
|
for (const file of COMPILER_FILES) {
|
|
27
|
-
const target = path.resolve(ctx.workspacePath, ctx.compilerFolder, 'node_modules', '@travetto/compiler', file);
|
|
28
|
-
const src =
|
|
27
|
+
const target = path.resolve(ctx.workspacePath, ctx.compilerFolder, 'node_modules', '@travetto/compiler', file).replace(/[.]tsx?$/, '.js');
|
|
28
|
+
const src = path.resolve(compMod, file);
|
|
29
29
|
|
|
30
30
|
const targetTime = await fs.stat(target).then(s => Math.max(s.mtimeMs, s.ctimeMs)).catch(() => 0);
|
|
31
31
|
const srcTime = await fs.stat(src).then(s => Math.max(s.mtimeMs, s.ctimeMs));
|
|
@@ -35,7 +35,7 @@ const $getLauncher = async (ctx) => {
|
|
|
35
35
|
const module = ctx.moduleType === 'module' ? ts.ModuleKind.ESNext : ts.ModuleKind.CommonJS;
|
|
36
36
|
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
37
37
|
const text = await fs.readFile(src, 'utf8');
|
|
38
|
-
if (
|
|
38
|
+
if (/[.]tsx?$/.test(file)) {
|
|
39
39
|
const content = ts.transpile(
|
|
40
40
|
text,
|
|
41
41
|
{ target: ts.ScriptTarget.ES2020, module, esModuleInterop: true, allowSyntheticDefaultImports: true }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/compiler",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.3",
|
|
4
4
|
"description": "The compiler infrastructure for the Travetto framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"compiler",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@parcel/watcher": "^2.1.0",
|
|
34
|
-
"@travetto/manifest": "^3.0.
|
|
35
|
-
"@travetto/terminal": "^3.0.
|
|
36
|
-
"@travetto/transformer": "^3.0.
|
|
34
|
+
"@travetto/manifest": "^3.0.3",
|
|
35
|
+
"@travetto/terminal": "^3.0.3",
|
|
36
|
+
"@travetto/transformer": "^3.0.3"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@travetto/cli": "^3.0.
|
|
39
|
+
"@travetto/cli": "^3.0.3"
|
|
40
40
|
},
|
|
41
41
|
"peerDependenciesMeta": {
|
|
42
42
|
"@travetto/cli": {
|
package/src/compiler.ts
CHANGED
|
@@ -3,7 +3,7 @@ import ts from 'typescript';
|
|
|
3
3
|
import fs from 'fs/promises';
|
|
4
4
|
|
|
5
5
|
import { GlobalTerminal, TerminalProgressEvent } from '@travetto/terminal';
|
|
6
|
-
import { RootIndex } from '@travetto/manifest';
|
|
6
|
+
import { ManifestModuleUtil, RootIndex } from '@travetto/manifest';
|
|
7
7
|
|
|
8
8
|
import { CompilerUtil } from './util';
|
|
9
9
|
import { CompilerState } from './state';
|
|
@@ -22,7 +22,7 @@ export class Compiler {
|
|
|
22
22
|
static async main(): Promise<void> {
|
|
23
23
|
const [dirty, watch] = process.argv.slice(2);
|
|
24
24
|
const state = await CompilerState.get(RootIndex);
|
|
25
|
-
const dirtyFiles = (await fs.readFile(dirty, 'utf8')).split(/\n/).filter(x => !!x);
|
|
25
|
+
const dirtyFiles = ManifestModuleUtil.getFileType(dirty) === 'ts' ? [dirty] : (await fs.readFile(dirty, 'utf8')).split(/\n/).filter(x => !!x);
|
|
26
26
|
await new Compiler(state, dirtyFiles, watch === 'true').run();
|
|
27
27
|
process.exit(0);
|
|
28
28
|
}
|
package/src/state.ts
CHANGED
|
@@ -104,17 +104,18 @@ export class CompilerState implements ts.CompilerHost {
|
|
|
104
104
|
return prog;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
writeInputFile(program: ts.Program, inputFile: string): ts.EmitResult | undefined {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
program.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
107
|
+
writeInputFile(program: ts.Program, inputFile: string): ts.EmitResult | undefined | void {
|
|
108
|
+
switch (ManifestModuleUtil.getFileType(inputFile)) {
|
|
109
|
+
case 'package-json':
|
|
110
|
+
return this.writeFile(this.#inputToEntry.get(inputFile)!.output!, this.readFile(inputFile)!, false);
|
|
111
|
+
case 'js':
|
|
112
|
+
return this.writeFile(this.#inputToEntry.get(inputFile)!.output!, ts.transpile(this.readFile(inputFile)!, this.#compilerOptions), false);
|
|
113
|
+
case 'ts':
|
|
114
|
+
return program.emit(
|
|
115
|
+
program.getSourceFile(inputFile)!,
|
|
116
|
+
(...args) => this.writeFile(...args), undefined, false,
|
|
117
|
+
this.#transformerManager.get()
|
|
118
|
+
);
|
|
118
119
|
}
|
|
119
120
|
}
|
|
120
121
|
|
|
@@ -131,7 +132,7 @@ export class CompilerState implements ts.CompilerHost {
|
|
|
131
132
|
const fileType = ManifestModuleUtil.getFileType(moduleFile);
|
|
132
133
|
const outputFile = fileType === 'typings' ?
|
|
133
134
|
undefined :
|
|
134
|
-
path.resolve(this.#outputPath,
|
|
135
|
+
path.resolve(this.#outputPath, ManifestModuleUtil.sourceToOutputExt(relativeInput));
|
|
135
136
|
|
|
136
137
|
const entry = { source: sourceFile, input: inputFile, output: outputFile, module, relativeInput };
|
|
137
138
|
|
package/src/util.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
|
|
3
|
-
import { ManifestContext, ManifestModuleFileType, ManifestRoot, Package, path } from '@travetto/manifest';
|
|
3
|
+
import { ManifestContext, ManifestModuleFileType, ManifestModuleUtil, ManifestRoot, Package, path } from '@travetto/manifest';
|
|
4
4
|
|
|
5
5
|
type OutputToSource = (outputFile: string) => ({ source: string } | undefined);
|
|
6
6
|
export type FileWatchEvent = { type: 'create' | 'delete' | 'update', path: string };
|
|
@@ -17,15 +17,6 @@ export class CompilerUtil {
|
|
|
17
17
|
*/
|
|
18
18
|
static validFile = (type: ManifestModuleFileType): boolean => type === 'ts' || type === 'package-json' || type === 'js';
|
|
19
19
|
|
|
20
|
-
/**
|
|
21
|
-
* Map input file to output format, generally converting ts extensions to js
|
|
22
|
-
* @param file
|
|
23
|
-
* @returns
|
|
24
|
-
*/
|
|
25
|
-
static inputToOutput(file: string): string {
|
|
26
|
-
return file.replace(/[.][tj]s$/, '.js');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
20
|
/**
|
|
30
21
|
* Determines if write callback data has sourcemap information
|
|
31
22
|
* @param data
|
|
@@ -41,7 +32,7 @@ export class CompilerUtil {
|
|
|
41
32
|
*/
|
|
42
33
|
static rewriteSourceMap(ctx: ManifestContext, text: string, outputToSource: OutputToSource): string {
|
|
43
34
|
const data: { sourceRoot?: string, sources: string[] } = JSON.parse(text);
|
|
44
|
-
const output =
|
|
35
|
+
const output = ManifestModuleUtil.sourceToOutputExt(path.resolve(ctx.workspacePath, ctx.outputFolder, data.sources[0]));
|
|
45
36
|
const { source: file } = outputToSource(output) ?? {};
|
|
46
37
|
|
|
47
38
|
if (file) {
|
|
@@ -77,7 +68,7 @@ export class CompilerUtil {
|
|
|
77
68
|
}
|
|
78
69
|
|
|
79
70
|
/**
|
|
80
|
-
* Rewrites the package.json to target
|
|
71
|
+
* Rewrites the package.json to target output file names, and pins versions
|
|
81
72
|
* @param manifest
|
|
82
73
|
* @param file
|
|
83
74
|
* @param text
|
|
@@ -86,10 +77,10 @@ export class CompilerUtil {
|
|
|
86
77
|
static rewritePackageJSON(manifest: ManifestRoot, text: string): string {
|
|
87
78
|
const pkg: Package = JSON.parse(text);
|
|
88
79
|
if (pkg.files) {
|
|
89
|
-
pkg.files = pkg.files.map(x =>
|
|
80
|
+
pkg.files = pkg.files.map(x => ManifestModuleUtil.sourceToOutputExt(x));
|
|
90
81
|
}
|
|
91
82
|
if (pkg.main) {
|
|
92
|
-
pkg.main =
|
|
83
|
+
pkg.main = ManifestModuleUtil.sourceToOutputExt(pkg.main);
|
|
93
84
|
}
|
|
94
85
|
pkg.type = manifest.moduleType;
|
|
95
86
|
for (const key of ['devDependencies', 'dependencies', 'peerDependencies'] as const) {
|
package/src/watch.ts
CHANGED
|
@@ -133,7 +133,10 @@ export class CompilerWatcher {
|
|
|
133
133
|
const remove = (outputFile: string): Promise<void> => fs.rm(outputFile, { force: true });
|
|
134
134
|
const handler = this.#getWatcher({ create: emit, update: emit, delete: remove });
|
|
135
135
|
const options: WatchConfig = {
|
|
136
|
-
filter: ev =>
|
|
136
|
+
filter: ev => {
|
|
137
|
+
const type = ManifestModuleUtil.getFileType(ev.file);
|
|
138
|
+
return type === 'ts' || type === 'typings' || type === 'js' || type === 'package-json';
|
|
139
|
+
},
|
|
137
140
|
ignore: ['node_modules']
|
|
138
141
|
};
|
|
139
142
|
|
package/support/transpile.ts
CHANGED
|
@@ -19,6 +19,27 @@ const RECENT_STAT = (stat: { ctimeMs: number, mtimeMs: number }): number => Math
|
|
|
19
19
|
* Transpile utilities for launching
|
|
20
20
|
*/
|
|
21
21
|
export class TranspileUtil {
|
|
22
|
+
/**
|
|
23
|
+
* Determine file type
|
|
24
|
+
*/
|
|
25
|
+
static getFileType(file: string): 'ts' | 'js' | 'package-json' | 'typings' | undefined {
|
|
26
|
+
return file.endsWith('package.json') ? 'package-json' :
|
|
27
|
+
(file.endsWith('.js') ? 'js' :
|
|
28
|
+
(file.endsWith('.d.ts') ? 'typings' : (/[.]tsx?$/.test(file) ? 'ts' : undefined)));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Convert a file to a given ext */
|
|
32
|
+
static #sourceToExtension(inputFile: string, ext: string): string {
|
|
33
|
+
return inputFile.replace(/[.][tj]sx?$/, ext);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Get the output file name for a given input
|
|
38
|
+
*/
|
|
39
|
+
static sourceToOutputExt(inputFile: string): string {
|
|
40
|
+
return this.#sourceToExtension(inputFile, '.js');
|
|
41
|
+
}
|
|
42
|
+
|
|
22
43
|
/**
|
|
23
44
|
* Write text file, and ensure folder exists
|
|
24
45
|
*/
|
|
@@ -45,8 +66,6 @@ export class TranspileUtil {
|
|
|
45
66
|
OPT_CACHE[ctx.workspacePath] = {
|
|
46
67
|
...options,
|
|
47
68
|
allowJs: true,
|
|
48
|
-
sourceMap: false,
|
|
49
|
-
inlineSourceMap: true,
|
|
50
69
|
resolveJsonModule: true,
|
|
51
70
|
sourceRoot: ctx.workspacePath,
|
|
52
71
|
rootDir: ctx.workspacePath,
|
|
@@ -61,7 +80,8 @@ export class TranspileUtil {
|
|
|
61
80
|
* Output a file, support for ts, js, and package.json
|
|
62
81
|
*/
|
|
63
82
|
static async transpileFile(ctx: ManifestContext, inputFile: string, outputFile: string): Promise<void> {
|
|
64
|
-
|
|
83
|
+
const type = this.getFileType(inputFile);
|
|
84
|
+
if (type === 'js' || type === 'ts') {
|
|
65
85
|
const compilerOut = path.resolve(ctx.workspacePath, ctx.compilerFolder, 'node_modules');
|
|
66
86
|
|
|
67
87
|
const text = (await fs.readFile(inputFile, 'utf8'))
|
|
@@ -69,12 +89,16 @@ export class TranspileUtil {
|
|
|
69
89
|
.replace(/from '(@travetto\/(.*?))'/g, (_, i, s) => `from '${path.resolve(compilerOut, `${i}${s.includes('/') ? '.js' : '/__index__.js'}`)}'`);
|
|
70
90
|
|
|
71
91
|
const ts = (await import('typescript')).default;
|
|
72
|
-
const content = ts.transpile(text,
|
|
92
|
+
const content = ts.transpile(text, {
|
|
93
|
+
...await this.getCompilerOptions(ctx),
|
|
94
|
+
sourceMap: false,
|
|
95
|
+
inlineSourceMap: true,
|
|
96
|
+
}, inputFile);
|
|
73
97
|
await this.writeTextFile(outputFile, content);
|
|
74
|
-
} else if (
|
|
98
|
+
} else if (type === 'package-json') {
|
|
75
99
|
const pkg: Package = JSON.parse(await fs.readFile(inputFile, 'utf8'));
|
|
76
|
-
const main = pkg.main
|
|
77
|
-
const files = pkg.files?.map(x =>
|
|
100
|
+
const main = pkg.main ? this.sourceToOutputExt(pkg.main) : undefined;
|
|
101
|
+
const files = pkg.files?.map(x => this.sourceToOutputExt(x));
|
|
78
102
|
|
|
79
103
|
const content = JSON.stringify({ ...pkg, main, type: ctx.moduleType, files }, null, 2);
|
|
80
104
|
await this.writeTextFile(outputFile, content);
|
|
@@ -107,10 +131,12 @@ export class TranspileUtil {
|
|
|
107
131
|
|
|
108
132
|
if (stat.isDirectory()) {
|
|
109
133
|
folders.push(resolvedInput);
|
|
110
|
-
} else
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
134
|
+
} else {
|
|
135
|
+
switch (this.getFileType(file)) {
|
|
136
|
+
case 'js':
|
|
137
|
+
case 'ts':
|
|
138
|
+
files.push(resolvedInput);
|
|
139
|
+
}
|
|
114
140
|
}
|
|
115
141
|
}
|
|
116
142
|
}
|
|
@@ -118,7 +144,7 @@ export class TranspileUtil {
|
|
|
118
144
|
const outputFolder = path.resolve(ctx.workspacePath, ctx.compilerFolder, 'node_modules', module);
|
|
119
145
|
const out: ModFile[] = [];
|
|
120
146
|
for (const input of files) {
|
|
121
|
-
const output = input.replace(inputFolder, outputFolder)
|
|
147
|
+
const output = this.sourceToOutputExt(input.replace(inputFolder, outputFolder));
|
|
122
148
|
const inputTs = await fs.stat(input).then(RECENT_STAT, () => 0);
|
|
123
149
|
if (inputTs) {
|
|
124
150
|
const outputTs = await fs.stat(output).then(RECENT_STAT, () => 0);
|