0

I have a bunch of TypeScript files in my project, and I want them all to be copied to the output directory on each build, preserving their structure. Here's what I've tried, but it does not work:

<ItemGroup>
    <TypeScriptFiles Include="Scripts\*.ts" />
</ItemGroup>

<Target Name="CopyTypeScriptsToOutput">
    <Copy SourceFiles="@(TypeScriptFiles)" DestinationFolder="$(OutputDir)\Scripts" />
</Target>

I've also used Include="Scripts\**\*.ts" but no success. What could be wrong?

1
  • I didn't check it yet. I'll check it and if it worked I'll mark it as answer. Thanks for reminding. Commented Dec 3, 2019 at 15:35

2 Answers 2

2

I've also used Include="Scripts***.ts" but no success. What could be wrong?

The contents of the Include are the relative path of the files in your project.

The main problem is that you did not specify how the target runs. If you only use the Build UI to build your project, the target will not run. You should add build dependencies to the target, usually like BeforeTargets and AfterTargets, so that you run the target at build time.

Second, you have a problem with the properties of the target generated path like $(OutputDir). I tried to test this property in vs2015,2017,2019, MSBuild does not have this property by default. If the property is not defined by yourself, the value will never be reached. So I recommend that you can use $(OutputPath) and $(OutputDir).

In addition, please place TypeScriptFiles in the target to prevent confusion when the csproj file is first loaded. If you define it globally, it will be recognized by the system and mapped to the project again.

Sample

This is the target that I successfully completed.

 <Target Name="CopyTypeScriptsToOutput" AfterTargets="Build">
   <ItemGroup>
    <TypeScriptFiles Include="Scripts\*.ts" />
   </ItemGroup>

   <Copy SourceFiles="@(TypeScriptFiles)" DestinationFolder="$(OutputPath)\Scripts" />
</Target>

Hope it could help you.

Sign up to request clarification or add additional context in comments.

Comments

0

in my package.json, I ma using cpx to copy files in the dist folder

  "scripts": {
    "copy-media": "$(npm bin)/cpx media/**/*.* dist/media",
    "build": "tsc && yarn copy-media",
  },

So it builds first tsc, and then copy all files from folder media to dist

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.