-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
192 lines (164 loc) · 4.73 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
/**
* Gulp Tasks
*
* @package MHW Calculator
* @author Scar Wu
* @copyright Copyright (c) Scar Wu (http://scar.tw)
* @link https://github.com/scarwu/MHRCalculator
*/
const gulp = require('gulp')
const del = require('del')
const $ = require('gulp-load-plugins')()
const log = require('fancy-log')
const colors = require('ansi-colors')
const webpack = require('webpack')
const webpackStream = require('webpack-stream')
const webpackConfig = require('./webpack.config.js')
const sentryWebpackPlugin = require("@sentry/webpack-plugin")
const postfix = (new Date()).getTime().toString()
let ENVIRONMENT = 'development'
let WEBPACK_NEED_WATCH = false
/**
* Compile Style & Script
*/
function handleCompileError(event) {
log.error(colors.red(event.message), 'error.')
}
function compileSass() {
return gulp.src('src/assets/styles/main.{sass,scss}')
.pipe($.sass({
outputStyle: ('production' === ENVIRONMENT) ? 'compressed' : 'expanded'
}).on('error', handleCompileError))
.pipe($.replace('../fonts/', '../../assets/fonts/vendor/'))
.pipe($.autoprefixer())
.pipe($.rename(function (path) {
path.basename = path.basename.split('.')[0]
path.extname = '.min.css'
}))
.pipe(gulp.dest('src/boot/assets/styles'))
}
function compileWebpack(callback) {
if ('production' === ENVIRONMENT) {
webpackConfig.mode = ENVIRONMENT
webpackConfig.plugins = webpackConfig.plugins || []
webpackConfig.plugins.push(new webpack.DefinePlugin({
'process.env': {
'ENV': "'production'",
'BUILD_TIME': postfix,
'NODE_ENV': JSON.stringify('production')
}
}))
webpackConfig.plugins.push(new sentryWebpackPlugin({
authToken: process.env.SENTRY_AUTH_TOKEN,
org: "scarstudio",
project: "mhrc",
release: postfix,
include: "src/boot"
}))
} else {
webpackConfig.plugins = webpackConfig.plugins || []
webpackConfig.plugins.push(new webpack.DefinePlugin({
'process.env': {
'ENV': "'development'",
'BUILD_TIME': postfix,
'NODE_ENV': JSON.stringify('development')
}
}))
}
if (WEBPACK_NEED_WATCH) {
webpackConfig.watch = true
}
let result = gulp.src([ 'src/assets/scripts/main.jsx', 'src/assets/scripts/worker.js' ])
.pipe(webpackStream(webpackConfig, webpack).on('error', handleCompileError))
.pipe(gulp.dest('src/boot/assets/scripts'))
if (WEBPACK_NEED_WATCH) {
callback()
} else {
return result
}
}
/**
* Copy Files & Folders
*/
function copyStatic() {
return gulp.src('src/static/**/*')
.pipe(gulp.dest('src/boot'))
}
function copyAssetsFonts() {
return gulp.src('src/assets/fonts/*')
.pipe(gulp.dest('src/boot/assets/fonts'))
}
function copyAssetsImages() {
return gulp.src('src/assets/images/**/*')
.pipe(gulp.dest('src/boot/assets/images'))
}
function copyVendorFonts() {
return gulp.src('node_modules/font-awesome/fonts/*.{otf,eot,svg,ttf,woff,woff2}')
.pipe(gulp.dest('src/boot/assets/fonts/vendor'))
}
/**
* Watching Files
*/
function watch() {
// Watch Files
gulp.watch('src/boot/**/*').on('change', $.livereload.changed)
gulp.watch('src/static/**/*', copyStatic)
gulp.watch('src/assets/fonts/*', copyAssetsFonts)
gulp.watch('src/assets/images/**/*', copyAssetsImages)
gulp.watch('src/assets/styles/**/*.{sass,scss}', compileSass)
// Start LiveReload
$.livereload.listen()
}
/**
* Release
*/
function releaseCopyBoot() {
return gulp.src('src/boot/**/*')
.pipe($.filter(['**', '!**/*.map']))
.pipe(gulp.dest('docs'))
}
function releaseReplaceIndex() {
return gulp.src('src/boot/index.html')
.pipe($.replace('?timestamp', `?${postfix}`))
.pipe(gulp.dest('src/boot'))
}
/**
* Set Variables
*/
function setEnv(callback) {
// Warrning: Change ENVIRONMENT to Prodctuion
ENVIRONMENT = 'production'
callback()
}
function setWatch(callback) {
// Webpack need watch
WEBPACK_NEED_WATCH = true
callback()
}
/**
* Clean Temp Folders
*/
function cleanBoot() {
return del('src/boot')
}
function cleanDocs() {
return del('docs')
}
/**
* Bundled Tasks
*/
gulp.task('prepare', gulp.series(
cleanBoot,
gulp.parallel(copyStatic, copyAssetsFonts, copyAssetsImages, copyVendorFonts),
gulp.parallel(compileSass, compileWebpack)
))
gulp.task('release', gulp.series(
setEnv, cleanDocs,
'prepare', releaseReplaceIndex,
releaseCopyBoot
))
gulp.task('default', gulp.series(
setWatch,
'prepare',
watch
))