-
Notifications
You must be signed in to change notification settings - Fork 403
Description
Right now, CosmJS produces CommonJS builds. This works well for all versions of Node.js as well as bundlers like Webpack in order to bundle for the web.
As mentioned in https://vuejsdevelopers.com/2019/02/04/vue-es-module-browser-build/ ("What benefit is there of using Vue as an ES module?"), there is little obvious benefit for switching from CommonJS to ESM since the consumers are either node.js apps or bundlers. However, there is hope that ESM helps optimizing bundle size through tree shaking because ESM can better do static analysis on what is used and what is not (https://webpack.js.org/guides/tree-shaking/). In #904 you see that right now web apps include a lot of unused code that is only needed for key storage.
Other libraries use bundlers to bundle ES modules for the web. This is not desired here as we don't have a single entry point and do not want to decide for that user what should be included in such a bundle.
Do we need CommonJS+ESM hybrid builds?
- Node.js 12 does not support ESM natively without feature flags. But this version reaches EOL at 2022-04-30.
- Webpack support ESM inputs since version 2 ("The webpack 2 release came with built-in support for ES2015 modules (alias harmony modules)")
Does this reduce bundle size
- Test in a next.js project with the analyze plugin
Testing
- Jasmine supports ESM build testing (see Support ES modules with .js extension and package type
module
jasmine/jasmine-npm#170) - JSON test vectors cannot be used easily in tests.
const bip39Vectors = require("./src/testdata/bip39.json");
works and TS transpilesimport bip39Vectors from "./testdata/bip39.json";
toconst bip39_json_1 = __importDefault(require("./testdata/bip39.json"))
. But an untranspiledimport bip39Vectors from "./testdata/bip39.json";
output is not working. See https://nodejs.org/api/esm.html#no-json-module-loading and https://nodejs.org/api/esm.html#json-modules. A workaround for tests is:but this does not work in Webpack/Karma.import { createRequire } from "module"; const require = createRequire(import.meta.url); const bip39Vectors = require("../src/testdata/bip39.json");
- File extensions must me used.
import { EnglishMnemonic } from "./englishmnemonic.js";
must be used for a local file instead ofimport { EnglishMnemonic } from "./englishmnemonic";
. TS will not do this for us.