Skip to content

Commit 02ae63c

Browse files
committed
feat: Allow the use of project-level external config file for js-compute-runtime CLI behavior
1 parent a6e4a1f commit 02ae63c

5 files changed

Lines changed: 196 additions & 25 deletions

File tree

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,89 @@ addEventListener("fetch", event => {
3030
});
3131
```
3232

33+
### CLI Flags and configuration
34+
35+
The CLI is typically invoked by the `build` script defined in your project's `package.json` scripts.
36+
37+
```json
38+
{
39+
"scripts": {
40+
"build": "js-compute-runtime src/index.js bin/main.wasm"
41+
}
42+
}
43+
```
44+
45+
The CLI is invoked as follows:
46+
47+
```sh
48+
js-compute-runtime [OPTIONS] <input-js-file> <output-wasm-file>
49+
```
50+
51+
Options can be specified on the command line or added to a persistent configuration file for the project. The CLI will search for a configuration starting from the current directory in the following order:
52+
53+
* A `fastlycompute` property in `package.json`
54+
* `.fastlycomputerc.json`
55+
* `fastlycompute.config.js`
56+
57+
If an option is defined in both the command line and the configuration file, the command line option takes precedence.
58+
59+
#### Example `.fastlycomputerc.json`
60+
61+
```json
62+
{
63+
"enableAOT": true,
64+
"enableStackTraces": true,
65+
"enableTopLevelAwait": true,
66+
"env": {
67+
"LOG_LEVEL": "debug"
68+
}
69+
}
70+
```
71+
72+
#### Example `package.json`
73+
74+
```json
75+
{
76+
"name": "my-fastly-service",
77+
"type": "module",
78+
"dependencies": {
79+
"@fastly/cli": "^13.0.0"
80+
},
81+
"scripts": {
82+
"build": "js-compute-runtime ./src/index.js ./bin/main.wasm",
83+
"dev": "fastly compute serve",
84+
"deploy": "fastly compute publish"
85+
},
86+
"fastlycompute": {
87+
"enableAOT": true,
88+
"enableStackTraces": true,
89+
"enableTopLevelAwait": true,
90+
"env": {
91+
"LOG_LEVEL": "debug"
92+
}
93+
}
94+
}
95+
```
96+
97+
#### Supported Options
98+
99+
| Config Key | CLI Flag | Type | Description |
100+
|:----------------------------------------------|:-----------------------------------------------------|:----------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------|
101+
| `enableAOT` | `--enable-aot` | `boolean` | Enable AOT compilation for performance |
102+
| `aotCache` | `--aot-cache` | `string` (path) | Specify a path to the AOT cache file |
103+
| `enableHttpCache` | `--enable-http-cache` | `boolean` | Enable the [HTTP cache hook API](https://www.fastly.com/documentation/guides/concepts/cache/#modifying-a-request-as-it-is-forwarded-to-a-backend) |
104+
| `enableExperimentalHighResolutionTimeMethods` | `--enable-experimental-high-resolution-time-methods` | `boolean` | Enable experimental fastly.now() method |
105+
| `enableExperimentalTopLevelAwait` | `--enable-experimental-top-level-await` | `boolean` | Enable experimental top level await |
106+
| `enableStackTraces` | `--enable-stack-traces` | `boolean` | Enable stack traces |
107+
| `excludeSources` | `--exclude-sources` | `boolean` | Don't include sources in stack traces |
108+
| `debugIntermediateFiles` | `--debug-intermediate-files` | `string` (path) | Output intermediate files in directory |
109+
| `debugBuild` | `--debug-build` | `boolean` | Use debug build of the SDK runtime |
110+
| `engineWasm` | `--engine-wasm` | `string` (path) | Specify a custom Wasm engine (advanced) |
111+
| `wevalBin` | `--weval-bin` | `string` (path) | Specify a custom weval binary (advanced) |
112+
| `env` | `--env` | `string \| object \| array` | Set environment variables, possibly inheriting from the current environment. Multiple variables can be comma-separated (e.g., --env ENV_VAR,OVERRIDE=val) |
113+
114+
NOTE: The `env` field is additive. Values defined on the command-line values append to, rather than replace, the values defined in any configuration file.
115+
33116
### API documentation
34117

35118
The API documentation for the JavaScript SDK is located at [https://js-compute-reference-docs.edgecompute.app](https://js-compute-reference-docs.edgecompute.app).

package-lock.json

Lines changed: 37 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@jridgewell/trace-mapping": "^0.3.31",
6464
"acorn": "^8.13.0",
6565
"acorn-walk": "^8.3.4",
66+
"cosmiconfig": "^9.0.1",
6667
"esbuild": "^0.25.0",
6768
"magic-string": "^0.30.12",
6869
"picomatch": "^4.0.3",

src/cli/js-compute-runtime-cli.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import { parseInputs } from '../parseInputs.js';
44
import { printHelp, printVersion } from '../printHelp.js';
55
import { addSdkMetadataField } from '../addSdkMetadataField.js';
6+
import { readConfigFileAndCliArguments } from '../config.js';
67

7-
const parsedInputs = await parseInputs(process.argv.slice(2));
8+
const argv = await readConfigFileAndCliArguments(process.argv.slice(2));
9+
10+
const parsedInputs = await parseInputs(argv);
811

912
if (parsedInputs === 'version') {
1013
await printVersion();

0 commit comments

Comments
 (0)