让django pipeline和bower一起打得很好



我正在我的项目根目录下将bower组件安装到external/bower_components/。我在static下有额外的静态文件,还有一些是已安装应用程序的一部分。我正试图使用django管道来缩小所有存在于bower_components中的静态文件,而不去管其他静态文件。

我的问题是,我不知道如何使用django管道来缩小我的bower组件,同时不将所有的bower包复制到目标目录。

settings.py:中

STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    os.path.join(os.path.dirname(__file__), '..', 'external'),
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
PIPELINE = True
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'pipeline.finders.PipelineFinder',
)

当我运行python manage.py collectstatic时,我最终在staticfiles中得到了我想要的一切(静态库中的东西以及从bower中提取的东西的缩小版本),再加上bower中每个包的完整来源。从最终用户的角度来看,这是可行的,但有很多多余的垃圾,我实际上并不想要。

为了解决这个问题,我尝试了python manage.py collectstatic -i bower_components。但在这种情况下,忽略不仅会导致collectstatic无法复制文件,还会导致django管道看不到文件,最终导致

如果我尝试使用一些自定义管道查找器,如pipeline.finders.FileSystemFinder,它会导致collectstatic忽略我想要的django.contrib.adminstatic/文件夹中的所有静态脚本和css。

有没有一种方法可以让我既有蛋糕又吃,这样我就可以让collectstatic的复制函数关注一个组,而django管道的组合/缩小则关注另一个组?

写完这篇文章后,我意识到答案可能是显式地收集并缩小我安装的应用程序中的所有脚本和css(使用PIPELINE_CSSPIPELINE_JS),但这似乎并不理想,因为每次集成新应用程序都会产生不寻常的开销。

是的,但您需要修改静态查找器,将静态查找器更改为使用django管道查找器:

STATICFILES_FINDERS = (
    'pipeline.finders.FileSystemFinder',
    'pipeline.finders.AppDirectoriesFinder',
    'pipeline.finders.CachedFileFinder',
    'pipeline.finders.PipelineFinder'
)

这应该只复制那里的相关文件。

相关内容

最新更新