Using the requirejs node optimizer module with Gulp

There is a gulp-requirejs plugin, but a blacklist with the following message: "use require.js directly.

The documents are pretty sparse, how would you best use it in conjunction with the Gulp build <task? >

There is an example in 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 it doesn’t help me much ... Here is my best attempt:

 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 does not use threads, so it will not work.

There is also a very gulp friendly amd-optimize , but it does not match r.js.

+5
source share
2 answers

I just finished working with 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 about 2 seconds to start, but not too long.

The r parameters are defined in an external build.js file, which Gulp knows nothing about.

+4
source

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

 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

+8
source

Source: https://habr.com/ru/post/1215193/


All Articles