1
0

requirejs.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * grunt-contrib-requirejs
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 Tyler Kellen, contributors
  6. * Licensed under the MIT license.
  7. */
  8. module.exports = function(grunt) {
  9. 'use strict';
  10. var requirejs = require('requirejs');
  11. var LOG_LEVEL_TRACE = 0, LOG_LEVEL_WARN = 2;
  12. // TODO: extend this to send build log to grunt.log.ok / grunt.log.error
  13. // by overriding the r.js logger (or submit issue to r.js to expand logging support)
  14. requirejs.define('node/print', [], function() {
  15. return function print(msg) {
  16. if (msg.substring(0, 5) === 'Error') {
  17. grunt.log.errorlns(msg);
  18. grunt.fail.warn('RequireJS failed.');
  19. } else {
  20. grunt.log.oklns(msg);
  21. }
  22. };
  23. });
  24. grunt.registerMultiTask('requirejs', 'Build a RequireJS project.', function() {
  25. var done = this.async();
  26. var options = this.options({
  27. logLevel: grunt.option('verbose') ? LOG_LEVEL_TRACE : LOG_LEVEL_WARN,
  28. done: function(done, response){
  29. done();
  30. }
  31. });
  32. // The following catches errors in the user-defined `done` function and outputs them.
  33. var tryCatch = function(fn, done, output) {
  34. try {
  35. fn(done, output);
  36. } catch(e) {
  37. grunt.fail.warn('There was an error while processing your done function: "' + e + '"');
  38. }
  39. };
  40. requirejs.optimize(options, tryCatch.bind(null, options.done, done));
  41. });
  42. };