|
1 | 1 | module.exports = function(grunt) { |
2 | | - 'use strict'; |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + var banner = createBanner(); |
| 5 | + |
| 6 | + // Project configuration. |
| 7 | + grunt.initConfig( { |
| 8 | + pkg: grunt.file.readJSON( 'package.json' ), |
| 9 | + |
| 10 | + connect: { |
| 11 | + server: { |
| 12 | + options: { |
| 13 | + hostname: '*', |
| 14 | + port: 3000, |
| 15 | + base: '.' |
| 16 | + } |
| 17 | + } |
| 18 | + }, |
| 19 | + |
| 20 | + jshint: { |
| 21 | + files: { |
| 22 | + src: [ 'src/**/*.js', 'tests/**/*.js' ] |
| 23 | + } |
| 24 | + }, |
| 25 | + |
| 26 | + jasmine: { |
| 27 | + src: 'dist/Autolinker.min.js', |
| 28 | + options: { |
| 29 | + specs: 'tests/*Spec.js', |
| 30 | + } |
| 31 | + }, |
| 32 | + |
| 33 | + concat: { |
| 34 | + development: { |
| 35 | + options: { |
| 36 | + banner : banner + createDistFileHeader(), |
| 37 | + footer : createDistFileFooter(), |
| 38 | + nonull : true, |
| 39 | + |
| 40 | + process : function( src, filepath ) { |
| 41 | + return '\t' + src.replace( /\n/g, '\n\t' ); // indent each source file, which is placed inside the UMD block |
| 42 | + } |
| 43 | + }, |
| 44 | + src: [ 'src/umdBegin.js', 'src/Autolinker.js', 'src/umdEnd.js' ], |
| 45 | + dest: 'dist/Autolinker.js', |
| 46 | + }, |
| 47 | + }, |
| 48 | + |
| 49 | + uglify: { |
| 50 | + production: { |
| 51 | + options: { |
| 52 | + banner: banner |
| 53 | + }, |
| 54 | + src: [ 'dist/Autolinker.js' ], |
| 55 | + dest: 'dist/Autolinker.min.js', |
| 56 | + } |
| 57 | + } |
| 58 | + } ); |
3 | 59 |
|
4 | | - var banner = [ |
5 | | - '/*!', |
6 | | - ' * Autolinker.js', |
7 | | - ' * <%= pkg.version %>', |
8 | | - ' *', |
9 | | - ' * Copyright(c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>', |
10 | | - ' * <%= pkg.license %>', |
11 | | - ' *', |
12 | | - ' * <%= pkg.homepage %>', |
13 | | - ' */\n' |
14 | | - ].join('\n'); |
| 60 | + // Plugins |
| 61 | + grunt.loadNpmTasks( 'grunt-contrib-connect' ); |
| 62 | + grunt.loadNpmTasks( 'grunt-contrib-jasmine' ); |
| 63 | + grunt.loadNpmTasks( 'grunt-contrib-concat' ); |
| 64 | + grunt.loadNpmTasks( 'grunt-contrib-uglify' ); |
| 65 | + grunt.loadNpmTasks( 'grunt-contrib-jshint' ); |
15 | 66 |
|
16 | | - // Project configuration. |
17 | | - grunt.initConfig({ |
18 | | - pkg: grunt.file.readJSON('package.json'), |
19 | | - connect: { |
20 | | - server: { |
21 | | - options: { |
22 | | - hostname: '*', |
23 | | - port: 3000, |
24 | | - base: '.' |
25 | | - } |
26 | | - } |
27 | | - }, |
28 | | - jasmine: { |
29 | | - src: 'src/Autolinker.js', |
30 | | - options: { |
31 | | - specs: 'tests/*Spec.js', |
32 | | - } |
33 | | - }, |
34 | | - concat: { |
35 | | - development: { |
36 | | - options: { |
37 | | - banner: banner |
38 | | - }, |
39 | | - src: ['src/Autolinker.js'], |
40 | | - dest: 'dist/Autolinker.js', |
41 | | - }, |
42 | | - }, |
43 | | - uglify: { |
44 | | - production: { |
45 | | - options: { |
46 | | - banner: banner |
47 | | - }, |
48 | | - src: ['src/Autolinker.js'], |
49 | | - dest: 'dist/Autolinker.min.js', |
50 | | - } |
51 | | - }, |
52 | | - jshint: { |
53 | | - files: { |
54 | | - src: ['src/**/*.js', 'tests/**/*.js'] |
55 | | - } |
56 | | - } |
57 | | - }); |
58 | | - |
59 | | - // Plugins |
60 | | - grunt.loadNpmTasks('grunt-contrib-connect'); |
61 | | - grunt.loadNpmTasks('grunt-contrib-jasmine'); |
62 | | - grunt.loadNpmTasks('grunt-contrib-concat'); |
63 | | - grunt.loadNpmTasks('grunt-contrib-uglify'); |
64 | | - grunt.loadNpmTasks('grunt-contrib-jshint'); |
65 | | - |
66 | | - // Tasks |
67 | | - grunt.registerTask('default', ['lint', 'test', 'build']); |
68 | | - grunt.registerTask('lint', ['jshint']); |
69 | | - grunt.registerTask('test', ['jasmine']); |
70 | | - grunt.registerTask('build', ['concat:development', 'uglify:production']); |
71 | | - grunt.registerTask('serve', ['connect:server:keepalive']); |
| 67 | + // Tasks |
| 68 | + grunt.registerTask( 'default', [ 'lint', 'build', 'doTest' ] ); |
| 69 | + grunt.registerTask( 'lint', [ 'jshint' ] ); |
| 70 | + grunt.registerTask( 'test', [ 'build', 'doTest' ] ); |
| 71 | + grunt.registerTask( 'doTest', [ 'jasmine' ] ); |
| 72 | + grunt.registerTask( 'build', [ 'concat:development', 'uglify:production' ] ); |
| 73 | + grunt.registerTask( 'serve', [ 'connect:server:keepalive' ] ); |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates the banner comment with license header that is placed over the concatenated/minified files. |
| 79 | + * |
| 80 | + * @private |
| 81 | + * @return {String} |
| 82 | + */ |
| 83 | + function createBanner() { |
| 84 | + return [ |
| 85 | + '/*!', |
| 86 | + ' * Autolinker.js', |
| 87 | + ' * <%= pkg.version %>', |
| 88 | + ' *', |
| 89 | + ' * Copyright(c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>', |
| 90 | + ' * <%= pkg.license %>', |
| 91 | + ' *', |
| 92 | + ' * <%= pkg.homepage %>', |
| 93 | + ' */\n' |
| 94 | + ].join( "\n" ); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates the UMD (Universal Module Definition) header, which defines Autolinker as one of the following when loaded: |
| 100 | + * |
| 101 | + * 1. An AMD module, if an AMD loader is available (such as RequireJS) |
| 102 | + * 2. A CommonJS module, if a CommonJS module environment is available (such as Node.js), or |
| 103 | + * 3. A global variable if the others are unavailable. |
| 104 | + * |
| 105 | + * This UMD header is combined with the UMD footer to create the distribution JavaScript file. |
| 106 | + * |
| 107 | + * @private |
| 108 | + * @return {String} |
| 109 | + */ |
| 110 | + function createDistFileHeader() { |
| 111 | + return [ |
| 112 | + "/*global define, module */", |
| 113 | + "( function( root, factory ) {\n", |
| 114 | + "\tif( typeof define === 'function' && define.amd ) {", |
| 115 | + "\t\tdefine( factory ); // Define as AMD module if an AMD loader is present (ex: RequireJS).", |
| 116 | + "\t} else if( typeof exports !== 'undefined' ) {", |
| 117 | + "\t\tmodule.exports = factory(); // Define as CommonJS module for Node.js, if available.", |
| 118 | + "\t} else {", |
| 119 | + "\t\troot.Autolinker = factory(); // Finally, define as a browser global if no module loader.", |
| 120 | + "\t}", |
| 121 | + "}( this, function() {\n\n" |
| 122 | + ].join( "\n" ); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + /** |
| 127 | + * Creates the UMD (Universal Module Definition) footer. See {@link #createDistFileHeader} for details. |
| 128 | + * |
| 129 | + * @private |
| 130 | + * @return {String} |
| 131 | + */ |
| 132 | + function createDistFileFooter() { |
| 133 | + var umdEnd = [ |
| 134 | + '\n\n\treturn Autolinker;\n', |
| 135 | + '} ) );' |
| 136 | + ]; |
| 137 | + |
| 138 | + return umdEnd.join( "\n" ); |
| 139 | + } |
| 140 | + |
72 | 141 | }; |
0 commit comments