### Added

- Add dedicated ESM, CommonJS, Node.js, and browser distribution entry points.
- Add streaming support to the native Node.js SHA-256, SHA-224, and HMAC implementations.
- Add distribution contract tests and real browser Web Worker tests with Playwright.
- Add Node.js 16, 18, 20, 22, and 24 runtime tests against the packed npm artifact.

### Changed
- Refactor the implementation into environment-independent core and Node.js modules.
- Use the native Node.js `crypto` implementation for hashing and HMAC.
- Load the package version automatically when generating distribution banners.
- Export unminified ESM source directly and build only CommonJS and browser/minified distributions.
- Replace `expect.js`, NYC, Tiny Worker, and UglifyJS with Node.js assert, c8, Playwright, and Rollup/Terser.
- Build automatically before `npm pack` and `npm publish`.
- Throw error if update after finalize

### Removed
- Remove runtime CommonJS, AMD, Node.js, Web Worker, and ArrayBuffer environment switches from the core implementation.
- Remove legacy environment flags and obsolete compatibility polyfills.
- Remove the old generated `sha256.mjs` and `sha256.node.mjs` entry points.
This commit is contained in:
Yi-Cyuan Chen
2026-07-27 14:30:34 +08:00
parent b766562f6a
commit a6d0f636aa
40 changed files with 1910 additions and 3363 deletions
+54 -33
View File
@@ -1,48 +1,69 @@
import commonjs from '@rollup/plugin-commonjs';
import { readFileSync } from 'fs';
import terser from '@rollup/plugin-terser';
const disableNodeJs = {
name: 'disable-node-js',
transform(code, id) {
if (id.endsWith('/src/sha256.js')) {
const nodeJsDeclaration = /^\s*var NODE_JS = .*;$/m;
if (!nodeJsDeclaration.test(code)) {
this.error('Could not find the NODE_JS declaration');
}
return code.replace(nodeJsDeclaration, ' var NODE_JS = false;');
}
}
};
const { version } = JSON.parse(
readFileSync(new URL('package.json', import.meta.url), 'utf8')
);
const banner = `/**
* [js-sha256]{@link https://github.com/emn178/js-sha256}
*
* @version ${version}
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
*/`;
export default [
{
input: 'src/sha256.mjs',
external: ['crypto', 'buffer'],
input: 'src/cjs.mjs',
output: {
file: 'build/sha256.node.mjs',
format: 'es'
},
plugins: [commonjs({
strictRequires: false
})]
banner,
exports: 'default',
file: 'build/sha256.cjs',
format: 'cjs'
}
},
{
input: 'src/sha256.mjs',
input: 'src/umd.mjs',
output: {
file: 'build/sha256.mjs',
format: 'es'
},
plugins: [disableNodeJs, commonjs({
strictRequires: false,
ignore: ['crypto', 'buffer']
})]
banner,
exports: 'default',
file: 'build/sha256.js',
format: 'umd',
name: 'sha256'
}
},
{
input: 'build/sha256.mjs',
external: ['crypto'],
input: 'src/node-cjs.mjs',
output: {
banner,
exports: 'default',
file: 'build/sha256.node.cjs',
format: 'cjs'
}
},
{
input: 'src/umd.mjs',
output: [
{
banner,
exports: 'default',
file: 'build/sha256.min.js',
format: 'umd',
name: 'sha256',
plugins: [terser()]
}
]
},
{
input: 'src/index.mjs',
output: {
banner,
file: 'build/sha256.min.mjs',
format: 'es'
},
plugins: [terser()]
format: 'es',
plugins: [terser()]
}
}
];