javascript - Puppeteer Typescript: Eșuează cu erori la transpile


Sursa problemei: Nu pot folosi Javascript așa cum este deoarece versiunea Node.Js a Firebase Functions nu suportă încă Async/Await. Așa că am pus asta în Typescript și acum încerc să transpilez în commonJs.

Atunci o fac.

tsc -p config.json, care apoi produce aceste erori.

../../../node_modules/firebase-functions/lib/providers/auth.d.ts(5,22): error TS2420: Class 'UserRecordMetadata' incorrectly implements interface 'UserMetadata'.
  Property 'lastSignedInAt' is missing in type 'UserRecordMetadata'.
../../../node_modules/firebase-functions/lib/providers/firestore.d.ts(17,19): error TS2694: Namespace 'admin' has no exported member 'firestore'.

De asemenea, folosirea Firebase Serve -only-functions a funcționat bine folosind Vanilla Js, a început să dea eroare abia la Deploy, altceva, când rulez scriptul vanilla folosind node getTags.js, rulează fără probleme.

Deci probabil că problema este tsconfig-ul meu? Vă rog să mă ajutați.

TSCONFIG.JSON

{
    "compilerOptions": {
        "lib": [
            "es6",
            "dom"
        ],
        "module": "commonjs",
        "noImplicitReturns": true,
        "outDir": "lib",
        "target": "es6"
    },
    "files": [
        "getTags.ts"
    ]
}

Typescript.

import puppeteer from 'puppeteer';
import * as functions from 'firebase-functions'

function getTitle() {
    const ogSelector: any = document.querySelector('meta[property="og:title"]');
    if (ogSelector) {
        return ogSelector.content;
    }

    const imgSelector: any = document.querySelector('[itemprop="name"]');
    if (imgSelector) {
        return imgSelector.text;
    }
    if (document.querySelector('title')) {
        return document.querySelector('title').text;
    }
    return window.location.href; // Print URL as a fallback
}

function getDescription() {
    const ogDesc: any = document.querySelector('meta[property="og:description"]');
    if (ogDesc) {
        return ogDesc.content;
    }

    const descSelector: any = document.querySelector('[itemprop="description"]');
    if (descSelector) {
        return descSelector.text;
    }

    const nameDescSelector: any = document.querySelector('meta[name="description"]');
    if (nameDescSelector) {
        return nameDescSelector.content;
    }

    return document.body.innerText.substring(0, 180) + '...';
}

function getImage() {
    const ogImgSelector: any = document.querySelector('meta[property="og:image"]');
    if (ogImgSelector) {
        return ogImgSelector.content;
    }

    const imgTagSelector: any = document.querySelector('[itemprop="image"]');
    if (imgTagSelector) {
        return imgTagSelector.text;
    }

    return null;
}

exports.getTags = functions.https.onRequest((req, res) => {
    (async () => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto('https://itunes.apple.com/za/album/my-dear-melancholy/1363309866?app=music&ign-itsct=1363309866-1363309866&ign-itscg=0176&ign-mpt=uo%3D4');

        const title = await page.evaluate(getTitle);
        const description = await page.evaluate(getDescription);
        const image = await page.evaluate(getImage) || await page.screenshot({ path: 'temp.png' });

        browser.close();

        const tags = {
            title,
            description,
            image,
        };
        console.log("Tags " + JSON.stringify(tags));
        res.send("Done Tags :: " + tags);
    })();
});

Răspuns #1

Când folosesc TS și Puppeteer, se pare că transpiling-ul în ES5 funcționează mai bine pentru mine atunci când folosesc Mocha (ok, nu este implementat pe Firebase), dar merită încercat.

{
    "compilerOptions": {
        //"target": "esnext",
        "target": "es5", //needed for node!
        "declaration": true,
        "lib": [
            "es2015", "dom"
        ]        
    },
    "exclude": [
        "node_modules"
    ]
}