javascript Django的Gulpfile
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Django的Gulpfile相关的知识,希望对你有一定的参考价值。
/*
* ------------------------------------------------------------------------------
* Gulpfile.js
* https://gulpjs.com/
* ------------------------------------------------------------------------------
*/
const gulp = require('gulp');
// const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const autoprefixer = require('gulp-autoprefixer');
const cleanCSS = require('gulp-clean-css');
const sass = require('gulp-sass');
const rename = require('gulp-rename');
const sassGlob = require('gulp-sass-glob');
const browserify = require('browserify');
const babelify = require('babelify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const es = require('event-stream');
const project_name = "";
const paths = {
styles: {
scssInput: project_name + '/static/uncompiled/scss/*.scss',
modules: 'templates/partials/**/**/*.scss',
cssInput: project_name + '/static/uncompiled/css/*.css',
cssDest: project_name + '/static/uncompiled/css/',
cssDestMin: project_name + '/static/build/css/',
},
scripts: {
inputs: [
'./'+ project_name +'/static/uncompiled/js/main.js',
],
modules: project_name + '/static/uncompiled/js/modules/*.js',
dest: project_name + '/static/build/js/',
},
};
const sassOptions = {
errLogToConsole: true,
outputStyle: 'expanded',
};
// * Compiles Sass and minifies CSS
function styles() {
return gulp
.src(paths.styles.scssInput)
.pipe(sassGlob())
.pipe(sass(sassOptions)).on('error', sass.logError)
.pipe(autoprefixer({
grid: true,
}))
.pipe(gulp.dest(paths.styles.cssDest))
.pipe(cleanCSS())
.pipe(rename({
suffix: '.min',
}))
.pipe(gulp.dest(paths.styles.cssDestMin));
}
exports.styles = styles;
// * Compresses Scripts
async function scripts() {
const tasks = await paths.scripts.inputs.map((entry) => {
return (
browserify({
entries: [entry],
debug: true,
})
.transform(babelify, {
presets: ['@babel/preset-env'],
})
.bundle()
.pipe(source(entry))
.pipe(buffer())
// .pipe(sourcemaps.init())
.pipe(uglify())
// .pipe(sourcemaps.write('./'))
.pipe(rename({
dirname: '',
extname: '.min.js',
}))
.pipe(gulp.dest(paths.scripts.dest))
);
});
return es.merge.apply(null, tasks);
}
exports.scripts = scripts;
// * Watch Task
function watch() {
gulp.watch(paths.styles.scssInput, styles).on('all', (event, path, stats) => {
console.log(`File ${path} was ${event}, running tasks...`);
});
gulp.watch(paths.styles.modules, styles).on('all', (event, path, stats) => {
console.log(`File ${path} was ${event}, running tasks...`);
});
gulp.watch(paths.scripts.inputs, scripts).on('all', (event, path, stats) => {
console.log(`File ${path} was ${event}, running tasks...`);
});
gulp.watch(paths.scripts.modules, scripts).on('all', (event, path, stats) => {
console.log(`File ${path} was ${event}, running tasks...`);
});
}
exports.watch = watch;
const build = gulp.parallel(styles, scripts, watch);
gulp.task('default', build);
以上是关于javascript Django的Gulpfile的主要内容,如果未能解决你的问题,请参考以下文章
Django - 如何将 javascript 变量保存到 Django 数据库中? [复制]