1+ import type { Plugin } from 'vite'
12import { copyFile , cp , mkdir } from 'node:fs/promises'
23import path from 'node:path'
3- import { fileURLToPath , URL } from 'node:url'
44
5+ import { fileURLToPath , URL } from 'node:url'
56import react from '@vitejs/plugin-react'
67import { defineConfig } from 'vite'
7- import topLevelAwait from 'vite-plugin-top-level-await'
88import wasm from 'vite-plugin-wasm'
99
10- function copyComlinkPlugin ( ) {
10+ function copyComlinkPlugin ( ) : Plugin {
1111 return {
1212 name : 'copy-comlink-worker-dependency' ,
1313 apply : 'build' ,
@@ -21,57 +21,79 @@ function copyComlinkPlugin() {
2121 await mkdir ( distAssetsDir , { recursive : true } )
2222 await copyFile ( src , dest )
2323
24+ // Copy worker WASM files if they exist
2425 const workerSrcDir = path . join ( distDir , 'worker-wasm' )
2526 const workerDestDir = path . join ( distAssetsDir , 'worker-wasm' )
26- await cp ( workerSrcDir , workerDestDir , { recursive : true } )
27+ try {
28+ await cp ( workerSrcDir , workerDestDir , { recursive : true } )
29+ }
30+ catch ( error ) {
31+ // Ignore if worker-wasm doesn't exist
32+ if ( ( error as NodeJS . ErrnoException ) . code !== 'ENOENT' ) {
33+ throw error
34+ }
35+ }
2736 } ,
2837 }
2938}
3039
40+ /**
41+ * Vite plugin for Nimiq blockchain integration
42+ * Configures WebAssembly support and optimizations required for @nimiq/core
43+ *
44+ * Note: This plugin does not include top-level await support. Modern browsers
45+ * support top-level await natively. If you need support for older browsers,
46+ * you can add vite-plugin-top-level-await to your plugins array manually.
47+ *
48+ * @param {object } [options] - Plugin options
49+ * @param {boolean } [options.worker] - Configure worker support for WASM
50+ * @returns {import('vite').Plugin[] } Array of Vite plugins
51+ */
52+ function nimiq ( { worker = true } = { } ) {
53+ return [
54+ wasm ( ) ,
55+ {
56+ name : 'vite-plugin-nimiq' ,
57+ config ( ) {
58+ return {
59+ optimizeDeps : {
60+ exclude : [ '@nimiq/core' ] ,
61+ } ,
62+ build : {
63+ target : 'esnext' ,
64+ rollupOptions : {
65+ output : {
66+ format : 'es' ,
67+ } ,
68+ } ,
69+ } ,
70+ ...( worker && {
71+ worker : {
72+ format : 'es' ,
73+ plugins : [ wasm ( ) ] ,
74+ } ,
75+ } ) ,
76+ }
77+ } ,
78+ } ,
79+ ]
80+ }
81+
3182// https://vite.dev/config/
3283export default defineConfig ( {
3384 plugins : [
3485 react ( ) ,
35- wasm ( ) ,
36- topLevelAwait ( {
37- // The module that contains the top-level await
38- promiseExportName : '__tla' ,
39- // The function to generate the promise export name
40- promiseImportName : i => `__tla_${ i } ` ,
41- } ) ,
86+ nimiq ( ) ,
4287 copyComlinkPlugin ( ) ,
4388 ] ,
44- worker : {
45- format : 'es' ,
46- rollupOptions : {
47- output : {
48- entryFileNames : 'assets/[name]-[hash].js' ,
49- chunkFileNames : 'assets/[name]-[hash].js' ,
50- assetFileNames : 'assets/[name]-[hash].[ext]' ,
51- } ,
52- } ,
53- plugins : ( ) => [
54- wasm ( ) ,
55- topLevelAwait ( {
56- promiseExportName : '__tla' ,
57- promiseImportName : i => `__tla_${ i } ` ,
58- } ) ,
59- ] ,
60- } ,
6189 resolve : {
6290 alias : {
6391 '@' : fileURLToPath ( new URL ( './src' , import . meta. url ) ) ,
6492 } ,
6593 } ,
66- optimizeDeps : {
67- exclude : [ '@nimiq/core' ] ,
68- } ,
69- // Additional build configuration for Nimiq
7094 build : {
71- target : 'esnext' ,
7295 rollupOptions : {
7396 output : {
74- format : 'es' ,
7597 entryFileNames : 'assets/[name]-[hash].js' ,
7698 chunkFileNames : 'assets/[name]-[hash].js' ,
7799 assetFileNames : 'assets/[name]-[hash].[ext]' ,
0 commit comments