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

182 lines
5.2 KiB
JavaScript
Raw Permalink 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();
const concat = require('gulp-concat');
const tildeImporter = require('node-sass-tilde-importer');
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')(require('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 {
vendorsJs: [
`${vendorsRoot}/@popperjs/core/dist/umd/popper.js`,
`${vendorsRoot}/bootstrap/dist/js/bootstrap.js`,
],
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
}
const 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() {
const processCss = [
autoprefixer(), // adds vendor prefixes
pixrem(), // add fallbacks for rem units
];
2019-02-23 09:12:19 +03:00
const minifyCss = [
cssnano({ preset: 'default' }), // minify result
];
2019-02-23 09:12:19 +03:00
return src(`${paths.sass}/project.scss`)
.pipe(
sass({
importer: tildeImporter,
includePaths: [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));
2019-02-23 09:12:19 +03:00
}
// 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' }))
.pipe(dest(paths.js));
2019-02-23 09:12:19 +03:00
}
// Vendor Javascript minification
2019-02-23 09:12:19 +03:00
function vendorScripts() {
return src(paths.vendorsJs, { sourcemaps: true })
.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' }))
.pipe(dest(paths.js, { sourcemaps: '.' }));
2019-02-23 09:12:19 +03:00
}
// 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
.pipe(dest(paths.images));
2019-02-23 09:12:19 +03:00
}
2021-03-03 23:00:18 +03:00
{%- if cookiecutter.use_async == 'y' -%}
// Run django server
2020-04-16 11:18:58 +03:00
function asyncRunServer() {
const cmd = spawn(
'gunicorn',
['config.asgi', '-k', 'uvicorn_worker.UvicornWorker', '--reload'],
{stdio: 'inherit'},
);
cmd.on('close', function (code) {
console.log('gunicorn exited with code ' + code);
})
}
{%- else %}
// Run django server
2019-02-23 09:12:19 +03:00
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);
});
2019-02-23 09:12:19 +03:00
}
{%- endif %}
// 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`],
{
{%- 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 %}
// https://www.browsersync.io/docs/options/#option-proxy
proxy: {
{%- 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 %}
target: 'django:8000',
{%- endif %}
proxyReq: [
function (proxyReq, req) {
// Assign proxy 'host' header same as current request at Browsersync server
proxyReq.setHeader('Host', req.headers.host);
},
],
},
},
);
2019-02-23 09:12:19 +03:00
}
// Watch
2019-02-23 09:12:19 +03:00
function watchPaths() {
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
}
// Generate all assets
const generateAssets = parallel(styles, scripts, vendorScripts, imgCompression);
2019-02-23 09:12:19 +03:00
// Set up dev environment
{%- if cookiecutter.use_docker == 'n' %}
{%- if cookiecutter.use_async == 'y' %}
const dev = parallel(asyncRunServer, initBrowserSync, watchPaths);
{%- else %}
const dev = parallel(runServer, initBrowserSync, watchPaths);
{%- endif %}
{%- else %}
const dev = parallel(initBrowserSync, watchPaths);
{%- endif %}
exports.default = series(generateAssets, dev);
exports['generate-assets'] = generateAssets;
exports['dev'] = dev;