Skip to content

Commit

Permalink
feat: esm
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailDeng authored and AbigailDeng committed Oct 21, 2024
1 parent b086c81 commit 24ea35f
Show file tree
Hide file tree
Showing 26 changed files with 179 additions and 181 deletions.
11 changes: 10 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
"class-methods-use-this": "off",
"no-plusplus": "off",
"implicit-arrow-linebreak": "off",
"object-curly-newline": "off"
"object-curly-newline": "off",
"import/extensions": [
"error",
"ignorePackages",
{
"js": "always",
"json": "always"
}
],
"operator-linebreak": ["warn", "after"]
}
}
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
HUSKY_GIT_PARAMS=$1 node scripts/verify-commit-msg.js
HUSKY_GIT_PARAMS=$1 node scripts/verify-commit-msg.cjs
11 changes: 3 additions & 8 deletions build/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/**
* @file utils
* @author atom-yang
* @date 2019-06-28
*/
const path = require('path');
import path from 'path';

module.exports.ROOT = path.resolve(__dirname, '..');
export const ROOT = path.resolve(process.cwd(), '.');

module.exports.OUTPUT_PATH = path.resolve(__dirname, '..', 'dist/');
export const OUTPUT_PATH = path.resolve(process.cwd(), '.', 'dist/');
11 changes: 4 additions & 7 deletions build/webpack.analyze.js → build/webpack.analyze.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@

/* eslint-env node */

const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const DeadCodePlugin = require('webpack-deadcode-plugin');
const { merge } = require('webpack-merge');
const nodeConfig = require('./webpack.node');
const nodeConfig = require('./webpack.node.js');
const browserConfig = require('./webpack.browser');

const unusedAnalyzeConfig = {
patterns: ['src/**/*.*'],
globOptions: {
ignore: [
'**/*.md',
'node_modules/**/*'
]
ignore: ['**/*.md', 'node_modules/**/*']
}
};

module.exports = merge(process.env.RUNTIME_ENV === 'node' ? nodeConfig : browserConfig, {
plugins: [
new BundleAnalyzerPlugin({analyzerMode: 'static', generateStatsFile: true}),
new BundleAnalyzerPlugin({ analyzerMode: 'static', generateStatsFile: true }),
new DeadCodePlugin(unusedAnalyzeConfig)
]
});
15 changes: 8 additions & 7 deletions build/webpack.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
* @author atom-yang
*/

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

const require = createRequire(import.meta.url);

const browserConfig = {
mode: 'production',
Expand Down Expand Up @@ -63,11 +65,10 @@ const browserConfig = {
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer']
}),
// fix "process is not defined" error:
new webpack.ProvidePlugin({
process: 'process/browser'
})
]
};

module.exports = merge(baseConfig, browserConfig);
export default merge(baseConfig, browserConfig);
18 changes: 8 additions & 10 deletions build/webpack.common.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/**
* @file common config
* @author atom-yang
*/

/* eslint-env node */
const path = require('path');
const webpack = require('webpack');
const { ROOT } = require('./utils');
const { version, name } = require(path.resolve(ROOT, './package.json'));
import path from 'path';
import webpack from 'webpack';
import { ROOT } from './utils.js';
const pkg = await import(path.resolve(ROOT, './package.json'), {
assert: { type: 'json' }
});
const { version, name } = pkg;

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

Expand Down Expand Up @@ -43,4 +41,4 @@ const baseConfig = {
}
};

module.exports = baseConfig;
export default baseConfig;
30 changes: 30 additions & 0 deletions build/webpack.esModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @file node config
* @author atom-yang
*/

/* eslint-env node */
import { merge } from 'webpack-merge';
import baseConfig from './webpack.common.js';
import { OUTPUT_PATH } from './utils.js';

const nodeConfig = {
mode: 'production',
output: {
path: OUTPUT_PATH,
filename: 'aelf.esm.js',
libraryTarget: 'module'
},
experiments: {
outputModule: true
},
resolve: {
alias: {},
fallback: {
crypto: 'crypto-browserify',
stream: 'stream-browserify'
}
}
};

