Skip to content

Commit b63ab68

Browse files
committed
add readmes and add guardrails to debugging flag
Signed-off-by: karthik2804 <[email protected]>
1 parent 4c3af7e commit b63ab68

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed

packages/build-tools/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# `@spinframework/build-tools`
2+
3+
This pacakge provides tooling for building wasm components from JavaScript source code.
4+
5+
## Contents of the package
6+
7+
### Build Tool CLI
8+
9+
The package also provides a node executable `j2w` that enables compiling JS source code to wasm components.
10+
11+
```bash
12+
$ npx j2w --help
13+
Options:
14+
--help Show help [boolean]
15+
--version Show version number [boolean]
16+
-i, --input Path to the input file [required]
17+
-o, --output Path to the output file [default: "component.wasm"]
18+
--aot Enable Ahead of Time compilation [boolean]
19+
-d, --debug Enable JavaScript debugging [boolean]
20+
```
21+
22+
**Note:** The input source should be Javascript.
23+
24+
#### `wit-tools` crate
25+
26+
This crate provides tools for interacting with `wit` files. It is compiled into a wasm component and then consumed from the CLI.
27+
28+
### Webpack Plugin
29+
30+
The webpack plugin can be found at [`./plugins/webpack/index.js`](./plugins/webpack/index.js). It can be used to add externals (modules that will be available at runtime) to the config automatically based on the `wit` imports of the packages being bundled. The plugin needs to be initialized before it can be used. The initialization is `async`, so it has to be awaited. A example usage:
31+
32+
```js
33+
import SpinSdkPlugin from "@spinframework/build-tools/plugins/webpack/index.js";
34+
const config = async () => {
35+
return {
36+
...
37+
plugins: [
38+
await SpinSdkPlugin.init()
39+
]
40+
}
41+
}
42+
43+
export default config
44+
```
45+
46+
## How the Build System Works
47+
48+
The build system works by parsing through the dependencies of a package as defined in the `package.json`. It recursively parses the dependencies to parse out all the package that have a `wit` dependency. The list of `wit` dependencies is then used to create a merged target world which is the union of all the dependencies.
49+
50+
### Configuring `package.json` for `wit` Dependencies
51+
52+
For packages that leverage `wit` dependencies, they need to configure the `package.json` to include information about the location of the `wit`, the target world and the package name. The configuration needs nested under a `witDependencies` field be under the `config` field of the `package.json`. An example configuration:
53+
54+
```json
55+
...
56+
"config": {
57+
"witDependencies": [
58+
{
59+
"witPath": "./wit",
60+
"package": "spinframework:[email protected]",
61+
"world": "wasi-cli"
62+
}
63+
]
64+
}
65+
...
66+
```
67+
68+
### Testing the package
69+
70+
The package has some tests for the `wit` dependency parser and can be run using
71+
72+
```bash
73+
npm run test
74+
```
75+
76+
**Note:** Make sure to have a new enough version of `node.js`that can run typescript natively.

packages/build-tools/crates/wit-tools/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "wit-tools"
33
version.workspace = true
44
edition.workspace = true
5+
description = "package containing build tools to help with managing wit dependencies"
56

67
[lib]
78
crate-type = ["cdylib"]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# `wit-tools`
2+
3+
This crate provides useful functionality for mananging wit dependencies in projects. It currently provides the functionality of:
4+
- Merging multiple `wit` packages and outputting a combined world.
5+
- Getting all the imports of a given world.
6+
7+
This crate can be compiled as a webassembly component and then consumed in language that do not have direct support for `wasm-tools` but has a wasm runtime capable of running components. (e.g) Can be used in Javascript programs using JCO.
8+
9+
## Developing
10+
11+
The `wit` world that the library implements is defined in the [`wit` directory](./wit).
12+
13+
### Building
14+
15+
The component can be build by running
16+
17+
```bash
18+
cargo build --release
19+
```

packages/build-tools/src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from './utils.js';
1111
import { getCliArgs } from './cli.js';
1212
import { getBuildDataPath, ShouldComponentize } from './build.js';
13-
import { readFile, writeFile } from 'node:fs/promises';
13+
import { writeFile } from 'node:fs/promises';
1414
import { mergeWit } from '../lib/wit_tools.js';
1515

1616
async function main() {
@@ -32,12 +32,20 @@ async function main() {
3232

3333
// generate wit world string
3434
let wasiDeps = getPackagesWithWasiDeps(process.cwd(), new Set(), true);
35-
3635
let { witPaths, targetWorlds } = processWasiDeps(wasiDeps);
3736

3837
// Debugging requires some interfaces around reading env vars and making
3938
// socket connections to be available.
4039
if (CliArgs.debug) {
40+
// check if @spinframework/http-trigger is already in the targetWorlds
41+
let httpTrigger = targetWorlds.find(
42+
(world) => world.packageName === 'spinframework:[email protected]',
43+
);
44+
if (!httpTrigger) {
45+
throw new Error(
46+
'Debugging requires the @spinframework/http-trigger package to be included in the target worlds.',
47+
);
48+
}
4149
targetWorlds.push({
4250
packageName: 'spinframework:[email protected]',
4351
worldName: 'debugging-support',
@@ -49,7 +57,8 @@ async function main() {
4957
witPaths,
5058
targetWorlds,
5159
'combined',
52-
'combined-wit:[email protected]',
60+
// Hardcode the version to atleast 0.2.0 to deal with wasm-tools bug with "include" slurping original package when `@since` is present
61+
'combined-wit:[email protected]',
5362
);
5463

5564
const { component } = await componentize({

0 commit comments

Comments
 (0)