Skip to content

Commit 40d642d

Browse files
authored
refactor(sdk-install-modal-web): migrate from i18next to custom SimpleI18n implementation (#1141)
* feat: migrate i18n away from sdk dependency * refactor(sdk-install-modal-web): replace i18next with SimpleI18n implementation - Remove i18next dependency in favor of lighter custom i18n solution - Add SimpleI18n class for handling translations - Update modal components to use new i18n implementation - Improve translation loading with proper error handling * build: optimize build configurations and add size limits - Enable build optimizations in stencil config - Add proper size limits for bundle tracking - Enable file name hashing for better caching - Add build statistics tracking - Update size limit configurations across packages * chore: clean up dependencies - Remove unused react-refresh from devnext - Remove react-native-webview direct dependency - Update size-limit dependency version - Clean up redundant peer dependencies * refactor(sdk): improve code organization and error handling - Refactor ModalLoader class for better maintainability - Add proper error handling for modal rendering - Improve mobile port stream documentation - Update rollup config with better bundle visualization - Remove debug console logs - Add type improvements * feat: remove i18n temporarily * build: lazy loading but esm error * feat: working state but still included in bundle * feat: add browser language detection and dynamic translation loading Implemented browser language detection and dynamic loading of translations from a remote URL. Removed hardcoded locale files and updated the SimpleI18n class to handle supported locales and fallback mechanisms. * feat: fully working simple i18n * chore: cleanup * chore: cleanup * fix: unit tests * fix: types export * chore: nodenv conf * chore: reset sdk version * fix: build wip * feat: move global types * fix: build issues * fix: sdk react unit tests
1 parent 5201001 commit 40d642d

40 files changed

+1714
-995
lines changed

packages/devnext/next.config.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,27 @@ const nextConfig = withExpo({
3333
experimental: {
3434
forceSwcTransforms: true,
3535
},
36-
webpack: (config) => {
36+
webpack: (config, { dev, isServer }) => {
3737
config.resolve = {
3838
...config.resolve,
3939
symlinks: false, // tells webpack to follow the real path of symlinked module instead of the symlink path itself
40+
fallback: {
41+
...config.resolve.fallback,
42+
stream: require.resolve('stream-browserify'),
43+
crypto: require.resolve('crypto-browserify'),
44+
http: require.resolve('stream-http'),
45+
https: require.resolve('https-browserify'),
46+
os: require.resolve('os-browserify/browser'),
47+
path: require.resolve('path-browserify'),
48+
zlib: require.resolve('browserify-zlib'),
49+
},
4050
alias: {
4151
...config.resolve.alias,
4252
// Make sure the react version used is from our node_modules
53+
'@babel/runtime': path.resolve(
54+
__dirname,
55+
'./node_modules/@babel/runtime',
56+
),
4357
react: path.resolve(__dirname, './node_modules/react'),
4458
'react-native$': require.resolve('react-native-web'),
4559
// 'expo-asset': path.resolve(__dirname, './node_modules/expo-asset'),
@@ -62,6 +76,36 @@ const nextConfig = withExpo({
6276
),
6377
},
6478
};
79+
80+
// Disable caching for development
81+
config.cache = {
82+
type: 'filesystem',
83+
buildDependencies: {
84+
config: [__filename],
85+
},
86+
cacheDirectory: path.resolve(__dirname, '.next/cache/webpack'),
87+
// Only cache node_modules except for sdk-install-modal-web
88+
managedPaths: [
89+
// Include all node_modules except sdk-install-modal-web
90+
path.resolve(__dirname, 'node_modules'),
91+
],
92+
// Exclude the sdk package from caching using version check
93+
version: `${
94+
require('@metamask/sdk-install-modal-web/package.json').version
95+
}-${Date.now()}`,
96+
};
97+
98+
// Add watch options to detect changes
99+
config.watchOptions = {
100+
aggregateTimeout: 300,
101+
poll: 1000,
102+
ignored: [
103+
'**/.git/**',
104+
'**/node_modules/**',
105+
'!**/node_modules/@metamask/sdk-install-modal-web/**',
106+
],
107+
};
108+
65109
config.module.rules.push(
66110
{
67111
test: /\.(js|jsx)$/,
@@ -127,6 +171,41 @@ const nextConfig = withExpo({
127171
},
128172
);
129173

174+
// Find the rule that handles JavaScript files
175+
const jsRule = config.module.rules.find(
176+
(rule) => rule.test && rule.test.test('.js'),
177+
);
178+
179+
if (jsRule) {
180+
// Include @babel/runtime in the Babel loader
181+
jsRule.include = [
182+
...(Array.isArray(jsRule.include)
183+
? jsRule.include
184+
: [jsRule.include]
185+
).filter(Boolean),
186+
path.resolve(__dirname, 'node_modules/@babel/runtime'),
187+
];
188+
}
189+
190+
// Add the babel-plugin-transform-import-meta to the loader options
191+
config.module.rules.forEach((rule) => {
192+
if (rule.use) {
193+
const use = Array.isArray(rule.use) ? rule.use : [rule.use];
194+
use.forEach((loader) => {
195+
if (
196+
typeof loader === 'object' &&
197+
loader.loader &&
198+
loader.loader.includes('babel-loader') &&
199+
loader.options
200+
) { loader.options.plugins = [
201+
...(loader.options.plugins || []),
202+
'babel-plugin-transform-import-meta',
203+
];
204+
}
205+
});
206+
}
207+
});
208+
130209
// write config to disk for debugging
131210
fs.writeFileSync(
132211
'./next.webpack.config.json',

packages/devnext/package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"prepack": "../../scripts/prepack.sh"
1717
},
1818
"dependencies": {
19-
"@babel/runtime": "^7.18.6",
2019
"@fortawesome/fontawesome-svg-core": "^6.4.2",
2120
"@fortawesome/free-solid-svg-icons": "^6.4.2",
2221
"@fortawesome/react-fontawesome": "^0.2.0",
@@ -63,27 +62,40 @@
6362
"@babel/plugin-proposal-class-properties": "^7.18.6",
6463
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
6564
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
66-
"@babel/preset-env": "^7.23.3",
65+
"@babel/plugin-transform-modules-commonjs": "^7.25.9",
66+
"@babel/plugin-transform-runtime": "^7.25.9",
67+
"@babel/preset-env": "^7.26.0",
6768
"@babel/preset-flow": "^7.23.3",
6869
"@babel/preset-react": "^7.23.3",
6970
"@babel/preset-typescript": "^7.23.3",
71+
"@babel/runtime": "^7.26.0",
7072
"@expo/next-adapter": "^5.0.2",
7173
"@lavamoat/allow-scripts": "^2.3.1",
7274
"@svgr/webpack": "^8.1.0",
7375
"@types/eslint": "^8",
7476
"@typescript-eslint/eslint-plugin": "^6.9.1",
7577
"@typescript-eslint/parser": "^6.9.1",
78+
"assert": "^2.1.0",
7679
"autoprefixer": "^10.4.16",
7780
"babel-loader": "^9.1.3",
7881
"babel-plugin-react-native-web": "^0.19.12",
82+
"babel-plugin-transform-import-meta": "^2.2.1",
7983
"babel-preset-expo": "^9.5.2",
84+
"browserify-zlib": "^0.2.0",
85+
"crypto-browserify": "^3.12.1",
8086
"eslint": "^8.53.0",
8187
"eslint-config-next": "13.2.4",
8288
"expo": "^49.0.18",
89+
"https-browserify": "^1.0.0",
8390
"jest": "^29.6.4",
8491
"next": "13.2.4",
92+
"os-browserify": "^0.3.0",
93+
"path-browserify": "^1.0.1",
8594
"postcss": "^8.4.31",
8695
"prettier": "^3.0.3",
87-
"tailwindcss": "^3.3.5"
96+
"stream-browserify": "^3.0.0",
97+
"stream-http": "^3.2.0",
98+
"tailwindcss": "^3.3.5",
99+
"url": "^0.11.4"
88100
}
89101
}

packages/devnext/tsconfig.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@
3838
"@metamask/sdk": [
3939
"../sdk/src"
4040
],
41-
"react": ["./node_modules/@types/react"]
41+
"react": [
42+
"./node_modules/@types/react"
43+
]
4244
}
4345
},
4446
"include": [
4547
"next-env.d.ts",
4648
"**/*.ts",
47-
"**/*.tsx" ],
49+
"**/*.tsx"
50+
],
4851
"exclude": [
4952
"node_modules"
5053
],
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"path": "./dist/es/index.js",
4-
"limit": "100000 ms"
3+
"path": "dist/components/*.js",
4+
"limit": "30 KB"
55
}
66
]

packages/sdk-install-modal-web/package.json

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,20 @@
1313
},
1414
"author": "MetaMask",
1515
"packageManager": "[email protected]",
16-
"main": "dist/components/index.js",
17-
"module": "dist/components/index.js",
18-
"types": "dist/components/index.d.ts",
16+
"main": "./dist/index.cjs.js",
17+
"module": "./dist/index.js",
18+
"collection": "dist/collection/collection-manifest.json",
19+
"types": "dist/types/index.d.ts",
1920
"files": [
2021
"./dist"
2122
],
22-
"exports": {
23-
".": {
24-
"import": "./dist/components/index.js",
25-
"types": "./dist/components/index.d.ts"
26-
},
27-
"./components/mm-install-modal": {
28-
"import": "./dist/components/mm-install-modal.js",
29-
"types": "./dist/components/mm-install-modal.d.ts"
30-
},
31-
"./components/mm-pending-modal": {
32-
"import": "./dist/components/mm-pending-modal.js",
33-
"types": "./dist/components/mm-pending-modal.d.ts"
34-
},
35-
"./components/mm-select-modal": {
36-
"import": "./dist/components/mm-select-modal.js",
37-
"types": "./dist/components/mm-select-modal.d.ts"
38-
}
39-
},
4023
"scripts": {
4124
"allow-scripts": "",
4225
"generate": "stencil generate",
4326
"build:types": "tsc --project tsconfig.json --emitDeclarationOnly --outDir dist/types",
4427
"build:clean": "yarn clean && yarn build",
4528
"build": "stencil build --prod",
46-
"build:dev": "stencil build --debug --dev",
29+
"build:dev": "NODE_ENV=development stencil build --debug --dev",
4730
"build:tsc": "tsc --build tsconfig.json --verbose",
4831
"build:watch": "stencil build --watchAll",
4932
"build:post-tsc": "echo 'N/A'",
@@ -84,17 +67,14 @@
8467
"eslint-plugin-node": "^11.1.0",
8568
"eslint-plugin-prettier": "^3.4.0",
8669
"eslint-plugin-react": "^7.32.2",
87-
"i18next": "23.11.5",
8870
"jest": "^29.6.4",
8971
"prettier": "^2.8.8",
9072
"puppeteer": "^23.8.0",
9173
"rimraf": "^5.0.0",
92-
"size-limit": "^11.0.2",
74+
"rollup-plugin-visualizer": "^5.12.0",
75+
"size-limit": "^11.1.6",
9376
"typescript": "^5.6.3"
9477
},
95-
"peerDependencies": {
96-
"i18next": "23.11.5"
97-
},
9878
"publishConfig": {
9979
"access": "public",
10080
"registry": "https://registry.npmjs.org/"

packages/sdk-install-modal-web/rollup.config.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,7 @@ const config = [
2828
format: 'umd',
2929
exports: 'named',
3030
name: 'MetaMaskSDKInstallModal',
31-
sourcemap: true,
32-
globals: {
33-
'react': 'React',
34-
'react-dom': 'ReactDOM',
35-
'react-dom/client': 'ReactDOM.createRoot',
36-
'react-native': 'ReactNative',
37-
'i18next': 'i18next',
38-
'tslib': 'tslib'
39-
}
31+
sourcemap: true
4032
},
4133
{
4234
file: packageJson.main,
@@ -45,12 +37,6 @@ const config = [
4537
},
4638
],
4739
external: [
48-
'react',
49-
'react-dom',
50-
'react-dom/client',
51-
'react-native',
52-
'i18next',
53-
'tslib',
5440
external(),
5541
],
5642
plugins: [

packages/sdk-install-modal-web/src/components.d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
* It contains typing information for all components that exist in this project.
66
*/
77
import { HTMLStencilElement, JSXBase } from "@stencil/core/internal";
8-
import { i18n } from "i18next";
9-
export { i18n } from "i18next";
108
export namespace Components {
119
interface MmInstallModal {
12-
"i18nInstance": i18n;
1310
/**
1411
* The QR code link
1512
*/
@@ -22,12 +19,10 @@ export namespace Components {
2219
* The QR code link
2320
*/
2421
"displayOTP"?: boolean;
25-
"i18nInstance": i18n;
2622
"otpCode"?: string;
2723
"sdkVersion"?: string;
2824
}
2925
interface MmSelectModal {
30-
"i18nInstance": i18n;
3126
/**
3227
* The QR code link
3328
*/
@@ -111,7 +106,6 @@ declare global {
111106
}
112107
declare namespace LocalJSX {
113108
interface MmInstallModal {
114-
"i18nInstance"?: i18n;
115109
/**
116110
* The QR code link
117111
*/
@@ -126,15 +120,13 @@ declare namespace LocalJSX {
126120
* The QR code link
127121
*/
128122
"displayOTP"?: boolean;
129-
"i18nInstance"?: i18n;
130123
"onClose"?: (event: MmPendingModalCustomEvent<any>) => void;
131124
"onDisconnect"?: (event: MmPendingModalCustomEvent<any>) => void;
132125
"onUpdateOTPValue"?: (event: MmPendingModalCustomEvent<{ otpValue: string }>) => void;
133126
"otpCode"?: string;
134127
"sdkVersion"?: string;
135128
}
136129
interface MmSelectModal {
137-
"i18nInstance"?: i18n;
138130
/**
139131
* The QR code link
140132
*/

0 commit comments

Comments
 (0)