export default merge(baseConfig, nodeConfig);
13 changes: 4 additions & 9 deletions build/webpack.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/

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

const nodeConfig = {
mode: 'production',
Expand All @@ -18,11 +18,6 @@ const nodeConfig = {
},
libraryExport: 'default'
},
resolve: {
alias: {
// 'scryptsy$': '../scrypt-polyfill.js',
}
},
target: 'node',
optimization: {
removeEmptyChunks: true,
Expand All @@ -33,4 +28,4 @@ const nodeConfig = {
}
};

module.exports = merge(baseConfig, nodeConfig);
export default merge(baseConfig, nodeConfig);
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
{
"name": "aelf-sdk",
"version": "3.4.16",
"version": "3.4.17-alpha.0",
"description": "aelf-sdk js library",
"type": "module",
"main": "dist/aelf.cjs.js",
"module": "dist/aelf.esm.js",
"browser": "dist/aelf.umd.js",
"unpkg": "dist/aelf.umd.js",
"jsdelivr": "dist/aelf.umd.js",
"exports": {
".": {
"import": "./dist/aelf.esm.js",
"require": "./dist/aelf.cjs.js"
}
},
"scripts": {
"build": "npm run clean && npm run build:browser && npm run build:node && npm run copy-ts",
"build": "npm run clean && npm run build:browser && npm run build:node && npm run build:esm && npm run copy-ts",
"build:browser": "cross-env RUNTIME_ENV=browser webpack --progress --color --config ./build/webpack.browser.js",
"build:node": "cross-env RUNTIME_ENV=node webpack --progress --color --config ./build/webpack.node.js",
"build:esm": "cross-env --es-module-specifier-resolution=node webpack --progress --color --config ./build/webpack.esModule.js",
"analyze:node": "npm run clean && cross-env RUNTIME_ENV=node webpack --config ./build/webpack.analyze.js",
"analyze:browser": "npm run clean && cross-env RUNTIME_ENV=browser webpack --config ./build/webpack.analyze.js",
"clean": "rimraf dist/*",
Expand Down
18 changes: 18 additions & 0 deletions scripts/verify-commit-msg.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const chalk = require('chalk');
const msgPath = process.env.HUSKY_GIT_PARAMS;
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim();

const commitRE =
/^(revert: )?(feat|security|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build)(\(.+\))?: .{1,50}/;

if (!commitRE.test(msg)) {
console.error(
` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` +
chalk.red(` Proper commit message format is required for automated changelog generation. Examples:\n\n`) +
` ${chalk.green(`feat(wallet): add getContractAddress`)}\n` +
` ${chalk.green(`fix(contract): handle contract conflict (close #28)`)}\n\n` +
chalk.red(` See .github/COMMIT_CONVENTION.md for more details.\n`) +
chalk.red(` You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`)
);
process.exit(1);
}
17 changes: 0 additions & 17 deletions scripts/verify-commit-msg.js

This file was deleted.

17 changes: 4 additions & 13 deletions src/chain/chainMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
* @file method on chain
* @author atom-yang
*/
import { isFunction, noop, isBoolean } from '../util/utils';
import { isFunction, noop, isBoolean } from '../util/utils.js';

export default class ChainMethod {
constructor({
name,
call,
method = 'GET',
params = [],
inputFormatter = [],
outputFormatter = null,
}) {
constructor({ name, call, method = 'GET', params = [], inputFormatter = [], outputFormatter = null }) {
this.name = name;
this.call = call;
this.requestMethod = method;
Expand All @@ -39,9 +32,7 @@ export default class ChainMethod {
}

formatOutput(result) {
return this.outputFormatter && result
? this.outputFormatter(result)
: result;
return this.outputFormatter && result ? this.outputFormatter(result) : result;
}

extractArgumentsIntoObject(args) {
Expand All @@ -53,7 +44,7 @@ export default class ChainMethod {
requestMethod: this.requestMethod,
isSync: false,
callback: noop,
params: {},
params: {}
};
this.formatInput(args).forEach((arg, index) => {
if (index > this.params.length - 1) {
Expand Down
10 changes: 5 additions & 5 deletions src/chain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @file chain
* @author atom-yang
*/
import { isBoolean, isFunction, noop, setPath } from '../util/utils';
import { CHAIN_METHODS } from '../common/constants';
import ChainMethod from './chainMethod';
import * as merkleTree from '../util/merkleTree';
import { isBoolean, isFunction, noop, setPath } from '../util/utils.js';
import { CHAIN_METHODS } from '../common/constants.js';
import ChainMethod from './chainMethod.js';
import * as merkleTree from '../util/merkleTree.js';

import ContractFactory from '../contract';
import ContractFactory from '../contract/index.js';

export default class Chain {
constructor(requestManager) {
Expand Down
2 changes: 1 addition & 1 deletion src/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file AElf-sdk constants
* @author atom-yang
*/
import { inputAddressFormatter, outputFileDescriptorSetFormatter } from '../util/formatters';
import { inputAddressFormatter, outputFileDescriptorSetFormatter } from '../util/formatters.js';

/**
* unsigned 256 int
Expand Down
8 changes: 4 additions & 4 deletions src/contract/contractMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
* @file contract method
* @author atom-yang
*/
import { getTransaction, Transaction } from '../util/proto';
import { getTransaction, Transaction } from '../util/proto.js';
import {
transformArrayToMap,
transformMapToArray,
transform,
INPUT_TRANSFORMERS,
OUTPUT_TRANSFORMERS
} from '../util/transform';
import { isBoolean, isFunction, isNumber, noop, uint8ArrayToHex, unpackSpecifiedTypeData } from '../util/utils';
import wallet from '../wallet';
} from '../util/transform.js';
import { isBoolean, isFunction, isNumber, noop, uint8ArrayToHex, unpackSpecifiedTypeData } from '../util/utils.js';
import wallet from '../wallet/index.js';

export default class ContractMethod {
constructor(chain, method, contractAddress, walletInstance, option) {
Expand Down
10 changes: 5 additions & 5 deletions src/contract/contractMultiTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* @file contract method
* @author atom-yang
*/
import HttpProvider from '../util/httpProvider';
import { getTransactionAndChainId, TransactionAndChainId, MultiTransaction } from '../util/proto';
import { transformMapToArray, transform, INPUT_TRANSFORMERS } from '../util/transform';
import { isBoolean, isFunction, isObject, noop, uint8ArrayToHex, validateMulti } from '../util/utils';
import wallet from '../wallet';
import HttpProvider from '../util/httpProvider.js';
import { getTransactionAndChainId, TransactionAndChainId, MultiTransaction } from '../util/proto.js';
import { transformMapToArray, transform, INPUT_TRANSFORMERS } from '../util/transform.js';
import { isBoolean, isFunction, isObject, noop, uint8ArrayToHex, validateMulti } from '../util/utils.js';
import wallet from '../wallet/index.js';

export default class ContractMultiTransaction {
constructor(contract, walletInstance, option) {
Expand Down
8 changes: 4 additions & 4 deletions src/contract/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*/
// eslint-disable-next-line max-classes-per-file
import * as protobuf from '@aelfqueen/protobufjs';
import ContractMethod from './contractMethod';
import { noop } from '../util/utils';
import { deserializeLog } from '../util/proto';
import ContractMultiTransaction from './contractMultiTransaction';
import ContractMethod from './contractMethod.js';
import { noop } from '../util/utils.js';
import { deserializeLog } from '../util/proto.js';
import ContractMultiTransaction from './contractMultiTransaction.js';

const getServicesFromFileDescriptors = descriptors => {
const root = protobuf.Root.fromDescriptor(descriptors, 'proto3').resolveAll();
Expand Down
Loading

0 comments on commit 24ea35f

Please sign in to comment.