Cocos Creator 2.x 转 3.x 过程分享

大佬牛逼啊,正好也在 2.x转 3.1

大佬,牛逼

感谢大佬分享,已star

感谢大佬分享。请问crypto-js怎么转ts啊?

我都是手动转的,转成class

https://github.com/mapic91/CocosCreator2to3Tools/tree/master/ts/crypto-js
帮你转好了,其实也不用转成class,在开头写个export就可以。我没跑过,你跑跑看,看行不行

这个protobuf怎么用?

大佬牛皮(破音)

我现在的用法是将 proto 转成js。然后生成 *.d.ts 文件。是否可以直接生成 ts

用这个client,https://github.com/mapic91/CocosCreator2to3Tools/blob/master/ts/protobuf/protobuf-client.ts

多谢,我跑跑看

我试过,ts文件js也认的,转ts只要把js文件后缀改为ts,然后加一个export就可以。
比如js里是这样的

(function (global, factory) {

    /* AMD */ if (typeof define === 'function' && define["amd"])
        define(["long"], factory);
    /* CommonJS */ else if (typeof require === 'function' && typeof module === "object" && module && module["exports"])
        module['exports'] = (function () {
            let Long; try { Long = require("long"); } catch (e) { }
            return factory(Long);
        })();
    /* Global */ else
        (global["dcodeIO"] = global["dcodeIO"] || {})["ByteBuffer"] = factory(global["dcodeIO"]["Long"]);

})(this, function (Long) {
    "use strict";

    let ByteBuffer = function (capacity, littleEndian, noAssert) {
		...............
    };  
	............
    return ByteBuffer;
});

改成ts就是这样的:

import {Long} from "./long"

export let ByteBuffer = function () {
    "use strict";

    let ByteBuffer = function (capacity, littleEndian, noAssert) {
		...............
    };  
	............
    return ByteBuffer;
}()
1赞

多谢。我试试。

你的方法应该是正确的方法,可否分享下,我转ts都是手动写class的,不是自动的

这样转也能跑,就是在vs code里没有自动提示

我是直接pbjs 生成 js文件.然后使用pbts命令 按照js文件 生成 .d.ts 声明。这样。只是在vscode中使用的时候有代码提示。其实我在想如果能够用什么工具直接转成 ts文件(不要js)那样感觉更好。这里说的ts就是ts的写法,但是不是.d.ts声明文件

image
pbjs不是能直接生成ts文件吗?

_decodeBuf (key: any, buff: any, len?:any) {

        let Message = protonet.getProto(key);

        let msg = Message.decode(buff);

        if ((msg.ret || msg.ret == 0) && _notNeedProcessRet[key] == undefined) {

            this._disposeErrorMsg(msg.ret)

        }

        if(key != "cs.S2C_MockFights"){

            console.log("【recv】 " + key, msg, Math.floor((new Date().getTime()) / 1000));

        }

        return msg;

    }

    _encodeBuf (key: any, msg: any) {

        if (key != "cs.C2S_KeepAlive" && key != "cs.C2S_MockFights") console.log("【send】 " + key, msg, Math.floor((new Date().getTime()) / 1000));

        let Message = protonet.getProto(key);

        let message = new Message();

        for (let k in msg) {

            message.set(k, msg[k]);

        }

        return message.encode().toBuffer();

    }

我用的不是。pbjs 只能生成 js 然后 pbts -o aa.d.ts aa.js