Using Gulp to build requireJS project - gulp-requirejs

前端 未结 7 507
轮回少年
轮回少年 2020-12-12 20:05

I am trying to use gulp-requirejs to build a demo project. I expect result to be a single file with all js dependencies and template included. Here is my gulpfile.js

相关标签:
7条回答
  • UPDATE

    My previous answer always reported taskReady even if requirejs reported an error. I reconsidered this approach and added error logging. Also I try to fail the build completely as described here gulp-jshint: How to fail the build? because a silent fail really eats your time. See updated code below.

    Drew's comment about blacklist was very helpfull and gulp folks suggest using requirejs directly. So I post my direct requirejs solution:

    var DIST = './dist';
    var requirejs = require('requirejs');
    var requirejsConfig = require('./requireConfig.js').RJSConfig;
    
    gulp.task('requirejs', function (taskReady) {
        requirejsConfig.name = 'index';
        requirejsConfig.out = DIST + 'app.js';
        requirejsConfig.optimize = 'uglify';
        requirejs.optimize(requirejsConfig, function () {
            taskReady();
        }, function (error) {
            console.error('requirejs task failed', JSON.stringify(error))
            process.exit(1);
        });
    });
    

    The file at ./dist/app.js is built and uglified. And this way gulp will know when require has finished building. So the task can be used as a dependency.

    0 讨论(0)
提交回复
热议问题