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

Rewrite in Typescript #21

Closed
11 tasks done
berstend opened this issue Jan 29, 2019 · 9 comments
Closed
11 tasks done

Rewrite in Typescript #21

berstend opened this issue Jan 29, 2019 · 9 comments
Assignees
Labels
enhancement New feature or request idea work-in-progress This is currently being worked on

Comments

@berstend
Copy link
Owner

berstend commented Jan 29, 2019

Current progress (28 Nov - not pushed yet!) 📚

  • Rewrite puppeteer-extra in TypeScript 🎉
  • New addExtra export to support non-standard or multiple puppeteer instances
  • Find a way to stay compatible with the existing default export and behavior
  • Make all existing tests go green
  • Find a way to use documentation.js with TypeScript
  • Find a way to have plugins extend Puppeteer types (e.g. page.solveCaptchas())
  • Add TypeScript usage examples to readme
  • Add puppeteer-firefox usage examples to readme
  • Beautify the documentation.js output to the current look
  • Update all plugins to use the new core
  • Update all plugins to use the new documentation approach

(initial comment)

I started this repo before noticing how awesome typescript is. 😄

Whenever I find the time I shall refactor this project into typescript.

Potential issues:

Update

Parts have been rewritten in TypeScript 🎉

Before I can rewrite the rest I need to improve the generated typedoc output.

We're using typedoc-plugin-markdown (the current best thing) and the output looks like this, compared to the previous documentation.js based docs. 😢

I'm really close to start developing my own minimal typedoc-plugin-readme plugin. 😅

@outlabeau
Copy link

Agreed! At this point, i will only use npm modules that have an @types package that goes along with it. Especially important for puppeteer-extra since it's used as a drop-in replacement for puppeteer in the code. Can't wait to see a @types package become available for this!

@berstend
Copy link
Owner Author

@outlabeau I'm gonna create a d.ts for the time being (until everything is written in typescript) 😄 No need for @types though, the new version will come pre-shipped with the definitions.

@outlabeau
Copy link

@berstend that's great! Can't wait for the d.ts.

@n1xn
Copy link

n1xn commented May 11, 2019

What is the status for this intend?

@berstend
Copy link
Owner Author

Apologies, got swamped with $work related things. Given that I love TS and my documentation system seems to be supporting TS now (this was the main blocker) I'll be rewriting the puppeteer-extra core in TS now after some hygiene tasks (updating deps, etc) :)

@berstend berstend self-assigned this Nov 24, 2019
@berstend berstend added the work-in-progress This is currently being worked on label Nov 24, 2019
@berstend berstend pinned this issue Nov 24, 2019
@berstend
Copy link
Owner Author

berstend commented Nov 25, 2019

Current workarounds are mentioned here:
#38 (comment)

I'm currently checking that documentation.js will work with TypeScript as advertised (auto-generated docs are super important 😄).

In case that doesn't work I'll whip up a custom d.ts for the time being with the essentials.

@berstend
Copy link
Owner Author

berstend commented Nov 28, 2019

The heavy lifting is done and the TypeScript version makes all tests go green. The auto-completion support in JS is a game changer, even for non-TS users.

I ended up finding a way to stay backwards compatible with the current default export/behaviour:

// existing simple case: defaults to require "puppeteer" or "puppeteer-core"
const puppeteer = require('puppeteer-extra')
puppeteer.use(...)

// new: optionally provide a puppeteer instance to patch
const { addExtra } = require('puppeteer-extra')
const puppeteer = addExtra(require('puppeteer-firefox'))
puppeteer.use(...)

Using this opportunity to cleanup and overhaul some other things as well before pushing this release.

@berstend
Copy link
Owner Author

First class TypeScript support has just landed!

import puppeteer from 'puppeteer-extra'

import RecaptchaPlugin from 'puppeteer-extra-plugin-recaptcha'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'

puppeteer.use(RecaptchaPlugin()).use(StealthPlugin())

puppeteer.launch({ headless: false }).then(async browser => {
  const page = await browser.newPage()
  await page.setViewport({ width: 800, height: 600 })

  await page.goto('https://bot.sannysoft.com')
  await page.waitFor(5000)
  await page.screenshot({ path: 'stealth.png', fullPage: true })

  await browser.close()
})

image

cc @outlabeau @NikoMontana

They only thing I haven't figured out yet is how plugins could automatically extend built-in Puppeteer types (e.g. page.solveCaptchas()).

Not sure if that's even possible.

But that's not too critical and can be overridden by the user easily ((page as any).solveCaptchas()). :)

@berstend
Copy link
Owner Author

Found a way to have plugins extend Puppeteer types (e.g. page.solveCaptchas()) fully type safe for consumers by shipping an ambient d.ts file. :)

Closing this for now.

@berstend berstend unpinned this issue Nov 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request idea work-in-progress This is currently being worked on
Projects
None yet
Development

No branches or pull requests

3 participants