Search code examples
javascripttypescriptvisual-studiomsbuildtsconfig

Is there a way to change MSBuild output directory to a custom project name?


I'm trying to compile my typescript code into javascript. I want to merge this with another project, but since my primary project is a different name, I cannot merge it unless I find a way to either output my files without the project name entirely, or change the compiled output project name. I also have many dependencies on different projects which project names cannot change. I want to know if it is possible to change the names in the csproj file itself, or using targets to change the output dir project name.

I am using .targets files, and I have already tried using gulp and tsconfig, but with no luck.

Files:

C:\ProjectMain\Project1\ts

helloWorld.ts

C:\ProjectMain\Project1\js >

Current output:

C:\ProjectMain\Project1\js\Project1

helloWorld.js

Wanted output:

C:\ProjectMain\Project1\js\Project2

helloWorld.js


Solution

  • I am using .targets files, and I have already tried using gulp and tsconfig, but with no luck.

    Not familiar with how to get it work in tsconfig. But it seems you're using msbuild and targets file to build that project, you can edit the project file to customize your build process.

    Since we can specify custom target in .njsprj(project file of Node.js), .csproj(project file of normal C# projects) or xx.targets file.You can use a script below to copy the content of C:\ProjectMain\Project1\js\Project1 to C:\ProjectMain\Project1\js\Project2.

    <Project...>
    ...
      <Target Name="CustomPostBuild" AfterTargets="build">
        <ItemGroup>
          <!--Specify which files we will move-->
          <FilesToMove Include="C:\ProjectMain\Project1\js\Project1\*.*"/> 
        </ItemGroup>
        <MakeDir Directories="C:\ProjectMain\Project1\js\Project2" />
        <Move SourceFiles="@(FilesToMove)" DestinationFolder="C:\ProjectMain\Project1\js\Project2"/>
      </Target>
    </Project>
    

    You can use msbuild tasks to customize your build, copy task, delete task, move task... And those paths can be replaced by macros like $(ProjectDir),$(SolutionDir)... If you have project file in path C:\ProjectMain\Project1\, $(ProjectDir)\ts <=> C:\ProjectMain\Project1\ts.

    Build order: tsc.exe compile xx.ts to xx.js => custom build target

    enter image description here