-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
executable file
·175 lines (150 loc) · 3.88 KB
/
gulpfile.js
File metadata and controls
executable file
·175 lines (150 loc) · 3.88 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
const { join } = require('node:path')
// External dependencies
const browserSync = require('browser-sync')
const gulp = require('gulp')
const babel = require('gulp-babel')
const clean = require('gulp-clean')
const nodemon = require('gulp-nodemon')
const gulpSass = require('gulp-sass')
const { createProxyMiddleware } = require('http-proxy-middleware')
const PluginError = require('plugin-error')
const dartSass = require('sass-embedded')
// Local dependencies
const config = require('./app/config')
const { findAvailablePort } = require('./lib/utils')
// Set configuration variables
const port = parseInt(process.env.PORT || config.port, 10) || 2000
// Delete all the files in /public build directory
function cleanPublic() {
return gulp.src('public', { allowEmpty: true }).pipe(clean())
}
// Set Sass compiler
const sass = gulpSass(dartSass)
// Compile SASS to CSS
function compileStyles() {
return gulp
.src(['app/assets/sass/**/*.scss'], {
sourcemaps: true
})
.pipe(
sass({
loadPaths: ['node_modules'],
sourceMap: true,
sourceMapIncludeSources: true
}).on('error', (error) => {
throw new PluginError('compileCSS', error.messageFormatted, {
showProperties: false
})
})
)
.pipe(
gulp.dest('public/css', {
sourcemaps: '.'
})
)
}
// Compile JavaScript (with ES6 support)
function compileScripts() {
return gulp
.src(['app/assets/javascript/**/*.js'], {
sourcemaps: true
})
.pipe(babel())
.pipe(
gulp.dest('public/js', {
sourcemaps: '.'
})
)
}
// Compile assets
function compileAssets() {
return gulp
.src(
[
'app/assets/**/**/*.*',
'!**/assets/**/**/*.js', // Don't copy JS files
'!**/assets/**/**/*.scss' // Don't copy SCSS files
],
{ encoding: false }
)
.pipe(gulp.dest('public'))
}
// Start nodemon
async function startNodemon(done) {
let availablePort
try {
availablePort = await findAvailablePort(port)
if (!availablePort) {
throw new Error(`Port ${port} in use`)
}
} catch (error) {
done(new PluginError('startNodemon', error))
return
}
process.env.PORT = availablePort
process.env.WATCH = 'true'
const server = nodemon({
script: 'app.js',
stdout: true,
ext: 'js json',
watch: ['.env', 'app.js', 'app', 'lib'],
ignore: ['app/assets', '**.test.*'],
quiet: false
})
let starting = false
const onReady = () => {
starting = false
done()
}
server.on('start', () => {
starting = true
setTimeout(onReady)
})
server.on('stdout', (stdout) => {
process.stdout.write(stdout)
if (starting) {
onReady()
}
})
}
// Start browsersync
async function startBrowserSync(done) {
const proxyPort = parseInt(process.env.PORT, 10)
browserSync.init(
{
port: proxyPort + 1000,
ui: false,
files: ['app/views/**/*.*', 'lib/example-templates/**/*.*'],
ghostMode: false,
open: false,
notify: true,
watch: true,
// Proxy to Node.js server
middleware: createProxyMiddleware({
changeOrigin: true,
target: `http://localhost:${proxyPort}`
}),
// Serve static assets
server: {
baseDir: join(__dirname, 'public')
}
},
done
)
gulp.watch('public/**/*.*').on('change', browserSync.reload)
}
// Watch for changes within assets/
function watch() {
gulp.watch('app/assets/sass/**/*.scss', compileStyles)
gulp.watch('app/assets/javascript/**/*.js', compileScripts)
gulp.watch('app/assets/**/**/*.*', compileAssets)
}
exports.watch = watch
exports.compileStyles = compileStyles
exports.compileScripts = compileScripts
exports.cleanPublic = cleanPublic
gulp.task(
'build',
gulp.series(cleanPublic, compileStyles, compileScripts, compileAssets)
)
gulp.task('default', gulp.series(startNodemon, startBrowserSync, watch))