-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
170 lines (153 loc) · 5.23 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
/*
* Base Gulp File
* - 01 - Requirements
* - 02 - Paths
* - 03 - Entry Points
* - 04 - Styles
* - 05 - Scripts
* - 06 - Exports
*/
/*------------------------------------*\
01 - Requirements
Although Gulp inherently does not require any other libraries in order to
work, other NPM libraries will be used to generate sourcemaps, be able to
automatically view in a browser and to minify distributed files.
\*------------------------------------*/
const browserSync = require("browser-sync").create();
const dotenv = require("dotenv").config();
const gulp = require("gulp");
const ignore = require("gulp-ignore");
const plumber = require("gulp-plumber");
const postcss = require("gulp-postcss");
const rename = require("gulp-rename");
const sass = require("gulp-sass")(require("sass"));
const sourcemaps = require("gulp-sourcemaps");
const uglify = require("gulp-uglify");
const webpack = require("webpack-stream");
const glob = require("glob");
const sassGlob = require('gulp-sass-glob');
/*------------------------------------*\
02 - Paths
Paths are defined here as to where Gulp should look for files to compile,
as well as where to put files that have been compiled already.
\*------------------------------------*/
const paths = {
base: {
styles: {
src: "assets/scss/**/*.scss",
dest: "dist/css",
},
scripts: {
src: "./assets/js/**/*.js",
dest: "dist",
},
},
};
/*------------------------------------*\
03 - Entry Points
In order for Webpack to compile multiple entry points, we will need to glob
together an array of paths. This needs to be done for both components and
any base JavaScript.
\*------------------------------------*/
const baseEntryPoints = glob
.sync(paths.base.scripts.src)
.reduce((entries, entry) => {
const name = entry.replace("/assets", "");
entries[name] = entry;
return entries;
}, {});
/*------------------------------------*\
04 - Styles
Define both compilation of SASS files during development and also when ready for Production and final
build / minification. Autoprefixer is included in PostCSS Preset Env, thus is not defined here.
\*------------------------------------*/
gulp.task("baseStylesWatch", function () {
return gulp
.src(paths.base.styles.src, { sourcemaps: true })
.pipe(sourcemaps.init())
.pipe(sassGlob())
.pipe(sass())
.on("error", sass.logError)
.pipe(postcss()) // PostCSS will automatically grab any additional plugins and settings from postcss.config.js
.pipe(sourcemaps.write())
.pipe(gulp.dest(paths.base.styles.dest, { sourcemaps: true }))
.pipe(browserSync.stream());
});
gulp.task("baseStylesBuild", function () {
return gulp
.src(paths.base.styles.src)
.pipe(sassGlob())
.pipe(sass())
.on("error", sass.logError)
.pipe(postcss([]))
.pipe(gulp.dest(paths.base.styles.dest));
});
/*------------------------------------*\
05 - Scripts
Define both compilation of JavaScript files during development and also when
ready for Production and final build / minification. Here, Webpack is
defined and streamed into the Gulp process.
We first define each task as a function that accepts a callback. This allows us leverage gulp's
lastRun feature which will only recompile files that have changed since the last time the task was run.
This greatly increases performance when running gulp watch with many files.
\*------------------------------------*/
gulp.task("baseScriptsWatch", function () {
return gulp
.src(paths.base.scripts.src)
.pipe(plumber())
.pipe(
webpack({
...require("./webpack.config.js"),
entry: baseEntryPoints,
output: {
path: `${__dirname}/dist/js`,
filename: "[name]",
},
})
)
.pipe(gulp.dest(paths.base.scripts.dest));
});
gulp.task("baseScriptsBuild", function () {
return gulp
.src(paths.base.scripts.src)
.pipe(plumber())
.pipe(
webpack({
...require("./webpack.config.js"),
entry: baseEntryPoints,
output: {
path: `${__dirname}/dist/js`,
filename: "[name]",
},
})
)
.pipe(ignore.exclude(["**/*.map"]))
.pipe(uglify())
.pipe(gulp.dest(paths.base.scripts.dest));
});
/*------------------------------------*\
06 - Exports
Define both the developmental, "Watch" and final production, "Build"
processes for compiling files. The final production, "Build" process includes
minified files.
The BrowserSync Proxy address is determined by creating a custom version of a .env file, from the .env-example file.
Here you will specify the exact local address of your website.
\*------------------------------------*/
exports.watch = () => {
console.log("You are currently in development watch mode.");
// browserSync.init({
// proxy: process.env.BS_PROXY || "http://prototype.lndo.site",
// browser: process.env.BS_BROWSER || "google chrome",
// open: false,
// logConnections: true,
// });
gulp.watch(paths.base.styles.src, gulp.series("baseStylesWatch"));
gulp.watch(paths.base.scripts.src, gulp.series("baseScriptsWatch"));
};
exports.build = (done) => {
console.log("You are building for production.");
gulp.parallel(
"baseStylesBuild",
"baseScriptsBuild",
)(done);
};