diff --git a/karma-esm.conf.js b/karma-esm.conf.js index 1287d01d68..8c85c7c3aa 100644 --- a/karma-esm.conf.js +++ b/karma-esm.conf.js @@ -2,10 +2,11 @@ module.exports = function(config) { config.set({ + basePath: "./output/karma", frameworks: [ 'mocha' ], files: [ - { pattern: "./packages/ethers/dist/ethers-all.esm.min.js", type: "module" }, - { pattern: "./packages/tests/dist/tests.esm.js", type: "module" } + { pattern: "./ethers.esm.js", type: "module" }, + { pattern: "./tests.esm.js", type: "module" } ], reporters: [ 'karma' ], plugins: [ diff --git a/karma-umd.conf.js b/karma-umd.conf.js index eb7883bb24..5231a9b9aa 100644 --- a/karma-umd.conf.js +++ b/karma-umd.conf.js @@ -2,10 +2,11 @@ module.exports = function(config) { config.set({ + basePath: "./output/karma", frameworks: [ 'mocha' ], files: [ - "./packages/ethers/dist/ethers-all.umd.min.js", - "./packages/tests/dist/tests.umd.js", + "./ethers.umd.js", + "./tests.umd.js", ], reporters: [ 'karma' ], plugins: [ @@ -41,7 +42,7 @@ module.exports = function(config) { /* client: { mocha: { - grep: 'Etherscan', + grep: 'Test WebSocketProvider', } } */ diff --git a/misc/admin/lib/cmds/bundle-testcases.js b/misc/admin/lib/cmds/bundle-testcases.js index 0f0bf9923d..e2455d30d9 100644 --- a/misc/admin/lib/cmds/bundle-testcases.js +++ b/misc/admin/lib/cmds/bundle-testcases.js @@ -25,6 +25,7 @@ const path_1 = require("path"); const zlib_1 = __importDefault(require("zlib")); const log_1 = require("../log"); const path_2 = require("../path"); +const utils_1 = require("../utils"); const config = { dirs: [ "./input/easyseed-bip39", @@ -51,6 +52,9 @@ const config = { console.log(` - Added ${key} (${data[key].length} bytes)`); }); }); + utils_1.mkdir(path_2.resolve("packages/testcases/lib")); + utils_1.mkdir(path_2.resolve("packages/testcases/lib._esm")); + utils_1.mkdir(path_2.resolve("packages/testcases/lib.esm")); // We write it out to all needed places fs_1.default.writeFileSync(path_2.resolve("packages/testcases/lib/browser-data.json"), JSON.stringify(data)); fs_1.default.writeFileSync(path_2.resolve("packages/testcases/lib._esm/browser-data.json"), JSON.stringify(data)); diff --git a/misc/admin/lib/cmds/setup-karma-tests.d.ts b/misc/admin/lib/cmds/setup-karma-tests.d.ts new file mode 100644 index 0000000000..cb0ff5c3b5 --- /dev/null +++ b/misc/admin/lib/cmds/setup-karma-tests.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/misc/admin/lib/cmds/setup-karma-tests.js b/misc/admin/lib/cmds/setup-karma-tests.js new file mode 100644 index 0000000000..96180c9ffa --- /dev/null +++ b/misc/admin/lib/cmds/setup-karma-tests.js @@ -0,0 +1,37 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const fs_1 = __importDefault(require("fs")); +const path_1 = require("../path"); +const utils_1 = require("../utils"); +function copy(src, dst, transform) { + let data = fs_1.default.readFileSync(path_1.resolve(src)); + if (transform) { + data = Buffer.from(transform(data.toString())); + } + fs_1.default.writeFileSync(dst, data); +} +(function () { + return __awaiter(this, void 0, void 0, function* () { + yield utils_1.mkdir(path_1.resolve("output/karma")); + copy(path_1.resolve("packages/ethers/dist/ethers.esm.js"), path_1.resolve("output/karma/ethers.esm.js")); + copy(path_1.resolve("packages/tests/dist/tests.esm.js"), path_1.resolve("output/karma/tests.esm.js"), (data) => { + return data.replace(/^(import [^;]* from ')(ethers)(';)/, (all, prefix, id, suffix) => { + return prefix + "./ethers.esm.js" + suffix; + }); + }); + copy(path_1.resolve("packages/ethers/dist/ethers.umd.js"), path_1.resolve("output/karma/ethers.umd.js")); + copy(path_1.resolve("packages/tests/dist/tests.umd.js"), path_1.resolve("output/karma/tests.umd.js")); + }); +})(); diff --git a/misc/admin/src.ts/cmds/bundle-testcases.ts b/misc/admin/src.ts/cmds/bundle-testcases.ts index f48bef0962..4930f5d33c 100644 --- a/misc/admin/src.ts/cmds/bundle-testcases.ts +++ b/misc/admin/src.ts/cmds/bundle-testcases.ts @@ -16,6 +16,7 @@ import zlib from "zlib"; import { colorify } from "../log"; import { resolve } from "../path"; +import { mkdir } from "../utils"; const config = { dirs: [ @@ -45,6 +46,10 @@ const config = { }); }); + mkdir(resolve("packages/testcases/lib")); + mkdir(resolve("packages/testcases/lib._esm")); + mkdir(resolve("packages/testcases/lib.esm")); + // We write it out to all needed places fs.writeFileSync(resolve("packages/testcases/lib/browser-data.json"), JSON.stringify(data)); fs.writeFileSync(resolve("packages/testcases/lib._esm/browser-data.json"), JSON.stringify(data)); diff --git a/misc/admin/src.ts/cmds/setup-karma-tests.ts b/misc/admin/src.ts/cmds/setup-karma-tests.ts new file mode 100644 index 0000000000..6bca882e47 --- /dev/null +++ b/misc/admin/src.ts/cmds/setup-karma-tests.ts @@ -0,0 +1,27 @@ + +import fs from "fs"; + +import { resolve } from "../path"; +import { mkdir } from "../utils"; + +function copy(src: string, dst: string, transform?: (data: string) => string): void { + let data = fs.readFileSync(resolve(src)); + if (transform) { + data = Buffer.from(transform(data.toString())); + } + fs.writeFileSync(dst, data); +} + +(async function() { + await mkdir(resolve("output/karma")); + + copy(resolve("packages/ethers/dist/ethers.esm.js"), resolve("output/karma/ethers.esm.js")); + copy(resolve("packages/tests/dist/tests.esm.js"), resolve("output/karma/tests.esm.js"), (data) => { + return data.replace(/^(import [^;]* from ')(ethers)(';)/, (all, prefix, id, suffix) => { + return prefix + "./ethers.esm.js" + suffix; + }); + }); + + copy(resolve("packages/ethers/dist/ethers.umd.js"), resolve("output/karma/ethers.umd.js")); + copy(resolve("packages/tests/dist/tests.umd.js"), resolve("output/karma/tests.umd.js")); +})(); diff --git a/package.json b/package.json index 0e345be43a..262a9882f9 100644 --- a/package.json +++ b/package.json @@ -16,14 +16,14 @@ "_build-cjs": "node ./misc/admin/lib/cmds/echo 'Building CommonJS libraries...' && node ./misc/admin/lib/cmds/set-build-option cjs && npm run build", "_build-esm": "node ./misc/admin/lib/cmds/echo 'Building ES modules libraries...' && node ./misc/admin/lib/cmds/set-build-option esm && npm run build", "clean": "node ./misc/admin/lib/cmds/echo 'Cleaning all build files...' && rm -rf packages/*/lib packages/*/lib._esm packages/*/lib.esm packages/*/node_modules packages/*/dist/*.js packages/*/tsconfig.tsbuildinfo node_modules .package_node_modules package-lock.json", - "_test-dist": "node ./misc/admin/lib/cmds/bundle-testcases && rollup -c rollup-tests.config.js", + "_test-dist": "rollup -c rollup-tests.config.js && node ./misc/admin/lib/cmds/setup-karma-tests", "_test-browser-umd": "karma start --single-run --browsers HeadlessLittleLiar karma-umd.conf.js", "_test-browser-esm": "karma start --single-run --browsers HeadlessLittleLiar karma-esm.conf.js", "test-browser-umd": "npm run _test-dist && npm run _test-browser-umd", "test-browser-esm": "npm run _test-dist && npm run _test-browser-esm", "test-coverage": "nyc --report-dir=output --reporter=lcov --reporter=text mocha --reporter ./packages/tests/reporter-keepalive ./packages/tests/lib/test-*.js | tee output/summary.txt", "test-node": "mocha --no-colors --reporter ./packages/tests/reporter ./packages/tests/lib/test-*.js", - "test-react": "npm run _test-dist && npm run build-dist-shims && pushd misc/ReactNativeTestApp && ./run-tests.sh && popd", + "test-react": "npm run _test-dist && pushd misc/ReactNativeTestApp && ./run-tests.sh && popd", "test": "npm run build-all && npm run test-node", "build-docs": "flatworm docs.wrm docs", "serve-docs": "node ./misc/admin/lib/cmds/serve-docs.js", @@ -31,7 +31,7 @@ "spell-check": "node ./misc/admin/lib/cmds/spell-check.js", "_reset-build": "node ./misc/admin/lib/cmds/set-build-option cjs", "_esm-alias": "node ./misc/admin/lib/cmds/echo 'Bundling ESM elliptic (lib._esm/browser-elliptic.js)...' && rollup -c rollup-pre-alias.config.js && node misc/admin/lib/cmds/esm-alias", - "build-libs": "node ./misc/admin/lib/cmds/update-exports.js && node ./misc/admin/lib/cmds/bundle-testcases && npm run _build-cjs && npm run _build-esm && npm _reset-build && node packages/asm/generate.js && npm run _esm-alias && chmod 755 packages/*/lib/bin/*.js", + "build-libs": "node ./misc/admin/lib/cmds/update-exports.js && node ./misc/admin/lib/cmds/bundle-testcases && npm run _build-cjs && npm run _build-esm && npm run _reset-build && node packages/asm/generate.js && npm run _esm-alias && chmod 755 packages/*/lib/bin/*.js", "build-dist-shims": "rollup -c rollup-shims.config.js && uglifyjs --compress --mangle --output ./packages/shims/dist/index.min.js -- ./packages/shims/dist/index.js", "build-dist-libs": "rollup -c rollup-dist.config.js && uglifyjs packages/ethers/dist/ethers.umd.js --output packages/ethers/dist/ethers.umd.min.js --source-map 'content=packages/ethers/dist/ethers.umd.js.map' && uglifyjs packages/ethers/dist/ethers.esm.js --output packages/ethers/dist/ethers.esm.min.js --source-map 'content=packages/ethers/dist/ethers.esm.js.map'", "_build-dist": "node ./misc/admin/lib/cmds/echo 'Building dist files...' && npm run build-dist-libs && npm run build-dist-shims", @@ -54,22 +54,20 @@ "aws-sdk": "2.137.0", "diff": "4.0.1", "flatworm": "0.0.2-beta.5", - "jison": "^0.4.18", - "karma": "5.1.0", + "jison": "0.4.18", + "karma": "5.2.3", "karma-chrome-launcher": "3.1.0", "karma-mocha": "2.0.1", "libnpmpublish": "3.0.1", "mocha": "^7.1.1", "nyc": "15.1.0", - "rollup": "1.20.1", + "rollup": "2.33.2", "rollup-plugin-node-polyfills": "0.2.1", "rollup-plugin-sourcemaps": "0.6.3", "scrypt-js": "3.0.1", "semver": "^5.6.0", "typescript": "3.8.3", - "uglify-es": "3.3.9", - "webpack": "^4.44.2", - "webpack-cli": "4.1.0" + "uglify-es": "3.3.9" }, "dependencies": { "@babel/parser": "7.8.4", @@ -81,7 +79,6 @@ "aes-js": "3.0.0", "bech32": "1.1.4", "bn.js": "^4.4.0", - "browserify-zlib": "^0.2.0", "elliptic": "6.5.3", "hash.js": "1.1.3", "js-sha3": "0.5.7", diff --git a/packages/ethers/src.ts/index.ts b/packages/ethers/src.ts/index.ts index caa266aac7..dbcdf4b8f1 100644 --- a/packages/ethers/src.ts/index.ts +++ b/packages/ethers/src.ts/index.ts @@ -4,14 +4,6 @@ import * as ethers from "./ethers"; -try { - const anyGlobal = (window as any); - - if (anyGlobal._ethers == null) { - anyGlobal._ethers = ethers; - } -} catch (error) { } - export { ethers }; export { diff --git a/packages/hardware-wallets/src.ts/browser-ethers.ts b/packages/hardware-wallets/src.ts/browser-ethers.ts deleted file mode 100644 index a755756a09..0000000000 --- a/packages/hardware-wallets/src.ts/browser-ethers.ts +++ /dev/null @@ -1,12 +0,0 @@ -"use strict"; - -let ethers: any = { }; - -const w = (window as any); -if (w._ethers == null) { - console.log("WARNING: @ethersproject/hardware-wallet requires ethers loaded first"); -} else { - ethers = w._ethers; -} - -export { ethers } diff --git a/packages/tests/src.ts/browser-ethers.ts b/packages/tests/src.ts/browser-ethers.ts deleted file mode 100644 index 7d3ea2d588..0000000000 --- a/packages/tests/src.ts/browser-ethers.ts +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -console.log("Using global.ethers"); - -const anyGlobal = (window as any); - -const ethers = anyGlobal._ethers; - -export { ethers } diff --git a/rollup-dist.config.js b/rollup-dist.config.js index 399625a3ff..73b8349df7 100644 --- a/rollup-dist.config.js +++ b/rollup-dist.config.js @@ -149,7 +149,7 @@ function getConfig() { } */ const configs = [ -// getEsmConfig(), + getEsmConfig(), getUmdConfig() ]; diff --git a/rollup-tests.config.js b/rollup-tests.config.js index 8305ec73cf..40d9cbf995 100644 --- a/rollup-tests.config.js +++ b/rollup-tests.config.js @@ -8,7 +8,19 @@ import nodePolyfills from "rollup-plugin-node-polyfills"; import json from "@rollup/plugin-json"; -function getConfig(format, input, mainFields) { +function getConfig(format) { + + // ESM config + let input = "./packages/tests/lib.esm/index.js"; + let mainFields = undefined; + let globals = undefined; + + if (format === "umd") { + input = "./packages/tests/lib/index.js"; + mainFields = [ "browser", "main" ]; + globals = { ethers: "ethers" }; + } + const plugins = [ json(), /* @@ -34,19 +46,20 @@ function getConfig(format, input, mainFields) { input: input, output: { file: ("./packages/tests/dist/tests." + format + ".js"), - format: format, - name: "testing" + name: "testing", + format, + globals }, context: "window", treeshake: false, + external: [ "ethers" ], plugins: plugins }; } const configs = [ - getConfig("umd", "./packages/tests/lib/index.js", [ "_browser-all", "browser", "main" ]), - getConfig("esm", "./packages/tests/lib.esm/index.js", undefined), + getConfig("umd"), + getConfig("esm"), ]; export default configs; -