Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use singleton Chrome instance to convert into PDF #19

Merged
merged 2 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Add `--watch` (`-w`) option to support watch mode ([#18](https://github.com/marp-team/marp-cli/pull/18))

### Fixed

- Use singleton Chrome instance to convert into PDF ([#19](https://github.com/marp-team/marp-cli/pull/19))

## v0.0.8 - 2018-09-15

### Added
Expand Down
39 changes: 26 additions & 13 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ export class Converter {

try {
const browser = await Converter.runBrowser()
const page = await browser.newPage()

try {
const page = await browser.newPage()
await page.goto(uri, {
waitUntil: ['domcontentloaded', 'networkidle0'],
})
Expand All @@ -124,13 +124,19 @@ export class Converter {
preferCSSPageSize: true,
})
} finally {
await browser.close()
await page.close()
}
} finally {
if (tmpFile) await tmpFile.cleanup()
}
}

static async closeBrowser() {
if (Converter.browser) await Converter.browser.close()
}

private static browser?: puppeteer.Browser

private generateEngine(mergeOptions: MarpitOptions) {
const { html, options } = this.options
const opts: any = { ...options, ...mergeOptions }
Expand All @@ -149,18 +155,25 @@ export class Converter {
return engine
}

private static runBrowser() {
const args: string[] = []
const finder: () => string[] = require('is-wsl')
? chromeFinder.wsl
: chromeFinder[process.platform]
private static async runBrowser() {
if (!Converter.browser) {
const args: string[] = []
const finder: () => string[] = require('is-wsl')
? chromeFinder.wsl
: chromeFinder[process.platform]

if (process.env.IS_DOCKER)
args.push('--no-sandbox', '--disable-dev-shm-usage')
if (process.env.IS_DOCKER)
args.push('--no-sandbox', '--disable-dev-shm-usage')

return puppeteer.launch({
args,
executablePath: finder ? finder()[0] : undefined,
})
Converter.browser = await puppeteer.launch({
args,
executablePath: finder ? finder()[0] : undefined,
})

Converter.browser.once('disconnected', () => {
Converter.browser = undefined
})
}
return Converter.browser
}
}
2 changes: 2 additions & 0 deletions src/marp-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,7 @@ export default async function(argv: string[] = []): Promise<number> {

cli.error(e.message)
return e.errorCode
} finally {
await Converter.closeBrowser()
}
}
1 change: 1 addition & 0 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CLIError } from '../src/error'

jest.mock('fs')

afterAll(() => Converter.closeBrowser())
afterEach(() => jest.restoreAllMocks())

describe('Converter', () => {
Expand Down