作者:奥奥奥尼瀚 | 来源:互联网 | 2023-05-18 13:31
有gulp-requirejs插件,但它被列入黑名单,并显示以下消息:" 直接使用require.js模块".
文档非常稀疏,我如何最好地将它与Gulp构建任务结合使用?
在文档中有一个例子:
var requirejs = require('requirejs');
var cOnfig= {
baseUrl: '../appDir/scripts',
name: 'main',
out: '../build/main-built.js'
};
requirejs.optimize(config, function (buildResponse) {
//buildResponse is just a text output of the modules
//included. Load the built file for the contents.
//Use config.out to get the optimized file contents.
var cOntents= fs.readFileSync(config.out, 'utf8');
}, function(err) {
//optimization err callback
});
但这对我没什么帮助......这是我最好的尝试:
var gulp = require('gulp'),
r = require('requirejs').optimize;
var config2 = {
baseUrl: 'src/js',
name: 'config',
out: 'dist/js/main-built.js'
};
gulp.task('scripts3', function() {
gulp.src(['src/js/**/*.js'])
.pipe(r(config)
.pipe(gulp.dest(config.out))
});
但requirejs模块不使用流,因此它不起作用.
还有Gulp友好的amd-optimize,但它与r.js不一样.
1> 小智..:
要在不调用shell的情况下执行此操作,您可以这样做:
var gulp = require('gulp'),
rjs = require('requirejs'),
cOnfig= {
baseUrl: '../appDir/scripts',
name: 'main',
out: '../build/main-built.js'
};
gulp.task('scripts', function(cb){
rjs.optimize(config, function(buildResponse){
// console.log('build response', buildResponse);
cb();
}, cb);
});
请参阅:https://github.com/phated/requirejs-example-gulpfile/blob/master/gulpfile.js