Search code examples
reinforced-typings

Configure reinforced-typings to export class as AMD module


I'm using TypeScript and RequireJS in my project. RequireJS requires Typescript to export modules in a special way (export = ) to model the traditional AMD workflow:

class Foo {
...
}
export = Foo

Reinforced-typings project helps me to convert C# class to TypeScript module, and get output like

module Module1 {
   export class Foo {
   ...
   }
}

I can't find in Reinforced-typings documentation how can I get export = Module1 directive at the end of the generated file.

Probably I should go for a custom ClassCodeGenerator, but I can't find how can I instruct it to append the resulted module with e.g. RtRaw code.


Solution

  • In version 1.5.2 you can use visitor overriding for that:

    using Reinforced.Typings.Visitors.TypeScript;
    class AmdExportVisitor : TypeScriptExportVisitor
            {
                public AmdExportVisitor(TextWriter writer, ExportContext exportContext) : base(writer, exportContext)
                {
                }
    
    
                public override void VisitFile(ExportedFile file)
                {
                    base.VisitFile(file);
                    var ns = file.Namespaces.FirstOrDefault();
                    if (ns != null)
                    {
                        WriteLines($@"
    export = {ns.Name};
    
    ");
                    }
                }
            }
    

    then

    confBuilder.Global(a => a.UseVisitor<AmdExportVisitor>());
    

    And you will get desired result.