使用gulp-usemin不会在html文件中重写uglified文件

时间:2014-07-25 08:46:06

标签: javascript node.js gruntjs gulp grunt-usemin

我遇到了gulp-usemin插件的问题。当usemin任务正在运行时,它正在查询和整理cssjs个文件,但是生成的uglified js文件不会在html文件中重写。我正在分享我的gulpfile.js和index.html代码。

转换前

index.html

<html>
    <head>
        <!-- build:css build/css -->
        <link rel="stylesheet" href="css/blue.css">
        <link rel="stylesheet" href="css/green.css">
        <link rel="stylesheet" href="css/orange.css">
        <!-- endbuild -->
        <!-- build:js build/js1 -->
        <script src="js/add.js"></script>
        <script src="js/sub.js"></script>
        <script src="js/mul.js"></script>
        <!-- endbuild -->
    </head>
    <body>

        <p class="blue" id="sum"></p>
        <p class="green" id="diff"></p>
        <p class="orange" id="mul"></p>
        <img src="images/1.jpg" width="304" height="228">
        <img src="images/2.jpg" width="304" height="228">

        <script>
        document.getElementById('sum').innerHTML=add(4,4);
        document.getElementById('diff').innerHTML=sub(4,4);
        document.getElementById('mul').innerHTML=mul(4,4);

        </script>
    </body>
</html>

gulpfile.js

var usemin = require('gulp-usemin');
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var minifyHtml = require('gulp-minify-html');
var minifyCss = require('gulp-minify-css');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var rev = require('gulp-rev'),
    notify = require('gulp-notify');

gulp.task('usemin', function() {
  gulp.src('./*.html')
    .pipe(usemin({
      css: [minifyCss(), 'concat'],
      html: [minifyHtml({empty: true})],
      js: [uglify()]
    }))
    .pipe(gulp.dest('build/'));
});
转换后

index.html

//you can see that build/css is generated but build/js is not generated
<html>
    <head>
    <link rel="stylesheet" href="build/css"/>

    </head>
    <body>

    <p class="blue" id="sum"></p>
    <p class="green" id="diff"></p>
    <p class="orange" id="mul"></p>
    <img src="images/1.jpg" width="304" height="228">
    <img src="images/2.jpg" width="304" height="228">

    <script>
    document.getElementById('sum').innerHTML=add(4,4);
    document.getElementById('diff').innerHTML=sub(4,4);
    document.getElementById('mul').innerHTML=mul(4,4);

    </script>
    </body>
</html>

帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:2)

首先,您错过了rev js来电中的usemin,将task更改为:

gulp.task('usemin', function() {

  gulp.src('./*.html')
    .pipe(usemin({
      css: [minifyCss(), 'concat'],
      html: [minifyHtml({empty: true})],
      js: [uglify(), rev()]
    }))
    .pipe(gulp.dest('build/'));

  });

此外,blocksgulp-usemin的最后一个参数代表file的路径。你应该确定扩展名不要与目录混淆

<!-- build:css build/style.css -->

<!-- build:js build/js1.js -->

为清楚起见,您还应在两个不同的块之间放置换行符。