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

Patch os.arch() to fix Puppeteer issue on Apple M1 #305

Merged
merged 2 commits into from
Nov 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Compatibility with Apple Silicon ([#301](https://github.com/marp-team/marp-cli/issues/301), [#305](https://github.com/marp-team/marp-cli/pull/305))

## v0.22.0 - 2020-10-18

### Added
Expand Down
3 changes: 2 additions & 1 deletion src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ThemeSet } from './theme'
import {
generatePuppeteerDataDirPath,
generatePuppeteerLaunchArgs,
launchPuppeteer,
} from './utils/puppeteer'
import { isChromeInWSLHost, resolveWSLPathToHost } from './utils/wsl'
import { notifier } from './watcher'
Expand Down Expand Up @@ -461,7 +462,7 @@ export class Converter {
if (!Converter.browser) {
const baseArgs = generatePuppeteerLaunchArgs()

Converter.browser = await puppeteer.launch({
Converter.browser = await launchPuppeteer({
...baseArgs,
userDataDir: await generatePuppeteerDataDirPath('marp-cli-conversion', {
wslHost: isChromeInWSLHost(baseArgs.executablePath),
Expand Down
3 changes: 2 additions & 1 deletion src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { File, FileType } from './file'
import {
generatePuppeteerDataDirPath,
generatePuppeteerLaunchArgs,
launchPuppeteer,
} from './utils/puppeteer'
import TypedEventEmitter from './utils/typed-event-emitter'
import { isChromeInWSLHost } from './utils/wsl'
Expand Down Expand Up @@ -135,7 +136,7 @@ export class Preview extends TypedEventEmitter<Preview.Events> {
private async launch() {
const baseArgs = generatePuppeteerLaunchArgs()

this.puppeteerInternal = await puppeteer.launch({
this.puppeteerInternal = await launchPuppeteer({
...baseArgs,
args: [
...baseArgs.args,
Expand Down
20 changes: 20 additions & 0 deletions src/utils/puppeteer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os from 'os'
import path from 'path'
import { Launcher } from 'chrome-launcher'
import puppeteer from 'puppeteer-core'
import { warn } from '../cli'
import { CLIErrorCode, error } from '../error'
import { findEdgeInstallation } from './edge-finder'
Expand Down Expand Up @@ -73,3 +74,22 @@ export const generatePuppeteerLaunchArgs = () => {
: undefined,
}
}

export const launchPuppeteer = (
...args: Parameters<typeof puppeteer['launch']>
) => {
const { arch } = os

try {
os.arch = () => {
// Patch for Apple M1 (arm64)
// @see https://github.com/puppeteer/puppeteer/issues/6634
if (process.platform === 'darwin' && arch() === 'arm64') return 'x64'
return arch()
}

return puppeteer.launch(...args)
} finally {
os.arch = arch
}
}