问题描述
我想用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 + " ");
}