Minification in the Gulp

Asked

Viewed 126 times

1

Someone knows how to minify js and css files at runtime. For example : In the HTML file, the call to the Javascript file looks like this :

<script src="~/Content/Site/js/funcoes.js"></script>

I would like to generate a minified file dese script at the time of application execution, this would be possible ?

  • 2

    When you say "the time of implementation of the" you mean "the moment the file is requested from the server"? What language do you have on the server?

  • You need to do this directly in the server environment. If you are using PHP, here is a function to minify css, js and html: https://gist.github.com/tovic/d7b310dea3b33e4732c0

  • Yes, in case I will upload html calling the files normally. and I want the server to be minified.

1 answer

1

This way Javascript and css are minified

var htmlReplace = require('gulp-html-replace') // troca os textos dentro do html
,uglify = require('gulp-uglify') // ninifica js
,usemin = require('gulp-usemin')              /
,cssmin = require('gulp-cssmin') // minifica o tamanho do css

gulp.task('usemin', function() {
  return gulp.src('dist/**/*.html')
    .pipe(usemin({
      js: [uglify],
      css: [cssmin]
    }))
    .pipe(gulp.dest('dist'));
});

In index.html you should comment to show Gulp htmlReplace where the files will be minified

example:

inserir a descrição da imagem aqui


inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.