@@ -5,6 +5,8 @@ module.exports = function(grunt) {
55
66 var exec = require ( 'child_process' ) . exec ;
77 var banner = createBanner ( ) ;
8+ var distPath = 'dist/Autolinker.js' ;
9+ var minDistPath = 'dist/Autolinker.min.js' ;
810
911 // Project configuration.
1012 grunt . initConfig ( {
@@ -29,25 +31,19 @@ module.exports = function(grunt) {
2931 jasmine : {
3032 dist : {
3133 options : {
32- specs : 'tests/*Spec.js' ,
34+ specs : 'tests/*Spec.js'
3335 } ,
34- src : 'dist/Autolinker.min.js'
36+ src : minDistPath
3537 }
3638 } ,
3739
3840 concat : {
3941 development : {
4042 options : {
41- banner : banner + createDistFileHeader ( ) ,
42- footer : createDistFileFooter ( ) ,
43- nonull : true ,
44-
45- process : function ( src , filepath ) {
46- return '\t' + src . replace ( / \n / g, '\n\t' ) ; // indent each source file, which is placed inside the UMD block
47- }
43+ banner : banner ,
44+ nonull : true
4845 } ,
4946 src : [
50- 'src/umdBegin.js' ,
5147 'src/Autolinker.js' ,
5248 'src/Util.js' ,
5349 'src/HtmlParser.js' ,
@@ -57,19 +53,18 @@ module.exports = function(grunt) {
5753 'src/match/Email.js' ,
5854 'src/match/Twitter.js' ,
5955 'src/match/Url.js' ,
60- 'src/umdEnd.js'
6156 ] ,
62- dest : 'dist/Autolinker.js' ,
63- } ,
57+ dest : distPath
58+ }
6459 } ,
6560
6661 uglify : {
6762 production : {
6863 options : {
6964 banner : banner
7065 } ,
71- src : [ 'dist/Autolinker.js' ] ,
72- dest : 'dist/Autolinker.min.js' ,
66+ src : [ distPath ] ,
67+ dest : minDistPath
7368 }
7469 } ,
7570
@@ -88,6 +83,15 @@ module.exports = function(grunt) {
8883 'title' : 'Autolinker API Docs'
8984 }
9085 }
86+ } ,
87+
88+ umd : {
89+ main : {
90+ src : distPath ,
91+ globalAlias : 'Autolinker' , // Changes the name of the global variable
92+ objectToExport : 'Autolinker' ,
93+ indent : '\t'
94+ }
9195 }
9296 } ) ;
9397
@@ -98,16 +102,15 @@ module.exports = function(grunt) {
98102 grunt . loadNpmTasks ( 'grunt-contrib-uglify' ) ;
99103 grunt . loadNpmTasks ( 'grunt-contrib-jshint' ) ;
100104 grunt . loadNpmTasks ( 'grunt-jsduck' ) ;
105+ grunt . loadNpmTasks ( 'grunt-umd' ) ;
101106
102107 // Tasks
103108 grunt . registerTask ( 'default' , [ 'jshint' , 'build' , 'jasmine' ] ) ;
104- grunt . registerTask ( 'build' , [ 'concat:development' , 'uglify:production' ] ) ;
109+ grunt . registerTask ( 'build' , [ 'concat:development' , 'umd' , ' uglify:production' ] ) ;
105110 grunt . registerTask ( 'test' , [ 'build' , 'jasmine' ] ) ;
106111 grunt . registerTask ( 'doc' , "Builds the documentation." , [ 'jshint' , 'jsduck' ] ) ;
107112 grunt . registerTask ( 'serve' , [ 'connect:server:keepalive' ] ) ;
108113
109-
110-
111114 /**
112115 * Creates the banner comment with license header that is placed over the concatenated/minified files.
113116 *
@@ -127,49 +130,4 @@ module.exports = function(grunt) {
127130 ' */\n'
128131 ] . join ( "\n" ) ;
129132 }
130-
131-
132- /**
133- * Creates the UMD (Universal Module Definition) header, which defines Autolinker as one of the following when loaded:
134- *
135- * 1. An AMD module, if an AMD loader is available (such as RequireJS)
136- * 2. A CommonJS module, if a CommonJS module environment is available (such as Node.js), or
137- * 3. A global variable if the others are unavailable.
138- *
139- * This UMD header is combined with the UMD footer to create the distribution JavaScript file.
140- *
141- * @private
142- * @return {String }
143- */
144- function createDistFileHeader ( ) {
145- return [
146- "/*global define, module */" ,
147- "( function( root, factory ) {" ,
148- "\tif( typeof define === 'function' && define.amd ) {" ,
149- "\t\tdefine( factory ); // Define as AMD module if an AMD loader is present (ex: RequireJS)." ,
150- "\t} else if( typeof exports !== 'undefined' ) {" ,
151- "\t\tmodule.exports = factory(); // Define as CommonJS module for Node.js, if available." ,
152- "\t} else {" ,
153- "\t\troot.Autolinker = factory(); // Finally, define as a browser global if no module loader." ,
154- "\t}" ,
155- "}( this, function() {\n\n"
156- ] . join ( "\n" ) ;
157- }
158-
159-
160- /**
161- * Creates the UMD (Universal Module Definition) footer. See {@link #createDistFileHeader} for details.
162- *
163- * @private
164- * @return {String }
165- */
166- function createDistFileFooter ( ) {
167- var umdEnd = [
168- '\n\n\treturn Autolinker;\n' ,
169- '} ) );'
170- ] ;
171-
172- return umdEnd . join ( "\n" ) ;
173- }
174-
175133} ;
0 commit comments