Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/added/988.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New ID function exports to TypeScript WASM target.
16 changes: 9 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ jobs:
if: github.event_name == 'release' && github.event.action == 'published'
runs-on: buildjet-4vcpu-ubuntu-2204
environment: npm-deploy
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -327,22 +330,21 @@ jobs:
uses: actions/setup-node@v3
with:
cache: "pnpm"
node-version: 18.14.1
node-version: 22.22.0
node-version-file: ".npm/package.json"
cache-dependency-path: ".npm/pnpm-lock.yaml"
registry-url: 'https://registry.npmjs.org'

# Ensure latest NPM is installed to support OIDC publishing
- name: Update npm
run: npm install -g npm@11

- name: Build and Test packages
run: |
pnpm -C .npm install
pnpm -C .npm pack:all

- name: Ensure NPM access
run: npm whoami
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_VM }}

- name: Publish
run: pnpm -C .npm publish -r --access public --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_VM }}
NPM_CONFIG_PROVENANCE: "true"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ Cargo.lock
.idea

.history

.pnpm-store
3 changes: 3 additions & 0 deletions .npm/.scripts/prepare-wasm-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ build_wasm_npm_pkg_for ()
write_template ${NAME_DASHED} ${NAME_UNDERSCORED} package.json
write_template ${NAME_DASHED} ${NAME_UNDERSCORED} rollup.config.mjs
write_template ${NAME_DASHED} ${NAME_UNDERSCORED} src/index.js
write_template ${NAME_DASHED} ${NAME_UNDERSCORED} src/index_slim.js
write_template ${NAME_DASHED} ${NAME_UNDERSCORED} src/index.d.ts
write_template ${NAME_DASHED} ${NAME_UNDERSCORED} src/index_slim.d.ts

# commenting out all `new URL()` and `fetch()` calls for great compatibility with JS bundlers
sed -i.bkp -r 's;(.+= new URL.+);//\1;g' ${PKG_DIR}/src/${NAME_UNDERSCORED}.js
Expand Down
31 changes: 24 additions & 7 deletions .npm/.scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,35 @@ WASM version of `{{NAME_DASHED}}` Rust crate:
- https://github.com/FuelLabs/fuel-vm/tree/master/{{NAME_DASHED}}


# Getting Started
## Getting Started

Be sure to `await` the WASM async initialization:
### Standard Usage (Browser / Node.js)

The default entrypoint includes WASM inlined as base64:

```ts
import * as {{NAME_UNDERSCORED}} from '@fuels/vm-{{PKG_NAME}}'

(async function() {
await {{NAME_UNDERSCORED}}.initWasm();
await {{NAME_UNDERSCORED}}.initWasm();

// {{NAME_UNDERSCORED}}.<?>();
// ...
```

### Slim Usage (Cloudflare Workers / Custom WASM Loading)

The `/slim` entrypoint omits the inlined WASM, requiring you to supply it.
This is necessary for environments like Cloudflare Workers where runtime WASM
compilation is disallowed.

#### Cloudflare Workers

```ts
import * as {{NAME_UNDERSCORED}} from '@fuels/vm-{{PKG_NAME}}/slim'
import wasm from '@fuels/vm-{{PKG_NAME}}/wasm'

// {{NAME_UNDERSCORED}}.<?>();
// ...
})();
await {{NAME_UNDERSCORED}}.initWasm(wasm);

// {{NAME_UNDERSCORED}}.<?>();
// ...
```
22 changes: 22 additions & 0 deletions .npm/.scripts/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
"main": "dist/node/index.cjs",
"types": "dist/node/index.d.ts",
"browser": "dist/web/index.mjs",
"exports": {
".": {
"node": {
"types": "./dist/node/index.d.ts",
"default": "./dist/node/index.cjs"
},
"types": "./dist/web/index.d.ts",
"import": "./dist/web/index.mjs",
"default": "./dist/node/index.cjs"
},
"./slim": {
"types": "./dist/slim/index.d.ts",
"import": "./dist/slim/index.mjs",
"require": "./dist/slim/index.cjs"
},
"./wasm": "./dist/{{NAME_UNDERSCORED}}_bg.wasm",
"./package.json": "./package.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/FuelLabs/fuel-vm.git"
},
"files": [
"dist"
],
Expand Down
33 changes: 29 additions & 4 deletions .npm/.scripts/template/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
import { wasm } from '@rollup/plugin-wasm'
import dts from 'rollup-plugin-dts'
import { copyFileSync } from 'node:fs'

