python - Django 的 'collectstatic' 与 Gulp

标签 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']);
});

最佳答案

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

  1. build.js 将我们所有的 JS 依赖项收集到一个已知位置。
  2. STATICFILES_DIRS 已更新为首先查看此位置(如果您在开发时忘记删除此目录,这可能会出现问题)。
  3. 模板继续照常引用 SCSS 和 JS 文件。
  4. Django Compressor 负责 (a) 编译 SCSS 和 (b) 缩小所有 Assets 。 (Compressor 也可以编译 CoffeeScript。)

我们只使用 Gulp 来运行测试和质量检查。我们对 collectstaticcompress 的调用如下所示:

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

关于python - Django 的 'collectstatic' 与 Gulp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31951677/

相关文章:

django - 如何在 Django 模型表单中显示用户的 get_full_name() 而不是用户名?

node.js - 使用 Node 检查器调试 Gulpfile 需要很长时间才能启动

python - 如果插入时间在时间​​范围内则插入表

python - 删除 pandas DataFrame 打印中的 `dtype: object`

python - Django Admin 报错admin.E008 The value of fieldsets must be a list or tuple

javascript - 使用 Gulp-babel 并获取 "Argument name clash in strict mode"

css - Gulp 缩小 css 在 IE9 中不起作用

python - 在不使用浏览器的情况下通过桌面应用程序完成 Spotify 授权代码流程

python - 测试 - 如何让所有记录器打印输出

python - orm ['model' ].objects.create(attr=some_value) 导致 IntegrityError