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

Add --browser option to export #1196

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
"lint-staged": "^15.1.0",
"mermaid": "^10.6.1",
"playwright-chromium": "^1.39.0",
"playwright-firefox": "^1.39.0",
"playwright-webkit": "^1.39.0",
"pnpm": "^8.10.5",
"rimraf": "^5.0.5",
"simple-git-hooks": "^2.9.0",
Expand Down
32 changes: 24 additions & 8 deletions packages/slidev/node/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ExportOptions {
* @default false
*/
perSlide?: boolean
browserArg?: 'chromium' | 'firefox' | 'webkit'
}

function addToTree(tree: TocItem[], info: SlideInfo, slideIndexes: Record<number, number>, level = 1) {
Expand Down Expand Up @@ -65,6 +66,7 @@ export interface ExportNotesOptions {
base?: string
output?: string
timeout?: number
browserArg?: 'chromium' | 'firefox' | 'webkit'
}

function createSlidevProgress(indeterminate = false) {
Expand Down Expand Up @@ -108,12 +110,18 @@ export async function exportNotes({
base = '/',
output = 'notes',
timeout = 30000,
browserArg = 'chromium',
}: ExportNotesOptions): Promise<string> {
if (!packageExists('playwright-chromium'))
throw new Error('The exporting for Slidev is powered by Playwright, please install it via `npm i -D playwright-chromium`')
const browserOption = ["chromium", "firefox", "webkit"].includes(browserArg) ? browserArg : null;
if (!browserOption) {
throw new Error("Available Playwright browsers are: chromium, firefox, webkit")
}
const browserPackage = `playwright-${browserOption}`;
if (!packageExists(browserPackage))
throw new Error('The exporting for Slidev is powered by Playwright, please install it via `npm i -D ' + browserPackage + '`')

const { chromium } = await import('playwright-chromium')
const browser = await chromium.launch()
const { playwright } = await import(browserPackage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this works fine?

I think the following seems better. Make bundler more friendly knows which is imported here, instead of a dynamic string, even if you have checked before.

function loadBrowser(browserPackage) {
  if (browserPackage == 'chromium') {
    return import('playwright-chromium')
  } else ...
}

const browser = await playwright.launch()
const context = await browser.newContext()
const page = await context.newPage()

Expand Down Expand Up @@ -163,14 +171,20 @@ export async function exportSlides({
executablePath = undefined,
withToc = false,
perSlide = false,
browserArg = 'chromium',
}: ExportOptions) {
if (!packageExists('playwright-chromium'))
throw new Error('The exporting for Slidev is powered by Playwright, please install it via `npm i -D playwright-chromium`')
const browserOption = ["chromium", "firefox", "webkit"].includes(browserArg) ? browserArg : null;
if (!browserOption) {
throw new Error("Available Playwright browsers are: chromium, firefox, webkit")
}
const browserPackage = `playwright-${browserOption}`;
if (!packageExists(browserPackage))
throw new Error('The exporting for Slidev is powered by Playwright, please install it via `npm i -D ' + browserPackage + '`')

const pages: number[] = parseRangeString(total, range)

const { chromium } = await import('playwright-chromium')
const browser = await chromium.launch({
const { playwright } = await import(browserPackage)
const browser = await playwright.launch({
executablePath,
})
const context = await browser.newContext({
Expand Down Expand Up @@ -428,6 +442,7 @@ export function getExportOptions(args: ExportArgs, options: ResolvedSlidevOption
executablePath: args['executable-path'],
withToc: args['with-toc'],
perSlide: args['per-slide'],
browserArg: args['browser'],
}
const {
entry,
Expand Down Expand Up @@ -459,5 +474,6 @@ export function getExportOptions(args: ExportArgs, options: ResolvedSlidevOption
executablePath,
withToc: withToc || false,
perSlide: perSlide || false,
browserArg,
}
}