2017-06-21 23:12:22 +03:00
|
|
|
////////////////////////////////
|
2019-02-23 09:12:19 +03:00
|
|
|
// Setup
|
2017-06-21 23:12:22 +03:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-02-23 09:12:19 +03:00
|
|
|
// Gulp and package
|
|
|
|
const { src, dest, parallel, series, watch } = require('gulp')
|
|
|
|
const pjson = require('./package.json')
|
2017-06-21 23:12:22 +03:00
|
|
|
|
2019-02-23 09:12:19 +03:00
|
|
|
// Plugins
|
|
|
|
const autoprefixer = require('autoprefixer')
|
|
|
|
const browserSync = require('browser-sync').create()
|
|
|
|
const concat = require('gulp-concat')
|
2023-01-29 15:12:12 +03:00
|
|
|
const tildeImporter = require('node-sass-tilde-importer');
|
2019-02-23 09:12:19 +03:00
|
|
|
const cssnano = require ('cssnano')
|
|
|
|
const imagemin = require('gulp-imagemin')
|
|
|
|
const pixrem = require('pixrem')
|
|
|
|
const plumber = require('gulp-plumber')
|
|
|
|
const postcss = require('gulp-postcss')
|
|
|
|
const reload = browserSync.reload
|
|
|
|
const rename = require('gulp-rename')
|
2021-11-16 23:29:43 +03:00
|
|
|
const sass = require('gulp-sass')(require('sass'))
|
2019-02-23 09:12:19 +03:00
|
|
|
const spawn = require('child_process').spawn
|
|
|
|
const uglify = require('gulp-uglify-es').default
|
2017-06-21 23:12:22 +03:00
|
|
|
|
|
|
|
// Relative paths function
|
2019-02-23 09:12:19 +03:00
|
|
|
function pathsConfig(appName) {
|
2019-02-27 02:11:20 +03:00
|
|
|
this.app = `./${pjson.name}`
|
|
|
|
const vendorsRoot = 'node_modules'
|
2017-06-21 23:12:22 +03:00
|
|
|
|
|
|
|
return {
|
2018-01-21 04:05:12 +03:00
|
|
|
vendorsJs: [
|
2021-07-28 02:39:15 +03:00
|
|
|
`${vendorsRoot}/@popperjs/core/dist/umd/popper.js`,
|
2019-02-27 02:11:20 +03:00
|
|
|
`${vendorsRoot}/bootstrap/dist/js/bootstrap.js`,
|
2018-01-21 04:05:12 +03:00
|
|
|
],
|
2017-06-21 23:12:22 +03:00
|
|
|
app: this.app,
|
2019-02-27 02:11:20 +03:00
|
|
|
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`,
|
2017-06-21 23:12:22 +03:00
|
|
|
}
|
2019-02-23 09:12:19 +03:00
|
|
|
}
|
2017-06-21 23:12:22 +03:00
|
|
|
|
2022-07-22 01:46:43 +03:00
|
|
|
const paths = pathsConfig()
|
2017-06-21 23:12:22 +03:00
|
|
|
|
|
|
|
////////////////////////////////
|
2019-02-23 09:12:19 +03:00
|
|
|
// Tasks
|
2017-06-21 23:12:22 +03:00
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
// Styles autoprefixing and minification
|
2019-02-23 09:12:19 +03:00
|
|
|
function styles() {
|
2022-07-22 01:46:43 +03:00
|
|
|
const processCss = [
|
2019-02-23 09:12:19 +03:00
|
|
|
autoprefixer(), // adds vendor prefixes
|
|
|
|
pixrem(), // add fallbacks for rem units
|
|
|
|
]
|
|
|
|
|
2022-07-22 01:46:43 +03:00
|
|
|
const minifyCss = [
|
2019-02-23 09:12:19 +03:00
|
|
|
cssnano({ preset: 'default' }) // minify result
|
|
|
|
]
|
|
|
|
|
2019-02-27 02:11:20 +03:00
|
|
|
return src(`${paths.sass}/project.scss`)
|
2018-01-21 04:05:12 +03:00
|
|
|
.pipe(sass({
|
2023-01-29 15:12:12 +03:00
|
|
|
importer: tildeImporter,
|
2018-01-21 04:05:12 +03:00
|
|
|
includePaths: [
|
|
|
|
paths.sass
|
|
|
|
]
|
|
|
|
}).on('error', sass.logError))
|
2017-06-21 23:12:22 +03:00
|
|
|
.pipe(plumber()) // Checks for errors
|
2019-02-23 09:12:19 +03:00
|
|
|
.pipe(postcss(processCss))
|
|
|
|
.pipe(dest(paths.css))
|
2017-06-21 23:12:22 +03:00
|
|
|
.pipe(rename({ suffix: '.min' }))
|
2019-02-23 09:12:19 +03:00
|
|
|
.pipe(postcss(minifyCss)) // Minifies the result
|
|
|
|
.pipe(dest(paths.css))
|
|
|
|
}
|
2017-06-21 23:12:22 +03:00
|
|
|
|
|
|
|
// Javascript minification
|
2019-02-23 09:12:19 +03:00
|
|
|
function scripts() {
|
2019-02-27 02:11:20 +03:00
|
|
|
return src(`${paths.js}/project.js`)
|
2017-06-21 23:12:22 +03:00
|
|
|
.pipe(plumber()) // Checks for errors
|
|
|
|
.pipe(uglify()) // Minifies the js
|
|
|
|
.pipe(rename({ suffix: '.min' }))
|
2019-02-23 09:12:19 +03:00
|
|
|
.pipe(dest(paths.js))
|
|
|
|
}
|
2018-01-21 04:05:12 +03:00
|
|
|
|
|
|
|
// Vendor Javascript minification
|
2019-02-23 09:12:19 +03:00
|
|
|
function vendorScripts() {
|
2023-01-20 01:45:56 +03:00
|
|
|
return src(paths.vendorsJs, { sourcemaps: true })
|
2018-01-21 04:05:12 +03:00
|
|
|
.pipe(concat('vendors.js'))
|
2019-02-23 09:12:19 +03:00
|
|
|
.pipe(dest(paths.js))
|
2018-01-21 04:05:12 +03:00
|
|
|
.pipe(plumber()) // Checks for errors
|
|
|
|
.pipe(uglify()) // Minifies the js
|
|
|
|
.pipe(rename({ suffix: '.min' }))
|
2023-01-20 01:45:56 +03:00
|
|
|
.pipe(dest(paths.js, { sourcemaps: '.' }))
|
2019-02-23 09:12:19 +03:00
|
|
|
}
|
2018-01-21 04:05:12 +03:00
|
|
|
|
2017-06-21 23:12:22 +03:00
|
|
|
// Image compression
|
2019-02-23 09:12:19 +03:00
|
|
|
function imgCompression() {
|
2019-02-27 02:11:20 +03:00
|
|
|
return src(`${paths.images}/*`)
|
2017-06-21 23:12:22 +03:00
|
|
|
.pipe(imagemin()) // Compresses PNG, JPEG, GIF and SVG images
|
2019-02-23 09:12:19 +03:00
|
|
|
.pipe(dest(paths.images))
|
|
|
|
}
|
|
|
|
|
2021-03-03 23:00:18 +03:00
|
|
|
{%- if cookiecutter.use_async == 'y' -%}
|
2020-04-13 17:57:42 +03:00
|
|
|
// Run django server
|
2020-04-16 11:18:58 +03:00
|
|
|
function asyncRunServer() {
|
2022-07-22 01:46:43 +03:00
|
|
|
const cmd = spawn('gunicorn', [
|
2020-04-13 17:57:42 +03:00
|
|
|
'config.asgi', '-k', 'uvicorn.workers.UvicornWorker', '--reload'
|
|
|
|
], {stdio: 'inherit'}
|
|
|
|
)
|
|
|
|
cmd.on('close', function(code) {
|
|
|
|
console.log('gunicorn exited with code ' + code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
{%- else %}
|
2017-06-21 23:12:22 +03:00
|
|
|
// Run django server
|
2019-02-23 09:12:19 +03:00
|
|
|
function runServer(cb) {
|
2022-07-22 01:46:43 +03:00
|
|
|
const cmd = spawn('python', ['manage.py', 'runserver'], {stdio: 'inherit'})
|
2017-06-21 23:12:22 +03:00
|
|
|
cmd.on('close', function(code) {
|
2019-02-23 09:12:19 +03:00
|
|
|
console.log('runServer exited with code ' + code)
|
|
|
|
cb(code)
|
|
|
|
})
|
|
|
|
}
|
2020-04-13 17:57:42 +03:00
|
|
|
{%- endif %}
|
2017-06-21 23:12:22 +03:00
|
|
|
|
|
|
|
// Browser sync server for live reload
|
2019-02-23 09:12:19 +03:00
|
|
|
function initBrowserSync() {
|
2021-12-17 20:53:43 +03:00
|
|
|
browserSync.init(
|
|
|
|
[
|
|
|
|
`${paths.css}/*.css`,
|
|
|
|
`${paths.js}/*.js`,
|
|
|
|
`${paths.templates}/*.html`
|
|
|
|
], {
|
2022-01-07 13:02:26 +03:00
|
|
|
{%- if cookiecutter.use_docker == 'y' %}
|
|
|
|
// https://www.browsersync.io/docs/options/#option-open
|
|
|
|
// Disable as it doesn't work from inside a container
|
|
|
|
open: false,
|
|
|
|
{%- endif %}
|
2021-12-17 20:53:43 +03:00
|
|
|
// https://www.browsersync.io/docs/options/#option-proxy
|
|
|
|
proxy: {
|
2019-03-25 15:10:55 +03:00
|
|
|
{%- if cookiecutter.use_docker == 'n' %}
|
2021-12-24 16:35:47 +03:00
|
|
|
target: '127.0.0.1:8000',
|
2021-03-03 23:00:18 +03:00
|
|
|
{%- else %}
|
2021-12-17 20:53:43 +03:00
|
|
|
target: 'django:8000',
|
2019-03-25 15:10:55 +03:00
|
|
|
{%- endif %}
|
2021-12-17 20:53:43 +03:00
|
|
|
proxyReq: [
|
|
|
|
function(proxyReq, req) {
|
|
|
|
// Assign proxy "host" header same as current request at Browsersync server
|
|
|
|
proxyReq.setHeader('Host', req.headers.host)
|
|
|
|
}
|
|
|
|
]
|
2019-02-27 02:11:20 +03:00
|
|
|
}
|
2021-12-17 20:53:43 +03:00
|
|
|
}
|
|
|
|
)
|
2019-02-23 09:12:19 +03:00
|
|
|
}
|
2017-06-21 23:12:22 +03:00
|
|
|
|
|
|
|
// Watch
|
2019-02-23 09:12:19 +03:00
|
|
|
function watchPaths() {
|
2020-06-28 19:38:29 +03:00
|
|
|
watch(`${paths.sass}/*.scss`{% if cookiecutter.windows == 'y' %}, { usePolling: true }{% endif %}, styles)
|
|
|
|
watch(`${paths.templates}/**/*.html`{% if cookiecutter.windows == 'y' %}, { usePolling: true }{% endif %}).on("change", reload)
|
|
|
|
watch([`${paths.js}/*.js`, `!${paths.js}/*.min.js`]{% if cookiecutter.windows == 'y' %}, { usePolling: true }{% endif %}, scripts).on("change", reload)
|
2019-02-23 09:12:19 +03:00
|
|
|
}
|
|
|
|
|
2019-02-27 02:11:20 +03:00
|
|
|
// Generate all assets
|
|
|
|
const generateAssets = parallel(
|
|
|
|
styles,
|
|
|
|
scripts,
|
2022-03-20 18:00:40 +03:00
|
|
|
vendorScripts,
|
2019-02-27 02:11:20 +03:00
|
|
|
imgCompression
|
|
|
|
)
|
|
|
|
|
2019-02-23 09:12:19 +03:00
|
|
|
// Set up dev environment
|
|
|
|
const dev = parallel(
|
2019-03-25 15:10:55 +03:00
|
|
|
{%- if cookiecutter.use_docker == 'n' %}
|
2020-04-13 17:57:42 +03:00
|
|
|
{%- if cookiecutter.use_async == 'y' %}
|
|
|
|
asyncRunServer,
|
|
|
|
{%- else %}
|
2019-02-23 09:12:19 +03:00
|
|
|
runServer,
|
2019-03-25 15:10:55 +03:00
|
|
|
{%- endif %}
|
2020-04-13 17:57:42 +03:00
|
|
|
{%- endif %}
|
2019-02-23 09:12:19 +03:00
|
|
|
initBrowserSync,
|
|
|
|
watchPaths
|
|
|
|
)
|
|
|
|
|
|
|
|
exports.default = series(generateAssets, dev)
|
2019-02-27 02:11:20 +03:00
|
|
|
exports["generate-assets"] = generateAssets
|
|
|
|
exports["dev"] = dev
|