简体   繁体   English

Django Pipeline,Heroku和SASS

[英]Django Pipeline, Heroku, and SASS

I've been trying to get django-pipeline setup so that I can compile and concat my assets. 我一直在尝试获取django-pipeline设置,以便我可以编译和连接我的资产。 I would also like to remove the compiled css files from my repository to avoid merge conflicts in pull requests. 我还想从我的存储库中删除已编译的css文件,以避免拉取请求中的合并冲突。

I've been trying to get django-pipeline to compile the files as part of the deploy process but can't figure this out. 我一直在尝试让django-pipeline编译文件作为部署过程的一部分,但无法解决这个问题。 I use SASS to write my CSS. 我用SASS写我的CSS。 My pipeline settings look like this: 我的管道设置如下所示:

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

PIPELINE_CSS = {
    'main': {
        'source_filenames': (
            'sass/blah.scss',
            'sass/main.scss',
        ),
        'output_filename': 'css/main.css',
        'extra_context': {
            'media': 'screen',
        },
    },
}

PIPELINE_COMPILERS = (
  'pipeline.compilers.sass.SASSCompiler',
)

This works great locally, and generates .css files in my /sass folder, which are then combined to make the main.css file. 这在本地工作得很好,并在my / sass文件夹中生成.css文件,然后将它们组合起来制作main.css文件。 If I check those CSS files into my git repository and push to Heroku, it also works fine. 如果我将这些CSS文件检查到我的git存储库并推送到Heroku,它也可以正常工作。 However, if I ignore them, which I would like to do so that I'm not committing compiled files, then django-pipeline can't find the files to combine. 但是,如果我忽略它们,我想这样做,我不提交编译文件,那么django-pipeline无法找到要合并的文件。 I'm not sure how I can get the sass compilation working on Heroku and I can't find anything about it. 我不知道如何在Heroku上运行sass编译,我找不到任何关于它的信息。

I can provide more information about my setup if needed, hopefully somebody knows something about this! 如果需要,我可以提供有关我的设置的更多信息,希望有人知道这件事!

OK, here's how I got this working using Compass to compile my SASS files. 好的,这就是我使用Compass编译我的SASS文件的方法。

  • Use multiple Heroku buildpacks - Heroku Buildpack Multi 使用多个Heroku构建包 - Heroku Buildpack Multi
  • Put the following in your .buildpacks file 将以下内容放在.buildpacks文件中

     https://github.com/heroku/heroku-buildpack-ruby.git https://github.com/heroku/heroku-buildpack-nodejs https://github.com/heroku/heroku-buildpack-python.git 
  • Create a Gemfile with compass and any other requirements you have. 使用指南针和您拥有的任何其他要求创建Gemfile。 Here's mine: 这是我的:

     source 'https://rubygems.org' ruby '1.9.3' gem 'bootstrap-sass' gem 'compass' 
  • Create a config.rb file. 创建config.rb文件。 Here's mine. 这是我的。 As you can see it, requires the bootstrap-sass that I included in my Gemfile: 正如你所看到的,需要我在Gemfile中包含的bootstrap-sass:

     # Require any additional compass plugins here. require 'bootstrap-sass' # Set this to the root of your project when deployed: http_path = "/" css_dir = "app_folder/static/css" sass_dir = "app_folder/static/sass" images_dir = "app_folder/static/images" output_style = :compact 

    more details on config.rb can be found here 有关config.rb的更多详细信息,请访问此处

  • Install node packages (django-pipeline wants yuglify). 安装节点包(django-pipeline想要yuglify)。 You'll need a package.json file: 你需要一个package.json文件:

     { "dependencies": { "yuglify": "0.1.4" }, "engines": { "node": "0.10.x", "npm": "1.3.x" }, "repository": { "type": "git", "url": "your repo url" } } 
  • almost ready... 快好了...
  • when Heroku runs the ruby buildpack, it will look for a rake task called assets:precompile. 当Heroku运行ruby buildpack时,它将寻找一个名为assets:precompile的rake任务。 So now you'll need to add a Rakefile with the following: 所以现在你需要添加一个带有以下内容的Rakefile:

     namespace 'assets' do desc 'Updates the stylesheets generated by Sass/Compass' task :precompile do print %x(compass compile --time) end end 

    this will put compile your stylesheets. 这将编译您的样式表。 You need to make sure you set the output (back in config.rb) to the place that django-pipeline is looking for CSS files (shown in the original question). 您需要确保将输出(在config.rb中)设置为django-pipeline正在查找CSS文件的位置(显示在原始问题中)。

  • You should get rid of this part in the original question as django-pipeline isn't compiling your SASS for you: 你应该在原始问题中摆脱这部分,因为django-pipeline没有为你编译你的SASS:

     PIPELINE_COMPILERS = ( 'pipeline.compilers.sass.SASSCompiler', ) 
  • That should be it! 那应该是它! Deploys should just work now, and it didn't really add a significant amount of time to my deploy process. 部署应该现在正常工作,并没有真正为我的部署过程增加大量时间。

All in all, it amounts to a lot of setup, but for me it was well worth it as I no longer have to commit compiled files into the repository, which was causing a lot of merge conflicts when working with branches and pull requests. 总而言之,它相当于很多设置,但对我来说这非常值得,因为我不再需要将编译后的文件提交到存储库中,这在使用分支和拉取请求时会导致很多合并冲突。

