-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
199 lines (177 loc) · 5.03 KB
/
gulpfile.babel.js
File metadata and controls
199 lines (177 loc) · 5.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
const TEST_FILES = 'scripts/**/__tests__/**/*.js';
const SRC_FILES = 'scripts/**/*.js';
const BABEL_PRESETS = ["stage-0", "es2015", "react"];
import gulp from 'gulp'
import source from 'vinyl-source-stream'
import gutil from 'gulp-util'
import browserify from 'browserify'
import babel from 'babel-core/register'
babel({ presets: BABEL_PRESETS })
import eslint from 'gulp-eslint'
import babelify from 'babelify'
import watchify from 'watchify'
import notify from 'gulp-notify'
import sass from 'gulp-sass'
import autoprefixer from 'gulp-autoprefixer'
import uglify from 'gulp-uglify'
import rename from 'gulp-rename'
import buffer from 'vinyl-buffer'
import browserSync from 'browser-sync'
let reload = browserSync.reload;
import historyApiFallback from 'connect-history-api-fallback'
import mocha from 'gulp-mocha'
import istanbul from 'gulp-istanbul'
import { Instrumenter } from 'isparta'
import runSequence from 'run-sequence'
function handleErrors() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'Compile Error',
message: '<%= error.message %>'
}).apply(this, args);
this.emit('end'); // Keep gulp from hanging on this task
}
function buildScript(file, watch) {
var props = {
entries: ['./scripts/' + file],
debug : true,
transform: [babelify.configure({presets: BABEL_PRESETS})]
};
// watchify() if watch requested, otherwise run browserify() once
var bundler = watch ? watchify(browserify(props)) : browserify(props);
function rebundle() {
var stream = bundler.bundle();
return stream
.on('error', handleErrors)
.pipe(source(file))
.pipe(gulp.dest('./build/'))
// If you also want to uglify it
// .pipe(buffer())
// .pipe(uglify())
// .pipe(rename('app.min.js'))
// .pipe(gulp.dest('./build'))
.pipe(reload({stream:true}))
}
// listen for an update and run rebundle
bundler.on('update', function() {
rebundle();
gutil.log('Rebundle...');
});
// run it once the first time buildScript is called
return rebundle();
}
/*
Styles Task
*/
gulp.task('styles', () => {
gulp.src('./styles/**/*.scss')
.pipe(sass({
outputStyle: 'compressed'
}))
.on('error', handleErrors)
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 9', 'ff 17', 'opera 12.1', 'ios 6', 'android 4'))
.on('error', handleErrors)
.pipe(gulp.dest('./build/'))
.pipe(reload({stream:true}))
});
/*
Copy over assets
*/
gulp.task('assets', () => {
gulp.src('./assets/**')
.pipe(gulp.dest('./build/assets/'))
});
/*
Browser Sync
*/
gulp.task('browser-sync', () => {
browserSync({
server : {},
middleware : [ historyApiFallback() ],
ghostMode: false
});
});
gulp.task('scripts', () => {
return buildScript('main.js', false); // this will run once because we set watch to false
});
gulp.task('eslint', function() {
return gulp.src(SRC_FILES)
.pipe(eslint({
baseConfig: {
"env": {
"browser": true,
"node": true,
"mocha": true,
"es6": true
},
"ecmaFeatures": {
"modules": true,
"jsx": true
},
"extends": "eslint:recommended",
"plugins": [
"react"
],
"rules": {
"react/jsx-uses-react": 2
}
}
}))
.pipe(eslint.format());
// .pipe(eslint.failAfterError());
});
/*
* Instrument files using istanbul and isparta
*/
gulp.task('coverage:instrument', () => {
return gulp.src(SRC_FILES)
.pipe(istanbul({
instrumenter: Instrumenter, // Use the isparta instrumenter (code coverage for ES6)
babel: {presets: BABEL_PRESETS}
// Istanbul configuration (see https://github.com/SBoudrias/gulp-istanbul#istanbulopt)
// ...
}))
.pipe(istanbul.hookRequire()); // Force `require` to return covered files
});
/*
* Write coverage reports after test success
*/
gulp.task('coverage:report', (done) => {
return gulp.src(SRC_FILES, {read: false})
.pipe(istanbul.writeReports({
// Istanbul configuration (see https://github.com/SBoudrias/gulp-istanbul#istanbulwritereportsopt)
// ...
}));
});
/**
* Run unit tests
*/
gulp.task('test', () => {
return gulp.src(TEST_FILES, {read: false})
.pipe(mocha({
compilers: { js: babel },
require: [__dirname + '/lib/jsdom'] // Prepare environement for React/JSX testing
}));
});
/**
* Run unit tests with code coverage
*/
gulp.task('test:coverage', (done) => {
runSequence('coverage:instrument', 'test', 'coverage:report', done);
});
/**
* Watch files and run unit tests on changes
*/
gulp.task('tdd', (done) => {
gulp.watch(SRC_FILES, ['eslint']);
gulp.watch([
TEST_FILES,
SRC_FILES
], ['test']).on('error', gutil.log);
});
// run 'scripts' task first, then watch for future changes
gulp.task('default', ['assets','styles','eslint','scripts','browser-sync'], () => {
gulp.watch('styles/**/*', ['styles']); // gulp watch for stylus changes
gulp.watch(SRC_FILES, ['eslint']);
return buildScript('main.js', true); // browserify watch for JS changes
});