-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
175 lines (145 loc) · 4.04 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
const gulp = require('gulp');
/**
* CSS build tools
*/
const sass = require('gulp-sass');
const cssnano = require('cssnano');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const sourcemaps = require('gulp-sourcemaps');
const postcssPresetEnv = require('postcss-preset-env');
/**
* Javascript build tools
*/
const webpack = require("webpack-stream");
/**
* Utilities
*/
const notify = require('gulp-notify');
const plumber = require('gulp-plumber');
const browserSync = require('browser-sync').create();
/**
* configure build dependencies
*/
sass.compiler = require("node-sass");
// ############################## Config ##############################
// Base
const env = 'development';
const projectURL = 'http://projecturl.test';
const publicDir = './public/';
const assets = publicDir + 'assets/';
// Styles
const styleSrcDir = assets + 'src/scss/';
const styleSrcFile = 'styles.scss';
const styleDistDir = assets + 'dist/styles/';
const styleDistFile = 'styles.css';
// Scripts
const jsSrcDir = assets + 'src/js/';
const jsSrcFile = 'index.js';
const jsDistDir = assets + 'dist/scripts/';
const jsDistFile = 'script.js';
// Watch
const phpWatch = ['./**/*.php', '!vendor/**', '!node_modules/**'];
const twigWatch = publicDir + 'views/**/*.twig';
const scssWatch = `${styleSrcDir}**/*.scss`;
const jsSrcWatch = `${jsSrcDir}**/*.js`;
const cssWatch = `${styleDistDir}**/*.css`;
const jsWatch = `${jsDistDir}**/*.js`;
const reloadWatch = [...phpWatch, cssWatch, jsWatch, twigWatch];
const watchIgnore = ['vendor/**', 'node_modules/**'];
// Other
const defaultBrowser = "C:\\Program Files (x86)\\Google\\Chrome Dev\\Application\\chrome.exe";
// ############################## Functions ##############################
/**
* error
*/
function error(err) {
notify.onError({
title: "Gulp error in " + err.plugin,
message: err.toString()
})(err);
}
/**
* launch browsersync proxy server
*/
function watch() {
// launch proxy
browserSync.init({
proxy: projectURL,
notify: false,
reloadOnRestart: true,
open: false,
browser: defaultBrowser
});
const watchOptions = {
ignore: watchIgnore
};
// compile assets
gulp.watch(scssWatch, exports.sass);
gulp.watch(jsSrcWatch, exports.js);
// reload browser
gulp.watch(reloadWatch, watchOptions).on('change', browserSync.reload);
}
/**
* compile sass using gulp/node-sass
*/
function compileSass() {
const sassOptions = {
outputStyle: 'expanded',
};
const postcssPlugins = [autoprefixer(), cssnano()];
return gulp.src(styleSrcDir + styleSrcFile)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
// .pipe(postcss(postcssPlugins))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(styleDistDir))
.pipe(browserSync.stream());
}
/**
* compile javascript using webpack
*/
function compileJs() {
const webpackConfig = {
output: {
filename: jsDistFile
},
mode: env,
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}]
}
};
if ('production' === env) {
webpackConfig.module.rules.use.options = {
presets: ['@babel/preset-env']
}
}
return gulp.src(jsSrcDir + jsSrcFile)
.pipe(plumber())
.pipe(webpack(webpackConfig))
.pipe(gulp.dest(jsDistDir))
.pipe(browserSync.stream());
}
// ############################## Exports ##############################
exports.js = compileJs;
exports.sass = compileSass;
exports.compile = gulp.parallel(exports.js, exports.sass);
exports.default = gulp.series(exports.compile, watch);
/**
* use this for simple debugging
*/
exports.debug = function (cb) {
// define variables to log to console
let check;
// debug code here
// log desired values to the console
console.log(check);
cb(); // signal completioon
}