Skip to content

Commit

Permalink
🚸 Use dynamic import for WASI in loadPrism
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
hoshinotsuyoshi committed Dec 27, 2024
1 parent 064997b commit bf5b33a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/blue-worms-retire.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions frontend/packages/cli/vite-plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './remove-import-wasi.js'
export * from './set-env.js'
19 changes: 19 additions & 0 deletions frontend/packages/cli/vite-plugins/remove-import-wasi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from '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
9 changes: 7 additions & 2 deletions frontend/packages/cli/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -26,7 +26,12 @@ export default defineConfig({
],
},
},
plugins: [react(), tsconfigPaths(), setEnvPlugin()],
plugins: [
react(),
tsconfigPaths(),
renameImportWasi(),
setEnvPlugin(),
],
test: {
globals: true,
environment: 'node',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
Expand Down

0 comments on commit bf5b33a

Please sign in to comment.