From da16a8e5c4d7d5c4002f7d7a8fa41b12c93dbba7 Mon Sep 17 00:00:00 2001 From: Nick Babcock Date: Thu, 3 Mar 2022 09:58:18 -0600 Subject: [PATCH] Add export map to package Following the guidelines here: https://nodejs.org/api/packages.html#package-entry-points We now score 100% on skypack quality: https://docs.skypack.dev/package-authors/package-checks This shouldn't have a visible impact except for node js users who install with `--no-addons`, which will cause them to receive the Wasm hasher by default. --- .github/workflows/verifier.yml | 26 ----------- bench/index.js | 2 +- bench/package.json | 3 +- package.json | 46 +++++++++++++------ rollup.config.js | 45 +++++++++--------- src/main/index_browser_fat.ts | 11 +++++ ...index_browser.ts => index_browser_slim.ts} | 0 src/main/index_node.ts | 8 ++++ src/main/wasm.ts | 16 +++++-- tests/e2e/hash.test.js | 1 - tests/e2e/package.json | 8 +++- tests/unit/hash.test.js | 2 +- tests/verifier/.gitignore | 1 - tests/verifier/.npmrc | 1 - tests/verifier/README.md | 1 - tests/verifier/package.json | 26 ----------- tests/verifier/pre.js | 14 ------ tsconfig.json | 1 + vite.config.js | 7 +++ 19 files changed, 104 insertions(+), 115 deletions(-) delete mode 100644 .github/workflows/verifier.yml create mode 100644 src/main/index_browser_fat.ts rename src/main/{index_browser.ts => index_browser_slim.ts} (100%) delete mode 120000 tests/e2e/hash.test.js delete mode 100644 tests/verifier/.gitignore delete mode 100644 tests/verifier/.npmrc delete mode 100644 tests/verifier/README.md delete mode 100644 tests/verifier/package.json delete mode 100644 tests/verifier/pre.js create mode 100644 vite.config.js diff --git a/.github/workflows/verifier.yml b/.github/workflows/verifier.yml deleted file mode 100644 index 3d111e8..0000000 --- a/.github/workflows/verifier.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: Verify - -on: - workflow_dispatch: - -jobs: - verify: - runs-on: ${{ matrix.os }} - strategy: - matrix: - node: [12, 16] - os: [windows-latest, macos-latest, ubuntu-latest] - - steps: - - uses: actions/checkout@v1 - - name: Use Node - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node }} - - name: verifier test - working-directory: ./tests/verifier - run: | - npm install - npm test - env: - CI: true \ No newline at end of file diff --git a/bench/index.js b/bench/index.js index bf28318..634d211 100644 --- a/bench/index.js +++ b/bench/index.js @@ -1,4 +1,4 @@ -const { WasmHighwayHash, HighwayHash } = require(".."); +const { WasmHighwayHash, HighwayHash } = require("highwayhasher"); const { asBuffer } = require("highwayhash"); const { assert } = require("console"); diff --git a/bench/package.json b/bench/package.json index 0454855..8a93bd2 100644 --- a/bench/package.json +++ b/bench/package.json @@ -7,6 +7,7 @@ "author": "Nick Babcock", "license": "MIT", "dependencies": { - "highwayhash": "^3.1.1" + "highwayhash": "^3.1.1", + "highwayhasher": "file:.." } } diff --git a/package.json b/package.json index 22eb27c..8fb15fc 100644 --- a/package.json +++ b/package.json @@ -6,13 +6,33 @@ "type": "git", "url": "git://github.com/nickbabcock/highwayhasher.git" }, - "main": "./dist/node/cjs/index_node.js", - "module": "./dist/node/es/index_node.js", - "browser": { - "./dist/node/cjs/index_node.js": "./dist/browser/cjs/index_browser.js", - "./dist/node/es/index_node.js": "./dist/browser/es/index_browser.js" + "type": "module", + "main": "./dist/browser-fat/umd/index_browser_fat.js", + "module": "./dist/browser-fat/es/index_browser_fat.js", + "exports": { + ".": { + "types": "./dist/browser-fat/es/main/index_browser_fat.d.ts", + "node-addons": { + "import": "./dist/node/es/index_node.js", + "default": "./dist/node/cjs/index_node.cjs" + }, + "import": "./dist/browser-fat/es/index_browser_fat.js", + "default": "./dist/browser-fat/cjs/index_browser_fat.js" + }, + "./slim": { + "types": "./dist/browser-slim/es/main/index_browser_fat.d.ts", + "node-addons": { + "import": "./dist/node/es/index_node.js", + "default": "./dist/node/cjs/index_node.cjs" + }, + "import": "./dist/browser-slim/es/index_browser_slim.js", + "default": "./dist/browser-slim/cjs/index_browser_slim.js" + }, + "./sisd.wasm": "./dist/highwayhasher_wasm_bg.wasm", + "./simd.wasm": "./dist/highwayhasher_wasm_simd_bg.wasm", + "./package.json": "./package.json" }, - "types": "./dist/browser/es/main/index_browser.d.ts", + "types": "./dist/browser-fat/es/main/index_browser_fat.d.ts", "files": [ "dist" ], @@ -27,13 +47,9 @@ "test": "npm run build && npm run test:inplace", "test:inplace": "npm run test:native && npm run test:types", "test:types": "tsc --noEmit tests/unit/usage.ts", - "test:native": "jest", + "test:native": "vitest run", "prepublishOnly": "npm test && npm run optimize && ./assets/download-releases.sh && npm run build:bundle" }, - "jest": { - "testRegex": "./tests/unit/.*.js$", - "testEnvironment": "node" - }, "keywords": [ "highwayhash", "hash", @@ -42,13 +58,13 @@ ], "author": "Nick Babcock ", "license": "MIT", - "dependencies": {}, "devDependencies": { + "@nickbabcock/plugin-wasm": "^5.2.0", "@rollup/plugin-typescript": "^8.3.0", - "@rollup/plugin-wasm": "^5.1.2", - "jest": "^27.4.4", + "@types/node": "^17.0.21", "rollup": "^2.61.1", "tslib": "^2.3.1", - "typescript": "^4.5.3" + "typescript": "^4.5.3", + "vitest": "^0.7.6" } } diff --git a/rollup.config.js b/rollup.config.js index 973f5f3..c3912be 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,4 +1,4 @@ -import { wasm } from "@rollup/plugin-wasm"; +import { wasm } from "@nickbabcock/plugin-wasm"; import typescript from "@rollup/plugin-typescript"; import pkg from "./package.json"; import path from "path"; @@ -6,34 +6,34 @@ import { execSync } from "child_process"; import os from "os"; import fs from "fs"; -const rolls = (fmt, platform) => ({ - input: `src/main/index_${platform}.ts`, +const outdir = (fmt, platform, inline) => + `dist/${platform}${inline ? `-${inline}` : ""}/${fmt}`; + +const rolls = (fmt, platform, inline) => ({ + input: `src/main/index_${platform}${inline ? `_${inline}` : ""}.ts`, output: { - dir: `dist/${platform}/${fmt}`, + dir: outdir(fmt, platform, inline), format: fmt, + entryFileNames: `[name].${fmt === "cjs" ? "cjs" : "js"}`, name: pkg.name, }, external: ["os"], plugins: [ - wasm({ maxFileSize: platform === "node" ? 0 : 100000000 }), - typescript({ outDir: `dist/${platform}/${fmt}` }), + inline !== "slim" && + wasm( + platform === "node" + ? { maxFileSize: 0, targetEnv: "node" } + : { targetEnv: "auto-inline" } + ), + typescript({ outDir: outdir(fmt, platform, inline) }), { name: "custom", + resolveImportMeta: () => `""`, generateBundle() { - // Remove the `import` bundler directive that wasm-bindgen spits out as webpack < 5 - // doesn't understand that directive - const removeImport = (fp) => { - const data = fs.readFileSync(path.resolve(fp), "utf8"); - fs.writeFileSync( - path.resolve(fp), - data.replace("import.meta.url", "input") - ); - }; - - removeImport("src/main/wasm/highwayhasher_wasm.js"); - removeImport("src/main/wasm-simd/highwayhasher_wasm.js"); if (fmt === "cjs" && platform === "node") { - fs.mkdirSync(path.resolve(__dirname, "dist/node"), { recursive: true }); + fs.mkdirSync(path.resolve(__dirname, "dist/node"), { + recursive: true, + }); distributeSharedNode(); // Copy over our wasm bundles to each out directory as a known name to @@ -84,8 +84,11 @@ const distributeSharedNode = () => { }; export default [ + rolls("umd", "browser", "fat"), rolls("cjs", "node"), rolls("es", "node"), - rolls("cjs", "browser"), - rolls("es", "browser"), + rolls("cjs", "browser", "fat"), + rolls("es", "browser", "fat"), + rolls("cjs", "browser", "slim"), + rolls("es", "browser", "slim"), ]; diff --git a/src/main/index_browser_fat.ts b/src/main/index_browser_fat.ts new file mode 100644 index 0000000..13e9d2b --- /dev/null +++ b/src/main/index_browser_fat.ts @@ -0,0 +1,11 @@ +export type { IHash, HashCreator, HighwayLoadOptions } from "./model"; +export { WasmHighwayHash, hasSimd as hasWasmSimd } from "./wasm"; +export { WasmHighwayHash as HighwayHash } from "./wasm"; +import wasm from "./wasm/highwayhasher_wasm_bg.wasm"; +import wasmSimd from "./wasm-simd/highwayhasher_wasm_bg.wasm"; +import { setWasmInit, setWasmSimdInit } from "./wasm"; + +// @ts-ignore +setWasmInit(() => wasm()); +// @ts-ignore +setWasmSimdInit(() => wasmSimd()); diff --git a/src/main/index_browser.ts b/src/main/index_browser_slim.ts similarity index 100% rename from src/main/index_browser.ts rename to src/main/index_browser_slim.ts diff --git a/src/main/index_node.ts b/src/main/index_node.ts index 461382c..dd2e622 100644 --- a/src/main/index_node.ts +++ b/src/main/index_node.ts @@ -1,3 +1,11 @@ export type { IHash, HashCreator, HighwayLoadOptions } from "./model"; export { WasmHighwayHash, hasSimd as hasWasmSimd } from "./wasm"; export { NativeHighwayHash as HighwayHash } from "./native"; +import wasm from "./wasm/highwayhasher_wasm_bg.wasm"; +import wasmSimd from "./wasm-simd/highwayhasher_wasm_bg.wasm"; +import { setWasmInit, setWasmSimdInit } from "./wasm"; + +// @ts-ignore +setWasmInit(() => wasm()); +// @ts-ignore +setWasmSimdInit(() => wasmSimd()); diff --git a/src/main/wasm.ts b/src/main/wasm.ts index ae7104d..2033f26 100644 --- a/src/main/wasm.ts +++ b/src/main/wasm.ts @@ -10,8 +10,16 @@ import init, { WasmHighway } from "./wasm/highwayhasher_wasm"; import simdInit, { WasmHighway as WasmSimdHighway, } from "./wasm-simd/highwayhasher_wasm"; -import wasm from "./wasm/highwayhasher_wasm_bg.wasm"; -import wasmSimd from "./wasm-simd/highwayhasher_wasm_bg.wasm"; + +let wasmInit: (() => InitInput) | undefined = undefined; +export const setWasmInit = (arg: () => InitInput) => { + wasmInit = arg; +}; + +let wasmSimdInit: (() => InitInput) | undefined = undefined; +export const setWasmSimdInit = (arg: () => InitInput) => { + wasmSimdInit = arg; +}; class WasmHash extends WasmHighway implements IHash { constructor(key?: Uint8Array | null | undefined) { @@ -110,7 +118,7 @@ let wasmSimdInitialized = false; const loadWasmSimd = async (module?: InitInput) => { if (!wasmSimdInitialized) { // @ts-ignore - await simdInit(module ?? wasmSimd()); + await simdInit(module ?? wasmSimdInit()); wasmSimdInitialized = true; } }; @@ -118,7 +126,7 @@ const loadWasmSimd = async (module?: InitInput) => { const loadWasm = async (module?: InitInput) => { if (!wasmInitialized) { // @ts-ignore - await init(module ?? wasm()); + await init(module ?? wasmInit()); wasmInitialized = true; } }; diff --git a/tests/e2e/hash.test.js b/tests/e2e/hash.test.js deleted file mode 120000 index 3c47e2f..0000000 --- a/tests/e2e/hash.test.js +++ /dev/null @@ -1 +0,0 @@ -../unit/hash.test.js \ No newline at end of file diff --git a/tests/e2e/package.json b/tests/e2e/package.json index 4fadcbc..a3b4383 100644 --- a/tests/e2e/package.json +++ b/tests/e2e/package.json @@ -4,15 +4,19 @@ "description": "", "main": "index.js", "scripts": { - "test": "karmatic --coverage false hash.test.js" + "pretest": "cp ../unit/hash.test.js .", + "test": "karmatic --coverage false hash.test.js", + "posttest": "rm hash.test.js" }, "keywords": [], "author": "Nick Babcock", "license": "MIT", "devDependencies": { - "jest": "^26.6.0", "karmatic": "^2.1.0", "puppeteer": "^5.4.0", "webpack": "^4.44.2" + }, + "dependencies": { + "highwayhasher": "file:../.." } } diff --git a/tests/unit/hash.test.js b/tests/unit/hash.test.js index 24bdc9b..73c7d3e 100644 --- a/tests/unit/hash.test.js +++ b/tests/unit/hash.test.js @@ -1,4 +1,4 @@ -const { HighwayHash, WasmHighwayHash, hasWasmSimd } = require("../.."); +const { HighwayHash, WasmHighwayHash, hasWasmSimd } = require("highwayhasher"); // ref: https://github.com/iliakan/detect-node function isNode() { diff --git a/tests/verifier/.gitignore b/tests/verifier/.gitignore deleted file mode 100644 index b59f7e3..0000000 --- a/tests/verifier/.gitignore +++ /dev/null @@ -1 +0,0 @@ -test/ \ No newline at end of file diff --git a/tests/verifier/.npmrc b/tests/verifier/.npmrc deleted file mode 100644 index 43c97e7..0000000 --- a/tests/verifier/.npmrc +++ /dev/null @@ -1 +0,0 @@ -package-lock=false diff --git a/tests/verifier/README.md b/tests/verifier/README.md deleted file mode 100644 index cf82b47..0000000 --- a/tests/verifier/README.md +++ /dev/null @@ -1 +0,0 @@ -Directory tests to ensure that what is uploaded to npm works \ No newline at end of file diff --git a/tests/verifier/package.json b/tests/verifier/package.json deleted file mode 100644 index b5f5fff..0000000 --- a/tests/verifier/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "verifier", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "jest && karmatic test/hash.test.js", - "pretest": "node pre.js" - }, - "keywords": [], - "author": "Nick Babcock", - "license": "MIT", - "jest": { - "testRegex": "./test/.*.js$", - "testEnvironment": "node" - }, - "dependencies": { - "highwayhasher": "*" - }, - "devDependencies": { - "jest": "^26.6.0", - "karmatic": "^2.1.0", - "puppeteer": "^5.4.0", - "webpack": "^4.44.2" - } -} diff --git a/tests/verifier/pre.js b/tests/verifier/pre.js deleted file mode 100644 index f180dfa..0000000 --- a/tests/verifier/pre.js +++ /dev/null @@ -1,14 +0,0 @@ -const path = require("path"); -const fs = require("fs"); - -// Copy the test file and change requiring ".." to "highwayhasher" -const data = fs.readFileSync(path.resolve(`../unit/hash.test.js/`), "utf8"); - -if (!fs.existsSync("test")) { - fs.mkdirSync("test"); -} - -fs.writeFileSync( - path.resolve(`test/hash.test.js`), - data.replace("../..", "highwayhasher") -); diff --git a/tsconfig.json b/tsconfig.json index 4b0ee08..c96d37b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "include": ["src"], + "exclude": ["node_modules"], "compilerOptions": { "target": "ES2017", // what follows is from the tsdx project diff --git a/vite.config.js b/vite.config.js new file mode 100644 index 0000000..fd73eca --- /dev/null +++ b/vite.config.js @@ -0,0 +1,7 @@ +import { defineConfig } from "vite"; + +export default defineConfig({ + test: { + globals: true, + }, +});