Skip to content

Commit fa8b7cd

Browse files
committed
feat(plugins): onbefore and onafter
1 parent b8f8931 commit fa8b7cd

4 files changed

Lines changed: 27 additions & 15 deletions

File tree

scripts/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const client = () => {
4646
importPath,
4747
correctSet,
4848
vendorSourceMap,
49-
dirnameAndFilenameSupport
49+
dirnameAndFilenameSupport,
5050
],
5151
external: ['fontkit-next'],
5252
})
@@ -81,7 +81,7 @@ const node = () => {
8181
importPath,
8282
correctSet,
8383
vendorSourceMap,
84-
dirnameAndFilenameSupport
84+
dirnameAndFilenameSupport,
8585
],
8686
external: ['fontkit-next', 'pdfkit'], // TODO: Remove external pdfkit
8787
})
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
const fs = require("fs");
2-
const path = require("path");
1+
const fs = require('fs')
2+
const path = require('path')
33

4-
const nodeModules = new RegExp(/^(?:.*[\\\/])?node_modules(?:[\\\/].*)?$/);
4+
const nodeModules = new RegExp(/^(?:.*[\\\/])?node_modules(?:[\\\/].*)?$/)
55

66
module.exports = {
7-
name: "dirname",
7+
name: 'dirname',
88
setup(build) {
99
build.onLoad({ filter: /.*/ }, ({ path: filePath }) => {
1010
if (!filePath.match(nodeModules)) {
11-
let contents = fs.readFileSync(filePath, "utf8");
12-
const loader = path.extname(filePath).substring(1);
13-
const dirname = path.dirname(filePath);
11+
let contents = fs.readFileSync(filePath, 'utf8')
12+
const loader = path.extname(filePath).substring(1)
13+
const dirname = path.dirname(filePath)
1414
contents = contents
15-
.replace("__dirname", `"${dirname}"`)
16-
.replace("__filename", `"${filePath}"`);
15+
.replace('__dirname', `"${dirname}"`)
16+
.replace('__filename', `"${filePath}"`)
1717
return {
1818
contents,
1919
loader,
20-
};
20+
}
2121
}
22-
});
22+
})
2323
},
24-
};
24+
}

src/runner/pdfeasy.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ export default class {
256256

257257
const runType = options?.type || 'client'
258258

259+
this.options?.plugins?.forEach(({ onBefore }) => onBefore && onBefore())
260+
259261
return new Promise(async (res, rej) => {
260262
if (!this.pdfkit) {
261263
this.reset()
@@ -286,6 +288,10 @@ export default class {
286288
.then(async () => {
287289
await runPluginBackground(this)
288290

291+
this.options?.plugins?.forEach(
292+
({ onAfter }) => onAfter && onAfter()
293+
)
294+
289295
this.pdfkit?.end()
290296

291297
pageHandler(this).then(() => {
@@ -305,6 +311,10 @@ export default class {
305311
this.pipeline()
306312
.then(() => {
307313
pageHandler(this).then(() => {
314+
this.options?.plugins?.forEach(
315+
({ onAfter }) => onAfter && onAfter()
316+
)
317+
308318
this.pdfkit?.end()
309319
})
310320
})

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ export interface PluginGenerate {
146146

147147
export interface Plugin {
148148
background?: PluginBackgroundCallback<string | false>
149-
page?: PluginPageCallback<void>[]
149+
page: PluginPageCallback<void>[]
150+
onAfter?: () => void
151+
onBefore?: () => void
150152
}
151153

152154
export type PluginBackgroundCallback<T> = (context: PDFKit.PDFPage) => T

0 commit comments

Comments
 (0)