Skip to content

Commit

Permalink
fix: Improve "require" and "commonjs" support
Browse files Browse the repository at this point in the history
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
  • Loading branch information
GordonSmith committed Dec 8, 2023
1 parent bd41149 commit 15e1ace
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
21 changes: 12 additions & 9 deletions README.md
Expand Up @@ -41,7 +41,7 @@ graphviz.dot(dot).then(svg => {
graphvizVersion.then(version => console.log(version));
```

v2.x.x
v2.x.x - ESM (Modern ECMAScript Modules)
```ts
import { Graphviz } from "@hpcc-js/wasm/graphviz";

Expand All @@ -52,16 +52,19 @@ const svg = graphviz.dot(dot);
console.log(graphviz.version());
```

v2.x.x - CommonJS
```ts
const { Graphviz } = require("@hpcc-js/wasm/graphviz");

Graphviz.load().then(graphviz => {
const dot = "digraph G { Hello -> World }";
const svg = graphviz.dot(dot);
console.log(graphviz.version());
});
```

Notes:
* Import must specify which wasm library you are using
* wasmFolder is no longer needed
* All wasm libraries have the same asynchronous load pattern
- `const instance = await Wasm.load();`

### ⚠⚠⚠ TypeScript Notes ⚠⚠⚠

When importing an ESM package AND referencing explicit `exports` (like `@hpcc-js/wasm/graphviz` or `@hpcc-js/wasm/expat`), you should change the following tsconfig.json setting:
* `moduleResolution: Node16`

This will ensure the correct "types" are auto discovered.

9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -21,30 +21,35 @@
".": {
"types": "./types/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.umd.js"
},
"./base91": {
"types": "./types/base91.d.ts",
"import": "./dist/base91.js",
"require": "./dist/base91.cjs",
"default": "./dist/base91.umd.js"
},
"./expat": {
"types": "./types/expat.d.ts",
"import": "./dist/expat.js",
"require": "./dist/expat.cjs",
"default": "./dist/expat.umd.js"
},
"./graphviz": {
"types": "./types/graphviz.d.ts",
"import": "./dist/graphviz.js",
"require": "./dist/graphviz.cjs",
"default": "./dist/graphviz.umd.js"
},
"./zstd": {
"types": "./types/zstd.d.ts",
"import": "./dist/zstd.js",
"require": "./dist/zstd.cjs",
"default": "./dist/zstd.umd.js"
}
},
"main": "./dist/index.umd.js",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"browser": "./dist/index.umd.js",
"unpkg": "./dist/index.umd.js",
Expand Down Expand Up @@ -167,4 +172,4 @@
"url": "https://github.com/hpcc-systems/hpcc-js-wasm/issues"
},
"homepage": "https://hpcc-systems.github.io/hpcc-js-wasm/"
}
}
10 changes: 10 additions & 0 deletions rollup.config.js
Expand Up @@ -20,6 +20,11 @@ const browserTplIndex = (input, umdOutput, esOutput) => ({
format: "umd",
sourcemap: true,
name: pkg.name
}, {
file: esOutput + ".cjs",
format: "commonjs",
sourcemap: true,
name: pkg.name
}, {
file: esOutput + ".js",
format: "es",
Expand All @@ -42,6 +47,11 @@ const browserTpl = (input, umdOutput, esOutput) => ({
format: "umd",
sourcemap: true,
name: pkg.name
}, {
file: esOutput + ".cjs",
format: "commonjs",
sourcemap: true,
name: pkg.name
}, {
file: esOutput + ".js",
format: "es",
Expand Down

0 comments on commit 15e1ace

Please sign in to comment.