如何准备好jQuery文档来包装Gulp脚本

问题描述

我想用gulp包装jQuery(document).ready(function($){...});处理过的咖啡脚本。这是因为jQuery的项目实现使用jQuery,而coffeescript文件使用$

由于它是一个有着古怪历史的古老项目,所以我无法更改jQuery的实现方式。另外,编辑所有JS文件以使用jQuery代替$可能会在代码库的其他区域产生问题。

gulpfile.js

中的相关代码
gulp.task('components-scripts',function () {
    var components = getComponentGlobs('coffee');
    gulp.src(components)
        .pipe(sourcemaps.init())
        .pipe(concat('main.js'))
        .pipe(coffee().on('error',util.log))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest(path.source + 'scripts'));
});

将在script.js中产生如下内容:

(function() {
  // All processed coffee script code are here
}).call(this);

我希望它看起来像这样:

(function() {
  jQuery(document).ready(function($) {
    // All processed coffee script code are here
  });
}).call(this);

我在gulpfile.js中使用gulp-coffee

解决方法

我最终使用了gulp-inject-string

if (count>0)
{
    sb.append(" " + count + " ");
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...