4

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.

5
  • there is MVc bundle, wont this work for you ? Commented Feb 27, 2019 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. Commented Feb 27, 2019 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.stackoverflow.com/questions/3788605/… Commented Feb 27, 2019 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. Commented Feb 28, 2019 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. Commented Apr 20, 2019 at 0:56

2 Answers 2

7

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Appears to work and is super simple, and understandable.
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

3 Comments

I had tried something similar to this, without success. Because "Never" is used, I was negating my patterns to allow only min file versions. <Content Update="['!wwwroot/js/**/*.min.js','wwwroot/js/**/*.js']" CopyToPublishDirectory="Never" />
Ah, good point; did you try the Exclude attribute? learn.microsoft.com/en-us/visualstudio/msbuild/… - <Compile Include="*.res" Exclude="Form1.cs">
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.

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.