Automatically switch to mini CSS and JS files in different environments

I am using ASP.Net MVC 5 as well as using the WebEssentials LESS and bundle functions.

WebEssentials is configured to create mini css and js files on save. This is the function that I want, because I can control the output, and it reduces the startup time of the application (without using Asp.Net MVC packages).

How can I automatically use my application to use a smaller version of files when it is deployed without having to manually change the file names?

So, for example, from:

 <link href="somestyles.css" .. > 

in

 <link href="somestyles-min.css" .. > 

I read that I can combine WebEssentials and Asp.Net MVC packages, giving me the option to disable the thumbnail option in ASP.NET MVC packages. This would then replace the MIN files in production ( web.config [debug="false"] ).

Is this my only option or is there a better way to achieve this?

+3
source share
3 answers

This is definitely not the only way. Another way would be to completely disable all Microsoft-based tools (e.g., binding, web components, etc.) and use the Javascript Task Runner. Then compilation of supersets and preprocessors, minimization, and any other external heavy lifting can be in one place. It can also be environment based.

So let me touch on some of your specific issues.

Task done in the style of nodejs and gulp

  • Download nodejs
  • After downloading, open a command prompt and go to the project source. For instance:

     cd "C:\Users\beloud\Documents\Visual Studio 2013\Projects\YourProject" 
  • Initialize the node project by running npm init . This will ask you a bunch of questions about your project. Upon completion, it will create a package.json file that will track your node dependencies and project details.

  • Now we need to install several packages. At the command prompt, enter the following commands:

     npm install gulp -g npm install gulp --save-dev npm install gulp-less --save-dev npm install gulp-minify-css --save-dev npm install gulp-if --save-dev 

    We install gulp globally, so we can use it anywhere (it will add you a path). Then we install several packages locally in our project, which will do this actual work (minimization, processing, etc.).

  • Create a file in the same directory as your package.json named gulpfile.js .

  • Now we need to create our urgent tasks. In gulpfile.js add the following:

     //these are the modules that we'll be using in our task var gulp = require('gulp'), less = require('gulp-less'), gulpif = require('gulp-if'), minifycss = require('gulp-minify-css'); var isDebug = true; // I usually have a config.js file that has this and some handy static paths that I'll be referencing regularly. Then I just require it above. gulp.task('default', function() { return gulp.src('Content/less/**/*.less') .pipe(less()) .pipe(gulpif(isDebug === false, minifycss())) //the first argument is the condition, the second is the method to call if the condition is true .pipe(gulp.dest('Content/css')); }); 
  • Run gulp at the command prompt. This will launch the default task. Basically, he will look for less and less files in all directories under Content/less , compile them in css, minimize them if isDebug is false and output it to Content/css .

  • Let me make it a little more amazing by adding a watch. Add the following to gulpfile.js :

     gulp.task('watch', function() { // Watch .less files gulp.watch('Content/less/**/*.less', ['default']); }); 

    When gulp starts, the task will remain valid until it is completed manually. It will monitor the changes made to any file in Content/less , and will restart the task when the changes are saved.

  • Now you just need to specify the css file name, as it will remain unchanged regardless of the environment.

This is a very simple example of using a task runner to accomplish what you are trying to do. You can do a lot more with nodejs, gulp and everything else that I referenced. I personally would suggest this because it is much more efficient than the one-time tools you are currently using, and Visual Studio 2015 already relies heavily on this new methodology, so you will likely have to find out all this.

You can learn more by following this truly amazing guide, Getting Started with gulp , Mark Goodyear .

+8
source

Is this my only option or is there a better way to achieve this?

This is not the only option, but since you work in the field of MVC, this is one of the best options. Since it is intended for use at different levels, such as individual pages, as well as layouts, it will take care of creating the appropriate link.

In general, I would recommend using MVC-oriented server-side batch infrastructure so that it can handle link building and provide you with an intuitive API.

SquishIt is an open source environment that integrates well with MVC and can also be switched based on criteria such as a debug flag to generate the original source and minimized versions.

Both SquishIt and the new built-in MVC packages are pretty similar in terms of what they are meant to be executed.

0
source

Support for Grunt (and gulp) is added in the next Visual Studio. These are tools for javascript developers to accomplish the same task - production equipment.

Grunt can create a version of the assembly that is not intended for testing, but intended for production. I could spend more time and effort, but this is the future instead of MS, which they did when recruiting. You can already use grunt if you have Node.js installed and be prepared for the future.

There are many resources available . See Also Visual Studio Support for Gulp, Grunt, Bower, and npm.

-1
source

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


All Articles