Closed MiracleWeb3 closed 3 months ago
Uploaded full code from folder to repo for better understanding! https://github.com/MiracleWeb3/vite-tronweb/
You have to move below code before importing TronWeb:
import { Buffer } from 'buffer';
import process from 'process';
window.Buffer = Buffer;
window.process = process;
You can move them to a file named init.js
, then load it before prepareTransaction.js
:
<script type="module" src="./init.js"></script>
<script type="module" src="./prepareTransaction.js"></script>
Including init.js didn't make any changes, so I've been little bit lost, but solution came when I recreated vite app and changed vite.config.js for a little bit.
import { defineConfig } from 'vite';
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
import rollupNodePolyFill from 'rollup-plugin-node-polyfills';
import inject from '@rollup/plugin-inject';
import terser from '@rollup/plugin-terser';
import obfuscatorPlugin from 'vite-plugin-javascript-obfuscator';
import compression from 'vite-plugin-compression';
export default defineConfig({
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis',
},
plugins: [
NodeGlobalsPolyfillPlugin({
buffer: true
})
]
}
},
build: {
rollupOptions: {
plugins: [
rollupNodePolyFill(),
inject({
Buffer: ['buffer', 'Buffer'],
}),
]
},
cssCodeSplit: true, // Correct placement of cssCodeSplit
sourcemap: false, // Correct placement of sourcemap
},
plugins: [
terser({
compress: {
drop_console: true,
drop_debugger: true,
},
format: {
comments: false,
},
}),
obfuscatorPlugin({
options: {
compact: true,
controlFlowFlattening: true,
debugProtection: true,
},
include: ['src/**/*.js'],
exclude: [/node_modules/],
apply: 'build',
}),
compression({
algorithm: 'brotliCompress',
ext: '.br',
threshold: 10240,
deleteOriginFile: false,
}),
],
});
Now everything works fine.
vite.config.js:
Js file:
Found same thing here - https://github.com/tronprotocol/tronweb/issues/488 Tried to do it, error changed:
Do I need to use something else from Vite or error will be here anyway even with Webpack and any other bundler? Any way to solve it? Tried to use vite-compatible-readable-stream, tried to use vite-plugin-node-polyfills. Even tried also to do that in package.json: "overrides": { "browserify-sign": "4.2.2" } Anyway in the end that is what I got:
Any thoughts on how to solve that?