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

Improve Puppetter utility based on several reports #234

Merged
merged 4 commits into from
Jun 13, 2020
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
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,
}
}