diff --git a/web3.js/babel.config.js b/web3.js/babel.config.js new file mode 100644 index 00000000000000..f98a217aa6942f --- /dev/null +++ b/web3.js/babel.config.js @@ -0,0 +1,62 @@ +const isCalledByBundler = caller => caller.name.startsWith('@rollup'); + +module.exports = api => { + const calledByBundler = api.caller(isCalledByBundler); + const transformEnvVariables = + !calledByBundler && + process.env.NODE_ENV && + process.env.BROWSER !== undefined; + + return { + presets: [ + [ + '@babel/preset-env', + { + modules: false, + bugfixes: true, + }, + ], + ['@babel/preset-typescript'], + ], + plugins: [ + [ + '@babel/plugin-proposal-class-properties', + { + loose: true, + }, + ], + [ + '@babel/plugin-proposal-private-methods', + { + loose: true, + }, + ], + [ + '@babel/plugin-proposal-private-property-in-object', + { + loose: true, + }, + ], + transformEnvVariables && [ + 'transform-inline-environment-variables', + { + include: ['NODE_ENV', 'BROWSER'], + }, + ], + transformEnvVariables && [ + 'minify-dead-code-elimination', + { + keepFnName: true, + keepFnArgs: true, + keepClassName: true, + }, + ], + transformEnvVariables && [ + 'babel-plugin-transform-remove-imports', + { + test: 'agent-manager', + }, + ], + ].filter(Boolean), + }; +}; diff --git a/web3.js/babel.config.json b/web3.js/babel.config.json deleted file mode 100644 index f8a99f501bf63e..00000000000000 --- a/web3.js/babel.config.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "presets": [ - [ - "@babel/preset-env", - { - "bugfixes": true - } - ], - ["@babel/preset-typescript"] - ], - "plugins": [ - [ - "@babel/plugin-proposal-class-properties", - { - "loose": true - } - ], - [ - "@babel/plugin-proposal-private-methods", - { - "loose": true - } - ], - [ - "@babel/plugin-proposal-private-property-in-object", - { - "loose": true - } - ] - ] -} \ No newline at end of file diff --git a/web3.js/package-lock.json b/web3.js/package-lock.json index a2a8b5c15a898d..6cc8ceee088ff9 100644 --- a/web3.js/package-lock.json +++ b/web3.js/package-lock.json @@ -25,6 +25,7 @@ "tweetnacl": "^1.0.0" }, "devDependencies": { + "@babel/cli": "^7.15.7", "@babel/core": "^7.12.13", "@babel/plugin-proposal-class-properties": "^7.12.1", "@babel/plugin-transform-runtime": "^7.12.10", @@ -53,6 +54,9 @@ "@types/sinon": "^10.0.0", "@typescript-eslint/eslint-plugin": "^4.14.2", "@typescript-eslint/parser": "^4.14.2", + "babel-plugin-minify-dead-code-elimination": "^0.5.1", + "babel-plugin-transform-inline-environment-variables": "^0.4.3", + "babel-plugin-transform-remove-imports": "^1.7.0", "chai": "^4.3.0", "chai-as-promised": "^7.1.1", "codecov": "^3.0.4", @@ -109,6 +113,53 @@ "integrity": "sha512-uZtkfKblCEQtZKBF6EBXVZeQNl82yqtDQdv+eck8u7tdPxjLu2/lp5/uPW+um2tpuxINHWy3GhiccY7QgEaVHQ==", "dev": true }, + "node_modules/@babel/cli": { + "version": "7.15.7", + "resolved": "https://registry.npmjs.org/@babel/cli/-/cli-7.15.7.tgz", + "integrity": "sha512-YW5wOprO2LzMjoWZ5ZG6jfbY9JnkDxuHDwvnrThnuYtByorova/I0HNXJedrUfwuXFQfYOjcqDA4PU3qlZGZjg==", + "dev": true, + "dependencies": { + "commander": "^4.0.1", + "convert-source-map": "^1.1.0", + "fs-readdir-recursive": "^1.1.0", + "glob": "^7.0.0", + "make-dir": "^2.1.0", + "slash": "^2.0.0", + "source-map": "^0.5.0" + }, + "bin": { + "babel": "bin/babel.js", + "babel-external-helpers": "bin/babel-external-helpers.js" + }, + "engines": { + "node": ">=6.9.0" + }, + "optionalDependencies": { + "@nicolo-ribaudo/chokidar-2": "2.1.8-no-fsevents.3", + "chokidar": "^3.4.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, + "node_modules/@babel/cli/node_modules/commander": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", + "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==", + "dev": true, + "engines": { + "node": ">= 6" + } + }, + "node_modules/@babel/cli/node_modules/slash": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/slash/-/slash-2.0.0.tgz", + "integrity": "sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==", + "dev": true, + "engines": { + "node": ">=6" + } + }, "node_modules/@babel/code-frame": { "version": "7.16.0", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.16.0.tgz", @@ -2997,6 +3048,13 @@ "node": ">=8" } }, + "node_modules/@nicolo-ribaudo/chokidar-2": { + "version": "2.1.8-no-fsevents.3", + "resolved": "https://registry.npmjs.org/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.3.tgz", + "integrity": "sha512-s88O1aVtXftvp5bCPB7WnmXc5IwOZZ7YPuwNPt+GtOOXpPvad1LfbmjYv+qII7zP6RU2QGnqve27dnLycEnyEQ==", + "dev": true, + "optional": true + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz", @@ -4817,6 +4875,24 @@ "follow-redirects": "^1.10.0" } }, + "node_modules/babel-helper-evaluate-path": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/babel-helper-evaluate-path/-/babel-helper-evaluate-path-0.5.0.tgz", + "integrity": "sha512-mUh0UhS607bGh5wUMAQfOpt2JX2ThXMtppHRdRU1kL7ZLRWIXxoV2UIV1r2cAeeNeU1M5SB5/RSUgUxrK8yOkA==", + "dev": true + }, + "node_modules/babel-helper-mark-eval-scopes": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/babel-helper-mark-eval-scopes/-/babel-helper-mark-eval-scopes-0.4.3.tgz", + "integrity": "sha1-0kSjvvmESHJgP/tG4izorN9VFWI=", + "dev": true + }, + "node_modules/babel-helper-remove-or-void": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/babel-helper-remove-or-void/-/babel-helper-remove-or-void-0.4.3.tgz", + "integrity": "sha1-pPA7QAd6D/6I5F0HAQ3uJB/1rmA=", + "dev": true + }, "node_modules/babel-plugin-dynamic-import-node": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.3.tgz", @@ -4826,6 +4902,18 @@ "object.assign": "^4.1.0" } }, + "node_modules/babel-plugin-minify-dead-code-elimination": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/babel-plugin-minify-dead-code-elimination/-/babel-plugin-minify-dead-code-elimination-0.5.1.tgz", + "integrity": "sha512-x8OJOZIrRmQBcSqxBcLbMIK8uPmTvNWPXH2bh5MDCW1latEqYiRMuUkPImKcfpo59pTUB2FT7HfcgtG8ZlR5Qg==", + "dev": true, + "dependencies": { + "babel-helper-evaluate-path": "^0.5.0", + "babel-helper-mark-eval-scopes": "^0.4.3", + "babel-helper-remove-or-void": "^0.4.3", + "lodash": "^4.17.11" + } + }, "node_modules/babel-plugin-polyfill-corejs2": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/babel-plugin-polyfill-corejs2/-/babel-plugin-polyfill-corejs2-0.2.3.tgz", @@ -4874,6 +4962,21 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/babel-plugin-transform-inline-environment-variables": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-inline-environment-variables/-/babel-plugin-transform-inline-environment-variables-0.4.3.tgz", + "integrity": "sha1-o7CYgzU76LXiM24/8e+KXZP5xIk=", + "dev": true + }, + "node_modules/babel-plugin-transform-remove-imports": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-remove-imports/-/babel-plugin-transform-remove-imports-1.7.0.tgz", + "integrity": "sha512-gprmWf6ry5qrnxMyiDaxZpXzZJP6R9FRA+p0AImLIWRmSEGtlKcFprHKeGMZYkII9rJR6Q1hYou+n1fk6rWf2g==", + "dev": true, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, "node_modules/backo2": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", @@ -8067,6 +8170,12 @@ "node": ">=12" } }, + "node_modules/fs-readdir-recursive": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz", + "integrity": "sha512-GNanXlVr2pf02+sPN40XN8HG+ePaNcvM0q5mZBd668Obwb0yD5GiUbZOFgwn8kGMY6I3mdyDJzieUy3PTYyTRA==", + "dev": true + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -18134,6 +18243,37 @@ } } }, + "@babel/cli": { + "version": "7.15.7", + "resolved": "https://registry.npmjs.org/@babel/cli/-/cli-7.15.7.tgz", + "integrity": "sha512-YW5wOprO2LzMjoWZ5ZG6jfbY9JnkDxuHDwvnrThnuYtByorova/I0HNXJedrUfwuXFQfYOjcqDA4PU3qlZGZjg==", + "dev": true, + "requires": { + "@nicolo-ribaudo/chokidar-2": "2.1.8-no-fsevents.3", + "chokidar": "^3.4.0", + "commander": "^4.0.1", + "convert-source-map": "^1.1.0", + "fs-readdir-recursive": "^1.1.0", + "glob": "^7.0.0", + "make-dir": "^2.1.0", + "slash": "^2.0.0", + "source-map": "^0.5.0" + }, + "dependencies": { + "commander": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", + "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==", + "dev": true + }, + "slash": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/slash/-/slash-2.0.0.tgz", + "integrity": "sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==", + "dev": true + } + } + }, "@babel/code-frame": { "version": "7.16.0", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.16.0.tgz", @@ -20194,6 +20334,13 @@ "integrity": "sha512-tsAQNx32a8CoFhjhijUIhI4kccIAgmGhy8LZMZgGfmXcpMbPRUqn5LWmgRttILi6yeGmBJd2xsPkFMs0PzgPCw==", "dev": true }, + "@nicolo-ribaudo/chokidar-2": { + "version": "2.1.8-no-fsevents.3", + "resolved": "https://registry.npmjs.org/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.3.tgz", + "integrity": "sha512-s88O1aVtXftvp5bCPB7WnmXc5IwOZZ7YPuwNPt+GtOOXpPvad1LfbmjYv+qII7zP6RU2QGnqve27dnLycEnyEQ==", + "dev": true, + "optional": true + }, "@nodelib/fs.scandir": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz", @@ -21639,6 +21786,24 @@ "follow-redirects": "^1.10.0" } }, + "babel-helper-evaluate-path": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/babel-helper-evaluate-path/-/babel-helper-evaluate-path-0.5.0.tgz", + "integrity": "sha512-mUh0UhS607bGh5wUMAQfOpt2JX2ThXMtppHRdRU1kL7ZLRWIXxoV2UIV1r2cAeeNeU1M5SB5/RSUgUxrK8yOkA==", + "dev": true + }, + "babel-helper-mark-eval-scopes": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/babel-helper-mark-eval-scopes/-/babel-helper-mark-eval-scopes-0.4.3.tgz", + "integrity": "sha1-0kSjvvmESHJgP/tG4izorN9VFWI=", + "dev": true + }, + "babel-helper-remove-or-void": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/babel-helper-remove-or-void/-/babel-helper-remove-or-void-0.4.3.tgz", + "integrity": "sha1-pPA7QAd6D/6I5F0HAQ3uJB/1rmA=", + "dev": true + }, "babel-plugin-dynamic-import-node": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.3.tgz", @@ -21648,6 +21813,18 @@ "object.assign": "^4.1.0" } }, + "babel-plugin-minify-dead-code-elimination": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/babel-plugin-minify-dead-code-elimination/-/babel-plugin-minify-dead-code-elimination-0.5.1.tgz", + "integrity": "sha512-x8OJOZIrRmQBcSqxBcLbMIK8uPmTvNWPXH2bh5MDCW1latEqYiRMuUkPImKcfpo59pTUB2FT7HfcgtG8ZlR5Qg==", + "dev": true, + "requires": { + "babel-helper-evaluate-path": "^0.5.0", + "babel-helper-mark-eval-scopes": "^0.4.3", + "babel-helper-remove-or-void": "^0.4.3", + "lodash": "^4.17.11" + } + }, "babel-plugin-polyfill-corejs2": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/babel-plugin-polyfill-corejs2/-/babel-plugin-polyfill-corejs2-0.2.3.tgz", @@ -21686,6 +21863,19 @@ "@babel/helper-define-polyfill-provider": "^0.2.4" } }, + "babel-plugin-transform-inline-environment-variables": { + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-inline-environment-variables/-/babel-plugin-transform-inline-environment-variables-0.4.3.tgz", + "integrity": "sha1-o7CYgzU76LXiM24/8e+KXZP5xIk=", + "dev": true + }, + "babel-plugin-transform-remove-imports": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-remove-imports/-/babel-plugin-transform-remove-imports-1.7.0.tgz", + "integrity": "sha512-gprmWf6ry5qrnxMyiDaxZpXzZJP6R9FRA+p0AImLIWRmSEGtlKcFprHKeGMZYkII9rJR6Q1hYou+n1fk6rWf2g==", + "dev": true, + "requires": {} + }, "backo2": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz", @@ -24246,6 +24436,12 @@ "universalify": "^2.0.0" } }, + "fs-readdir-recursive": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz", + "integrity": "sha512-GNanXlVr2pf02+sPN40XN8HG+ePaNcvM0q5mZBd668Obwb0yD5GiUbZOFgwn8kGMY6I3mdyDJzieUy3PTYyTRA==", + "dev": true + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/web3.js/package.json b/web3.js/package.json index 00c31f8df3cf9b..3fcce55164db05 100644 --- a/web3.js/package.json +++ b/web3.js/package.json @@ -19,9 +19,9 @@ "publishConfig": { "access": "public" }, + "sideEffects": false, "browser": { - "./lib/index.cjs.js": "./lib/index.browser.esm.js", - "./lib/index.esm.js": "./lib/index.browser.esm.js" + "./lib/index.esm.js": "./lib/transpiled-esm/index.js" }, "main": "lib/index.cjs.js", "module": "lib/index.esm.js", @@ -37,9 +37,10 @@ "/src" ], "scripts": { - "build": "npm run clean; cross-env NODE_ENV=production rollup -c; npm run type:gen; npm run flow:gen; npm run flow:check", + "build": "npm run clean; cross-env NODE_ENV=production rollup -c; npm run build:transpiled-esm; npm run type:gen; npm run flow:gen; npm run flow:check", "build:browser-test": "rollup -c test/rollup.config.js", "build:fixtures": "set -ex; ./test/fixtures/noop-program/build.sh", + "build:transpiled-esm": "cross-env BROWSER=true babel src --extensions .ts --source-maps true --out-dir ./lib/transpiled-esm", "clean": "rimraf ./coverage ./lib", "codecov": "set -ex; npm run test:cover; cat ./coverage/lcov.info | codecov", "dev": "cross-env NODE_ENV=development rollup -c", @@ -78,6 +79,7 @@ "tweetnacl": "^1.0.0" }, "devDependencies": { + "@babel/cli": "^7.15.7", "@babel/core": "^7.12.13", "@babel/plugin-proposal-class-properties": "^7.12.1", "@babel/plugin-transform-runtime": "^7.12.10", @@ -106,6 +108,9 @@ "@types/sinon": "^10.0.0", "@typescript-eslint/eslint-plugin": "^4.14.2", "@typescript-eslint/parser": "^4.14.2", + "babel-plugin-minify-dead-code-elimination": "^0.5.1", + "babel-plugin-transform-inline-environment-variables": "^0.4.3", + "babel-plugin-transform-remove-imports": "^1.7.0", "chai": "^4.3.0", "chai-as-promised": "^7.1.1", "codecov": "^3.0.4", diff --git a/web3.js/rollup.config.js b/web3.js/rollup.config.js index f98382be0fa295..9d3ac1d5a38ad0 100644 --- a/web3.js/rollup.config.js +++ b/web3.js/rollup.config.js @@ -69,36 +69,6 @@ function generateConfig(configType, format) { switch (configType) { case 'browser': switch (format) { - case 'esm': { - config.output = [ - { - file: 'lib/index.browser.esm.js', - format: 'es', - sourcemap: true, - }, - ]; - - // Prevent dependencies from being bundled - config.external = [ - /@babel\/runtime/, - '@solana/buffer-layout', - 'bn.js', - 'borsh', - 'bs58', - 'buffer', - 'crypto-hash', - 'http', - 'https', - 'jayson/lib/client/browser', - 'js-sha3', - 'rpc-websockets', - 'secp256k1', - 'superstruct', - 'tweetnacl', - ]; - - break; - } case 'iife': { config.external = ['http', 'https']; @@ -150,8 +120,4 @@ function generateConfig(configType, format) { return config; } -export default [ - generateConfig('node'), - generateConfig('browser', 'esm'), - generateConfig('browser', 'iife'), -]; +export default [generateConfig('node'), generateConfig('browser', 'iife')];