-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
86 lines (76 loc) · 2.37 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
var gulp = require("gulp"),
browserSync = require("browser-sync").create(),
browserify = require("browserify"),
babelify = require("babelify"),
source = require("vinyl-source-stream"),
buffer = require("vinyl-buffer"),
history = require("connect-history-api-fallback"),
uglify = require("gulp-uglify"),
gulpif = require("gulp-if"),
sass = require("gulp-sass"),
autoprefixer = require("gulp-autoprefixer"),
sourcemaps = require('gulp-sourcemaps');
var config = {
port: 3009,
paths: {
dist: "./dist",
entry: "./src/js/index.js",
js: "src/js/**/*.js",
sass: "src/sass/**/*.scss",
sassMain: "./src/sass/main.scss"
}
}
function logError(e) {
console.error(e.message);
this.emit('end');
}
function isProd() {
return process.env.NODE_ENV === "production";
}
gulp.task("default", ["browserSync"], function() {
gulp.watch(config.paths.js, ["build"]);
gulp.watch(config.paths.sass, ["sass"]);
});
gulp.task("build", function () {
return browserify({ entries: config.paths.entry, debug: true })
.transform("babelify", {
presets: ["es2015", "react"],
plugins: ["transform-object-rest-spread"],
})
.bundle()
.on("error", function(e) {
console.error(e.message);
this.emit('end');
})
.pipe(source("bundle.js"))
.pipe(gulpif(isProd, buffer()))
.pipe(gulpif(isProd, uglify()))
.pipe(gulp.dest(config.paths.dist + "/js"))
.pipe(browserSync.stream());
process.env.NODE_ENV = "development";
});
gulp.task("setProd", function () {
process.env.NODE_ENV = "production";
});
gulp.task("build:prod", ["setProd", "build"]);
gulp.task("sass", function() {
return gulp.src(config.paths.sassMain)
.pipe(sourcemaps.init())
.pipe(sass())
.on("error", sass.logError)
.pipe(autoprefixer({
browsers: ['last 5 versions']
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest(config.paths.dist + "/css"))
.pipe(browserSync.stream());
});
gulp.task("browserSync", function () {
browserSync.init({
port: config.port,
server: {
baseDir: config.paths.dist,
middleware: [history()]
}
})
});