在 Gulp 中使用 requirejs 优化器节点模块 [英] Using requirejs optimizer node module with Gulp

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

问题描述

gulp-requirejs 插件,但它是 列入黑名单 并带有以下消息:使用require.js 模块直接.

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

文档非常稀少,我如何最好地将它与 Gulp 构建任务结合使用?

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

docs 中有一个例子:

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

但是 requirejs 模块不使用流,所以它不会工作.

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

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

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

推荐答案

我刚刚用 gulp-shell 运行了 r.js 构建命令:

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'
]))

运行大约需要 2 秒,根本不会太长.

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

r 设置在 Gulp 一无所知的外部 build.js 文件中定义.

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

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

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