Skip to content

Commit c561f93

Browse files
committed
Add PDF worker build and path utilities
Introduces worker build configuration, source and path utilities for PDF worker scripts. Updates package.json to include new worker entry points and build script, and adds supporting TypeScript and declaration files for worker module handling.
1 parent 7e99a3c commit c561f93

File tree

7 files changed

+76
-4
lines changed

7 files changed

+76
-4
lines changed

.npmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package-lock=false
22
audit=false
33
fund=false
4-
loglevel=warn
4+
#loglevel=warn
55
prefer-offline=false
66
save=false

bin/worker.cjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { join } = require('node:path');
2+
3+
/**
4+
* Gets the absolute path to the PDF worker file.
5+
*
6+
* @returns {string} The absolute path to the PDF worker module (pdf.worker.mjs)
7+
*/
8+
function getPath() {
9+
return join(__dirname, '../dist/node/pdf.worker.mjs');
10+
}
11+
12+
exports.getPath = getPath;

bin/worker.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { dirname, join } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
4+
const __filename = fileURLToPath(import.meta.url);
5+
const __dirname = dirname(__filename);
6+
7+
/**
8+
* Gets the absolute path to the PDF worker file.
9+
*
10+
* @returns {string} The absolute path to the PDF worker module (pdf.worker.mjs)
11+
*/
12+
export function getPath() {
13+
return join(__dirname, '../dist/node/pdf.worker.mjs');
14+
}

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pdf-parse",
3-
"version": "2.3.5",
3+
"version": "2.3.6",
44
"type": "module",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.js",
@@ -14,6 +14,14 @@
1414
"./node": {
1515
"types": "./dist/esm/index.d.ts",
1616
"default": "./dist/node/index.cjs"
17+
},
18+
"./worker/source": {
19+
"import": "./dist/worker/source.js",
20+
"require": "./dist/worker/source.cjs"
21+
},
22+
"./worker/path": {
23+
"import": "./bin/worker.js",
24+
"require": "./bin/worker.cjs"
1725
}
1826
},
1927
"description": "Pure TypeScript, cross-platform module for extracting text, images, and tabular data from PDFs. Run directly in your browser or in Node!",
@@ -56,16 +64,18 @@
5664
"author": "Mehmet Kozan",
5765
"files": [
5866
"dist/",
59-
"src/"
67+
"src/",
68+
"bin/"
6069
],
6170
"scripts": {
62-
"build": "npm-run-all clean build:esm build:cjs build:cjs:rename build:node build:browser build:browser:min",
71+
"build": "npm-run-all clean build:esm build:cjs build:cjs:rename build:node build:browser build:browser:min build:worker",
6372
"build:esm": "tsc",
6473
"build:cjs": "tsc --project tsconfig.node.json",
6574
"build:cjs:rename": "node scripts/rename-cjs.js",
6675
"build:node": "vite build --config vite.config.node.ts",
6776
"build:browser": "vite build --config vite.config.browser.ts",
6877
"build:browser:min": "vite build --config vite.config.browser.min.ts",
78+
"build:worker": "vite build --config vite.config.worker.ts",
6979
"clean:build": "rimraf dist",
7080
"clean:site": "rimraf reports_site/test-report reports_site/coverage reports_site/live_demo/dist-browser",
7181
"clean:test": "rimraf --glob dist test/**/*.txt test/**/imgs test/**/*_images",

src_worker/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as WorkerUrl from 'pdfjs-dist/legacy/build/pdf.worker.mjs?url';
2+
3+
/**
4+
* Gets base64 data URL string for worker pdf.worker.mjs script.
5+
* @returns {string} The worker base64 data URL string.
6+
*/
7+
export function getSource() {
8+
return WorkerUrl.default || WorkerUrl;
9+
}

src_worker/vite-url.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*?url' {
2+
const value: string;
3+
export default value;
4+
}

vite.config.worker.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineConfig } from 'vite';
2+
3+
export default defineConfig({
4+
build: {
5+
outDir: 'dist/worker',
6+
emptyOutDir: false,
7+
sourcemap: false,
8+
minify: false,
9+
lib: {
10+
entry: 'src_worker/index.ts',
11+
name: 'PdfParse',
12+
fileName: (format) => `source.${format === 'es' ? 'js' : 'cjs'}`,
13+
formats: ['es', 'cjs'],
14+
},
15+
rollupOptions: {
16+
output: {
17+
preserveModules: false,
18+
compact: false,
19+
},
20+
},
21+
},
22+
esbuild: false,
23+
});

0 commit comments

Comments
 (0)