Skip to content

Commit be517a3

Browse files
feat(setup): add puppeteerLaunchOptions param
1 parent 83d1461 commit be517a3

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/commands/htmlExportPdf/htmlExportPdf.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export async function htmlExportPdf(args: undefined | string[], options: HtmlExp
134134
output = replaceExt(output, ".html");
135135
}
136136
else if (options.debug === true) {
137-
await printer.preview(inputPath);
137+
await printer.render(inputPath);
138138
}
139139
else {
140140
const format = options.pageSize;
@@ -175,7 +175,7 @@ export async function htmlExportPdf(args: undefined | string[], options: HtmlExp
175175
});
176176

177177
await Promise.all(promises);
178-
await printer.close();
178+
await printer.closeBrowser();
179179
progress.stop();
180180
!isSingleFile && process.stdout.write(`\n\n ${green(" ✓ ")}${dim("Saved to ")} ${path.join(dir, options.outDir ?? "")}\n\n`);
181181
process.exit(0);

src/core/printer.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ export class Printer extends EventEmitter {
7676
}
7777
}
7878

79-
async setup() {
79+
async setup(puppeteerLaunchOptions?: LaunchOptions) {
8080
const puppeteerOptions = {
8181
// https://github.com/puppeteer/puppeteer/issues/2735#issuecomment-470309033
8282
// pipe: true,
8383
headless: this.headless,
8484
args: ["--disable-dev-shm-usage", "--export-tagged-pdf"],
8585
ignoreHTTPSErrors: this.ignoreHTTPSErrors,
86+
...puppeteerLaunchOptions,
8687
};
8788

8889
if (this.allowLocal)
@@ -168,7 +169,7 @@ export class Printer extends EventEmitter {
168169
return page;
169170
}
170171
catch (error) {
171-
this.closeAfter && this.close();
172+
this.closeAfter && await this.closeBrowser();
172173
throw error;
173174
}
174175
}
@@ -232,7 +233,7 @@ export class Printer extends EventEmitter {
232233
throw e;
233234
});
234235

235-
this.closeAfter && page.close();
236+
this.closeAfter && await page.close();
236237
this.pages.delete(input);
237238

238239
this.emit("postprocessing");
@@ -244,32 +245,35 @@ export class Printer extends EventEmitter {
244245
return await pdfDoc.save();
245246
}
246247
catch (error) {
247-
this.closeAfter && this.close();
248+
this.closeAfter && await this.closeBrowser();
248249
throw error;
249250
}
250251
}
251252

252253
async html(input: string) {
253-
const page = await this.render(input);
254+
let page = this.pages.get(input);
255+
if (!page) {
256+
page = await this.render(input)
257+
.catch((e) => {
258+
throw e;
259+
});
260+
}
254261

255262
const content = await page.content();
256263

257264
if (this.closeAfter) {
258-
page.close();
259-
this.close();
265+
await page.close();
266+
await this.closeBrowser();
260267
}
261268

262269
return content;
263270
}
264271

265-
async preview(input: string) {
266-
const page = await this.render(input);
267-
this.closeAfter && this.close();
268-
return page;
269-
}
270-
271-
async close() {
272-
return this.browser && this.browser.close();
272+
async closeBrowser() {
273+
if (this.browser) {
274+
await this.browser.close();
275+
this.browser = undefined;
276+
}
273277
}
274278

275279
needsAllowedRules() {

0 commit comments

Comments
 (0)