|
| 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. |
0 commit comments