Skip to content

Commit

Permalink
Merge pull request #234 from marp-team/improve-puppeteer-handling
Browse files Browse the repository at this point in the history
Improve Puppetter utility based on several reports
  • Loading branch information
yhatt authored Jun 13, 2020
2 parents 1494f46 + f1322e5 commit 6f90e1b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

## [Unreleased]

### Added

- Recognize `CHROME_ENABLE_EXTENSIONS` environment value for enabling Chrome extensions while converting ([#231](https://github.com/marp-team/marp-cli/issues/231), [#234](https://github.com/marp-team/marp-cli/pull/234))

### Fixed

- Recover experimental preview window option (`--preview`, `-p`) and back out deprecation ([#211](https://github.com/marp-team/marp-cli/issues/211), [#232](https://github.com/marp-team/marp-cli/pull/232))
- Show helpful message if the executable Chrome path could not find out ([#220](https://github.com/marp-team/marp-cli/issues/220), [#234](https://github.com/marp-team/marp-cli/pull/234))

### Changed

- Reduce direct dependencies ([#233](https://github.com/marp-team/marp-cli/pull/233))

## v0.18.0 - 2020-06-08
Expand Down
19 changes: 2 additions & 17 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ import templates, {
import { ThemeSet } from './theme'
import { notifier } from './watcher'

type ResolvedType<T> = T extends Promise<infer U> ? U : never
type GeneratedPuppeteerLaunchArgs = ResolvedType<
ReturnType<typeof generatePuppeteerLaunchArgs>
>

export enum ConvertType {
html = 'html',
pdf = 'pdf',
Expand Down Expand Up @@ -352,7 +347,7 @@ export class Converter {
baseFile: File,
processer: (page: puppeteer.Page, uri: string) => Promise<T>
) {
const { executablePath } = await Converter.puppeteerLaunchArgs()
const { executablePath } = generatePuppeteerLaunchArgs()

const tmpFile: File.TmpFileInterface | undefined = await (() => {
if (!this.options.allowLocalFiles) return undefined
Expand Down Expand Up @@ -443,12 +438,11 @@ export class Converter {
}

private static browser?: puppeteer.Browser
private static cachedPuppeteerLaunchArgs?: GeneratedPuppeteerLaunchArgs

private static async runBrowser() {
if (!Converter.browser) {
Converter.browser = await puppeteer.launch({
...(await Converter.puppeteerLaunchArgs()),
...generatePuppeteerLaunchArgs(),
userDataDir: await generatePuppeteerDataDirPath('marp-cli-conversion'),
})
Converter.browser.once('disconnected', () => {
Expand All @@ -457,13 +451,4 @@ export class Converter {
}
return Converter.browser
}

private static async puppeteerLaunchArgs(): Promise<
GeneratedPuppeteerLaunchArgs
> {
if (!Converter.cachedPuppeteerLaunchArgs) {
Converter.cachedPuppeteerLaunchArgs = await generatePuppeteerLaunchArgs()
}
return Converter.cachedPuppeteerLaunchArgs
}
}
6 changes: 3 additions & 3 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ export class Preview extends TypedEventEmitter<Preview.Events> {
}

private async launch() {
const baseArgs = await generatePuppeteerLaunchArgs()
const baseArgs = generatePuppeteerLaunchArgs()

this.puppeteerInternal = await puppeteer.launch({
...baseArgs,
args: [
...baseArgs.args,
'--app=data:text/html,<title>Marp CLI</title>',
`--app=data:text/html,<title>${encodeURIComponent('Marp CLI')}</title>`,
`--window-size=${this.options.width},${this.options.height}`,
],
defaultViewport: null,
executablePath: baseArgs.executablePath,
headless: process.env.NODE_ENV === 'test',
userDataDir: await generatePuppeteerDataDirPath('marp-cli-preview'),
})
Expand Down
22 changes: 20 additions & 2 deletions src/utils/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { promisify } from 'util'
import os from 'os'
import path from 'path'
import { Launcher } from 'chrome-launcher'
import { CLIError } from '../error'

const execPromise = promisify(exec)

Expand Down Expand Up @@ -35,7 +36,7 @@ export const generatePuppeteerDataDirPath = async (
return path.resolve(os.tmpdir(), name)
}

export async function generatePuppeteerLaunchArgs() {
export const generatePuppeteerLaunchArgs = () => {
const args = new Set<string>()

// Docker environment and WSL environment need to disable sandbox. :(
Expand All @@ -54,7 +55,24 @@ export async function generatePuppeteerLaunchArgs() {
} else {
;[executablePath] = Launcher.getInstallations()
}

if (!executablePath) {
throw new CLIError(
'You have to install Google Chrome or Chromium to convert slide deck with current options.'
)
}
}

return { executablePath, args: [...args] }
return {
executablePath,
args: [...args],

// Workaround to avoid force-extensions policy for Chrome enterprise (SET CHROME_ENABLE_EXTENSIONS=1)
// https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-windows
//
// @see https://github.com/marp-team/marp-cli/issues/231
ignoreDefaultArgs: process.env.CHROME_ENABLE_EXTENSIONS
? ['--disable-extensions']
: undefined,
}
}

0 comments on commit 6f90e1b

Please sign in to comment.