cookiecutter-django/{{cookiecutter.project_slug}}/gulpfile.js

160 lines
4.1 KiB
JavaScript
Raw Normal View History

////////////////////////////////
2019-02-23 09:12:19 +03:00
// Setup
////////////////////////////////
2019-02-23 09:12:19 +03:00
// Gulp and package
const { src, dest, parallel, series, watch } = require('gulp')
const pjson = require('./package.json')
2019-02-23 09:12:19 +03:00
// Plugins
const autoprefixer = require('autoprefixer')
const browserSync = require('browser-sync').create()
{% if cookiecutter.custom_bootstrap_compilation == 'y' %}
const concat = require('gulp-concat')
{% endif %}
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')
const sass = require('gulp-sass')
const spawn = require('child_process').spawn
const uglify = require('gulp-uglify-es').default
// Relative paths function
2019-02-23 09:12:19 +03:00
function pathsConfig(appName) {
this.app = `./${pjson.name}`
const vendorsRoot = 'node_modules'
return {
{% if cookiecutter.custom_bootstrap_compilation == 'y' %}
bootstrapSass: `${vendorsRoot}/bootstrap/scss`,
vendorsJs: [
`${vendorsRoot}/jquery/dist/jquery.slim.js`,
`${vendorsRoot}/popper.js/dist/umd/popper.js`,
`${vendorsRoot}/bootstrap/dist/js/bootstrap.js`,
],
{% endif %}
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`,
}
2019-02-23 09:12:19 +03:00
}
2019-02-23 09:12:19 +03:00
var paths = pathsConfig()
////////////////////////////////
2019-02-23 09:12:19 +03:00
// Tasks
////////////////////////////////
// Styles autoprefixing and minification
2019-02-23 09:12:19 +03:00
function styles() {
var processCss = [
autoprefixer(), // adds vendor prefixes
pixrem(), // add fallbacks for rem units
]
var minifyCss = [
cssnano({ preset: 'default' }) // minify result
]
return src(`${paths.sass}/project.scss`)
.pipe(sass({
includePaths: [
{% if cookiecutter.custom_bootstrap_compilation == 'y' %}
paths.bootstrapSass,
{% endif %}
paths.sass
]
}).on('error', sass.logError))
.pipe(plumber()) // Checks for errors
2019-02-23 09:12:19 +03:00
.pipe(postcss(processCss))
.pipe(dest(paths.css))
.pipe(rename({ suffix: '.min' }))
2019-02-23 09:12:19 +03:00
.pipe(postcss(minifyCss)) // Minifies the result
.pipe(dest(paths.css))
}
// Javascript minification
2019-02-23 09:12:19 +03:00
function scripts() {
return src(`${paths.js}/project.js`)
.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))
}
{% if cookiecutter.custom_bootstrap_compilation == 'y' %}
// Vendor Javascript minification
2019-02-23 09:12:19 +03:00
function vendorScripts() {
return src(paths.vendorsJs)
.pipe(concat('vendors.js'))
2019-02-23 09:12:19 +03:00
.pipe(dest(paths.js))
.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))
}
{% endif %}
// Image compression
2019-02-23 09:12:19 +03:00
function imgCompression() {
return src(`${paths.images}/*`)
.pipe(imagemin()) // Compresses PNG, JPEG, GIF and SVG images
2019-02-23 09:12:19 +03:00
.pipe(dest(paths.images))
}
// Run django server
2019-02-23 09:12:19 +03:00
function runServer(cb) {
var cmd = spawn('python', ['manage.py', 'runserver'], {stdio: 'inherit'})
cmd.on('close', function(code) {
2019-02-23 09:12:19 +03:00
console.log('runServer exited with code ' + code)
cb(code)
})
}
// Browser sync server for live reload
2019-02-23 09:12:19 +03:00
function initBrowserSync() {
browserSync.init(
[
`${paths.css}/*.css`,
`${paths.js}/*.js`,
`${paths.templates}/*.html`
], {
proxy: "localhost:8000"
}
)
2019-02-23 09:12:19 +03:00
}
// Watch
2019-02-23 09:12:19 +03:00
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)
2019-02-23 09:12:19 +03:00
}
// Generate all assets
const generateAssets = parallel(
styles,
scripts,
{% if cookiecutter.custom_bootstrap_compilation == 'y' %}vendorScripts,{% endif %}
imgCompression
)
2019-02-23 09:12:19 +03:00
// Set up dev environment
const dev = parallel(
runServer,
initBrowserSync,
watchPaths
)
exports.default = series(generateAssets, dev)
exports["generate-assets"] = generateAssets
exports["dev"] = dev