| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * grunt-contrib-requirejs |
| 3 | * http://gruntjs.com/ |
| 4 | * |
| 5 | * Copyright (c) 2016 Tyler Kellen, contributors |
| 6 | * Licensed under the MIT license. |
| 7 | */ |
| 8 | |
| 9 | module.exports = function(grunt) { |
| 10 | 'use strict'; |
| 11 | |
| 12 | var requirejs = require('requirejs'); |
| 13 | var LOG_LEVEL_TRACE = 0, LOG_LEVEL_WARN = 2; |
| 14 | |
| 15 | // TODO: extend this to send build log to grunt.log.ok / grunt.log.error |
| 16 | // by overriding the r.js logger (or submit issue to r.js to expand logging support) |
| 17 | requirejs.define('node/print', [], function() { |
| 18 | return function print(msg) { |
| 19 | if (msg.substring(0, 5) === 'Error') { |
| 20 | grunt.log.errorlns(msg); |
| 21 | grunt.fail.warn('RequireJS failed.'); |
| 22 | } else { |
| 23 | grunt.log.oklns(msg); |
| 24 | } |
| 25 | }; |
| 26 | }); |
| 27 | |
| 28 | grunt.registerMultiTask('requirejs', 'Build a RequireJS project.', function() { |
| 29 | |
| 30 | var done = this.async(); |
| 31 | var options = this.options({ |
| 32 | logLevel: grunt.option('verbose') ? LOG_LEVEL_TRACE : LOG_LEVEL_WARN, |
| 33 | error: false, |
| 34 | done: function(done){ |
| 35 | done(); |
| 36 | } |
| 37 | }); |
| 38 | // The following catches errors in the user-defined `done` function and outputs them. |
| 39 | var tryCatchDone = function(fn, done, output) { |
| 40 | try { |
| 41 | fn(done, output); |
| 42 | } catch(e) { |
| 43 | grunt.fail.warn('There was an error while processing your done function: "' + e + '"'); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | // The following catches errors in the user-defined `error` function and passes them. |
| 48 | // if the error function options is not set, this value should be undefined |
| 49 | var tryCatchError = function(fn, done, err) { |
| 50 | try { |
| 51 | fn(done, err); |
| 52 | } catch(e) { |
| 53 | grunt.fail.fatal('There was an error while processing your error function: "' + e + '"'); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | requirejs.optimize( |
| 58 | options, |
| 59 | tryCatchDone.bind(null, options.done, done ), |
| 60 | options.error ? tryCatchError.bind(null, options.error, done ):undefined |
| 61 | ); |
| 62 | |
| 63 | }); |
| 64 | }; |