This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
forked from appdev-academy/appdev.academy-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
108 lines (96 loc) · 3.25 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
var gulp = require('gulp');
var sass = require('gulp-sass');
var rename = require('gulp-rename');
var util = require('gulp-util');
var size = require('gulp-size')
var uglify = require('gulp-uglify')
var cleanCSS = require('gulp-clean-css');
var browserify = require('browserify');
var babelify = require('babelify');
var watchify = require('watchify');
var buffer = require('vinyl-buffer');
var source = require('vinyl-source-stream');
var browserSync = require('browser-sync');
// No matter what URL query - serve ./index.html
var historyApiFallback = require('connect-history-api-fallback')
// Init JS bundler
var bundler = watchify(browserify('src/js/app.js', watchify.args));
// Use ES6, React, Object rest spread (3 dots syntax)
bundler.transform(babelify, {
presets: ["es2015", "react", "stage-1"],
plugins: ["transform-decorators-legacy", "transform-object-rest-spread"]
})
function bundle() {
return bundler.bundle()
.on('error', util.log.bind(util, 'Browserify Error'))
.pipe(source('app.js'))
.pipe(buffer())
.pipe(size())
.pipe(gulp.dest('dist'))
.pipe(browserSync.stream({ once: true }));
}
function minBundle() {
process.env.NODE_ENV = 'production';
browserify('src/js/app.js')
.transform(babelify, {
presets: ["es2015", "react", "stage-1"],
plugins: ["transform-decorators-legacy", "transform-object-rest-spread"]
})
.bundle()
.pipe(source('app.js'))
.pipe(buffer())
.pipe(size({ title: 'JS before uglify' }))
.pipe(uglify())
.pipe(size({ title: 'JS after uglify' }))
.pipe(gulp.dest('dist'));
}
// Rebundle JS for development (fast, big file)
gulp.task('js', bundle);
// Rebundle JS for production (slow, small file)
gulp.task('min-js', minBundle);
// Rebuild SASS for development
gulp.task('sass', function() {
gulp.src('src/css/app.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('dist'))
.pipe(browserSync.stream());
});
// Rebuild SASS for production
gulp.task('min-sass', function() {
gulp.src('src/css/app.scss')
.pipe(sass().on('error', sass.logError))
.pipe(size({ title: 'SASS before cleanCSS' }))
.pipe(cleanCSS())
.pipe(size({ title: 'SASS after cleanCSS' }))
.pipe(gulp.dest('dist'));
});
// Copy HTML files (single index.html) to dist folder
gulp.task('copy-html', function() {
gulp.src('src/index.html')
.pipe(gulp.dest('dist'))
.pipe(browserSync.stream({ once: true }));
});
// Copy constants depending on environment
gulp.task('copy-constants-development', function() {
gulp.src('src/js/constants-development.js')
.pipe(rename({ basename: 'constants'}))
.pipe(gulp.dest('src/js'));
});
gulp.task('copy-constants-production', function() {
gulp.src('src/js/constants-production.js')
.pipe(rename({ basename: 'constants'}))
.pipe(gulp.dest('src/js'));
});
gulp.task('default', ['copy-constants-development', 'js', 'sass', 'copy-html'], function() {
browserSync({
server: {
baseDir: 'dist',
middleware: [ historyApiFallback() ]
},
browser: 'google chrome'
});
gulp.watch('src/js/**/*.*', ['js']);
gulp.watch('src/css/**/*.*', ['sass']);
gulp.watch('src/index.html', ['copy-html']);
});
gulp.task('build', ['copy-constants-production', 'min-js', 'min-sass', 'copy-html'], function() {});