This repository was archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathbuild.js
More file actions
238 lines (202 loc) · 7.03 KB
/
build.js
File metadata and controls
238 lines (202 loc) · 7.03 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var path = require('path'),
colors = require('colors'),
Q = require('q'),
webpack = require('webpack'),
logging = require('../logging'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
watch = require('gulp-watch'),
cache = require('gulp-cached');
vfs = require('vinyl-fs'),
Utils = require('../utils');
var Build = module.exports;
//TODO defaults should be provided when config is loaded initially, but for now
// this requires the least amount of changes to the rest of the CLI
var defaultPaths = {
html : {
src: ['app/**/*.html'],
dest: "www/build"
},
sass: {
src: ['app/app.+(ios|md).scss'],
dest: 'www/build/css',
include: ['node_modules/ionic-framework']
},
fonts: {
src: ['node_modules/ionic-framework/fonts/**/*.ttf'],
dest: "www/build/fonts"
},
watch: {
sass: ['app/**/*.scss'],
html: ['app/**/*.html'],
livereload: [
'www/build/**/*.html',
'www/build/**/*.js',
'www/build/**/*.css'
]
}
};
var defaultAutoPrefixerOptions = {
browsers: [
'last 2 versions',
'iOS >= 7',
'Android >= 4',
'Explorer >= 10',
'ExplorerMobile >= 11'
],
cascade: false
};
function isDefined(val) {return typeof val !== 'undefined';}
Build.bundle = function(options) {
var q = Q.defer();
//optional callback to be trigged on builds
options.callback = options.callback || function() { };
var firstTime = true;
// load webpack config
var config,
webPackConfigPath = path.join(options.appDirectory, 'webpack.config.js');
try {
logging.logger.info('\n∆ Compiling and bundling with Webpack...'.yellow.bold);
config = require(webPackConfigPath);
logging.logger.info('√ Using your webpack.config.js file'.green.bold);
} catch (ex) {
//TODO generate one?
logging.logger.error('There was an error loading webpack.config.js:'.red.bold);
Utils.fail(ex);
}
var compiler = webpack(config);
if (options.watch) {
//TODO expose watch options
var watchOptions = {};
if (options.watchPoll) {
watchOptions.poll = true;
}
compiler.watch(watchOptions, compileHandler);
} else {
compiler.run(compileHandler);
}
function compileHandler(err, stats){
if (err) {
return q.reject(err);
}
var jsonStats = stats.toJson();
if (firstTime) {
firstTime = false;
options.callback();
nonTypeErrors = jsonStats.errors.filter(function(err){
// lame heuristic for type errors emitted by awesome-typescript-loader
return err.charAt(0) !== '[';
})
if (nonTypeErrors.length > 0) {
// bail if error on initial build
// return q.reject(jsonStats.errors.toString());
//hard fail until error reporting is updated
Utils.fail(jsonStats.errors.toString());
}
printStats(stats, jsonStats);
} else {
printStats(stats, jsonStats);
var Serve = require('./serve');
// console.log();
// Serve.printCommandTips();
// process.stdout.write('ionic $'); //fake prompt
var bundleFile = path.resolve(path.join(compiler.options.output.path,
compiler.options.output.filename));
Serve._postToLiveReload(bundleFile, options);
}
logging.logger.info('\n√ Webpack complete\n'.green.bold);
q.resolve();
}
return q.promise;
};
Build.watch = function(options, cb) {
Build.sass(options);
Build.fonts(options);
Build.html(options);
console.log();
var configPaths = options.config.paths;
var sassWatchPaths;
if (configPaths && configPaths.watch && configPaths.watch.sass) {
sassWatchPaths = configPaths.watch.sass;
} else {
// logging.logger.info(('√ No sass watch patterns found, using ' + defaultPaths.watch.sass).green.bold);
sassWatchPaths = defaultPaths.watch.sass;
}
var htmlWatchPaths;
if (configPaths && configPaths.watch && configPaths.watch.html) {
htmlWatchPaths = configPaths.watch.html;
} else {
// logging.logger.info(('√ No html watch patterns found, using ' + defaultPaths.watch.html).green.bold);
htmlWatchPaths = defaultPaths.watch.html;
}
watch(sassWatchPaths.concat(htmlWatchPaths), function(file){
if (/.html$/.test(file.path)) {
Build.html(options);
}
else if (/.scss/.test(file.path)) {
Build.sass(options);
}
})
}
Build.sass = function(options) {
var configPaths = options.config.paths || defaultPaths;
var sassPaths = configPaths.sass || defaultPaths.sass;
var sassSrcPaths = isDefined(sassPaths.src) ? sassPaths.src : defaultPaths.sass.src;
var sassIncludePaths = isDefined(sassPaths.include) ? sassPaths.include : defaultPaths.sass.include;
var sassDestPath = isDefined(sassPaths.dest) ? sassPaths.dest : defaultPaths.sass.dest;
var autoPrefixerOptions = options.config.autoPrefixerOptions || defaultAutoPrefixerOptions;
logging.logger.info('\n∆ Compiling Sass to CSS'.yellow.bold);
logging.logger.info(('√ Matching patterns: ' + sassSrcPaths).green.bold);
vfs.src(sassSrcPaths)
.pipe(sass({ includePaths: sassIncludePaths }))
.on('error', function(err){
console.error(err.message);
this.emit('end');
})
.pipe(autoprefixer(autoPrefixerOptions))
.pipe(vfs.dest(path.resolve(sassDestPath)))
.on('end', function(){
logging.logger.info('√ Sass compilation complete'.green.bold);
//cb()
});
}
Build.fonts = function(options) {
var fontPaths = (options.config.paths && options.config.paths.fonts) || defaultPaths.fonts;
var fontSrcPaths = isDefined(fontPaths.src) ? fontPaths.src : defaultPaths.fonts.src;
var fontDestPath = isDefined(fontPaths.dest) ? fontPaths.dest : defaultPaths.fonts.dest;
logging.logger.info(('\n∆ Copying fonts').yellow.bold);
logging.logger.info(('√ Matching patterns: ' + fontSrcPaths).green.bold);
vfs.src(fontSrcPaths)
.pipe(vfs.dest(fontDestPath))
.on('end', function(){
logging.logger.info(('√ Fonts copied to ' + fontDestPath).green.bold);
});
}
Build.html = function(options) {
var htmlPaths = (options.config.paths && options.config.paths.html) || defaultPaths.html;
var htmlSrcPaths = isDefined(htmlPaths.src) ? htmlPaths.src : defaultPaths.html.src;
var htmlDestPath = isDefined(htmlPaths.dest) ? htmlPaths.dest : defaultPaths.html.dest;
logging.logger.info('\n∆ Copying HTML'.yellow.bold);
logging.logger.info(('√ Matching patterns: ' + htmlSrcPaths).green.bold);
vfs.src(htmlSrcPaths)
.pipe(cache('html'))
.pipe(vfs.dest(htmlDestPath))
.on('end', function(){
logging.logger.info(('√ HTML copied to ' + htmlDestPath).green.bold);
});
}
function printStats(stats, jsonStats) {
if (jsonStats.warnings.length > 0) {
logging.logger.debug('There are some warnings');
logging.logger.debug(jsonStats.warnings);
}
// TODO expose this
// https://github.com/webpack/docs/wiki/node.js-api#statstojsonoptions
var statsOptions = {
'colors': true,
'modules': true,
'chunks': false,
'exclude': ['node_modules']
}
logging.logger.info('\n' + stats.toString(statsOptions));
}