【问题标题】:Using Twitter Bootstrap for SASS without Rails or Compass在没有 Rails 或 Compass 的情况下使用 Twitter Bootstrap for SASS
【发布时间】:2014-02-12 22:12:58
【问题描述】:

我正在尝试使用 Twitter Bootstrap for SASS,但目前它没有集成到 Rails 项目中,我不需要从 Compass 获得的东西。我(想我)想要一个 Ruby 脚本来生成我的 CSS、JS 和字体文件并将它们放在正确的位置,但是花了几个小时在文档中转圈,我无法弄清楚这一点。

我有一个类似的目录:

site/
  css/
    bootstrap_variables.scss
    application.scss
    site.scss
  fonts/
  index.html
  js/

css/bootstrap_variables.scss 包含我对 Bootstrap 的默认变量的自定义变体。

css/site.scss 包含特定于站点的非引导程序 CSS。

css/application.scss 看起来像这样:

@import "bootstrap_variables.scss";

// Import Bootstrap modules I need, but not others.
@import "bootstrap/variables";
@import "bootstrap/mixins";
// ... etc

@import "site.scss"

我已经安装了bootstrap-sass gem。现在我想要一个脚本:

  1. 使用 SASS 处理 css/application.scss 并输出 css/application.css
  2. 将我需要的 Bootstrap JavaScript 放入 js/ 的单个文件中。 (如何指定需要哪些 JS 模块?)
  3. 如果我使用 Bootstrap 的图标,请将 glyphicons 字体放入 fonts/

如果这听起来很疯狂,而我完全错了,也请随时告诉我!

【问题讨论】:

  • 抱歉这个无用的评论,但是这个问题需要CSS标签吗?
  • 你需要一个sass编译器,compass有什么问题?
  • @HashemQolami 你是对的,是的 - 我猜我的头被 CSS 卡住了,但这里不相关。
  • @SergioAristizábal 也许我理解错了,但 SASS 不需要 Compass - it can generate compiled CSS by itself
  • 我花了一些时间使用 Compass 并让它使用 bootstrap-sass gem 生成自定义 Bootstrap CSS。但看不到如何让它处理它的 JS 或字体。

标签: ruby twitter-bootstrap sass


【解决方案1】:

现在我想要一个脚本:

我不知道你为什么需要这个脚本。因为如果你可以运行脚本,你也可以安装 sass 并运行sass css/application.scss css/application.css。您可以通过运行bower install bootstrap-sass 将所有引导程序的文件安装在您的site 文件夹中,然后运行带有--load-path 选项的sass 命令,该选项应该指向您的bower_components 文件夹。您还可以从 bower_components 文件夹中复制/或链接所需的 javascript 和字体。

或者考虑使用 Gulp 或 Grunt 来设置你的构建链。

但您确实可以通过编程方式使用 Sass。

使用 SASS 处理 css/application.scss 并输出 css/application.css。

您的 script.rb 应该如下所示:

/usr/bin/env ruby

require 'sass'
require 'bootstrap-sass'

sass_engine = Sass::Engine.for_file('css/application.scss',{
  :style => :compact,
  :syntax => :scss
})

output = sass_engine.render

File.open("css/application.css", 'w+') {|f| f.write(output) }

请注意,您还应该集成自动前缀。

将我需要的 Bootstrap JavaScript 放到 js/ 中的单个文件中

安装 bootstrap-sass gem 后,您可以从 gem 中读取 javascript 文件(请参阅:How to find the path a Ruby Gem is installed at (i.e. Gem.lib_path c.f. Gem.bin_path))。

所以获取你可以使用的词缀插件的内容:

file = File.new(Gem::Specification.find_by_name("bootstrap-sass").gem_dir + "/assets/javascripts/bootstrap/affix.js", "r")

您可以阅读所需的脚本,连接它们的内容,缩小和复制

如果我使用的是 Bootstrap 的图标,请将 glyphicons 字体放在 fonts/ 中。

如上,从Gem::Specification.find_by_name("bootstrap-sass").gem_dir + "/assets/fonts复制它们

【讨论】:

  • 这个问题是前一段时间的问题,我现在会做不同的事情(Gulp 或 Grunt,正如你所建议的那样)。但我会将此标记为正确,因为这听起来像是回答了我当时的问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多