Free change of module name

The name can be changed, as shown in the tutorial:

TypeScript.Definitions()
    .ForLoadedAssemblies()
    .WithFormatter((type, f) => "I" + ((TypeLite.TsModels.TsClass)type).Name)

How to change the module name using free formatting?

+4
source share
3 answers

Currently, it is not possible to globally change a module name using a free configuration.

However, you can change the module name for each class:

TypeScript.Definitions().For<MyClass>().ToModule("ModuleName")
+1
source

As with TypeLite version 1.4.0, the following works just fine for changing the module name:

<# var ts = TypeScript.Definitions()
    .WithModuleNameFormatter((module) => "my.module.name");#>

Or you can also play with what is already set as the module name to have more control as follows:

<# var ts = TypeScript.Definitions()
    .WithModuleNameFormatter((module) => "I" + ((TypeLite.TsModels.TsModule)module).Name);#>
+5
source

Now there is a command TypeScriptFluent.WithFormatterthat accepts a delegate TsModuleNameFormatterso that you can translate module names. For example, this code converts the module name to the last part of your namespace ( MyCompany.Business.Module1Module1):

<#
var ts = TypeScript.Definitions()
          .WithFormatter((TsModuleNameFormatter)FormatModuleName)
#>
<#+
string FormatModuleName(string module) {
    var parts = module.Split(new []{'.'});
    var newModule = parts[parts.Length - 1];
    return newModule;
}
#>

Update:

In the latest version of TypeLite 1.1.2.0, the API has changed a bit, so the previous example would look like this:

<#
var ts = TypeScript.Definitions().WithModuleNameFormatter((TsModuleNameFormatter)FormatModuleName)
#>
<#+
string FormatModuleName(TsModel module) {
    var parts = module.Name.Split(new []{'.'});
    var newName = parts[parts.Length - 1];
    return newName;
}
#>
+3
source

Source: https://habr.com/ru/post/1530466/


All Articles