const path = require('path'); const webpack = require('webpack'); const TerserPlugin = require('terser-webpack-plugin'); const mode = process.env.NODE_ENV ?? 'production'; // The following configuration is required in order to run BitGoJS in the browser. // This file is assumed to be loaded by a package within the modules directory module.exports = { mode, resolve: { alias: { // this is only required if using bitgo instead of just the sdk-api '@hashgraph/sdk': path.resolve('../../node_modules/@hashgraph/sdk/src/browser.js'), // use the default version here since we're webpacking ourselves '@bitgo/sdk-api': path.resolve('../sdk-api/dist/src/index.js'), async: path.resolve('../../node_modules/async/index.js'), // Force ESM versions for browser bundles - required for proper WASM initialization. // Note: We can't use global `conditionNames: ['browser', 'import', ...]` because // third-party packages like @solana/spl-token and @bufbuild/protobuf have broken ESM builds. '@bitgo/wasm-dot': path.resolve('../../node_modules/@bitgo/wasm-dot/dist/esm/js/index.js'), '@bitgo/wasm-ton': path.resolve('../../node_modules/@bitgo/wasm-ton/dist/esm/js/index.js'), '@bitgo/wasm-utxo': path.resolve('../../node_modules/@bitgo/wasm-utxo/dist/esm/js/index.js'), '@bitgo/wasm-solana': path.resolve('../../node_modules/@bitgo/wasm-solana/dist/esm/js/index.js'), '@bitgo/wasm-mps/web': path.resolve('../../node_modules/@bitgo/wasm-mps/dist/web/js/wasm/wasm_mps.js'), '@bitgo/wasm-mps': path.resolve('../../node_modules/@bitgo/wasm-mps/dist/esm/js/wasm/wasm_mps.js'), '@bitgo/utxo-ord': path.resolve('../utxo-ord/dist/esm/index.js'), '@bitgo/sdk-coin-ton': path.resolve('../sdk-coin-ton/dist/esm/index.js'), // @mysten/utils (transitive dep of @mysten/sui, loaded by the SUI gRPC // client) imports the process polyfill with an extension-less request // from a strict-ESM file; webpack's fullySpecified rule rejects it. // Alias the request to the actual polyfill file. 'process/browser': require.resolve('process/browser.js'), // Node-only (http2 at module scope); stubbed out of browser bundles. '@grpc/grpc-js': false, '@protobuf-ts/grpc-transport': false, }, fallback: { constants: false, crypto: require.resolve('crypto-browserify'), dns: false, fs: false, http: require.resolve('stream-http'), https: require.resolve('https-browserify'), http2: require.resolve('stream-http'), net: false, os: false, path: false, stream: require.resolve('stream-browserify'), tls: false, url: require.resolve('url/'), vm: false, zlib: false, async: require.resolve('async'), }, }, externals: ['morgan', 'wasmer_wasi_js_bg.wasm'], plugins: [ new webpack.ProvidePlugin({ Buffer: ['buffer', 'Buffer'], process: 'process/browser', }), new webpack.NormalModuleReplacementPlugin( /\@emurgo\/cardano-serialization-lib-nodejs/, '@emurgo/cardano-serialization-lib-browser' ), new webpack.NormalModuleReplacementPlugin( /\@silencelaboratories\/dkls-wasm-ll-node/, '@silencelaboratories/dkls-wasm-ll-web' ), new webpack.NormalModuleReplacementPlugin( /\@silencelaboratories\/dkls-wasm-ll-vrf-node/, '@silencelaboratories/dkls-wasm-ll-vrf-web' ), new webpack.NormalModuleReplacementPlugin( /\@silencelaboratories\/eddsa-wasm-node/, '@silencelaboratories/eddsa-wasm-web' ), new webpack.ContextReplacementPlugin(/cardano-serialization-lib-browser/), ], node: { global: true, }, experiments: { backCompat: false, asyncWebAssembly: true, syncWebAssembly: true, }, optimization: { minimizer: [ new TerserPlugin({ parallel: true, terserOptions: { ecma: 6, warnings: true, mangle: false, keep_classnames: true, keep_fnames: true, }, }), ], }, };