const sync = ['src/{{NAME_UNDERSCORED}}_bg.wasm'];

const copyWasm = {
name: 'copy-wasm',
writeBundle() {
copyFileSync('src/{{NAME_UNDERSCORED}}_bg.wasm', 'dist/{{NAME_UNDERSCORED}}_bg.wasm');
}
};

export default [
{
plugins: [wasm({ sync, targetEnv: 'auto-inline' })],
plugins: [wasm({ sync, targetEnv: 'auto' })],
input: 'src/index.js',
output: [
{ file: 'dist/node/index.cjs', format: 'cjs' },
]
}, {
plugins: [wasm({ sync, targetEnv: 'auto-inline' })],
plugins: [wasm({ sync, targetEnv: 'auto' })],
input: 'src/index.js',
output: [
{ file: 'dist/web/index.mjs', format: 'es' },
]
}, {
input: 'src/index_slim.js',
output: [
{ file: 'dist/slim/index.cjs', format: 'cjs' },
]
}, {
plugins: [copyWasm],
input: 'src/index_slim.js',
output: [
{ file: 'dist/slim/index.mjs', format: 'es' },
]
}, {
plugins: [dts()],
input: 'src/{{NAME_UNDERSCORED}}.d.ts',
input: 'src/index.d.ts',
output: [
{ file: 'dist/web/index.d.ts', format: 'es' },
{ file: 'dist/node/index.d.ts', format: 'es' }
{ file: 'dist/node/index.d.ts', format: 'es' },
],
}, {
plugins: [dts()],
input: 'src/index_slim.d.ts',
output: [
{ file: 'dist/slim/index.d.ts', format: 'es' },
],
}
]
9 changes: 9 additions & 0 deletions .npm/.scripts/template/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './{{NAME_UNDERSCORED}}.js';

export type InitInput =
| WebAssembly.Module
| BufferSource
| Response
| Promise<Response>;

export function initWasm(module_or_path?: InitInput): Promise<void>;
15 changes: 8 additions & 7 deletions .npm/.scripts/template/src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import init from './{{NAME_UNDERSCORED}}.js'
import wasm from './{{NAME_UNDERSCORED}}_bg.wasm'
import wasmModule from './{{NAME_UNDERSCORED}}_bg.wasm'

export async function initWasm () {
return await init({ module_or_path: wasm() });
let _initPromise;

export async function initWasm (module_or_path) {
if (!_initPromise) {
_initPromise = init({ module_or_path: module_or_path ?? wasmModule() });
}
return _initPromise;
}

/**
* calling it right away for pre-caching
* the wasm async initialization at startup
*/
initWasm();

export * from './{{NAME_UNDERSCORED}}.js'
9 changes: 9 additions & 0 deletions .npm/.scripts/template/src/index_slim.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './{{NAME_UNDERSCORED}}.js';

export type InitInput =
| WebAssembly.Module
| BufferSource
| Response
| Promise<Response>;

export function initWasm(module_or_path: InitInput): Promise<void>;
18 changes: 18 additions & 0 deletions .npm/.scripts/template/src/index_slim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import init from './{{NAME_UNDERSCORED}}.js'

let _initPromise;

export async function initWasm (module_or_path) {
if (!_initPromise) {
if (module_or_path == null) {
throw new Error(
'@fuels/vm-{{PKG_NAME}}/slim: initWasm() requires a WebAssembly.Module or BufferSource. ' +
'Use "@fuels/vm-{{PKG_NAME}}" for automatic WASM loading.'
);
}
_initPromise = init({ module_or_path });
}
return _initPromise;
}

export * from './{{NAME_UNDERSCORED}}.js'
4 changes: 2 additions & 2 deletions .npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"chai": "^4.3.10",
"mocha": "^10.2.0",
"npm-run-all": "^4.1.5",
"rollup": "^3.29.4",
"rollup-plugin-dts": "^5.3.1",
"rollup": "^4.57.1",
"rollup-plugin-dts": "^6.3.0",
"turbo": "^2.1.2"
}

Expand Down
Loading
Loading