3

Although there are several articles on this issue, I can't seem to find any that are recent and apply to ASPNET Core 2.x, Visual Studio 2017.

How do I only publish my minified versions of JavaScript (.js) files?

It would be nice to do this via the publish profile (.pubxml) so that I can include/exclude by setting up different publish profiles (Dev, UAT, Staging, Production.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
paultechguy
  • 2,318
  • 4
  • 25
  • 34
  • there is MVc bundle, wont this work for you ? – Alen.Toma Feb 27 '19 at 15:21
  • 1
    If you are referring to creating a bundle of minified files, that doesn't solve the original .js file from being published...where anyone can view it by adjusting the URL. – paultechguy Feb 27 '19 at 18:34
  • I mean you could have two diffrent bundles, one On release and other on debugg, the same could also be applied with diffrent attr to. have a look at this, not sure if this is the answer your looking for though.https://stackoverflow.com/questions/3788605/if-debug-vs-conditionaldebug – Alen.Toma Feb 27 '19 at 18:47
  • Sure, I can create two bundles, but it just seems so hacky. I just don't know why you can't have something in your publish config that would help say, don't deploy these files. That is my question. – paultechguy Feb 28 '19 at 23:48
  • This just doesn't appear possible, so I wrote a post build powershell that just deletes CSS and JavaScript files where a minified version exists. – paultechguy Apr 20 '19 at 00:56

2 Answers2

6

Just exclude all .css/.js files and then include .min files. The last rule overrides the previous.

<ItemGroup>
   <Content Update="wwwroot\**\*.css" CopyToPublishDirectory="never" />
   <Content Update="wwwroot\**\*.min.css" CopyToPublishDirectory="always" />
   <Content Update="wwwroot\**\*.js" CopyToPublishDirectory="never" />
   <Content Update="wwwroot\**\*.min.js" CopyToPublishDirectory="always" />
</ItemGroup>
Retro
  • 76
  • 1
  • 4
0

Publish profiles are essentially project file overrides and use the same schema as project files. Microsoft supplies an example here: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-2.2#exclude-files

<ItemGroup>
  <Content Update="wwwroot/content/**/*.txt" CopyToPublishDirectory="Never" />
</ItemGroup>

Additionally, you can set any file in your project to be included or excluded based on build configuration directly in the project: Conditional Content Based Upon Configuration

  • I had tried something similar to this, without success. Because "Never" is used, I was negating my patterns to allow only min file versions. – paultechguy May 16 '19 at 21:12
  • Ah, good point; did you try the Exclude attribute? https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-items?view=vs-2019#use-the-exclude-attribute - – DeProgrammer May 17 '19 at 13:59
  • Bottom line...publish doesn't support multiple globbing syntax, either by placing patterns in an array using [] or by just separating them with commas. I've tried so many variables and nothing works. – paultechguy May 20 '19 at 16:31