第一步:准备webpack.config.sdk.js
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const base = {
entry: path.resolve(__dirname, `../src/index.js`),
output: {
filename: 'okexchain-sdk.js',
library:'OKExChainClient',
libraryTarget:'global'
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react'],
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
],
},
},
],
},
],
},
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
compress: {
warnings: false,
drop_console: true,
collapse_vars: true,
reduce_vars: true,
},
},
}),
],
}
};
module.exports = base;
第二步:在src目录创建一个index.js文件
const OKExChainClient = require('@okexchain/javascript-sdk');
module.exports = OKExChainClient;
第三步:在package.json文件中,添加命令
"scripts": {
"dev": "cross-env CLIENT=desktop npm run dev:web",
"dev:mock": "cross-env MOCK_DATA=mock npm run dev",
"dev:web": "webpack-dev-server --config build/webpack.config.dev.js",
"dev:web:mock": "cross-env MOCK_DATA=mock npm run dev:web",
"desktop": "cross-env CLIENT=desktop webpack --config build/webpack.config.desktop.js",
"web": "cross-env NODE_ENV=production webpack --config build/webpack.config.web.js",
"web-sdk": "cross-env NODE_ENV=production webpack --config build/webpack.config.sdk.js",
"build": "npm run desktop && npm run web",
"prettier": "prettier '{src,build}/**/*.{js,jsx}' --trailing-comma es5 --single-quote --write",
"test": "echo \"Error: no test specified\" && exit 1"
},
最后运行:
$ npm run web-sdk
得到压缩后的文件okexchain-sdk.js文件。