Django与Gulp的'collectstatic'

时间:2015-08-11 20:49:30

标签: python django gulp bower

我最近在Django工作流程中采用了Gulp,但我对静态文件的存储感到很难过。

目前,我将Django的STATIC_ROOT设置为E:/path/to/project/collectstatic/。所以,在我运行python src/manage.py collectstatic之后;我的静态文件按预期收集。但是,我配置了gulpfile.js,以便在collectedstaic目录中的文件上执行各种任务。此输出 - 导致各种缩小和连接的文件 - 最终在新目录E:/path/to/project/static/内。

可悲的是,当访问网站的STATIC_URL时,正在从collectedstatic目录中获取静态文件;不是我想要的static目录。

他们无论如何要分开收集文件的目录和服务的目录?或者,有没有人有更好的方法来配置我的Gulp文件?

gulpfile.js

var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var bowerlib = require('bower-files')();
var coffee = require('gulp-coffee');
var concat = require('gulp-concat');
var del = require('del');
var gzip = require('gulp-gzip');
var rename = require('gulp-rename');
var minifycss = require('gulp-minify-css');
var watch = require('gulp-watch');
var shell = require('gulp-shell');
var uglify = require('gulp-uglify');
var gutil = require('gulp-util');
var sass = require('gulp-sass');

var OPTIONS = {
  COLLECTSTATIC: {
    watch: 'src/**/static/**/*.*'
  },

  CSS: {
    src: 'collectedstatic/**/css/**/*.css',
    dest: 'static',
    watch: 'collectedstatic/**/css/**/*.css'
  },

  SCSS: {
    src: 'collectedstatic/scss/main.scss',
    dest: 'static/css',
    filename: "main.css",
    include: ['./bower_components/foundation/scss'],
    watch: 'collectedstatic/scss/**/*.scss'
  },

  BOWERJS: {
    dest: 'static/js',
    filename: "lib.js",
  },

  JS: {
    src: 'collectedstatic/**/js/**/*.js',
    dest: 'static',
    watch: 'collectedstatic/**/js/**/*.js'
  },

  COFFEE: {
    src: 'collectedstatic/coffee/**/*.coffee',
    dest: 'static/js',
    filename: "main.js",
    watch: 'collectedstatic/coffee/**/*.coffee'
  },

  GZIP: {
    threshold: '1kb',
    gzipOptions: {
      level: 9
    }
  },

  DEL: ['collectedstatic', 'static']
}

// Delete task
gulp.task('delete', function() {
  del(OPTIONS.DEL);
});

// Execute collectstatic
gulp.task('collectstatic', shell.task([
  'python src/manage.py collectstatic --noinput'
]));

// Compile our CSS
gulp.task('css', function() {
  return gulp.src(OPTIONS.CSS['src'])
    .pipe(autoprefixer())
    .pipe(minifycss())
    .pipe(gzip(OPTIONS.GZIP))
    .pipe(gulp.dest(OPTIONS.CSS['dest']))
});

// Compile our SCSS
gulp.task('scss', function() {
  return gulp.src(OPTIONS.SCSS['src'])
    .pipe(sass().on('error', gutil.log))
    .pipe(autoprefixer())
    .pipe(gulp.dest(OPTIONS.SCSS['dest']))
    .pipe(rename({suffix: '.min'}))
    .pipe(minifycss())
    .pipe(gulp.dest(OPTIONS.SCSS['dest']))
    .pipe(gzip(OPTIONS.GZIP))
    .pipe(gulp.dest(OPTIONS.SCSS['dest']))
});

// Compile Bower JavaScript
gulp.task('bowerjs', function() {
  return gulp.src(bowerlib.ext('js').files)
    .pipe(concat(OPTIONS.BOWERJS['filename']))
    .pipe(gulp.dest(OPTIONS.BOWERJS['dest']))
    .pipe(rename({suffix: '.min'}))
    .pipe(uglify())
    .pipe(gzip(OPTIONS.GZIP))
    .pipe(gulp.dest(OPTIONS.BOWERJS['dest']))
});

// Compile our JavaScript
gulp.task('js', function() {
  return gulp.src(OPTIONS.JS['src'])
    .pipe(uglify())
    .pipe(gulp.dest(OPTIONS.JS['dest']))
});

// Compile our CoffeeScript
gulp.task('coffee', function() {
  return gulp.src(OPTIONS.COFFEE['src'])
    .pipe(coffee({bare: true}).on('error', gutil.log))
    .pipe(uglify())
    .pipe(concat(OPTIONS.COFFEE['filename']))
    .pipe(gulp.dest(OPTIONS.COFFEE['dest']))
});

// Default task
gulp.task('default', ['delete', 'collectstatic'], function() {
  gulp.start('css');
  gulp.start('scss');
  gulp.start('js');
  gulp.start('coffee');
});

// Watch Files For Changes
gulp.task('watch', ['default'], function() {
  gulp.watch(OPTIONS.COLLECTSTATIC['watch'], ['collectstatic']);
  gulp.watch(OPTIONS.CSS['watch'], ['css']);
  gulp.watch(OPTIONS.SCSS['watch'], ['scss']);
  gulp.watch(OPTIONS.JS['watch'], ['js']);
  gulp.watch(OPTIONS.COFFEE['watch'], ['coffee']);
});

2 个答案:

答案 0 :(得分:2)

由于集成Gulp的复杂性,我们选择主要使用Django Compressor:https://github.com/edx/ecommerce

  1. build.js将我们所有的JS依赖项收集到一个已知位置。
  2. STATICFILES_DIRS会更新以首先查看此位置(如果您在开发时忘记删除此目录,则可能会出现问题。)
  3. 模板继续正常引用SCSS和JS文件。
  4. Django Compressor负责(a)编制SCSS和(b)缩小所有资产。 (Compressor也可以编译CoffeeScript。)
  5. 我们只使用Gulp来进行测试和质量检查。我们对collectstaticcompress的来电如下:

    $ $(NODE_BIN)/r.js -o build.js $ python manage.py collectstatic --noinput -v0 $ python manage.py compress -v0

答案 1 :(得分:1)

您可以使用适用于runserver和collectstatic的django-gulp