I would like to figure out how to do this using only two buildpacks (obviously only one would be ideal but I don't know if it's possible). 我想弄清楚如何只使用两个buildpack来做到这一点(显然只有一个是理想的,但我不知道是否可能)。 The problem is trying to find binary paths so that pipeline can do it's thing when it doesn't find the defaults. 问题是试图找到二进制路径,以便管道在找不到默认值时可以做到这一点。 I'm not sure if the reason I can't do this is because of how Heroku is installing things, or because there is a bug in django-pipeline, but right now this is good enough for me. 我不确定我不能这样做的原因是因为Heroku是如何安装的,还是因为django-pipeline中存在错误,但是现在这对我来说已经足够了。

If you try this and it doesn't work for you, let me know, if I missed something I'm happy to make updates. 如果您尝试这个并且它对您不起作用,请告诉我,如果我遗漏了一些内容,我很乐意进行更新。

I don't want to take away from your excellent solution, but I tried this today and found a few differences that made things simpler for me - likely due to updates in django-pipeline and/or Heroku. 我不想从你的优秀解决方案中拿走,但我今天尝试了这一点并发现了一些让我更简单的差异 - 可能是由于django-pipeline和/或Heroku的更新。 My full solution is below, in case anyone else comes looking. 我的完整解决方案如下,万一其他人来看。

Add the 3 buildpacks to Heroku: 将3个buildpack添加到Heroku:

heroku buildpacks:set https://github.com/heroku/heroku-buildpack-ruby.git
heroku buildpacks:add https://github.com/heroku/heroku-buildpack-nodejs
heroku buildpacks:add https://github.com/heroku/heroku-buildpack-python.git

Add django-pipeline and django-pipeline-compass to requirements.txt : 将django-pipeline和django-pipeline-compass添加到requirements.txt

django-pipeline==1.5.2
django-pipeline-compass==0.1.5

Create a Gemfile to install Sass: 创建一个Gemfile来安装Sass:

source 'https://rubygems.org'
ruby '2.1.5'
gem 'bootstrap-sass'

Create a package.json file to install Yuglify: 创建一个package.json文件来安装Yuglify:

{
  "dependencies": {
    "yuglify": "0.1.4"
  },
  "engines": {
    "node": "0.10.x",
    "npm": "1.4.x"
  }
}

I did not need a Rakefile or config.rb . 我不需要Rakefileconfig.rb

For reference, here are relevant settings from my settings.py : 作为参考,以下是我的settings.py中的相关设置

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '_generated_media')
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'pipeline.finders.PipelineFinder',
)

PIPELINE_COMPILERS = (
    'pipeline_compass.compiler.CompassCompiler',
)

PIPELINE_YUGLIFY_BINARY = os.path.join(BASE_DIR, 'node_modules', '.bin', 'yuglify')

And I also had to add this entry to urls.py : 我还必须将此条目添加到urls.py

url(r'^static/(?P<path>.*)$', serve, kwargs={'document_root': settings.STATIC_ROOT})

Hope it helps someone! 希望它可以帮到某人!

您可能需要设置PIPELINE_SASS_BINARY以便django-pipeline可以找到您的SASS编译器。

You can use the libsass compiler for django-pipeline that uses Sass packaged as a Python package: 您可以将libsass编译器用于使用Sass打包为Python包的django-pipeline:

pip install libsasscompiler

Update your config: 更新您的配置:

PIPELINE['COMPILERS'] = (
  'libsasscompiler.LibSassCompiler',
)

The default Yuglify compressor is also a problem on Heroku, which you can temporarily overcome by disabling it. 默认的Yuglify压缩器在Heroku上也是一个问题,你可以通过禁用它来暂时克服它。 This is my config for enabling Sass on Django for example: 这是我在Django上启用Sass的配置,例如:

PIPELINE = {
    'COMPILERS': (
        'libsasscompiler.LibSassCompiler',
    ),
    'STYLESHEETS': {
        'main': {
            'source_filenames': (
              'styles/main.scss',
            ),
            'output_filename': 'css/main.css'
        },
    },
    # disable the default Yuglify compressor not available on Heroku
    'CSS_COMPRESSOR': 'pipeline.compressors.NoopCompressor',
    'JS_COMPRESSOR': 'pipeline.compressors.NoopCompressor'
}

The longer-term solution would be to move towards a JS-only build toolchain (as most projects are doing). 长期解决方案是转向仅使用JS的构建工具链(正如大多数项目所做的那样)。 Rails integrates pretty nicely with Webpack for example and is maintained by the same team. 例如,Rails 与Webpack很好地集成并由同一个团队维护。 Until that happens in Django (if ever) and trickles into the Heroku Python builpack, you can use Heroku's multiple buildpacks and add an official Node buildpack step that runs npm install; npm run build 直到Django中发生这种情况(如果有的话)并且进入Heroku Python构建包,您可以使用Heroku的多个构建包并添加运行npm install; npm run build的官方Node buildpack步骤npm install; npm run build npm install; npm run build for you. npm install; npm run build为你npm install; npm run build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM