-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
275 lines (245 loc) · 7.7 KB
/
gulpfile.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var gulp = require('gulp');
var gutil = require("gulp-util");
var shell = require('gulp-shell')
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var runSequence = require('run-sequence');
var path = require("path");
var preprocess = require('gulp-preprocess');
var packageJson = require('./package.json');
var clean = require('gulp-clean');
var myip = require('quick-local-ip');
const WEBPACK_NETWORK_IP = myip.getLocalIP4();
const WEBPACK_SERVER_HOST = 'http://' + WEBPACK_NETWORK_IP;
const WEBPACK_SERVER_PORT = 3000;
const PHONEGAP_SERVER_PORT = 3001;
const STATIC_PATH = 'static';
const BUNDLE_FILE = 'bundle.js';
const APP_NAME = packageJson.name;
const APP_ID = packageJson.id;
const APP_VERSION = packageJson.version;
var webpackOptionsLoader = {
test: /.jsx?$/,
loaders: ['babel?presets[]=react,presets[]=es2015,presets[]=stage-0'],
include: path.join(__dirname, 'src'),
exclude: /node_modules/
};
var webpackOptions = {
module: {
loaders: [
webpackOptionsLoader,
{
test: /\.scss$/,
loaders: ['style', 'css', 'sass']
},
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=100000'
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'eslint-loader'
}
]
},
entry: [
'./src/index.jsx'
],
output: {
path: path.join(__dirname, './release/www/' + STATIC_PATH + '/'),
filename: BUNDLE_FILE
},
sassLoader: {
includePaths: [
path.resolve(__dirname, './node_modules/bootstrap-sass/assets/stylesheets/'),
path.resolve(__dirname, './node_modules/compass-mixins/lib/')
]
},
eslint: {
},
debug: true,
progress: false,
emitError: true,
emitWarning: true,
failOnError: true,
stats: {
colors: true,
reasons: true
},
devtool: 'source-map'
};
/**
* remove release directory, which allow to install new cordova project
*/
gulp.task('clear-release', function () {
return gulp.src('release', {read: false})
.pipe(clean());
});
/**
* copy non bundled files from src to dist directory
*/
gulp.task('copy-layout', function() {
return gulp.src(['./src/index.html'])
.pipe(preprocess({
context: {
BUNDLE_PATH: './' + STATIC_PATH + '/' + BUNDLE_FILE,
APP_NAME: APP_NAME
}
}))
.pipe(gulp.dest('./release/www'))
});
/**
* copy non bundled files from src to dist directory with path to hot loader server
*/
gulp.task('copy-layout-hot', function() {
return gulp.src(['./src/index.html'])
.pipe(preprocess({
context: {
BUNDLE_PATH: WEBPACK_SERVER_HOST + ':' + WEBPACK_SERVER_PORT +'/' + STATIC_PATH + '/' + BUNDLE_FILE,
APP_NAME: APP_NAME
}
}))
.pipe(gulp.dest('./release/www'))
});
/**
* Compile react jsx ES6 & ES7 to ES5 js
*/
gulp.task('compile-react', function(done) {
webpack(webpackOptions, function(err, stats) {
if(err) console.log(err);
gutil.log("[webpack]", stats.toString({}));
done();
});
});
/**
* Compile react jsx ES6 & ES7 to ES5 js and run webpack hot loader server
*/
gulp.task('compile-react-hot', function(done) {
webpackOptions.entry = [
'webpack-dev-server/client?' + WEBPACK_SERVER_HOST + ':' + WEBPACK_SERVER_PORT,
'webpack/hot/only-dev-server'
].concat(webpackOptions.entry);
webpackOptions.plugins = [
new webpack.HotModuleReplacementPlugin({})
];
webpackOptionsLoader.loaders.unshift('react-hot');
webpackOptions.output.publicPath = WEBPACK_SERVER_HOST + ':' + WEBPACK_SERVER_PORT + '/' + STATIC_PATH + '/';
new WebpackDevServer(webpack(webpackOptions), {
hot: true,
publicPath: '/' + STATIC_PATH + '/'
}).listen(WEBPACK_SERVER_PORT, WEBPACK_NETWORK_IP, function(err) {
if(err) console.log(err);
done();
console.log('webpack dev server listening at ' + WEBPACK_SERVER_HOST + ':' + WEBPACK_SERVER_PORT);
});
});
/**
* Create defauld cordova project in release directory.
* It could be run once
*/
gulp.task('create-cordova', ['clear-release'], shell.task('./node_modules/.bin/cordova create release ' + APP_ID + ' ' + APP_NAME));
/**
* Add ios platform to created cordova project
* It could be run once
*/
gulp.task('platform-ios', shell.task('cd release && ../node_modules/.bin/cordova platform add ios'));
/**
* Add android platform to created cordova project
* It could be run once
*/
gulp.task('platform-android', shell.task('cd release && ../node_modules/.bin/cordova platform add android'));
/**
* Add browser platform to created cordova project
* It could be run once
*/
gulp.task('platform-browser', shell.task('cd release && ../node_modules/.bin/cordova platform add browser'));
/**
* Clear previous html code from release/www
*/
gulp.task('clear-cordova-www', function () {
return gulp.src('release/www', {read: false})
.pipe(clean());
});
/**
* Build mobile apps for all installed platforms
* Run after any changes
*/
gulp.task('build-cordova', shell.task('cd release && ../node_modules/.bin/cordova build'));
/**
* Build mobile apps for ios platform
* Run after any changes instead `build-cordova`
*/
gulp.task('build-ios', shell.task('cd release && ../node_modules/.bin/cordova build ios'));
/**
* Build mobile apps for android platform
* Run after any changes instead `build-cordova`
*/
gulp.task('build-android', shell.task('cd release && ../node_modules/.bin/cordova build android'));
/**
* Build mobile apps for browser platform
* Run after any changes instead `build-cordova`
*/
gulp.task('build-browser', shell.task('cd release && ../node_modules/.bin/cordova build browser'));
/**
* Run ios emulation - this also build app
*/
gulp.task('emulate-ios', shell.task('cd release && ../node_modules/.bin/cordova emulate ios'));
/**
* Run android emulation - this also build app
*/
gulp.task('emulate-android', shell.task('cd release && ../node_modules/.bin/cordova emulate android'));
/**
* Run ripple emulation
*/
gulp.task('emulate-ripple', shell.task('cd release && ../node_modules/.bin/ripple emulate'));
/**
* run phonegap server
*/
gulp.task('phonegap-serve', shell.task('cd release && ../node_modules/.bin/phonegap serve --no-autoreload -p ' + PHONEGAP_SERVER_PORT));
/**
* Run app in browser - this also build app
*/
gulp.task('run-browser', shell.task('cd release && ../node_modules/.bin/cordova run browser'));
/**
* Higher level task, should be run once for create cordova project (it could be run more times but it is time consuming)
*/
gulp.task('init-cordova', function(done) {
runSequence('create-cordova', 'platform-ios', 'platform-android', 'platform-browser', done);
});
/**
* Fill cordova project with proper html, js, css
*/
gulp.task('prepare-build', function(done) {
runSequence('clear-cordova-www', 'copy-layout', 'compile-react', done);
});
/**
* Emulate ios app with hot loader
*/
gulp.task('prebuild-ios-hot', function(done) {
runSequence('clear-cordova-www', 'copy-layout-hot', 'compile-react-hot', 'emulate-ios', done);
});
/**
* Emulate android app with hot loader
*/
gulp.task('prebuild-android-hot', function(done) {
runSequence('clear-cordova-www', 'copy-layout-hot', 'compile-react-hot', 'emulate-android', done);
});
/**
* Emulate browser app with hot loader
*/
gulp.task('prebuild-browser-hot', function(done) {
runSequence('clear-cordova-www', 'copy-layout-hot', 'compile-react-hot', 'run-browser', done);
});
/**
* Emulate app by ripple and hot loader
*/
gulp.task('prebuild-ripple-hot', function(done) {
runSequence('clear-cordova-www', 'copy-layout-hot', 'compile-react-hot', 'emulate-ripple', done);
});
/**
* Run test by 'The PhoneGap Developer App'
*/
gulp.task('prebuild-phonegap-hot', function(done) {
runSequence('clear-cordova-www', 'copy-layout-hot', 'compile-react-hot', 'phonegap-serve', done);
});