This repository has been archived by the owner on Sep 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
124 lines (115 loc) · 3.33 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
"use strict"
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');
var ts = require('gulp-typescript');
var notify = require('gulp-notify');
var browserify = require("browserify");
var tsify = require('tsify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
// tsProject over all functions
let tsProject = ts.createProject('tsconfig.json', {rootDir: "", outDir: "build"});
// src files for the project
let srcFiles = [
'src/**/*.tsx',
'src/**/*.ts'
];
// gulp task to build typescript files into build
gulp.task('default', ['build']);
// gulp taks to watch the build and reouput when changed
gulp.task('watch', ['build'], createGulpWatch(srcFiles, ['build']));
/**
* Runs all the needed build scripts to copy and compile into build/
*/
gulp.task('build', ['tsbuild']);
/**
* Builds the ts project with tsconfig.json and outputs to build
*/
gulp.task('tsbuild', function(){
let options = {
onLast: true,
message: "Build Finished!"
};
// get the project source and pipe it to gulp-typescript
// include sourcemaps when react-native doesn't suck
// https://github.com/ivogabe/gulp-typescript#source-maps
// let tsResult = tsProject.src()
let tsResult = gulp.src(srcFiles, {base: "src"})
.pipe(sourcemaps.init()) // generate sourcemaps
.pipe(tsProject()).js
.pipe(sourcemaps.write('.')) // append sourcemap to each file
.pipe(gulp.dest('build'))
.pipe(notify(options));
return tsResult;
});
function createGulpWatch(sources, tasks){
return function() {
var watcher = gulp.watch(sources, tasks);
watcher.on('change', function(event){
gutil.log('File ' + event.path + ' was ' + event.type);
});
};
}
/**
* Bundle the JS in the Html folder
*/
gulp.task('bundleHtml', function(){
let bundler = createTsBrowserify();
return bundleHtml(bundler);
});
function createTsBrowserify(){
return browserify({
entries: ['./html/ts/index.ts'],
extensions: ['.ts'],
debug: true,
fullPaths : true,
cache : {}, // <---- here is important things for optimization
packageCache : {} // <---- and here
})
.plugin(tsify, { typescript: require('ntypescript')})
.transform(babelify, {extensions: ['.ts']});
}
function bundleHtml(bundler){
return bundler.bundle()
.on('error', function (error) { gutil.log(error.toString()); })
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('html/build/', {base: './html/build'}));
}
/**
* Move the Html Directory to the Build Folders for release mode
*/
gulp.task('moveHtml', function(){
let htmlSrcs = [
'html/index.html',
'html/build/**',
'html/css/**',
'html/images/**',
'html/libs/**',
'html/js/**'
];
return gulp.src(htmlSrcs, { base: './' })
.pipe(gulp.dest('android/app/src/main/assets'));
});
/** tests */
let testFiles = [
'src/**/*.tsx',
'src/**/*.ts',
'__tests__/**/**/*.tsx',
'__tests__/**/**/*.ts',
];
let options = {
onLast: true,
message: "Test Build Finished!"
};
gulp.task('testWatch', ['test'], createGulpWatch(testFiles, ['test']));
gulp.task('test', function() {
return gulp.src(testFiles)
.pipe(tsProject()).js
.pipe(gulp.dest('__tests__/build'))
.pipe(notify(options))
});