-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
123 lines (109 loc) · 3.37 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
////////////////////////////////
// Setup
////////////////////////////////
// Gulp and package
const { src, dest, parallel, series, watch } = require('gulp');
const pjson = require('/app/package.json');
// Plugins
const autoprefixer = require('autoprefixer');
const browserSync = require('browser-sync').create();
const cssnano = require('cssnano');
const pixrem = require('pixrem');
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const reload = browserSync.reload;
const rename = require('gulp-rename');
const sass = require('gulp-sass')(require('sass'));
const uglify = require('gulp-uglify-es').default;
// Relative paths function
function pathsConfig(appName) {
this.app = '/app/app';
return {
app: this.app,
templates: `${this.app}/templates`,
css: `${this.app}/static/css`,
sass: `${this.app}/static/sass`,
fonts: `${this.app}/static/fonts`,
images: `${this.app}/static/images`,
js: `${this.app}/static/js`,
};
}
const paths = pathsConfig();
////////////////////////////////
// Tasks
////////////////////////////////
// Styles autoprefixing and minification
const processCss = [
autoprefixer(), // adds vendor prefixes
pixrem(), // add fallbacks for rem units
];
const minifyCss = [
cssnano({ preset: 'default' }), // minify result
];
function project_styles() {
return src(`${paths.sass}/project.scss`)
.pipe(
sass({
silenceDeprecations: [
'legacy-js-api',
'mixed-decls',
'color-functions',
'global-builtin',
'import',
],
includePaths: [paths.sass],
}).on('error', sass.logError),
)
.pipe(plumber()) // Checks for errors
.pipe(postcss(processCss))
.pipe(dest(paths.css))
.pipe(rename({ suffix: '.min' }))
.pipe(postcss(minifyCss)) // Minifies the result
.pipe(dest(paths.css));
}
// Javascript minification
function scripts() {
return src([`${paths.js}/project.js`, `${paths.js}/*-page.js`])
.pipe(plumber()) // Checks for errors
.pipe(uglify()) // Minifies the js
.pipe(rename({ suffix: '.min' }))
.pipe(dest(paths.js));
}
// Browser sync server for live reload
function initBrowserSync() {
console.log(`Listening at: ${process.env.APP_SERVER_HOST}`);
browserSync.init(
[`${paths.css}/*.css`, `${paths.js}/*.js`, `${paths.templates}/*.html`],
{
// https://www.browsersync.io/docs/options/#option-open
// Disable as it doesn't work from inside a container
open: false,
// https://www.browsersync.io/docs/options/#option-proxy
proxy: {
target: process.env.APP_SERVER_HOST,
proxyReq: [
function (proxyReq, req) {
// Assign proxy 'host' header same as current request at Browsersync server
proxyReq.setHeader('Host', req.headers.host);
},
],
},
// https://browsersync.io/docs/options/#option-notify
notify: false,
},
);
}
// Watch
function watchPaths() {
watch(`${paths.sass}/*.scss`, project_styles);
watch(`${paths.templates}/**/*.html`).on('change', reload);
watch([`${paths.js}/*.js`, `!${paths.js}/*.min.js`], scripts).on(
'change',
reload,
);
}
const generateAssets = parallel(project_styles, scripts);
const dev = parallel(initBrowserSync, watchPaths);
exports.default = series(generateAssets, dev);
exports['generate-assets'] = generateAssets;
exports['dev'] = dev;