Skip to content

Commit

Permalink
feat: Enable Local Testing of Webpack Federation Module (get-starknet…
Browse files Browse the repository at this point in the history
… remoteEntry.js) (#249)

* feat: add webpack.config.dev.js for local serving of remoteEntry.js

* chore: lint and optional env variable for SNAP_ID

* fix: update get-starknet/webpack-dev-server dep to address security vulnerability

* fix: remoteEntry.js serving done on static file from dist/webpack folder

* chore: added .env.sample example file

* feat: optional PUBLIC_PATH env variable to produce local remoteEntry.js

* chore: updated README.md

* fix: env variable for local environment resolved at build time
  • Loading branch information
khanti42 authored Jun 11, 2024
1 parent 0c91142 commit b06f9f2
Show file tree
Hide file tree
Showing 10 changed files with 676 additions and 112 deletions.
1 change: 1 addition & 0 deletions packages/get-starknet/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SNAPI_ID=local:http://localhost:8081
8 changes: 7 additions & 1 deletion packages/get-starknet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Execute the following cmd to build the project in [module federation]([https://w
yarn build:fed
```


## How to use

```javascript
Expand All @@ -21,3 +20,10 @@ const walletInstance = new MetaMaskSnapWallet(
`provider` refer to the instance from window.ethereum
`snapVersion` refer to the version of the Starknet Snap that is connecting to, "*" for latest version

## how to dev
Execute the following cmd to serve a local version of the module federation
```bash
yarn serve:fed
```

> The `remoteEntry.js` will be available at `http://localhost:8081/remoteEntry.js` the location can be modified in the package.json if needed, by updating the `PUBLIC_PATH` env variable.
7 changes: 5 additions & 2 deletions packages/get-starknet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"setup": "yarn install --no-immutable",
"build": "tsc --project tsconfig.build.json",
"build:jsonp": "webpack --config webpack.config.jsonp.js",
"build:fed": " webpack --config webpack.config.js",
"build:fed": "webpack --config webpack.config.prod.js",
"serve:fed:local": "SNAP_ID=local:http://localhost:8081/ webpack --config webpack.config.prod.js --env PUBLIC_PATH=http://localhost:8082/ && webpack-cli serve --config webpack.config.dev.js",
"prettier": "prettier --write \"src/**/*.ts\"",
"lint": "eslint 'src/*.{js,ts,tsx}' --max-warnings 0 -f json -o eslint-report.json",
"lint:fix": "eslint '**/*.{js,ts,tsx}' --fix",
Expand All @@ -35,6 +36,8 @@
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"depcheck": "^1.4.7",
"dotenv": "^16.4.5",
"dotenv-webpack": "^8.1.0",
"eslint": "^8.13.0",
"get-starknet-core": "^3.2.0",
"html-webpack-plugin": "5.6.0",
Expand All @@ -47,7 +50,7 @@
"typescript": "^4.6.3",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "4.15.1"
"webpack-dev-server": "^5.0.4"
},
"publishConfig": {
"access": "public",
Expand Down
2 changes: 1 addition & 1 deletion packages/get-starknet/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class MetaMaskSnapWallet implements IStarknetWindowObject {
metamaskProvider: MetaMaskProvider;

private static readonly cairoVersion = '0';
private static readonly SNAPI_ID = 'npm:@consensys/starknet-snap';
private static readonly SNAPI_ID = process.env.SNAP_ID;

constructor(metamaskProvider: MetaMaskProvider, snapVersion = '*') {
this.id = 'metamask';
Expand Down
33 changes: 33 additions & 0 deletions packages/get-starknet/webpack.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
const Dotenv = require('dotenv-webpack');
const webpack = require('webpack');

module.exports = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, './dist/webpack'),
filename: 'index.js',
library: {
type: 'commonjs2',
},
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
plugins: [
new Dotenv(),
new webpack.DefinePlugin({
'process.env.SNAP_ID': JSON.stringify(process.env.SNAP_ID || 'npm:@consensys/starknet-snap')
})
]
};
17 changes: 17 additions & 0 deletions packages/get-starknet/webpack.config.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { merge } = require('webpack-merge');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const common = require('./webpack.common.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
module.exports = merge(common, {
mode: 'development',
output: {
publicPath: 'http://localhost:8082/', // Adjust the development publicPath as needed
},
devServer: {
static: path.join(__dirname, 'dist/webpack'),
compress: true,
port: 8082,
},
});
38 changes: 0 additions & 38 deletions packages/get-starknet/webpack.config.js

This file was deleted.

22 changes: 22 additions & 0 deletions packages/get-starknet/webpack.config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { merge } = require('webpack-merge');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const common = require('./webpack.common.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { ModuleFederationPlugin } = require('webpack').container;

module.exports = (env) => merge(common, {
mode: 'production',
output: {
publicPath: env.PUBLIC_PATH || 'https://snaps.consensys.io/starknet/get-starknet/v1/',
},
plugins: [
new ModuleFederationPlugin({
name: 'MetaMaskStarknetSnapWallet',
filename: 'remoteEntry.js',
exposes: {
'./index': './src/index.ts',
},
}),
],
});
4 changes: 2 additions & 2 deletions packages/wallet-ui/.env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
REACT_APP_SNAP_ID=local:http://localhost:8081/
REACT_APP_SNAP_ID=local:http://localhost:8081
REACT_APP_SNAP_VERSION=*
REACT_APP_MIN_SNAP_VERSION=2.2.0
REACT_APP_DEBUG_LEVEL=all
REACT_APP_DEBUG_LEVEL=all
Loading

0 comments on commit b06f9f2

Please sign in to comment.