使用带有Gulp的requirejs优化器节点模块 [英] Using requirejs optimizer node module with Gulp

查看:132
本文介绍了使用带有Gulp的requirejs优化器节点模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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只是模块的文本输出
//包含了加载构建文件的内容。
//使用config.out获取优化的文件内容
var contents = fs.readFileSync(config.out,'utf8');
},function(err){
//优化错误回调
});

但是这对我没有多大帮助...这是我最好的尝试:

  var gulp = require('gulp'),
r = require('requirejs')。optimize;

var config2 = {
baseUrl:'src / js',
名称:'config',
out:'dist / js / main-built.js '
};
$ b gulp.task('scripts3',function(){
gulp.src(['src / js / ** / *。js'])
.pipe( r(config)
.pipe(gulp.dest(config.out))
});

但requirejs模块不使用流,所以它不起作用。



还有非常友好的 amd-optimize ,但它与r.js不同。

解决方案

我刚刚用gulp-shell运行 r.js build命令:

  var gulp = require('gulp'),
shell = require('gulp-shell');

//运行r.js命令,如此简单的获取:)
gulp.task('scripts',shell.task([
'r.js -o build / r / build.js'
] ))

运行大概需要2秒钟,不会太长。

b
$ b

r设置是在一个外部的 build.js 文件中定义的,Gulp kn什么都没有。


There's gulp-requirejs plugin, but it's blacklisted with the following message: "use the require.js module directly".

The docs are quite sparse, how would I best use it in conjunction with Gulp build task?

In the docs there's an example:

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
});

But that doesn't help me much... Here's my best try:

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))
});

But the requirejs module doesn't use streams, so it won't work.

There's also the very Gulp friendly amd-optimize but it's not the same as r.js, yet.

解决方案

I just ended up running the r.js build command with gulp-shell:

var gulp = require('gulp'),
    shell = require('gulp-shell');

// Run the r.js command, so simple taks :)
gulp.task('scripts', shell.task([
    'r.js -o build/r/build.js'
]))

It takes roughly 2 seconds to run, not too long at all.

The r settings are defined in an external build.js file that Gulp knows nothing about.

这篇关于使用带有Gulp的requirejs优化器节点模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