johnsoncodehk / ts2gamesparks

Convert typescript to GameSparks Cloud Code
8 stars 5 forks source link

Some import statements don't work #1

Closed Ruhrpottpatriot closed 5 years ago

Ruhrpottpatriot commented 6 years ago

First: This is a very nice library for us developers, who don't want to deal with raw JS.

However, an import statement looking like import { Guid } from "../modules/Utils" will result in the library throwing an error. Using the import statement as in the provided example library makes this error go away, but makes my import more verbose.

johnsoncodehk commented 6 years ago

Thank you for your question.

In order to avoid runtime errors, the wording of typescript must correspond to the wording of cloud code. Cloud code can only import the entire module, so if typescript supports the import of some variables, the memory of typescript and cloud code will be different.

johnsoncodehk commented 6 years ago

This case has been supported on this commit. If there has any problem, please let me know.

Ruhrpottpatriot commented 6 years ago

Sorry, your commit isn't working. The result is something very weird looking js (see below). Keep also in mind, that the import { ... } from "..."doesn't just import a single module, but multiple modules separated by a comma, so

import {
    Guid,
    Foo
} from "../modules/Utils";

is perfectly valid. If you can support this, that'd be great. If not, you should add a note in the README, which tells people only to use ìmport * from "..."` as import statement.

Here's the code I tried to compile:

import {
    Guid
} from "../modules/Utils";

if (Spark.getData().error == undefined) {
    let player = Spark.getPlayer();
    let api = Spark.getGameDataService();

    let id = Guid.newGuid();

    let entry = api.createItem("characters", id);
    let data: any = entry.getData();

    if (Spark.getData().name != "null") {
        data.name = Spark.getData().name;
    }

    if (Spark.getData().class != -1) {
        data.class = Spark.getData().class;
    }

    data.playerId = player.getPlayerId();

    let status = entry.persistor().persist().error();
    if (status) {
        Spark.setScriptError("ERROR", status);
        Spark.exit();
    }

    Spark.setScriptData("characterId", id);
    Spark.setScriptData("name", Spark.getData().name);
    Spark.setScriptData("class", Spark.getData().class);
    Spark.setScriptData("level", 1);
}

and this is the result:


"use strict";
/modules/Utils_Guid;
from;
"../modules/Utils";
if (Spark.getData().error == undefined) {
    var player = Spark.getPlayer();
    var api = Spark.getGameDataService();
    var id = module_.. / modules / Utils_Guid.newGuid();
    var entry = api.createItem("characters", id);
    var data = entry.getData();
    if (Spark.getData().name != "null") {
        data.name = Spark.getData().name;
    }
    if (Spark.getData().class != -1) {
        data.class = Spark.getData().class;
    }
    data.playerId = player.getPlayerId();
    var status = entry.persistor().persist().error();
    if (status) {
        Spark.setScriptError("ERROR", status);
        Spark.exit();
    }
    Spark.setScriptData("characterId", id);
    Spark.setScriptData("name", Spark.getData().name);
    Spark.setScriptData("class", Spark.getData().class);
    Spark.setScriptData("level", 1);
}

As far as I can tell, the problematic lines (in the compiled file) are lines 2-4 as well as line 8. I hole this helps. I suspect the problem is that my utils file contains a class and that's the problem here.

johnsoncodehk commented 6 years ago

Thank you for your detailed report.

If you use relative paths, this problem occurs. Please change "../modules/Utils" to "Utils" and then write ""baseUrl": "./modules/" to your tsconfig.json.

Since Cloud Code doesn't use relative paths, I didn't notice this issue and I would consider whether to support relative paths.

johnsoncodehk commented 6 years ago

I make a commit to support export variable because I see your have this case, please update this package before build your scripts.

Ruhrpottpatriot commented 6 years ago

The compilation works, however VSCode (and I guess every other TS IDE) doesn't recognize from "Utils", even when "baseUrl": "./modules/" is set and prints "Cant find module ..."

johnsoncodehk commented 6 years ago

Is your modules folder in the root directory? If modules are in another directory, simply change to "baseUrl": "./your_folder/modules/"

johnsoncodehk commented 6 years ago

In addition, I consider that the result will not support compiling relative paths, because Cloud Code only supports module reference from the modules directory. If relative paths are supported, the directory that can be referenced in typescript is not limited to modules. I have thrown an error on the relative path at compile time.

johnsoncodehk commented 5 years ago

Closed because it has been fixed