Skip to content

Commit 90e166c

Browse files
committed
Modernize the build process
- Current process fails on the external dependencies - This modernizes both the test and build process - And fixes the package defined exports
1 parent be8ca56 commit 90e166c

File tree

13 files changed

+2004
-1785
lines changed

13 files changed

+2004
-1785
lines changed

examples/webpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"brace": "^0.11.1",
66
"browserify-zlib": "^0.2.0",
77
"buffer": "^6.0.3",
8-
"pdfkit": "^0.15.0",
8+
"pdfkit": "file:../..",
99
"process": "^0.11.10",
1010
"readable-stream": "^4.5.2",
1111
"util": "^0.12.5"
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// the fs here is not node fs but the provided virtual one
22
import fs from 'fs';
33
// the content file is returned as is (webpack is configured to load *.afm files as asset/source)
4-
import Courier from 'pdfkit/js/data/Courier.afm';
5-
import CourierBold from 'pdfkit/js/data/Courier-Bold.afm';
4+
import Helvetica from 'pdfkit/data/Helvetica.afm';
5+
import HelveticaBold from 'pdfkit/data/Helvetica-Bold.afm';
6+
import HelveticaBoldOblique from 'pdfkit/data/Helvetica-BoldOblique.afm';
7+
import HelveticaOblique from 'pdfkit/data/Helvetica-Oblique.afm';
8+
import Courier from 'pdfkit/data/Courier.afm';
9+
import CourierBold from 'pdfkit/data/Courier-Bold.afm';
610

