Skip to content

Commit

Permalink
Add export map to package
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nickbabcock committed Mar 20, 2022
1 parent c623db2 commit da16a8e
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 115 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/verifier.yml

This file was deleted.

2 changes: 1 addition & 1 deletion bench/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { WasmHighwayHash, HighwayHash } = require("..");
const { WasmHighwayHash, HighwayHash } = require("highwayhasher");
const { asBuffer } = require("highwayhash");
const { assert } = require("console");

Expand Down
3 changes: 2 additions & 1 deletion bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"author": "Nick Babcock",
"license": "MIT",
"dependencies": {
"highwayhash": "^3.1.1"
"highwayhash": "^3.1.1",
"highwayhasher": "file:.."
}
}
46 changes: 31 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand All @@ -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",
Expand All @@ -42,13 +58,13 @@
],
"author": "Nick Babcock <[email protected]>",
"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"
}
}
45 changes: 24 additions & 21 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
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";
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
Expand Down Expand Up @@ -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"),
];
11 changes: 11 additions & 0 deletions src/main/index_browser_fat.ts
Original file line number Diff line number Diff line change
@@ -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());
File renamed without changes.
8 changes: 8 additions & 0 deletions src/main/index_node.ts
Original file line number Diff line number Diff line change
@@ -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());
16 changes: 12 additions & 4 deletions src/main/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -110,15 +118,15 @@ let wasmSimdInitialized = false;
const loadWasmSimd = async (module?: InitInput) => {
if (!wasmSimdInitialized) {
// @ts-ignore
await simdInit(module ?? wasmSimd());
await simdInit(module ?? wasmSimdInit());
wasmSimdInitialized = true;
}
};

const loadWasm = async (module?: InitInput) => {
if (!wasmInitialized) {
// @ts-ignore
await init(module ?? wasm());
await init(module ?? wasmInit());
wasmInitialized = true;
}
};
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/hash.test.js

This file was deleted.

8 changes: 6 additions & 2 deletions tests/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:../.."
}
}
2 changes: 1 addition & 1 deletion tests/unit/hash.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { HighwayHash, WasmHighwayHash, hasWasmSimd } = require("../..");
const { HighwayHash, WasmHighwayHash, hasWasmSimd } = require("highwayhasher");

// ref: https://github.com/iliakan/detect-node
function isNode() {
Expand Down
1 change: 0 additions & 1 deletion tests/verifier/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion tests/verifier/.npmrc

This file was deleted.

1 change: 0 additions & 1 deletion tests/verifier/README.md

This file was deleted.

26 changes: 0 additions & 26 deletions tests/verifier/package.json

This file was deleted.

14 changes: 0 additions & 14 deletions tests/verifier/pre.js

This file was deleted.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"include": ["src"],
"exclude": ["node_modules"],
"compilerOptions": {
"target": "ES2017",
// what follows is from the tsdx project
Expand Down
7 changes: 7 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vite";

export default defineConfig({
test: {
globals: true,
},
});

0 comments on commit da16a8e

Please sign in to comment.