forked from Urbiwanus/ionic-app-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGruntfile.js
More file actions
198 lines (179 loc) · 5.91 KB
/
Gruntfile.js
File metadata and controls
198 lines (179 loc) · 5.91 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
module.exports = function (grunt) {
// Project configuration.
grunt.initConfig({
ngAnnotate: {
options: {
singleQuotes: true
},
app: {
files: [
{
expand: true,
src: ['js/*.js']
}
]
}
},
useminPrepare: {
html: 'index.html',
options: {
dest: '../www',
staging: '../tmp'
}
},
usemin: {
html: '../www/index.html',
options: {
dest: '../www'
}
},
filerev: {
options: {
algorithm: 'md5',
length: 8
}
},
copy: {
prod: {
files: [
{
expand: false,
src: ['index.html'],
dest: '../www/'
},
{
expand: true,
src: ['templates/*.html'],
dest: '../www/'
},
{
expand: false,
src: ['lib/cordova-app-loader/dist/bootstrap.js'],
dest: '../www/'
},
{
expand: false,
src: ['lib/cordova-app-loader/dist/cordova-app-loader-complete.js'],
dest: '../www/'
}
]
},
manifest: {
files: [
{
expand: false,
src: ['manifest.json'],
dest: '../www/'
}
]
}
},
jsonmanifest: {
generate: {
options: {
basePath: '../www',
exclude: [],
//load all found assets
loadall: true,
//manually add files to the manifest
files: {},
//manually define the files that should be injected into the page
load: [],
// root location of files to be loaded in the load array.
root: "./"
},
src: [
'app/*.js',
'lib/*.js',
'css/*.css',
'templates/*.html'
],
dest: ['manifest.json']
}
}
});
grunt.registerTask('build', [
'ngAnnotate',
'copy:prod',
'useminPrepare',
'concat:generated',
'cssmin:generated',
'uglify:generated',
'filerev',
'usemin',
'jsonmanifest',
'copy:manifest'
]);
grunt.loadNpmTasks('grunt-usemin');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-filerev');
grunt.loadNpmTasks('grunt-ng-annotate');
grunt.file.setBase('app');
//GRUNT TASK TO BUILD A JSON MANIFEST FILE FOR HOT CODE UPDATES
grunt.registerMultiTask('jsonmanifest', 'Generate JSON Manifest for Hot Updates', function () {
var options = this.options({loadall: true, root: "./", files: {}, load: []});
var done = this.async();
var path = require('path');
this.files.forEach(function (file) {
var files;
//manifest format
var json = {
"files": options.files,
"load": options.load,
"root": options.root
};
//clear load array if loading all found assets
if (options.loadall) {
json.load = [];
}
// check to see if src has been set
if (typeof file.src === "undefined") {
grunt.fatal('Need to specify which files to include in the json manifest.', 2);
}
// if a basePath is set, expand using the original file pattern
if (options.basePath) {
files = grunt.file.expand({cwd: options.basePath}, file.orig.src);
} else {
files = file.src;
}
// Exclude files
if (options.exclude) {
files = files.filter(function (item) {
return options.exclude.indexOf(item) === -1;
});
}
// Set default destination file
if (!file.dest) {
file.dest = ['manifest.json'];
}
// add files
if (files) {
files.forEach(function (item) {
var hasher = require('crypto').createHash('sha256');
var filename = encodeURI(item);
var key = filename.split("/");
key = key[key.length - 1];
json.files[key] = {}
json.files[key]['filename'] = filename;
json.files[key]['version'] = hasher.update(grunt.file.read(path.join(options.basePath, item))).digest("hex")
if (options.loadall) {
json.load.push(filename);
}
});
}
//write out the JSON to the manifest files
file.dest.forEach(function (f) {
grunt.file.write(f, JSON.stringify(json, null, 2));
});
done();
});
})
};