Table of Contents

Simple Module

As of CSharpToJavaScript 0.2.1

There is an option EnableModules. By default, if more than 2 files are supplied, it is enabled.

Program.cs:

namespace SimpleModule_Example;

public class Program
{
    static Program()
    {
        Hello.Module();
    }
}

Module.cs:

using static CSharpToJavaScript.APIs.JS.Ecma.GlobalObject;

namespace SimpleModule_Example;

public class Hello
{
    public static void Module()
    {
        GlobalThis.Console.Log("Hello from module!");
    }
}

cstojs_options_auto.xml:

<ProjectOptions>
  <Output Folder="Output_auto" />
  <File Source="./Program.cs" />
  <File Source="./Module.cs" />
</ProjectOptions>
Note

You need to run 'cstojs-cli translate --project ./cstojs_options_auto.xml'.

Translated Program.js:

import { Hello } from './Module.js';

class Program
{
    static
    {
        Hello.Module();
    }
}
export { Program };

Translated Module.js:


class Hello
{
    static Module()
    {
        globalThis.console.log("Hello from module!");
    }
}
export { Hello };

Now, if you run node ./Program.js, you will see "Hello from module!" in the console. console

See the full source code: https://github.com/TiLied/CSTOJS_Pages/tree/main/tutorials/SimpleModule_Example

As of CSharpToJavaScript 0.1.5

Using special syntax //...\\.

...
//export { Hello };\\
...

Will be translated as:

export { Hello };

Modifying cstojs_options.xml.

Program.cs:

namespace SimpleModule_Example;

public class Program
{
    static Program()
    {
        Hello.Module();
    }
}

Module.cs:

using static CSharpToJavaScript.APIs.JS.Ecma.GlobalObject;

namespace SimpleModule_Example;

public class Hello
{
    public static void Module()
    {
        GlobalThis.Console.Log("Hello from module!");
    }
}

cstojs_options.xml:

<ProjectOptions>
  <Output Folder="Output" />
  <File Source="./Program.cs">
    <Option AddSBAtTheTop="import { Hello } from './Module.js';" />
  </File>
  <File Source="./Module.cs">
    <Option AddSBAtTheBottom="export { Hello };"/>
  </File>
</ProjectOptions>

Translated Program.js:

import { Hello } from './Module.js';
class Program
{
    static
    {
        Hello.Module();
    }
}

Translated Module.js:


class Hello
{
    static Module()
    {
        globalThis.console.log("Hello from module!");
    }
}
export { Hello };

Now, if you run node ./Program.js, you will see "Hello from module!" in the console. console

See the full source code: https://github.com/TiLied/CSTOJS_Pages/tree/main/tutorials/SimpleModule_Example