-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.mjs
156 lines (137 loc) · 4.18 KB
/
gulpfile.mjs
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
////////////////////////////////
// Setup
////////////////////////////////
// Gulp and package
import { src, dest, parallel, series, task, watch } from 'gulp';
import pjson from './package.json' with { type: 'json' };
// Plugins
import autoprefixer from 'autoprefixer';
import browserSyncLib from 'browser-sync';
import concat from 'gulp-concat';
import tildeImporter from 'node-sass-tilde-importer';
import cssnano from 'cssnano';
import pixrem from 'pixrem';
import plumber from 'gulp-plumber';
import postcss from 'gulp-postcss';
import rename from 'gulp-rename';
import gulpSass from 'gulp-sass';
import * as dartSass from 'sass';
import gulUglifyES from 'gulp-uglify-es';
import { spawn } from 'node:child_process';
const browserSync = browserSyncLib.create();
const reload = browserSync.reload;
const sass = gulpSass(dartSass);
const uglify = gulUglifyES.default;
// Relative paths function
function pathsConfig() {
const appName = `./${pjson.name}`;
const vendorsRoot = 'node_modules';
return {
vendorsJs: [
],
app: appName,
templates: `${appName}/templates`,
css: `${appName}/static/css`,
sass: `${appName}/static/sass`,
fonts: `${appName}/static/fonts`,
images: `${appName}/static/images`,
js: `${appName}/static/js`,
};
}
const paths = pathsConfig();
////////////////////////////////
// Tasks
////////////////////////////////
// Styles autoprefixing and minification
function styles() {
const processCss = [
autoprefixer(), // adds vendor prefixes
pixrem(), // add fallbacks for rem units
];
const minifyCss = [
cssnano({ preset: 'default' }), // minify result
];
return src(`${paths.sass}/*.scss`)
.pipe(
sass({
importer: tildeImporter,
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`)
.pipe(plumber()) // Checks for errors
.pipe(uglify()) // Minifies the js
.pipe(rename({ suffix: '.min' }))
.pipe(dest(paths.js));
}
// Vendor Javascript minification
function vendorScripts() {
return src(paths.vendorsJs, { sourcemaps: true })
.pipe(concat('vendors.js'))
.pipe(dest(paths.js))
.pipe(plumber()) // Checks for errors
.pipe(uglify()) // Minifies the js
.pipe(rename({ suffix: '.min' }))
.pipe(dest(paths.js, { sourcemaps: '.' }));
}
// Image compression
async function imgCompression() {
const imagemin = (await import("gulp-imagemin")).default;
return src(`${paths.images}/*.{png,svg,jpg}`, { encoding: false })
.pipe(imagemin()) // Compresses PNG, JPEG, SVG images.
.pipe(dest(paths.images));
}
// Run django server
function runServer(cb) {
const cmd = spawn('python', ['manage.py', 'runserver'], { stdio: 'inherit' });
cmd.on('close', function (code) {
console.log('runServer exited with code ' + code);
cb(code);
});
}
// Browser sync server for live reload
function initBrowserSync() {
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: 'django:8000',
proxyReq: [
function (proxyReq, req) {
// Assign proxy 'host' header same as current request at Browsersync server
proxyReq.setHeader('Host', req.headers.host);
},
],
},
},
);
}
// Watch
function watchPaths() {
watch(`${paths.sass}/*.scss`, styles);
watch(`${paths.templates}/**/*.html`).on('change', reload);
watch([`${paths.js}/*.js`, `!${paths.js}/*.min.js`], scripts).on(
'change',
reload,
);
}
// Generate all assets
const build = parallel(styles, scripts, imgCompression);
// Set up dev environment
const dev = parallel(initBrowserSync, watchPaths);
task('default', series(build, dev));
task('build', build);
task('dev', dev);