Skip to content

Commit bc37dae

Browse files
committed
Move UMD wrapper to Gruntfile.
Should only be added when building the concatenated/minified files.
1 parent df72d8c commit bc37dae

File tree

4 files changed

+681
-628
lines changed

4 files changed

+681
-628
lines changed

.jshintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"undef": true,
3+
"smarttabs": true
4+
}

Gruntfile.js

Lines changed: 137 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,141 @@
11
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+
} );
359

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' );
1566

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+
72141
};

dist/Autolinker.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
* https://github.com/gregjacobs/Autolinker.js
99
*/
1010
/*global define, module */
11-
/*jshint undef:true, smarttabs:true */
12-
// Set up Autolinker appropriately for the environment.
1311
( function( root, factory ) {
12+
1413
if( typeof define === 'function' && define.amd ) {
1514
define( factory ); // Define as AMD module if an AMD loader is present (ex: RequireJS).
1615
} else if( typeof exports !== 'undefined' ) {
@@ -19,7 +18,7 @@
1918
root.Autolinker = factory(); // Finally, define as a browser global if no module loader.
2019
}
2120
}( this, function() {
22-
21+
2322
/**
2423
* @class Autolinker
2524
* @extends Object
@@ -260,7 +259,7 @@
260259
'>'
261260
].join( "" ), 'g' );
262261
} )(),
263-
262+
264263
/**
265264
* @private
266265
* @property {RegExp} urlPrefixRegex
@@ -413,7 +412,7 @@
413412
var anchorHref = matchStr, // initialize both of these
414413
anchorText = matchStr, // values as the full match
415414
linkType;
416-
415+
417416
// Process the urls that are found. We need to change URLs like "www.yahoo.com" to "http://www.yahoo.com" (or the browser
418417
// will try to direct the user to "http://current-domain.com/www.yahoo.com"), and we need to prefix 'mailto:' to email addresses.
419418
if( twitterMatch ) {
@@ -439,13 +438,13 @@
439438
prefixStr = charBeforeMatch + prefixStr; // re-add the character before the '//' to what will be placed before the <a> tag
440439
anchorHref = anchorHref.replace( protocolRelRegex, "//" ); // remove the char before the match for the href
441440
anchorText = anchorText.replace( protocolRelRegex, "" ); // remove both the char before the match and the '//' for the anchor text
442-
441+
443442
} else if( !/^[A-Za-z]{3,9}:/i.test( anchorHref ) ) {
444443
// url string doesn't begin with a protocol, assume http://
445444
anchorHref = 'http://' + anchorHref;
446445
}
447446
}
448-
447+
449448
// wrap the match in an anchor tag
450449
var anchorTag = me.createAnchorTag( linkType, anchorHref, anchorText );
451450
return prefixStr + anchorTag + suffixStr;
@@ -605,7 +604,6 @@
605604
return autolinker.link( text );
606605
};
607606

608-
609607
return Autolinker;
610-
611-
} ) );
608+
609+
} ) );

0 commit comments

Comments
 (0)