From b89605289e1e5ed39bc9f247391519eb9bf77337 Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Thu, 4 Jul 2024 17:25:13 +0800 Subject: [PATCH] feat: fix webpack v5 options --- .babelrc | 1 - build/webpack.browser.js | 13 ++++--- build/webpack.node.js | 10 ++--- src/index.js | 8 ++-- src/util/httpProvider.js | 83 ++++++++++++++++++---------------------- tsconfig.json | 4 +- 6 files changed, 55 insertions(+), 64 deletions(-) diff --git a/.babelrc b/.babelrc index 93462753..446ae819 100644 --- a/.babelrc +++ b/.babelrc @@ -10,7 +10,6 @@ } ] ], - "plugins": ["rewire"], "ignore": ["dist/*.js"], "env": { "test": { diff --git a/build/webpack.browser.js b/build/webpack.browser.js index 1a89b5de..9200706e 100644 --- a/build/webpack.browser.js +++ b/build/webpack.browser.js @@ -6,17 +6,19 @@ /* eslint-env node */ const { merge } = require('webpack-merge'); const baseConfig = require('./webpack.common'); -const {OUTPUT_PATH} = require('./utils'); +const { OUTPUT_PATH } = require('./utils'); const browserConfig = { mode: 'production', output: { path: OUTPUT_PATH, filename: 'aelf.umd.js', - library: 'AElf', - libraryTarget: 'umd', + library: { + name: 'AElf', + type: 'umd' + }, libraryExport: 'default', - globalObject: "globalThis", + globalObject: 'globalThis', umdNamedDefine: true }, resolve: { @@ -52,9 +54,8 @@ const browserConfig = { chunkIds: 'total-size', moduleIds: 'size', sideEffects: true, - minimize: true + minimize: false } }; - module.exports = merge(baseConfig, browserConfig); diff --git a/build/webpack.node.js b/build/webpack.node.js index 1bb41756..7f0e3199 100644 --- a/build/webpack.node.js +++ b/build/webpack.node.js @@ -6,15 +6,16 @@ /* eslint-env node */ const { merge } = require('webpack-merge'); const baseConfig = require('./webpack.common'); -const {OUTPUT_PATH} = require('./utils'); +const { OUTPUT_PATH } = require('./utils'); -const nodeConfig = { +const nodeConfig = { mode: 'production', output: { path: OUTPUT_PATH, filename: 'aelf.cjs.js', - library: 'AElf', - libraryTarget: 'commonjs2', + library: { + type: 'commonjs2' + }, libraryExport: 'default' }, resolve: { @@ -32,5 +33,4 @@ const nodeConfig = { } }; - module.exports = merge(baseConfig, nodeConfig); diff --git a/src/index.js b/src/index.js index 84aa7690..32c43be5 100644 --- a/src/index.js +++ b/src/index.js @@ -25,7 +25,7 @@ export default class AElf { static version = process.env.SDK_VERSION; static providers = { - HttpProvider, + HttpProvider }; /** @@ -41,11 +41,11 @@ export default class AElf { ...utils, ...bloom, sha256, - transform, + transform }; providers = { - HttpProvider, + HttpProvider }; settings = new Settings(); @@ -55,7 +55,7 @@ export default class AElf { * @type {{api: string}} */ version = { - api: process.env.SDK_VERSION, + api: process.env.SDK_VERSION }; /** diff --git a/src/util/httpProvider.js b/src/util/httpProvider.js index 61a93811..116a417f 100644 --- a/src/util/httpProvider.js +++ b/src/util/httpProvider.js @@ -30,7 +30,7 @@ if (process.env.RUNTIME_ENV === 'browser') { // eslint-disable-next-line global-require RequestLibraryXMLOnly = require('xmlhttprequest').XMLHttpRequest; // eslint-disable-next-line global-require - RequestLibrary = require('node-fetch'); + RequestLibrary = require('node-fetch').default; isFetch = true; } @@ -57,7 +57,7 @@ export default class HttpProvider { } else { this.headers = { ...defaultHeaders, - ...headers, + ...headers }; } } @@ -80,9 +80,9 @@ export default class HttpProvider { status: parseRequest.status, error: parseRequest.status === 200 ? 0 : parseRequest.status, Error: { - message: request.statusText, + message: request.statusText }, - statusText: request.statusText, + statusText: request.statusText }; } catch (e) { result = request; @@ -101,13 +101,8 @@ export default class HttpProvider { } requestSendByFetch(requestConfig, request) { - const { - url, - method = 'POST', - params = {}, - signal - } = requestConfig; - const path = `/api/${url}`.replace(/\/\//g, '\/'); + const { url, method = 'POST', params = {}, signal } = requestConfig; + const path = `/api/${url}`.replace(/\/\//g, '/'); let uri = `${this.host}${path}`.replace(); const myHeaders = new Headers(); let body = JSON.stringify(params); @@ -133,44 +128,40 @@ export default class HttpProvider { const control = typeof AbortController === 'function' ? new AbortController() : {}; const config = { ...requestConfig, signal: control.signal, credentials: 'omit' }; // Simulation timeout - return Promise.race([ - this.requestSendByFetch(config, request), - HttpProvider.timeoutPromise(timeout) - ]).then(result => new Promise((resolve, reject) => { - // @deprecated unuse timeout=1 - // if (timeout !== 1) { - try { - if (result.type === 'timeout') { - // Cancel timeout request - if (control.abort) control.abort(); - reject(result); - } else { - result - .text() - .then(text => { - const res = HttpProvider.formatResponse(text); - if (result.status !== 200 || !result.ok) { - reject(res); - return; - } - resolve(res); - }) - .catch(err => reject(err)); - } - } catch (e) { - reject(e); - } - // } - })); + return Promise.race([this.requestSendByFetch(config, request), HttpProvider.timeoutPromise(timeout)]).then( + result => + new Promise((resolve, reject) => { + // @deprecated unuse timeout=1 + // if (timeout !== 1) { + try { + if (result.type === 'timeout') { + // Cancel timeout request + if (control.abort) control.abort(); + reject(result); + } else { + result + .text() + .then(text => { + const res = HttpProvider.formatResponse(text); + if (result.status !== 200 || !result.ok) { + reject(res); + return; + } + resolve(res); + }) + .catch(err => reject(err)); + } + } catch (e) { + reject(e); + } + // } + }) + ); } requestSend(requestConfig, request, isAsync = false) { - const { - url, - method = 'POST', - params = {} - } = requestConfig; - const path = `/api/${url}`.replace(/\/\//g, '\/'); + const { url, method = 'POST', params = {} } = requestConfig; + const path = `/api/${url}`.replace(/\/\//g, '/'); let uri = `${this.host}${path}`.replace(); if (method.toUpperCase() === 'GET' || method.toUpperCase() === 'DELETE') { uri = Object.keys(params).length > 0 ? `${uri}?${stringify(params)}` : uri; diff --git a/tsconfig.json b/tsconfig.json index f5748540..34560839 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,12 +15,12 @@ "strictFunctionTypes": true, "noEmit": true, "allowJs": false, - "allowSyntheticDefaultImports": false, + "allowSyntheticDefaultImports": true, "baseUrl": ".", "paths": { "aelf-web3": ["types"] }, "esModuleInterop": true, - "declaration": true + "declaration": false } }