Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make setup-wasm script work for local dev #36355

Merged
merged 4 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions contributing.md
Expand Up @@ -63,6 +63,8 @@ yarn prepublish

By default the latest canary of the next-swc binaries will be installed and used. If you are actively working on Rust code or you need to test out the most recent Rust code that hasn't been published as a canary yet you can [install Rust](https://www.rust-lang.org/tools/install) and run `yarn --cwd packages/next-swc build-native`.

If you want to test out the wasm build locally, you will need to [install wasm-pack](https://rustwasm.github.io/wasm-pack/installer/). Run `yarn --cwd packages/next-swc build-wasm --target <wasm_target>` to build and `node ./scripts/setup-wasm.mjs` to copy it into your `node_modules`. Run next with `NODE_OPTIONS='--no-addons'` to force it to use the wasm binary.

If you need to clean the project for any reason, use `yarn clean`.

## Testing
Expand Down
42 changes: 27 additions & 15 deletions scripts/setup-wasm.mjs
@@ -1,21 +1,33 @@
import path from 'path'
import { readFile, writeFile } from 'fs/promises'
import { copy } from 'fs-extra'
import { copy, move, pathExists } from 'fs-extra'
;(async function () {
let wasmDir = path.join(process.cwd(), 'packages/next-swc/crates/wasm')
let wasmTarget = 'nodejs'
let wasmPkg = JSON.parse(
await readFile(path.join(wasmDir, `pkg-${wasmTarget}/package.json`))
)
wasmPkg.name = `@next/swc-wasm-${wasmTarget}`
try {
let wasmDir = path.join(process.cwd(), 'packages/next-swc/crates/wasm')
let wasmTarget = 'nodejs'

await writeFile(
path.join(wasmDir, `pkg-${wasmTarget}/package.json`),
JSON.stringify(wasmPkg, null, 2)
)
// CI restores artifact at pkg-${wasmTarget}
// This only runs locally
let folderName = (await pathExists(path.join(wasmDir, 'pkg')))
? 'pkg'
: `pkg-${wasmTarget}`

await copy(
path.join(wasmDir, `pkg-${wasmTarget}`),
path.join(process.cwd(), `node_modules/@next/swc-wasm-${wasmTarget}`)
)
let wasmPkg = JSON.parse(
await readFile(path.join(wasmDir, `${folderName}/package.json`))
)
wasmPkg.name = `@next/swc-wasm-${wasmTarget}`

await writeFile(
path.join(wasmDir, `${folderName}/package.json`),
JSON.stringify(wasmPkg, null, 2)
)

await move(
ijjk marked this conversation as resolved.
Show resolved Hide resolved
path.join(wasmDir, `${folderName}`),
path.join(process.cwd(), `node_modules/@next/swc-wasm-${wasmTarget}`),
{ overwrite: true }
)
} catch (e) {
console.error(e)
}
})()