如何使用gulp将html中的coffee-script和js组合在一起

时间:2015-12-31 04:14:54

标签: coffeescript gulp

我想将coffee编译为js,然后将两个js文件合并为一个js。我怎样才能做到这一点?我试过gulp-useref,但似乎无法实现。

在:

<html>
  <body>
    <script src="lib.js"></script>
    <script src="page.coffee" type="text/coffeescript"></script>
  </body>
</html>

后:

<html>
  <body>
    <script src="mian.js"></script>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

  1. 阅读并转换您的咖啡gulp-coffee
  2. 阅读您的javascript
  3. 使用merge2gulp-concat
  4. 合并它们
  5. uglify他们)这是可选的,但我建议缩小生产代码
  6. 将它们写入文件系统
  7. var gulp = require('gulp');
    var gulpCoffee = require('gulp-coffee');
    var gulpUglify = require('gulp-uglify');
    var gulpConcat = require('gulp-concat');
    var merge2 = require('merge2');
    
    gulp.task('default', function () {
        var coffee = gulp.src('file2.coffee') //1
            .pipe(gulpCoffee()); //1
        var js = gulp.src('file1.js'); //2
    
        merge2([coffee, js]) //3
            .pipe(gulpConcat('main.js')) //3
            .pipe(gulpUglify()) //4
            .pipe(gulp.dest('dest')); //5
    });