Skip to content

Commit

Permalink
fix ESM issues
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Jun 8, 2024
1 parent 4b37fbd commit 0c031ad
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ npm install -S pdf-to-img
NodeJS (using ESM Modules):

```js
const { promises: fs } = require("node:fs");
const { pdf } = require("pdf-to-img");
import { promises as fs } from "node:fs";
import { pdf } from "pdf-to-img";

async function main() {
let counter = 1;
Expand All @@ -45,8 +45,9 @@ main();
If your app does not support ESM modules, just change the import:

```diff
const { promises: fs } = require("node:fs");
- const { pdf } = require("pdf-to-img");
+ const { promises: fs } = require("node:fs");
- import { promises as fs } from "node:fs";
- import { pdf } from "pdf-to-img";

async function main() {
+ const { pdf } = await import("pdf-to-img");
Expand Down
1 change: 0 additions & 1 deletion bin/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node

// @ts-check
/* eslint-disable import/extensions */
import { promises as fs } from "node:fs";
import { parseArgs } from "node:util";
import { join } from "node:path";
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import "./polyfill"; // do this before pdfjs

import "./polyfill.js"; // do this before pdfjs
import { createRequire } from "node:module";
import path from "node:path";
import * as pdfjs from "pdfjs-dist/legacy/build/pdf.mjs";
import type { DocumentInitParameters } from "pdfjs-dist/types/src/display/api.js";
import { NodeCanvasFactory } from "./canvasFactory.js";
import { parseInput } from "./parseInput.js";

const pdfjsPath = path.dirname(require.resolve("pdfjs-dist/package.json"));
const pdfjsPath = path.dirname(
createRequire(import.meta.url).resolve("pdfjs-dist/package.json")
);

/** required since k-yle/pdf-to-img#58, the objects from pdfjs are weirdly structured */
const sanitize = (x: object) => {
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
setupFiles: ["./tests/setupTests.ts"],
coverage: {
provider: "v8",
exclude: ["node_modules", "bin", ".eslintrc.js"],
exclude: ["node_modules", "bin", ".eslintrc.cjs"],
},
testTimeout: 30_000,
},
Expand Down

0 comments on commit 0c031ad

Please sign in to comment.