forked from iamkeir/iscroll-ie8patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·143 lines (116 loc) · 3.78 KB
/
build.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
#!/usr/bin/env node
var pkg = require('./package.json');
var fs = require('fs');
var hint = require("jshint").JSHINT;
var uglify = require('uglify-js');
var banner = '/*! iScroll v' + pkg.version + ' ~ (c) 2008-' + (new Date().getFullYear()) + ' Matteo Spinelli ~ http://cubiq.org/license */\n';
var releases = {
// Main releases
lite: {
files: ['default/_animate.js', 'default/handleEvent.js']
},
iscroll: {
files: [
'indicator/_initIndicators.js',
'wheel/wheel.js',
'snap/snap.js',
'keys/keys.js',
'default/_animate.js',
'default/handleEvent.js',
'indicator/indicator.js'
],
postProcessing: [ 'indicator/build.json', 'wheel/build.json', 'snap/build.json', 'keys/build.json' ]
},
probe: {
files: [
'indicator/_initIndicators.js',
'wheel/wheel.js',
'snap/snap.js',
'keys/keys.js',
'probe/_animate.js',
'default/handleEvent.js',
'indicator/indicator.js'
],
postProcessing: [ 'indicator/build.json', 'wheel/build.json', 'snap/build.json', 'keys/build.json', 'probe/build.json' ]
},
zoom: {
files: [
'indicator/_initIndicators.js',
'zoom/zoom.js',
'wheel/wheel.js',
'snap/snap.js',
'keys/keys.js',
'default/_animate.js',
'zoom/handleEvent.js',
'indicator/indicator.js'
],
postProcessing: [ 'zoom/build.json', 'indicator/build.json', 'wheel/build.json', 'snap/build.json', 'keys/build.json' ]
}
// Additional releases TBD
};
var args = process.argv.slice(2);
if ( !args.length ) {
args = ['iscroll'];
}
if ( args[0] == 'dist' ) {
args = ['lite', 'iscroll', 'zoom', 'probe'];
}
// Get the list of files
args.forEach(function (release) {
if ( !(release in releases) ) {
console.log('Unrecognized release: ' + release);
return;
}
console.log('Building release: ' + release);
build(release);
});
function build (release) {
var out = '';
var value = '';
var fileList = ['open.js', 'utils.js', 'core.js'];
fileList = fileList.concat(releases[release].files);
fileList.push('close.js');
// Concatenate files
out = banner + fileList.map(function (filePath) {
return fs.readFileSync('src/' + filePath, 'utf-8');
}).join('');
// Update version
out = out.replace('/* VERSION */', pkg.version);
// Post processing
if ( releases[release].postProcessing ) {
releases[release].postProcessing.forEach(function (file) {
var postProcessing = require('./src/' + file);
// Insert point
for ( var i in postProcessing.insert ) {
value = postProcessing.insert[i].substr(postProcessing.insert[i].length-3) == '.js' ?
fs.readFileSync('src/' + postProcessing.insert[i]) :
postProcessing.insert[i];
out = out.replace('// INSERT POINT: ' + i, value + '\n\n// INSERT POINT: ' + i );
}
// Replace
for ( i in postProcessing.replace ) {
value = postProcessing.replace[i].substr(postProcessing.replace[i].length-3) == '.js' ?
fs.readFileSync('src/' + postProcessing.replace[i]) :
postProcessing.replace[i];
var regex = new RegExp('\\/\\* REPLACE START: ' + i + ' \\*\\/[\\s\\S]*\\/\\* REPLACE END: ' + i + ' \\*\\/');
out = out.replace(regex, '/* REPLACE START: ' + i + ' */' + value + '/* REPLACE END: ' + i + ' */');
}
});
}
// Write build file
var buildFile = './build/iscroll' + (release != 'iscroll' ? '-' + release : '') + '.js';
fs.writeFileSync(buildFile, out);
// JSHint
if ( !hint(out) ) {
var lines = out.split('\n');
hint.errors.forEach(function (err) {
console.log('\033[31m[' + err.code + ']\033[0m ' + err.line + ':' + err.character + '\t- ' + err.reason);
console.log('\033[33m' + lines[err.line-1].replace(/\t/g, ' ') + '\033[0m\n');
});
process.exit();
}
// Write dist file
var distFile = buildFile.replace('/build/', '/dist/').replace('.js', '-min.js');
out = uglify.minify(out, { fromString: true });
fs.writeFileSync(distFile, banner + out.code);
}