Skip to content

Commit

Permalink
feat: fix umd
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailDeng authored and AbigailDeng committed Jul 4, 2024
1 parent b896052 commit 556f6ac
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 81 deletions.
22 changes: 17 additions & 5 deletions build/webpack.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

/* eslint-env node */
const { merge } = require('webpack-merge');
const webpack = require('webpack');
const baseConfig = require('./webpack.common');
const { OUTPUT_PATH } = require('./utils');

Expand All @@ -13,17 +14,16 @@ const browserConfig = {
output: {
path: OUTPUT_PATH,
filename: 'aelf.umd.js',
library: {
name: 'AElf',
type: 'umd'
},
library: 'AElf',
libraryTarget: 'umd',
libraryExport: 'default',
globalObject: 'globalThis',
umdNamedDefine: true
},
resolve: {
alias: {},
fallback: {
process: false,
assert: require.resolve('assert'),
buffer: require.resolve('buffer'),
crypto: require.resolve('crypto-browserify'),
Expand All @@ -49,13 +49,25 @@ const browserConfig = {
}
},
target: 'web',
node: {
global: true
},
optimization: {
removeEmptyChunks: true,
chunkIds: 'total-size',
moduleIds: 'size',
sideEffects: true,
minimize: false
}
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer']
}),
// fix "process is not defined" error:
new webpack.ProvidePlugin({
process: 'process/browser'
})
]
};

module.exports = merge(baseConfig, browserConfig);
9 changes: 3 additions & 6 deletions build/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
/* eslint-env node */
const path = require('path');
const webpack = require('webpack');
const {ROOT} = require('./utils');
const {version, name} = require(path.resolve(ROOT, './package.json'));
const { ROOT } = require('./utils');
const { version, name } = require(path.resolve(ROOT, './package.json'));

const banner = `${name}.js v${version} \n(c) 2019-${new Date().getFullYear()} AElf \nReleased under MIT License`;

const baseConfig = {
entry: path.resolve(ROOT, 'src/index.js'),
devtool: 'source-map',
resolve: {
modules: [
path.resolve(ROOT, 'src'),
'node_modules'
],
modules: [path.resolve(ROOT, 'src'), 'node_modules'],
extensions: ['.ts', '.js']
},
module: {
Expand Down
119 changes: 54 additions & 65 deletions examples/browser/index.html
Original file line number Diff line number Diff line change
@@ -1,71 +1,60 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello AElf</title>
</head>
<body>
<p>Hello AElf.</p>

<script type="text/javascript" src="../../dist/aelf.umd.js"></script>
<script type="text/javascript">
const AElf = window.AElf;
const Wallet = AElf.wallet;
const sha256 = AElf.utils.sha256;
<head>
<title>Hello AElf</title>
</head>

const wallet = AElf.wallet.createNewWallet();
const walletGetByKey = AElf.wallet.getWalletByPrivateKey(wallet.privateKey);
const walletGetByMn = AElf.wallet.getWalletByMnemonic(wallet.mnemonic);
console.log(wallet.address);
console.log(walletGetByKey);
console.log(walletGetByMn);
// link to Blockchain
const aelf = new AElf(new AElf.providers.HttpProvider('http://18.162.41.20:8000'));
if (!aelf.isConnected()) {
alert('Blockchain Node is not running.');
}
<body>
<p>Hello AElf.</p>

// the contract you want to query
const tokenContractName = 'AElf.ContractNames.Token';
const {
// directly accessible information
GenesisContractAddress
} = aelf.chain.getChainStatus({ sync: true });
aelf.chain
.contractAt(GenesisContractAddress, wallet)
.then(zeroC => {
// return contract's address which you query by contract's name
return zeroC.GetContractAddressByName.call(sha256(tokenContractName));
})
.then(tokenContractAddress => {
// return contract's instance and you can call the methods on this instance
return aelf.chain.contractAt(tokenContractAddress, wallet, {
refBlockNumberStrategy: -8
});
})
.then(async tokenContract => {
// return token's info
const result = await tokenContract.GetTokenInfo.call({
symbol: 'ELF'
});
console.log(result, 'view token(ELF) info');
return tokenContract;
})
.then(async tokenContract => {
const sendResult = tokenContract.Transfer(
{
symbol: 'ELF',
// memo: '',
to: 'ELF_s3tE7DA1TNndCKsXM4frMAsYfiaU3kXc1obVxxSJ2bhQEC7HA_tDVW',
amount: 100
},
{
refBlockNumberStrategy: -16,
sync: true
}
);
console.log(sendResult, 'sendResult==');
// alert(JSON.stringify(res));
<script type="text/javascript" src="../../dist/aelf.umd.js"></script>
<script type="text/javascript">
const AElf = window.AElf;
const Wallet = AElf.wallet;
const sha256 = AElf.utils.sha256;

const wallet = AElf.wallet.createNewWallet();
const walletGotByKey = AElf.wallet.getWalletByPrivateKey(wallet.privateKey);
const walletGotByMn = AElf.wallet.getWalletByMnemonic(wallet.mnemonic);
console.log(wallet.address);
console.log(walletGotByKey);
console.log(walletGotByMn);
// link to Blockchain
const aelf = new AElf(new AElf.providers.HttpProvider(
'https://tdvw-test-node.aelf.io/'
));

if (!aelf.isConnected()) {
alert('Blockchain Node is not running.');
}

// the contract you want to query
const tokenContractName = 'AElf.ContractNames.Token';
const {
// directly accessible information
GenesisContractAddress
} = aelf.chain.getChainStatus({ sync: true });
aelf.chain.contractAt(GenesisContractAddress,wallet)
.then(zeroC => {
// return contract's address which you query by contract's name
return zeroC.GetContractAddressByName.call(sha256(tokenContractName));
})
.then(tokenContractAddress => {
// return contract's instance and you can call the methods on this instance
return aelf.chain.contractAt(tokenContractAddress,wallet);
})
.then(tokenContract => {
// return token's info
return tokenContract.GetTokenInfo.call({
symbol: 'ELF'
});
</script>
</body>
</html>
})
.then(res => {
console.log(res);
alert(JSON.stringify(res));
});
</script>
</body>

</html>
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@
"assert": "^2.0.0",
"babel-plugin-rewire": "^1.2.0",
"bignumber.js": "^9.0.0",
"crypto-browserify": "^3.12.0",
"gbk-string": "^0.1.0",
"isomorphic-fetch": "^3.0.0",
"js-sha256": "^0.9.0",
"keccak": "^3.0.3",
"bip39": "^3.0.2",
"bn.js": "^5.2.1",
"browserify-cipher": "^1.0.1",
"bs58": "^4.0.1",
"buffer": "^5.2.1",
"crypto-browserify": "^3.12.0",
"crypto-js": "^4.2.0",
"elliptic": "^6.4.1",
"gbk-string": "^0.1.0",
"hdkey": "^1.1.1",
"isomorphic-fetch": "^3.0.0",
"js-sha256": "^0.9.0",
"keccak": "^3.0.3",
"node-fetch": "2",
"query-string": "5.1.1",
"randombytes": "^2.1.0",
Expand Down Expand Up @@ -123,6 +123,7 @@
"jest-environment-jsdom-fifteen": "^1.0.2",
"jest-github-reporter": "^1.1.1",
"lint-staged": "^13.2.1",
"process": "^0.11.10",
"rimraf": "^5.0.0",
"size-limit": "^8.1.2",
"standard-version": "^9.5.0",
Expand Down

0 comments on commit 556f6ac

Please sign in to comment.