-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
215 lines (189 loc) · 6.26 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Gulp StarterKit with Handlebars & TailwindCSS
* Author : bimaindra
* URL : bimaindra.com
*/
// -- GENERAL
const { series, parallel, src, dest, watch } = require('gulp');
const path = require('path');
const del = require('del');
const merge = require('merge-stream');
const logSymbols = require('log-symbols');
const browserSync = require('browser-sync').create();
const handlebars = require('gulp-handlebars');
const wrap = require('gulp-wrap');
const declare = require('gulp-declare');
const rename = require('gulp-rename');
const concat = require('gulp-concat');
const noop = require('gulp-noop');
const uglify = require('gulp-uglify');
const sourcemaps = require('gulp-sourcemaps');
// -- JS
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const replace = require('rollup-plugin-replace');
const { terser } = require('rollup-plugin-terser');
// -- CSS
// const sass = require('gulp-sass'); // -- if needed
const postcss = require('gulp-postcss');
const postcssimport = require('postcss-import');
const postcssnested = require('postcss-nested');
const tailwindcss = require('tailwindcss');
const autoprefixer = require('autoprefixer');
const cleanCss = require('gulp-clean-css');
// -- CONFIG
const tailwindConfig = require('./tailwind.config');
// -- ENV
const isDebug = (process.env.NODE_ENV || 'development').trim().toLowerCase() !== 'production';
// -- DIRECTORY MAPPING
const root = {
src: './src',
build: './build',
assets: './src/assets',
npm: './node_modules'
};
const dir = {
source: {
public: `${root.src}/public`,
css: `${root.assets}/css`,
scss: `${root.assets}/scss`,
js: `${root.assets}/js`,
images: `${root.assets}/images`,
fonts: `${root.assets}/fonts`
},
build: {
base: `${root.build}`,
css: `${root.build}/assets/css`,
js: `${root.build}/assets/js`,
images: `${root.build}/assets/images`,
fonts: `${root.build}/assets/fonts`
}
};
// -- BROWSER-SYNC INIT
function browserInit(done) {
browserSync.init({
server: {
baseDir: dir.build.base
}
});
done();
}
// -- BROWSER-SYNC RELOAD
function browserReload(done) {
console.log(logSymbols.info, 'Reloading BrowserSync...');
browserSync.reload();
done();
}
// -- CLEANUP BUILD FOLDER
function cleanBuild() {
console.log(logSymbols.info, 'Clean up build folder...');
return del([root.build]);
}
// -- FINISHED COMPILE MESSAGE
function finishedCompileMessage(done) {
console.log(logSymbols.success, 'Finished compiling!');
done();
}
// -- COPY JS HBS RUNTIME
function hbsRuntime() {
return src(`${root.npm}/handlebars/dist/handlebars.runtime.js`)
.pipe(rename('handlebars.js'))
.pipe(isDebug ? noop() : uglify())
.pipe(dest(dir.build.js));
}
// -- COMPILE HBS TEMPLATE
function hbsCompile() {
// -- Assume all partials start with an underscore
// -- You could also put them in a folder such as source/templates/partials/*.hbs
const partials = src([`${dir.source.public}/partials/**/*.hbs`])
.pipe(handlebars())
.pipe(
wrap(
'Handlebars.registerPartial(<%= processPartialName(file.relative) %>, Handlebars.template(<%= contents %>));',
{},
{
imports: {
processPartialName: function (fileName) {
// -- Strip the extension and the underscore
// -- Escape the output with JSON.stringify
return JSON.stringify(path.basename(fileName, '.js').substr(1));
}
}
}
)
);
const templates = src(`${dir.source.public}/templates/**/*.hbs`)
.pipe(handlebars())
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(
declare({
namespace: 'MyApp.templates',
noRedeclare: true // -- Avoid duplicate declarations
})
);
// -- Output both the partials and the templates as build/js/templates.js
return merge(partials, templates)
.pipe(concat('templates.js'))
.pipe(isDebug ? noop() : uglify())
.pipe(dest(dir.build.js));
}
// -- COMPILE CSS
function cssCompile() {
return src(`${dir.source.css}/*.css`)
.pipe(!isDebug ? noop() : sourcemaps.init())
.pipe(postcss([postcssimport, tailwindcss(tailwindConfig), postcssnested, autoprefixer]))
.pipe(concat({ path: 'style.css' }))
.pipe(isDebug ? noop() : cleanCss({ compatibility: 'ie8' }))
.pipe(!isDebug ? noop() : sourcemaps.write('./maps'))
.pipe(dest(dir.build.css));
}
// -- COMPILE JS
function jsCompile() {
return rollup
.rollup({
input: `${dir.source.js}/main.js`,
plugins: [
babel({
exclude: 'node_modules/**'
}),
commonjs(),
replace({
'process.env.NODE_ENV': JSON.stringify('production')
}),
resolve(),
isDebug ? noop() : terser()
]
})
.then((bundle) =>
bundle.write({
file: `${dir.build.js}/main.js`,
format: 'es',
sourcemap: !!isDebug
})
);
}
// -- COPY IMAGES
function imagesCopy() {
return src(`${dir.source.images}/**/*`).pipe(dest(dir.build.images));
}
// -- COPY HTML
function htmlCopy() {
return src(`${dir.source.public}/pages/*.html`).pipe(dest(dir.build.base));
}
// -- WATCHING FILES CHANGES
function watchFiles() {
watch(`${dir.source.css}/**/*.css`, series(parallel(cssCompile), finishedCompileMessage, browserReload));
watch(`${dir.source.js}/**/*.js`, series(parallel(jsCompile), finishedCompileMessage, browserReload));
watch(`${dir.source.images}/**/*`, series(parallel(imagesCopy), finishedCompileMessage, browserReload));
watch(`${dir.source.public}/pages/*.html`, series(parallel(cssCompile, htmlCopy), finishedCompileMessage, browserReload));
watch(`${dir.source.public}/**/*.hbs`, series(parallel(cssCompile), hbsCompile, finishedCompileMessage, browserReload));
console.log(`\t${logSymbols.info}`, 'Watching for changes...');
}
// -- COMPILE FILES
exports.build = series(cleanBuild, parallel(cssCompile, jsCompile, imagesCopy, hbsRuntime, htmlCopy), hbsCompile, finishedCompileMessage);
// -- SERVE TASK W/ PROD ENVIRONMENT
exports.serveprod = series(this.build, browserInit, watchFiles);
// -- DEFAULT TASK
exports.default = isDebug ? series(this.build, browserInit, watchFiles) : series(this.build);