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 10, 2022
1 parent c623db2 commit 9187777
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 54 deletions.
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:.."
}
}
35 changes: 27 additions & 8 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/es/main/index_browser_fat.d.ts",
"files": [
"dist"
],
Expand Down Expand Up @@ -42,10 +62,9 @@
],
"author": "Nick Babcock <[email protected]>",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"@rollup/plugin-typescript": "^8.3.0",
"@rollup/plugin-wasm": "^5.1.2",
"@nickbabcock/plugin-wasm": "^5.2.0",
"jest": "^27.4.4",
"rollup": "^2.61.1",
"tslib": "^2.3.1",
Expand Down
43 changes: 22 additions & 21 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
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: "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 +82,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"),
];
12 changes: 12 additions & 0 deletions src/main/index_browser_fat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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.

7 changes: 6 additions & 1 deletion tests/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"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",
Expand All @@ -14,5 +16,8 @@
"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
2 changes: 1 addition & 1 deletion tests/verifier/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test/
test/
2 changes: 1 addition & 1 deletion tests/verifier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "jest && karmatic test/hash.test.js",
"pretest": "node pre.js"
"pretest": "cp -r ../unit test"
},
"keywords": [],
"author": "Nick Babcock",
Expand Down
14 changes: 0 additions & 14 deletions tests/verifier/pre.js

This file was deleted.

0 comments on commit 9187777

Please sign in to comment.