Closed MiracleWeb3 closed 2 weeks 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:![image](https://github.com/user-attachments/assets/16232d3c-a77e-4e49-8822-1b1f0228c1b1)
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:![image](https://github.com/user-attachments/assets/e46dfe57-8459-4c05-91f7-6b0360545886)
Any thoughts on how to solve that?