From a2999c5ea8b35eea06389bb52ee6dee08ededeb7 Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Fri, 27 Dec 2024 17:56:11 +0900 Subject: [PATCH] :children_crossing: Use dynamic import for WASI in loadPrism - To delay the warning `ExperimentalWarning: WASI is an experimental feature and might change at any time` for prism/wasm until the actual moment prism is used. - Introduced the `renameImportWasi` plugin to address an issue where `import 'wasi';` was being included in the build artifact despite being dynamically imported within the `db-structure` package. --- .changeset/blue-worms-retire.md | 6 ++++++ frontend/packages/cli/vite-plugins/index.ts | 1 + .../cli/vite-plugins/remove-import-wasi.ts | 19 +++++++++++++++++++ frontend/packages/cli/vite.config.ts | 4 ++-- .../src/parser/schemarb/loadPrism.ts | 6 ++++-- 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 .changeset/blue-worms-retire.md create mode 100644 frontend/packages/cli/vite-plugins/remove-import-wasi.ts diff --git a/.changeset/blue-worms-retire.md b/.changeset/blue-worms-retire.md new file mode 100644 index 000000000..28d136a19 --- /dev/null +++ b/.changeset/blue-worms-retire.md @@ -0,0 +1,6 @@ +--- +"@liam-hq/db-structure": patch +"@liam-hq/cli": patch +--- + +:children_crossing: Delay the warning `ExperimentalWarning: WASI is an experimental feature and might change at any time` for prism/wasm until the actual moment prism is used. diff --git a/frontend/packages/cli/vite-plugins/index.ts b/frontend/packages/cli/vite-plugins/index.ts index d315dddb0..b707f89f5 100644 --- a/frontend/packages/cli/vite-plugins/index.ts +++ b/frontend/packages/cli/vite-plugins/index.ts @@ -1 +1,2 @@ +export * from './remove-import-wasi.js' export * from './set-env.js' diff --git a/frontend/packages/cli/vite-plugins/remove-import-wasi.ts b/frontend/packages/cli/vite-plugins/remove-import-wasi.ts new file mode 100644 index 000000000..ebf732ed0 --- /dev/null +++ b/frontend/packages/cli/vite-plugins/remove-import-wasi.ts @@ -0,0 +1,19 @@ +import fs from 'node:fs' +import type { Plugin } from 'vite' + +// The `import 'wasi'` statement is dynamically imported within the db-structure package. +// However, `import 'wasi';` gets included in the build output. +// To address this, the buildEnd hook removes it from the build artifact. +export function renameImportWasi(): Plugin { + return { + name: 'remove-import-wasi', + buildEnd() { + const filePath = 'dist-cli/bin/cli.js' + const originalContent = fs.readFileSync(filePath, 'utf8') + const content = originalContent.replace(/import 'wasi';\s*/g, '') + fs.writeFileSync(filePath, content, 'utf8') + }, + } +} + +export default renameImportWasi diff --git a/frontend/packages/cli/vite.config.ts b/frontend/packages/cli/vite.config.ts index 945ea7d77..41374ad9e 100644 --- a/frontend/packages/cli/vite.config.ts +++ b/frontend/packages/cli/vite.config.ts @@ -2,7 +2,7 @@ import { rmSync } from 'node:fs' import react from '@vitejs/plugin-react' import tsconfigPaths from 'vite-tsconfig-paths' import { defineConfig } from 'vitest/config' -import { setEnvPlugin } from './vite-plugins/index.js' +import { renameImportWasi, setEnvPlugin } from './vite-plugins/index.js' const outDir = 'dist-cli/html' @@ -26,7 +26,7 @@ export default defineConfig({ ], }, }, - plugins: [react(), tsconfigPaths(), setEnvPlugin()], + plugins: [react(), tsconfigPaths(), renameImportWasi(), setEnvPlugin()], test: { globals: true, environment: 'node', diff --git a/frontend/packages/db-structure/src/parser/schemarb/loadPrism.ts b/frontend/packages/db-structure/src/parser/schemarb/loadPrism.ts index 3add9a85e..e02aca68e 100644 --- a/frontend/packages/db-structure/src/parser/schemarb/loadPrism.ts +++ b/frontend/packages/db-structure/src/parser/schemarb/loadPrism.ts @@ -16,8 +16,7 @@ Consider removing this patch once support for Node.js v18 is no longer necessary import { readFile } from 'node:fs/promises' // biome-ignore lint/correctness/noNodejsModules: This import is server-side specific because loadPrism() is exclusively invoked in server environments. import { fileURLToPath } from 'node:url' -// biome-ignore lint/correctness/noNodejsModules: This import is server-side specific because loadPrism() is exclusively invoked in server environments. -import { WASI } from 'node:wasi' + import type { ParseResult } from '@ruby/prism/src/deserialize.js' import { parsePrism } from '@ruby/prism/src/parsePrism.js' @@ -32,6 +31,9 @@ export async function loadPrism(): Promise<(source: string) => ParseResult> { overrideWasmUrl ?? fileURLToPath(new URL('prism.wasm', import.meta.url)) const wasm = await WebAssembly.compile(await readFile(path)) + // Dynamic import for WASI to avoid warnings unless necessary + // biome-ignore lint/correctness/noNodejsModules: This import is server-side specific because loadPrism() is exclusively invoked in server environments. + const { WASI } = await import('node:wasi') const wasi = new WASI({ version: 'preview1' }) // Patch applied for compatibility