711
function registerBinaryFiles(ctx) {
812
ctx.keys().forEach(key => {
@@ -11,23 +15,15 @@ function registerBinaryFiles(ctx) {
1115
});
1216
}
1317

14-
function registerAFMFonts(ctx) {
15-
ctx.keys().forEach(key => {
16-
const match = key.match(/([^/]*\.afm$)/);
17-
if (match) {
18-
// afm files must be stored on data path
19-
fs.writeFileSync(`data/${match[0]}`, ctx(key));
20-
}
21-
});
22-
}
23-
2418
// register all files found in assets folder (relative to src)
2519
registerBinaryFiles(require.context('./static-assets', true));
2620

2721
// register AFM fonts distributed with pdfkit
2822
// is good practice to register only required fonts to avoid the bundle size increase too much
29-
registerAFMFonts(require.context('pdfkit/js/data', false, /Helvetica.*\.afm$/));
30-
3123
// register files imported directly
24+
fs.writeFileSync('data/Helvetica.afm', Helvetica);
25+
fs.writeFileSync('data/Helvetica-Bold.afm', HelveticaBold);
26+
fs.writeFileSync('data/Helvetica-BoldOblique.afm', HelveticaBoldOblique);
27+
fs.writeFileSync('data/Helvetica-Oblique.afm', HelveticaOblique);
3228
fs.writeFileSync('data/Courier.afm', Courier);
3329
fs.writeFileSync('data/Courier-Bold.afm', CourierBold);

examples/webpack/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
symlinks: false,
1717
alias: {
1818
// maps fs to a virtual one allowing to register file content dynamically
19-
fs: __dirname + '/../../js/virtual-fs.js'
19+
fs: 'pdfkit/virtual-fs'
2020
},
2121
fallback: {
2222
// crypto module is not necessary at browser

examples/webpack/yarn.lock

Lines changed: 1536 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323
},
2424
"bugs": "https://github.com/foliojs/pdfkit/issues",
2525
"devDependencies": {
26-
"@babel/core": "^7.26.0",
27-
"@babel/plugin-external-helpers": "^7.25.9",
28-
"@babel/preset-env": "^7.26.0",
2926
"@eslint/js": "^9.17.0",
30-
"@rollup/plugin-babel": "^6.0.4",
31-
"babel-jest": "^29.7.0",
3227
"blob-stream": "^0.1.3",
3328
"brace": "^0.11.1",
3429
"brfs": "~2.0.2",
@@ -44,23 +39,26 @@
4439
"pdfjs-dist": "^2.14.305",
4540
"prettier": "3.4.2",
4641
"pug": "^3.0.3",
47-
"rollup": "^2.79.2",
48-
"rollup-plugin-copy": "^3.5.0",
42+
"vite": "^6.3.5",
43+
"vite-plugin-node-polyfills": "^0.23.0",
44+
"vite-plugin-static-copy": "^3.0.2",
4945
"vitest": "^3.2.4"
5046
},
5147
"dependencies": {
5248
"crypto-js": "^4.2.0",
5349
"fontkit": "^2.0.4",
5450
"jpeg-exif": "^1.1.4",
5551
"linebreak": "^1.1.0",
56-
"@cto.af/linebreak": "^3.0.0",
52+
"@cto.af/linebreak": "^3.0.1",
5753
"png-js": "^1.0.0"
5854
},
5955
"scripts": {
6056
"prepublishOnly": "npm run build",
61-
"build": "rollup -c && npm run build-standalone",
62-
"build-standalone": "browserify --standalone PDFDocument --ignore crypto js/pdfkit.js > js/pdfkit.standalone.js",
63-
"browserify-example": "browserify examples/browserify/browser.js > examples/browserify/bundle.js",
57+
"build": "npm run build-base && npm run build-virtual-fs && npm run build-standalone",
58+
"build-base": "vite build",
59+
"build-virtual-fs": "vite build --config vite.virtual-fs.config.js",
60+
"build-standalone": "vite build --config vite.standalone.config.js",
61+
"browserify-example": "browserify examples/browserify/browser.js -t brfs > examples/browserify/bundle.js",
6462
"pdf-guide": "node docs/generate.js",
6563
"website": "node docs/generate_website.js",
6664
"publish-website": "node docs/publish_website.js",
@@ -73,10 +71,17 @@
7371
},
7472
"main": "js/pdfkit.js",
7573
"module": "js/pdfkit.es.js",
76-
"browserify": {
77-
"transform": [
78-
"brfs"
79-
]
74+
"unpkg": "js/pdfkit.standalone.js",
75+
"exports": {
76+
".": {
77+
"require": "./js/pdfkit.js",
78+
"import": "./js/pdfkit.es.js"
79+
},
80+
"./virtual-fs": {
81+
"require": "./js/virtual-fs.js",
82+
"import": "./js/virtual-fs.es.js"
83+
},
84+
"./data/*": "./js/data/*"
8085
},
8186
"engines": {
8287
"node": ">=20"

rollup.config.js

Lines changed: 0 additions & 118 deletions
This file was deleted.

tests/unit/layout.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import PDFDocument from '../../lib/document';
2+
import LayoutEngine from '../../lib/text/layout';
3+
import { logData } from './helpers';
4+
import { test as baseTest } from 'vitest';
5+
6+
describe('LayoutEngine', () => {
7+
const test = baseTest.extend({
8+
doc: ({}, use) => use(new PDFDocument({ size: 'A4', margin: 72 })),
9+
layout: ({ doc }, use) => use(new LayoutEngine(doc)),
10+
docData: async ({ doc }, use) => use(logData(doc)),
11+
});
12+
13+
describe('boundsOfString', () => {
14+
afterEach(({ doc }) => {
15+
doc.end();
16+
});
17+
test.concurrent.for([
18+
['', undefined, { x: 72, y: 72, width: 0, height: 0 }],
19+
['', {}, { x: 72, y: 72, width: 0, height: 0 }],
20+
[
21+
'Hello world!',
22+
{},
23+
{ x: 72, y: 72, width: 62.736000000000004, height: 11.100000000000001 },
24+
],
25+
])(`'%s', %j -> %j`, ([text, options, expected], { layout, expect }) => {
26+
expect(layout.boundsOfString(text, options)).toEqual(expected);
27+
});
28+
});
29+
});

tests/visual/vector.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { runDocTest } from './helpers';
2-
var tiger = require('../../examples/tiger');
2+
import tiger from '../../examples/tiger';
33

44
describe('vector', function () {
55
test('simple shapes', function () {

vite.config.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { defineConfig } from 'vitest/config';
2+
import { viteStaticCopy } from 'vite-plugin-static-copy'
3+
4+
export const node_modules = [
5+
'stream',
6+
'zlib',
7+
'fs',
8+
'events',
9+
];
10+
11+
export const external_packages = [
12+
'fontkit',
13+
'linebreak',
14+
'png-js',
15+
'crypto-js',
16+
'jpeg-exif'
17+
]
18+
export const external = [
19+
...node_modules,
20+
...external_packages,
21+
];
22+
23+
export default defineConfig({
24+
test: {
25+
globals: true,
26+
setupFiles: './tests/unit/setupTests.js',
27+
coverage: {
28+
provider: 'istanbul'
29+
},
30+
fileParallelism: true,
31+
dir: 'tests'
32+
},
33+
esbuild: {
34+
keepNames: true,
35+
},
36+
build: {
37+
outDir: "js",
38+
lib: {
39+
entry: 'lib/document.js',
40+
fileName: 'pdfkit',
41+
},
42+
rollupOptions: {
43+
external,
44+
output: [
45+
{format: 'cjs', entryFileNames: 'pdfkit.js'},
46+
{format: 'es', entryFileNames: 'pdfkit.es.js'},
47+
]
48+
},
49+
},
50+
plugins: [
51+
viteStaticCopy({
52+
targets: [
53+
{ src: ['lib/font/data/*.afm', 'lib/mixins/data/*.icc'], dest: 'data' }
54+
]
55+
})
56+
]
57+
});

vite.standalone.config.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { defineConfig } from 'vite';
2+
import { nodePolyfills } from 'vite-plugin-node-polyfills';
3+
import { external_packages, node_modules } from './vite.config.js';
4+
import path from 'path';
5+
import fs from 'fs';
6+
7+
export default defineConfig({
8+
plugins: [
9+
nodePolyfills({ include: node_modules }),
10+
{
11+
name: 'inline',
12+
async transform(code, filename) {
13+
const dirname = path.dirname(filename);
14+
return {
15+
// Inline any file imports
16+
code: code.replaceAll(/fs\.readFileSync\(__dirname \+ '(.+?)', '(.+?)'\);/g, (match, filename, encoding) => {
17+
const source = fs.readFileSync(path.join(dirname, filename), encoding);
18+
return `"${source.replaceAll('\n', '\\n')}";`;
19+
})
20+
};
21+
}
22+
}
23+
],
24+
esbuild: {
25+
keepNames: true,
26+
},
27+
build: {
28+
outDir: "js",
29+
emptyOutDir: false,
30+
chunkSizeWarningLimit: 3000,
31+
rollupOptions: {
32+
input: 'lib/document.js',
33+
external: external_packages,
34+
output: {
35+
format: 'umd',
36+
name: 'pdfkit',
37+
entryFileNames: 'pdfkit.standalone.js',
38+
globals: {
39+
'crypto-js': 'CryptoJS',
40+
'fontkit': 'fontkit',
41+
'linebreak': 'LineBreaker',
42+
'jpeg-exif': 'exif',
43+
'png-js': 'PNG'
44+
}
45+
}
46+
}
47+
}
48+
});

0 commit comments

Comments
 (0)