From 6546e1dc776c60234c419862f3e3759bcdae606a Mon Sep 17 00:00:00 2001 From: Maia Teegarden Date: Wed, 20 Apr 2022 13:59:08 -0700 Subject: [PATCH 1/2] Make setup-wasm script work for local dev --- contributing.md | 2 ++ scripts/setup-wasm.mjs | 42 +++++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/contributing.md b/contributing.md index d4a844c08e7c..f64af46ad1db 100644 --- a/contributing.md +++ b/contributing.md @@ -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 ` 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 diff --git a/scripts/setup-wasm.mjs b/scripts/setup-wasm.mjs index 720cd8930163..9017f45baad2 100644 --- a/scripts/setup-wasm.mjs +++ b/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( + path.join(wasmDir, `${folderName}`), + path.join(process.cwd(), `node_modules/@next/swc-wasm-${wasmTarget}`), + { overwrite: true } + ) + } catch (e) { + console.error(e) + } })() From e2e1d94bfb17f10e7cd7fe230b28bf080467c520 Mon Sep 17 00:00:00 2001 From: Maia Teegarden Date: Thu, 21 Apr 2022 10:08:07 -0700 Subject: [PATCH 2/2] Use copy instead of move --- scripts/setup-wasm.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/setup-wasm.mjs b/scripts/setup-wasm.mjs index 9017f45baad2..3a382d5500cd 100644 --- a/scripts/setup-wasm.mjs +++ b/scripts/setup-wasm.mjs @@ -1,6 +1,6 @@ import path from 'path' import { readFile, writeFile } from 'fs/promises' -import { copy, move, pathExists } from 'fs-extra' +import { copy, pathExists } from 'fs-extra' ;(async function () { try { let wasmDir = path.join(process.cwd(), 'packages/next-swc/crates/wasm') @@ -22,7 +22,7 @@ import { copy, move, pathExists } from 'fs-extra' JSON.stringify(wasmPkg, null, 2) ) - await move( + await copy( path.join(wasmDir, `${folderName}`), path.join(process.cwd(), `node_modules/@next/swc-wasm-${wasmTarget}`), { overwrite: true }