-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
148 lines (127 loc) · 5.14 KB
/
Gruntfile.js
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
140
141
142
143
144
145
146
147
148
module.exports = function(grunt) {
var pkg = grunt.file.readJSON('package.json'); // grunt.config.data.pkg ...
var config = grunt.option('config') || 'local'; // Run as >grunt -config {config}
// Custom task for creating folders and moving the index file ...
grunt.registerTask('first', 'First task', function() {
grunt.file.mkdir('temp');
grunt.file.mkdir('dist');
// Move the index file into the dist directory ...
grunt.file.copy('src/index.html', 'dist/index.html');
// Move the favicon file into the dist directory ...
grunt.file.copy('src/favicon.ico', 'dist/favicon.ico');
});
// Custom task for moving config specific JSON data from 'config/' to '/js/config.json' ...
grunt.registerTask('injectConfig', 'inject configs into jsondata', function() {
// Simply copy the config file into the /temp/js ...
grunt.file.copy('configs/' + config + '.json', 'temp/js/data/config.json');
});
// Custom task for cleanups ....
grunt.registerTask('last', 'Last task', function() {
grunt.file.delete('temp');
});
// The majority of the tasks are defined here ...
grunt.initConfig({
pkg: pkg,
copy: {
build: {
files: [
{ expand: true, cwd: 'src/', src: ['js/**'], dest: 'temp'},
{ expand: true, cwd: 'src/', src: ['css/**'], dest: 'temp'},
{ expand: true, cwd: 'src/', src: ['fonts/**'], dest: 'dist'},
{ expand: true, cwd: 'src/', src: ['img/**'], dest: 'dist'}
]
}
},
concat: {
css: {
src: 'temp/css/*.css',
dest: 'temp/all.css'
}
},
requirejs: {
compile: {
options: {
baseUrl: './temp/js',
optimize: 'uglify',
mainConfigFile: 'temp/js/main.js',
name: 'libs/almond',
include: ['main'],
insertRequire: ['main'],
out: 'dist/all.min.js',
wrap: true
}
}
},
cssmin: {
css: {
src: "temp/all.css",
dest: "dist/all.min.css"
}
},
'string-replace': {
replaceJs: {
files: {
// TODO: I don't understand what the 'key' is for ...
'dist/all.min.js': 'dist/all.min.js'
},
options: {
replacements: [
{
pattern: /DOES THIS WORK\?/ig,
replacement: function (match, p1, offset, string) {
return 'YES THIS WORKS!';
}
}
]
}
},
replaceCssJsIncludes: {
files: {
// TODO: I don't understand what the 'key' is for ...
'dist/index.html': 'dist/index.html'
},
options: {
replacements: [
{
pattern: /<!--<REPLACE_CSS>-->([^]*)<!--<\/REPLACE_CSS>-->/ig,
replacement: function (match, p1, offset, string) {
return '<link rel="stylesheet" href="/all.min.css" />';
}
},
{
pattern: /<!--<REPLACE_JS>-->([^]*)<!--<\/REPLACE_JS>-->/ig,
replacement: function (match, p1, offset, string) {
return '<script src="/all.min.js"></script>';
}
}
]
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-css');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-string-replace');
grunt.loadNpmTasks('almond');
grunt.registerTask('default', [
// create the 'temp' and 'dist' directories, copy 'index.html' to 'dist/index.html'
'first',
// copy 'js' and 'css' directories to 'temp/js' and 'temp/css'
'copy',
// based on '-env' argument, copy 'env/:env.json' file to 'temp/js/env.json'
'injectConfig',
// wrap the entire 'js' codebase in almond.js, concat, minify, etc..., save as 'dist/all.min.js'
'requirejs',
// concat the 'temp/css' save as 'temp/all.css'
'concat',
// minify 'temp/all.css' save as 'dist/all.min.css'
'cssmin',
// replace js/css includes on 'dist/index.html' with the prepared 'dist/all.min.css' and 'dist/all.min.js' includes
'string-replace',
// delete 'temp' directory
'last'
]);
};