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的主要内容,如果未能解决你的问题,请参考以下文章

从 Javascript 到 Django 服务器进行查询

javascript中的Django模板值

Django - 如何将 javascript 变量保存到 Django 数据库中? [复制]

如何在 Django 中使用 Javascript 和经过身份验证的用户调用 Django API

将 Javascript 值解析为 Django 视图

Django - 在 javascript 函数中使用 和 % % 标签时出现语法错误