Using requirejs optimizer node module with Gulp
Asked Answered
D

2

5

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.

Duplex answered 11/3, 2015 at 23:56 Comment(5)
Well I have not used gulp. But I have good expertise around RequireJS and grunt and non grunt... So what is this streams, cant we use like normal task in gulp?Greig
Gulp uses Node streams, and vinyl. Vinyl is black magic for me at this point, here's a great post on it. I also just found this issue dealing with just this.Duplex
I could just run the shell command with gulp-shell.Duplex
Using amd-optimize is fine. Please check this answer #23978861Smudge
@OweRReLoaDeD amd-optimize is pretty great! But it has it's limitations compared to r.js.Duplex
D
4

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.

Duplex answered 14/3, 2015 at 16:9 Comment(1)
Sorry but for this you dont need Gupl. You can run it via: npm run "task"Yesman
S
8

To do this without invoking the shell, you can do it like:

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

see: https://github.com/phated/requirejs-example-gulpfile/blob/master/gulpfile.js

Scyphus answered 28/10, 2015 at 9:47 Comment(0)
D
4

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.

Duplex answered 14/3, 2015 at 16:9 Comment(1)
Sorry but for this you dont need Gupl. You can run it via: npm run "task"Yesman

© 2022 - 2024 — McMap. All rights reserved.