简体   繁体   English

使用gruntjs将CSS和SASS文件编译为单个CSS文件

[英]Compile css and sass files to single css file using gruntjs

I have a bootstrap.css file which I want to compile with my custom styles from style.sass into single output file, for example - style.css. 我有一个bootstrap.css文件,我想将其与style.sass中的自定义样式一起编译成单个输出文件,例如-style.css。 For sass compilation I use gruntjs with grunt-contrib-sass extension. 对于sass编译,我使用gruntjs和grunt-contrib-sass扩展名。 My Gruntfile.js config for sass looks like this: 我的Sass Gruntfile.js配置看起来像这样:

sass: {
    dist: {
        options: {
            //style: 'compressed',
            style: 'expanded',
            lineNumbers: true
        },
        files: {
            'build/styles/style.css': 'src/styles/style.sass'
        }
    }
}

I've tried to import bootstrap.css into sass file, but instead it only generates next code in output css (which is correct behavior http://sass-lang.com/documentation/file.SASS_REFERENCE.html#import ): 我尝试将bootstrap.css导入到sass文件中,但是它只会在输出CSS中生成下一个代码(这是正确的行为http://sass-lang.com/documentation/file.SASS_REFERENCE.html#import ):

@import url(bootstrap.css);

.....
/*my style.sass rules*/

I even tried to list multiple files in order of concatination and processing, like in uglifier settings: 我什至尝试按混淆和处理顺序列出多个文件,例如在uglifier设置中:

files: {
    'build/styles/style.css': ['src/styles/bootstrap.css', 'src/styles/style.sass']
}

But this only adds bootstrap.css into final style.css file ignoring style.sass existence. 但这只会将bootstrap.css添加到最终的style.css文件中,而忽略了style.sass的存在。

As I'm new in gruntjs, I can't figure out how this should be done properly. 由于我是gruntjs的新手,所以我不知道该如何正确完成。

The Grunt configuration is correct. Grunt配置正确。 The reason your file is not being imported is because of the way SASS is designed to work. 未导入文件的原因是由于SASS的设计工作方式。

The SASS documentation states: SASS文档指出:

By default, it looks for a Sass file to import directly, but there are a few circumstances under which it will compile to a CSS @import rule: 默认情况下,它会查找直接导入的Sass文件,但是在某些情况下,它将编译为CSS @import规则:

  • If the file's extension is .css. 如果文件的扩展名是.css。
  • If the filename begins with http://. 如果文件名以http://开头。
  • If the filename is a url(). 如果文件名是url()。
  • If the @import has any media queries. @import是否有任何媒体查询。

Since the file you are importing has a .css extension it will therefore not be imported directly but remain a standard CSS @import . 由于您要导入的文件具有.css扩展名,因此将不会直接导入该文件,而是保留标准CSS @import

You have three options to resolve this: 您可以通过以下三种方法解决此问题:

  1. Rename the included file to _bootstrap.scss . 将包含的文件重命名为_bootstrap.scss (If you don't add the underscore a bootstrap.css will be created along with your main output file which is unnecessary.) (如果不添加下划线,则会创建bootstrap.css以及不必要的主输出文件。)
  2. Include the Bootstrap SCSS source as a dependency of your project and build against that. Bootstrap SCSS源作为项目的依赖项包括在内,并以此为基础进行构建。 Install the Bootstrap source using Bower by typing $ bower install bootstrap-sass-official in your project root folder. 通过在项目根文件夹中键入$ bower install bootstrap-sass-official使用Bower安装Bootstrap源 (For instructions on setting up Bower see the Bower website .) Then you can replace your import above with @import 'bower_components/bootstrap-sass-official/assets/stylesheets/bootstrap'; (有关设置Bower的说明,请参见Bower网站 。)然后,可以使用@import 'bower_components/bootstrap-sass-official/assets/stylesheets/bootstrap';替换上面的导入@import 'bower_components/bootstrap-sass-official/assets/stylesheets/bootstrap'; .
  3. Use a concatenation library such as grunt-contrib-concat to combine Bootstrap.css and your main style sheet during your build process. 在构建过程中,请使用诸如grunt-contrib-concat之类的串联库来组合Bootstrap.css和您的主样式表。

This first option is fine if you downloaded the bootstrap CSS file into your project manually, however, if you are including it as a dependency with npm/bower it is not ideal. 如果您将引导CSS文件手动下载到项目中,则第一个选项很好,但是,如果将其作为依赖项包含在npm / bower中,则不理想。

I would recommend the second option since building Bootstrap from source will not only solve your problem but allow for customization of Bootstrap variables to fit your theme rather than overwriting them with subsequent style rules as well. 我建议使用第二种方法,因为从源代码构建Bootstrap不仅可以解决您的问题,还可以自定义Bootstrap变量以适合您的主题,而不是用后续的样式规则覆盖它们。 The only downside is that your build process might be slightly longer due to the rather large SASS build of the Bootstrap source. 唯一的缺点是,由于Bootstrap源代码的SASS构建量很大,因此构建过程可能会稍长一些。

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

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