Skip to content

Commit

Permalink
feat: switch from CommonJS to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
mojoaxel committed Dec 26, 2023
1 parent cdb9c05 commit 49a17e1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 25 deletions.
6 changes: 2 additions & 4 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { PDFDocument } = require('pdf-lib')
import { PDFDocument } from 'pdf-lib'

const globalObject =
typeof globalThis === 'object'
Expand All @@ -9,7 +9,7 @@ const globalObject =
? self // Worker
: this

class PDFMerger {
export default class PDFMerger {
constructor () {
this.reset()

Expand Down Expand Up @@ -167,5 +167,3 @@ class PDFMerger {
link.click()
}
}

module.exports = PDFMerger
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { PDFDocument } = require('pdf-lib')
const fs = require('fs').promises
import { PDFDocument } from 'pdf-lib'
import fs from 'fs/promises'

class PDFMerger {
export default class PDFMerger {
constructor () {
this.reset()

Expand Down Expand Up @@ -122,5 +122,3 @@ class PDFMerger {
return Buffer.from(uInt8Array)
}
}

module.exports = PDFMerger
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
"name": "pdf-merger-js",
"version": "4.3.1",
"description": "merge multiple PDF documents, or parts of them, to a new PDF document",
"type": "module",
"engines": {
"node": ">=14"
},
"main": "./index.js",
"types": "./index.d.ts",
"browser": "./browser.js",
"scripts": {
"standard": "standard",
"standard:fix": "standard --fix",
"test": "jest --detectOpenHandles",
"test:watch": "jest --watch --forceExit --detectOpenHandles"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest --detectOpenHandles",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --forceExit --detectOpenHandles"
},
"standard": {
"env": [
Expand Down
15 changes: 9 additions & 6 deletions test/browser-fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
* @jest-environment jsdom
*/

const path = require('path')
const fs = require('fs-extra')
const pdfDiff = require('pdf-diff')
const fetch = require('node-fetch')
import path from 'path'
import fs from 'fs-extra'
import pdfDiff from 'pdf-diff'
import fetch from 'node-fetch'
import { jest } from '@jest/globals'

import PDFMerger from '../browser'

/*
add a global `windows.fetch` to mock fetch
*/
global.window.fetch = jest.fn().mockImplementation((requestUrl) => {
return Promise.resolve(fetch(requestUrl))
})

const PDFMerger = require('../browser')

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const FIXTURES_DIR = path.join(__dirname, 'fixtures')
const TMP_DIR = path.join(__dirname, 'tmp')

Expand Down
12 changes: 7 additions & 5 deletions test/fixtures.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const path = require('path')
const fs = require('fs-extra')
const pdfDiff = require('pdf-diff')
const { PDFDocument } = require('pdf-lib')
import path from 'path'
import fs from 'fs-extra'
import pdfDiff from 'pdf-diff'
import { PDFDocument } from 'pdf-lib'
import { jest } from '@jest/globals'

const PDFMerger = require('../index')
import PDFMerger from '../index'

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const FIXTURES_DIR = path.join(__dirname, 'fixtures')
const TMP_DIR = path.join(__dirname, 'tmp')

Expand Down
8 changes: 5 additions & 3 deletions test/issues.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const path = require('path')
const fs = require('fs-extra')
import path from 'path'
import fs from 'fs-extra'
import { jest } from '@jest/globals'

const PDFMerger = require('../index')
import PDFMerger from '../index'

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const FIXTURES_DIR = path.join(__dirname, 'fixtures')
const TMP_DIR = path.join(__dirname, 'tmp')

Expand Down

0 comments on commit 49a17e1

Please sign in to comment.