1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*global module:false*/
module.exports = function( grunt ) {
'use strict';
var pkg = grunt.file.readJSON( 'package.json' ),
config = {
src : 'src/',
bookmarkletJs : 'bookmarklet',
printliminatorJs : 'printliminator',
printliminatorFunctionName : 'csstricksPrintliminator',
// bookmarklet builder URLs
indexHtml : 'index.html',
bookmarkHtml : 'bookmark.html',
production : {
printliminator : '//css-tricks.github.io/The-Printliminator/printliminator.min.js'
},
dev : {
printliminator : 'src/printliminator.js'
},
// note to add to dynamically created index.html in the root folder
note : '<!--\nThis file is dynamically generated\n' +
'█████▄ ▄████▄ █████▄ ▄████▄ ██████ ███████▄ ▄████▄ █████▄ ██ ██████ ██ ██\n' +
'██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██\n' +
'██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██▀▀ ▀▀▀▀██\n' +
'█████▀ ▀████▀ ██ ██ ▀████▀ ██ ██ ██ ██ ▀████▀ █████▀ ██ ██ █████▀\n' +
'To make changes, modify the "src/index.html"\n-->'
};
grunt.file.defaultEncoding = 'utf8';
grunt.file.preserveBOM = false;
// Project configuration.
grunt.initConfig({
pkg: pkg,
config: config,
jshint: {
options: {
globals: {
'<%= config.printliminatorFunctionName %>': false
},
browser: true,
undef: true
},
files: {
src: [
'<%= config.src %><%= config.printliminatorJs %>.js', '!<%= config.src %>*.min.js'
]
}
},
uglify: {
options: {
report: 'gzip'
},
main: {
files : {
'<%= config.printliminatorJs %>.min.js' : [ '<%= config.src %><%= config.printliminatorJs %>.js' ]
}
},
mark: {
files : {
'<%= config.bookmarkletJs %>.min.js' : [ '<%= config.src %><%= config.bookmarkletJs %>.js' ]
}
}
},
clean: {
build: {
src: [
config.indexHtml,
config.src + '*.min.js',
'*.min.js'
]
},
cleanup : {
src: [ config.bookmarkletJs + '.min.js' ]
}
}
});
grunt.loadNpmTasks( 'grunt-contrib-clean' );
grunt.loadNpmTasks( 'grunt-contrib-jshint' );
grunt.loadNpmTasks( 'grunt-contrib-uglify' );
grunt.registerTask( 'writeBookmarklet', function(){
// Add bookmarklet code for both production & development
// load bookmarket min file
var content = grunt.file.read( config.bookmarkletJs + '.min.js' ),
// load index.html template
baseHtml = grunt.file.read( config.src + config.indexHtml ),
bookmarkHtml = grunt.file.read( config.src + config.bookmarkHtml ),
modFile = function( mode ) {
var regex = new RegExp('\\{' + mode + '\\}'),
file = content
.replace( /\{printliminator\}/, config[ mode ].printliminator )
.replace( /\"/g, "'" )
// not using encodeURI because it changes "{}" into "%7B%7D"
// and just makes the bookmarklet bigger & harder to read
.replace( /\x20/g, '%20' );
// add javascript to HTML
baseHtml = baseHtml.replace( regex, file );
if ( mode === 'production' ) {
bookmarkHtml = bookmarkHtml.replace( regex, file );
}
};
// update production & dev bookmarklet href
modFile( 'production' );
modFile( 'dev' );
// add note so we don't mistakingly update the wrong index.html
// then lose all our changes when grunt is run!
baseHtml = baseHtml.replace( '<!-- src -->', config.note );
// write modified index.html
grunt.file.write( config.indexHtml, baseHtml );
grunt.file.write( config.bookmarkHtml, bookmarkHtml );
});
grunt.registerTask( 'default', 'Default build', function() {
grunt.task.run([
'clean:build',
'jshint',
'uglify',
'writeBookmarklet',
'clean:cleanup'
]);
});
};
|