【问题标题】:Rails/Compass/Sass Compile Super SlowRails/Compass/Sass 编译超慢
【发布时间】:2017-04-26 19:27:48
【问题描述】:

我知道这个问题已经被提出了一千次了,我觉得我已经阅读了大多数其他关于它的帖子,但我似乎仍然无法弄清楚这一点。

我刚开始使用 Ruby on Rails,并尝试在我的项目中使用 Compass/Sass/Suzy。它们都可以(大部分)工作,但指南针编译器存在一些问题。

首先使用bundle exec compass watch(有和没有--poll)什么都不做。 (这是假设我理解这个命令的意义,即它可以即时重新编译,我不必刷新页面来查看我的更改,但我可能是错的)

其次,当我在更改 .scss 文件后刷新页面时,编译完成并重新加载页面需要 30-35 秒。这是无法忍受的。我读过最新的“指南针导轨”存在一些问题,但大多数说这是几年前的帖子。最新的 compass-rails 是否可以完美运行,或者是否存在导致此问题的常见问题?

由于我刚刚学习,我只有一个控制器/视图/scss 文件,所以我很肯定我没有任何依赖循环问题。

这是我当前设置项目的方式。 (我认为它设置正确,但也许有人可以指出我做错了什么。)

  1. 我创建了一个新的 rails 项目并生成了一个“欢迎”控制器
  2. 修改了我的 application.rb

应用程序.rb

require_relative 'boot'

require 'rails/all'
require 'susy'
require 'compass'
require 'breakpoint'
require 'normalize-scss'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module PersonalWeb
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
  end
end
  1. 修改了我的 Gemfile

宝石文件

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'susy'
gem 'compass-rails'
gem 'breakpoint'
gem 'normalize-scss'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
  1. 运行命令bundle
  2. 将 application.css 重命名为 application.css.scss

application.css.scss

@import "compass";
@import "breakpoint";
@import "welcome.scss";
  1. 分别在我的welcome.erb 和welcome.scss 中添加了一些HTML 和sass。

作为进一步的说明,我遇到了每个编译指南针都抛出一个弃用警告的问题。我遵循了here 的建议,这似乎阻止了它。怀疑它涉及但为了防止我的无知妨碍解决方案,我想我会提到它。

编辑

我想我会加入我的welcome.scss,以防万一我做了什么愚蠢的事情。

welcome.scss

@import "normalize";
@import "partials/variables";
@import "partials/layout";
@import "partials/mixins";
@import "grids";

header {
  height: 100px;
  background: $blue;
  color: $white;
  margin-bottom: 10px;
  padding: 10px;
}

.wrapper {
  background: $white;
  margin: 0 auto;
  max-width: 900px;
}

nav {
  text-align: center;

  ul, li {
    padding: 0;
  }

  li {
    background: $gray;
  }

  a {
    text-decoration: none;
    color: $white;

    &:hover {
      color: $yellow;
    }
  }
}

.first-row {
  height: 100px;
  margin-bottom: 10px;
  padding: 10px;
}

.first-row .first {
  background: $yellow;
  height: 100%;
}

.first-row .second {
  height: 100%;
}

.first-row .second div {
  background: $orange;
  height: 100%;
}

.pic-gallery {
  div {
    background: $violet;
    height: 100%;
    margin-bottom: 10px;
    padding: 10px;
  }
}

.content-bar {
  div {
    background: $green;
    height: 100%;
    margin-bottom: 10px;
    padding: 10px;
  }
}

footer {
  height: 100px;
  background: $blue;
  color: $white;
  margin-top: 10px;
  padding: 10px;
  clear: both;
}

header {
  @include full;

  .logo {
    @include span(wide 1.75);
    @include border-radius(0px);
  }

  h1 {
    @include span(last 2);
    @include breakpoint((max-width 50em)){
      @include span(last 2);
    }
  }
}

.wrapper {
  @include container;
}

编辑 2 我想我应该提到我在 Windows 10 上。

【问题讨论】:

    标签: css ruby-on-rails compass-sass compass susy-compass


    【解决方案1】:

    compass watch 命令用于在基本项目或某些没有资产管道的框架上使用指南针。它监视文件系统的更改并将您的 SASS 文件编译为 CSS。它可以与livereloadx 一起使用,但这不是主要目的。

    您不想在 Rails 中使用compass watch。相反,Rails 有一个内置的 assets pipeline,它可以更好地完成这项工作。

    如果您仍想使用指南针的其他功能,您应该使用compass-rails gem。

    1。将 gem 添加到 gemfile 中

    gem 'sass-rails'
    gem 'compass-rails'
    

    然后运行bundle 来安装gem。

    2。设置您的application.scss

    如果您有 application.css 文件,请将其重命名为 application.scss。请注意,不应将其命名为 .css.scss。删除所有类似 cmets 的 sprockets 指令:

    #= require 'foo'
    

    改为使用 SASS @import 指令:

    @import "compass"
    

    然后follow the steps in the readme设置susy等扩展。

    【讨论】:

    • 你不需要application.rb 中的宝石,这就是Bundler.require(*Rails.groups) 的作用。
    • 另外,当您在 SASS 中导入文件时,不要包含扩展名。它应该是 @import "welcome"; 而不是 welcome.scss
    • 不将其命名为 .css.scss 是一种约定还是会导致问题?
    • Sass rails dropped support for importing .css.scss files 很久以前。它也是多余和愚蠢的,因为.scss 扩展告诉我们它无论如何都可以编译成 CSS。
    • 好的,@max,我已经试过两次了。我做了一个新项目,将 compass-rails 添加到 gemfile 中,运行 bundle。添加了宝石susy。再次跑捆绑。跑bundle exec compass install susy。我尝试添加config.compass.require "susy",但它说未知变量config,所以我把它拿出来了。添加了一个控制器并运行它,但 scss 更改仍然需要 20-30 秒才能编译
    【解决方案2】:

    尽量不要导入整个指南针。

    只导入你想要的模块,即:

    @import "compass/typography/links/link-colors"
    

    【讨论】:

    • 这确实加快了很多
    猜你喜欢
    • 2023-03-13
    • 2015-02-14
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 2011-08-17
    • 2012-10-12
    相关资源
    最近更新 更多