javascript - puppeteer 师 typescript : Fails with errors when transpilling

标签 javascript typescript firebase google-cloud-functions puppeteer

问题来源:我无法按原样使用 Javascript,因为 Firebase Functions Node.Js 版本尚不支持 Async/Await。所以我把它放在 Typescript 中,现在正在尝试转换为 commonJs。

然后我做。

tsc -p config.json,然后生成这些错误。

../../../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'.

此外,使用 Firebase Serve -only-functions 使用 Vanilla Js 工作正常,只是在 Deploy 上开始失败,另一件事,当使用 node getTags.js 运行 vanilla 脚本时>,运行没有问题。

所以我猜这可能是我的 tsconfig?请帮忙。

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);
    })();
});

最佳答案

当使用 TS 和 puppeteer 时,我似乎发现在使用 Mocha 时转换为 ES5 似乎对我来说效果更好(好吧,没有部署到 Firebase),但它值得一试。

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

关于javascript - puppeteer 师 typescript : Fails with errors when transpilling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646072/

相关文章:

javascript - 单击屏幕上的任意位置即可隐藏通知栏,无需使用 Jquery

java - firebase recyclerview 上的 "No such Method error"

javascript - 跨所有浏览器的 Rails 图像预览

javascript - 如何从TD Id中获取RowID(TR id)

angular - 来自 ngrx/entity 的 SelectAll 没有选择任何东西

javascript - 在这种情况下如何返回 promise ?

使用 cordova-plugin-fcm 通过 PhoneGap Build 进行 Firebase 推送通知

asp.net - Google Protocol Buffers 或类似的 .net/javascript

javascript - 在 javascript 数组中查找元素并返回匹配元素的位置

javascript - 将焦点状态设置为 field+angular 4