Skip to content

Commit 9ceb675

Browse files
authored
Vite as suite devserver (#17152)
* fix(suite): remove immer with Object.freeze from guideReducer as it contains 'react' structure * chore(suite): add ignore comments to dynamic imports for vite * fix(suite): make the calling of the GraphWorker as a function call, not constructor * fix(connect): make the imports of the dynamic connect methods "static" without template variables in the paths * fix(suite): edit the dynamic imports to make the more reliable * chore(suite): add info about yarn suite:dev:vite command to README.md * feat(suite): add option to have vite as a dev server * feat(suite): have Vite devserver serve the webpack original index.html file * fix(suite): make the dynamic import of the flag svg actaully dynamic * fix(suite): fix circular dependency * chore(connect-expolorer-theme): remove unused vitest from connect-expolorer-theme
1 parent 1748b5d commit 9ceb675

File tree

31 files changed

+851
-573
lines changed

31 files changed

+851
-573
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Historically, Trezor Connect had its [own repository](https://github.com/trezor/
4444
Run a dev build:
4545

4646
- `yarn suite:dev` (web app)
47+
- `yarn suite:dev:vite` (⚠️ EXPERIMENTAL: web app with Vite bundler used for **development only**, use `yarn suite:dev` if you want fidelity to production app)
4748
- `yarn suite:dev:desktop` (electron app)
4849

4950
## **Trezor Suite Lite** @suite-native/app

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"suite:build:web": "yarn workspace @trezor/suite-web build",
2626
"_______ Start Scripts _______": "Here are standalone scripts for running individual applications for development.",
2727
"suite:dev": "yarn workspace @trezor/suite-web dev",
28+
"suite:dev:vite": "yarn workspace @trezor/suite-web dev:vite",
2829
"suite:dev:desktop": "yarn workspace @trezor/suite-desktop dev",
2930
"native:start": "yarn workspace @suite-native/app start",
3031
"_______ Testing _______": "Testing, linting, type checking...",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const baseConfig = require('../../jest.config.base');
2+
3+
module.exports = {
4+
...baseConfig,
5+
transformIgnorePatterns: [
6+
...(baseConfig.transformIgnorePatterns ?? []),
7+
'/node_modules/(?!chai/)',
8+
],
9+
};

packages/address-validator/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"private": true,
77
"main": "src/index",
88
"scripts": {
9-
"test:unit": "yarn g:jest -c ../../jest.config.base.js"
9+
"test:unit": "yarn g:jest -c ./jest.config.js"
1010
},
1111
"dependencies": {
1212
"base-x": "^5.0.0",

packages/address-validator/src/crypto/cnBase58.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var JSBigInt = require('./biginteger')['JSBigInt'];
1+
var moduleImport = require('./biginteger');
2+
var JSBigInt = moduleImport.JSBigInt;
23

34
/**
45
Copyright (c) 2017, moneroexamples

packages/address-validator/tests/wallet_address_get_currencies.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var isNode = typeof module !== 'undefined' && typeof module.exports !== 'undefined';
1+
var isNode = true;
22

33
var chai = isNode ? require('chai') : window.chai,
44
expect = chai.expect;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { useEffect, useState } from 'react';
2+
13
import styled from 'styled-components';
24

35
import { FLAGS } from './flags';
6+
import { SkeletonRectangle } from '../skeletons/SkeletonRectangle';
47

58
export type FlagType = keyof typeof FLAGS;
69

@@ -15,13 +18,26 @@ export interface FlagProps {
1518
size?: number;
1619
}
1720

18-
export const Flag = ({ size = 24, country, className }: FlagProps) => (
19-
<Wrapper>
20-
<img
21-
src={require(`../../images/flags/${country.toLowerCase()}.svg`)}
22-
width={`${size}px`}
23-
alt={`flag-${country}`}
24-
className={className}
25-
/>
26-
</Wrapper>
27-
);
21+
export const Flag = ({ size = 24, country, className }: FlagProps) => {
22+
const [src, setSrc] = useState('');
23+
useEffect(() => {
24+
import(`../../images/flags/${country.toLowerCase()}.svg`)
25+
.then(module => {
26+
setSrc(module.default);
27+
})
28+
.catch(err => {
29+
// NOTE: keep error here as this is not a critical issue
30+
console.error('Flag image loading error: ', err);
31+
});
32+
}, [country]);
33+
34+
return (
35+
<Wrapper>
36+
{src ? (
37+
<img src={src} width={`${size}px`} alt={`flag-${country}`} className={className} />
38+
) : (
39+
<SkeletonRectangle width={size} height={size} />
40+
)}
41+
</Wrapper>
42+
);
43+
};

packages/connect-explorer-theme/package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"@trezor/components": "workspace:*",
2828
"@trezor/product-components": "workspace:*",
2929
"@trezor/theme": "workspace:*",
30-
"@vitejs/plugin-react": "^3.0.1",
3130
"clsx": "^2.1.1",
3231
"escape-string-regexp": "^5.0.0",
3332
"flexsearch": "^0.7.31",
@@ -58,8 +57,7 @@
5857
"postcss-import": "^15.1.0",
5958
"postcss-lightningcss": "^1.0.1",
6059
"react": "^18.2.0",
61-
"react-dom": "^18.2.0",
62-
"vitest": "^0.34.0"
60+
"react-dom": "^18.2.0"
6361
},
6462
"sideEffects": [
6563
"./src/polyfill.ts"

packages/connect-explorer-theme/vite.config.ts

-10
This file was deleted.

packages/connect-web/src/module/index.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const TrezorConnect = factory(
8080
export default TrezorConnect;
8181
export * from '@trezor/connect/src/exports';
8282

83-
window.addEventListener('beforeunload', () => {
84-
impl.dispose();
85-
});
83+
if (typeof window !== 'undefined') {
84+
window.addEventListener('beforeunload', () => {
85+
impl.dispose();
86+
});
87+
}

packages/connect/src/core/method.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export const getMethod = async (message: IFrameCallMessage): Promise<AbstractMet
1515

1616
const methodModule = getMethodModule(method);
1717
const methods = methodModule
18-
? await import(/* webpackChunkName: "[request]" */ `../api/${methodModule}/api`)
18+
? await import(
19+
/* webpackChunkName: "[request]" */ /* @vite-ignore */ `../api/${methodModule}/api/index.ts`
20+
)
1921
: Methods;
2022
const MethodConstructor = methods[method];
2123

packages/suite-build/configs/web.webpack.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const config: webpack.Configuration = {
1616
output: {
1717
path: path.join(baseDir, 'build'),
1818
},
19+
resolve: {
20+
fallback: { vm: require.resolve('vm-browserify') },
21+
},
1922
plugins: [
2023
new CopyWebpackPlugin({
2124
patterns: ['browser-detection', 'fonts', 'images', 'oauth', 'videos', 'guide/assets']

packages/suite-build/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"stream-browserify": "^3.0.0",
3737
"style-loader": "^3.3.4",
3838
"terser-webpack-plugin": "^5.3.11",
39+
"vm-browserify": "^1.1.2",
3940
"webpack": "^5.97.1",
4041
"webpack-bundle-analyzer": "^4.10.1",
4142
"webpack-merge": "^6.0.1",

packages/suite-web/eslint.config.mjs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import { eslint } from '@trezor/eslint';
1+
import { eslint, globalNoExtraneousDependenciesDevDependencies } from '@trezor/eslint';
22

33
export default [
44
...eslint,
55
{
66
rules: {
77
'import/no-default-export': 'off', // Todo: fix and solve
8+
'import/no-extraneous-dependencies': [
9+
'error',
10+
{
11+
devDependencies: [
12+
...globalNoExtraneousDependenciesDevDependencies,
13+
'**/vite.config.ts',
14+
],
15+
},
16+
],
817
},
918
},
1019
];

packages/suite-web/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"type-check:watch": "yarn type-check -- --watch",
88
"lint:styles": "npx stylelint './src/**/*{.ts,.tsx}' --cache --config ../../.stylelintrc",
99
"dev": "yarn rimraf ./build && yarn workspace @trezor/suite-build run dev:web",
10+
"dev:vite": "PROJECT=web yarn rimraf ./build && vite",
1011
"analyze": "ANALYZE=true yarn build",
1112
"build": "yarn rimraf ./build && yarn workspace @trezor/suite-build run build:web"
1213
},
@@ -28,6 +29,7 @@
2829
"worker-loader": "^3.0.8"
2930
},
3031
"devDependencies": {
32+
"@originjs/vite-plugin-commonjs": "^1.0.3",
3133
"@suite-common/test-utils": "workspace:*",
3234
"@suite-common/wallet-config": "workspace:*",
3335
"@trezor/connect": "workspace:*",
@@ -43,10 +45,13 @@
4345
"@types/react-dom": "18.2.19",
4446
"@types/react-router": "^5.1.20",
4547
"@types/react-router-dom": "^5.1.7",
48+
"@vitejs/plugin-react": "^4.2.1",
4649
"rimraf": "^6.0.1",
4750
"stylelint": "^16.14.1",
4851
"stylelint-config-standard": "^36.0.0",
4952
"tsx": "^4.19.3",
53+
"vite": "^5.4.14",
54+
"vite-plugin-wasm": "^3.4.1",
5055
"webpack": "^5.97.1",
5156
"ws": "^8.18.0",
5257
"yargs": "17.7.2"

packages/suite-web/src/static/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<meta name="google" content="notranslate" />
66
<meta name="viewport" content="width=device-width, initial-scale=1" />
77
<meta charset="utf-8" />
8-
98
<link media="all" rel="stylesheet" href="<%= assetPrefix %>/static/fonts/fonts.css" />
109
<link rel="icon" href="<%= assetPrefix %>/static/images/favicons/favicon.png" />
1110
<link rel="apple-touch-icon" href="<%= assetPrefix %>/static/images/favicons/favicon.png" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* This entry is only used for Vite dev server!
3+
*/
4+
5+
const appElement = document.getElementById('app');
6+
if (appElement) {
7+
import('../Main').then(comp => comp.init(appElement)).catch(err => console.error(err)); // Fatal error
8+
}
9+
10+
export {};

0 commit comments

Comments
 (0)