From 6f5d6f5b3d2c1854af31d3f5349b14e51cc9323c Mon Sep 17 00:00:00 2001 From: dynst <148708712+dynst@users.noreply.github.com> Date: Wed, 13 Aug 2025 00:00:00 +0000 Subject: [PATCH 1/6] use .cjs and .mjs file extensions --- package.json | 4 ++-- rollup.config.mjs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index f764536..46697f6 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,8 @@ "name": "hash-wasm", "version": "4.12.0", "description": "Lightning fast hash functions for browsers and Node.js using hand-tuned WebAssembly binaries (MD4, MD5, SHA-1, SHA-2, SHA-3, Keccak, BLAKE2, BLAKE3, PBKDF2, Argon2, bcrypt, scrypt, Adler-32, CRC32, CRC32C, RIPEMD-160, HMAC, xxHash, SM3, Whirlpool)", - "main": "dist/index.umd.js", - "module": "dist/index.esm.js", + "main": "dist/index.cjs", + "module": "dist/index.mjs", "types": "dist/lib/index.d.ts", "scripts": { "build": "sh -c ./scripts/build.sh", diff --git a/rollup.config.mjs b/rollup.config.mjs index fbbfa8f..326178e 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -53,12 +53,12 @@ const MAIN_BUNDLE_CONFIG = { input: "lib/index.ts", output: [ { - file: "dist/index.umd.js", + file: "dist/index.cjs", name: "hashwasm", format: "umd", }, { - file: "dist/index.esm.js", + file: "dist/index.mjs", format: "es", }, ], @@ -69,12 +69,12 @@ const MINIFIED_MAIN_BUNDLE_CONFIG = { input: "lib/index.ts", output: [ { - file: "dist/index.umd.min.js", + file: "dist/index.min.cjs", name: "hashwasm", format: "umd", }, { - file: "dist/index.esm.min.js", + file: "dist/index.min.mjs", format: "es", }, ], @@ -90,7 +90,7 @@ const INDIVIDUAL_BUNDLE_CONFIG = (algorithm) => ({ input: `lib/${algorithm}.ts`, output: [ { - file: `dist/${algorithm}.umd.min.js`, + file: `dist/${algorithm}.min.cjs`, name: "hashwasm", format: "umd", extend: true, From 0ab7aa9344d49bde624c323e517721abc4993adc Mon Sep 17 00:00:00 2001 From: dynst <148708712+dynst@users.noreply.github.com> Date: Wed, 13 Aug 2025 00:00:00 +0000 Subject: [PATCH 2/6] add exports field to package.json ESM projects can now import the tree-shakeable ESM code. Node.js doesn't use the "module" field. --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 46697f6..eb18cb8 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,10 @@ "name": "hash-wasm", "version": "4.12.0", "description": "Lightning fast hash functions for browsers and Node.js using hand-tuned WebAssembly binaries (MD4, MD5, SHA-1, SHA-2, SHA-3, Keccak, BLAKE2, BLAKE3, PBKDF2, Argon2, bcrypt, scrypt, Adler-32, CRC32, CRC32C, RIPEMD-160, HMAC, xxHash, SM3, Whirlpool)", - "main": "dist/index.cjs", - "module": "dist/index.mjs", + "exports": { + "require": "dist/index.cjs", + "default": "dist/index.mjs" + }, "types": "dist/lib/index.d.ts", "scripts": { "build": "sh -c ./scripts/build.sh", From 215c6135718c9dd270daba90496d577bbd3cfaf3 Mon Sep 17 00:00:00 2001 From: dynst <148708712+dynst@users.noreply.github.com> Date: Wed, 13 Aug 2025 00:00:00 +0000 Subject: [PATCH 3/6] keep redundant main field for webpack 4 This field is ignored by all modern tools when "exports" is present. --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index eb18cb8..52a206c 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "hash-wasm", "version": "4.12.0", "description": "Lightning fast hash functions for browsers and Node.js using hand-tuned WebAssembly binaries (MD4, MD5, SHA-1, SHA-2, SHA-3, Keccak, BLAKE2, BLAKE3, PBKDF2, Argon2, bcrypt, scrypt, Adler-32, CRC32, CRC32C, RIPEMD-160, HMAC, xxHash, SM3, Whirlpool)", + "main": "dist/index.cjs", "exports": { "require": "dist/index.cjs", "default": "dist/index.mjs" From e6c35b3227fb3fb49943ac12081bacfec88118d6 Mon Sep 17 00:00:00 2001 From: dynst <148708712+dynst@users.noreply.github.com> Date: Thu, 14 Aug 2025 00:00:00 +0000 Subject: [PATCH 4/6] move type declarations out of lib/ subdirectory now they live next to the files they describe, thanks to rootDir. tsconfig.json seems to be completely ignored and unused here. --- package.json | 2 +- scripts/build.sh | 2 +- tsconfig.json | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 52a206c..ebf7326 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "require": "dist/index.cjs", "default": "dist/index.mjs" }, - "types": "dist/lib/index.d.ts", + "types": "dist/index.d.ts", "scripts": { "build": "sh -c ./scripts/build.sh", "lint": "npx @biomejs/biome check lib test", diff --git a/scripts/build.sh b/scripts/build.sh index 5d04b31..9319a5c 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -32,6 +32,6 @@ docker volume rm hash-wasm-volume # node scripts/optimize node scripts/make_json node --max-old-space-size=4096 ./node_modules/rollup/dist/bin/rollup -c -npx tsc ./lib/index --outDir ./dist --downlevelIteration --emitDeclarationOnly --declaration --resolveJsonModule --allowSyntheticDefaultImports +npx tsc ./lib/index --rootDir ./lib --outDir ./dist --downlevelIteration --emitDeclarationOnly --declaration --resolveJsonModule --allowSyntheticDefaultImports #-s ASSERTIONS=1 \ diff --git a/tsconfig.json b/tsconfig.json index c462cad..c7eee89 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "module": "ES6", "lib": ["es2017", "es7", "es6", "dom"], "baseUrl": ".", + "rootDir": "lib", "strict": false, "esModuleInterop": true, "downlevelIteration": true, @@ -13,4 +14,4 @@ "include": [ "lib", ] -} \ No newline at end of file +} From 545d632dbf12fe5cc3bf7fa35cdf4126600c5d6f Mon Sep 17 00:00:00 2001 From: dynst <148708712+dynst@users.noreply.github.com> Date: Thu, 14 Aug 2025 00:00:00 +0000 Subject: [PATCH 5/6] transpile 1 chunk per file for ESM build index.ts -> index.mjs, argon2.ts -> argon2.mjs, rather than bundled into one giant file. --- rollup.config.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rollup.config.mjs b/rollup.config.mjs index 326178e..ccf997d 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -58,8 +58,11 @@ const MAIN_BUNDLE_CONFIG = { format: "umd", }, { - file: "dist/index.mjs", format: "es", + entryFileNames: "[name].mjs", + preserveModules: true, + preserveModulesRoot: "lib", + dir: "dist", }, ], plugins: [json(), typescript(), license(LICENSE_CONFIG)], From 615e2666a82501dc1e01765bf88c9aaa10ff594b Mon Sep 17 00:00:00 2001 From: dynst <148708712+dynst@users.noreply.github.com> Date: Thu, 14 Aug 2025 00:00:00 +0000 Subject: [PATCH 6/6] fix redundancy in tsconfig.json es2017 is a superset of es7, which is a superset of es6. So use es2017 consistently for target and lib. --- tsconfig.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index c7eee89..54c3c35 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,8 @@ { "compilerOptions": { - "target": "ES6", "module": "ES6", - "lib": ["es2017", "es7", "es6", "dom"], + "target": "es2017", + "lib": ["es2017", "dom"], "baseUrl": ".", "rootDir": "lib", "strict": false,