Import regular CSS file in SCSS file?

后端 未结 13 1384
旧巷少年郎
旧巷少年郎 2020-11-22 06:19

Is there anyway to import a regular CSS file with Sass\'s @import command? While I\'m not using all of the SCSS syntax from sass, I do still enjoy it\'s combini

相关标签:
13条回答
  • 2020-11-22 06:42

    This was implemented and merged starting from version 3.2 (pull #754 merged on 2 Jan 2015 for libsass, issues originaly were defined here: sass#193 #556, libsass#318).

    To cut the long story short, the syntax in next:

    1. to import (include) the raw CSS-file

      the syntax is without .css extension at the end (results in actual read of partial s[ac]ss|css and include of it inline to SCSS/SASS):

      @import "path/to/file";

    2. to import the CSS-file in a traditional way

      syntax goes in traditional way, with .css extension at the end (results to @import url("path/to/file.css"); in your compiled CSS):

      @import "path/to/file.css";

    And it is damn good: this syntax is elegant and laconic, plus backward compatible! It works excellently with libsass and node-sass.

    __

    To avoid further speculations in comments, writing this explicitly: Ruby based Sass still has this feature unimplemented after 7 years of discussions. By the time of writing this answer, it's promised that in 4.0 there will be a simple way to accomplish this, probably with the help of @use. It seems there will be an implementation very soon, the new "planned" "Proposal Accepted" tag was assigned for the issue #556 and the new @use feature.

    answer might be updated, as soon as something changes.

    0 讨论(0)
  • 2020-11-22 06:42

    I figured out an elegant, Rails-like way to do it. First, rename your .scss file to .scss.erb, then use syntax like this (example for highlight_js-rails4 gem CSS asset):

    @import "<%= asset_path("highlight_js/github") %>";
    

    Why you can't host the file directly via SCSS:

    Doing an @import in SCSS works fine for CSS files as long as you explicitly use the full path one way or another. In development mode, rails s serves assets without compiling them, so a path like this works...

    @import "highlight_js/github.css";
    

    ...because the hosted path is literally /assets/highlight_js/github.css. If you right-click on the page and "view source", then click on the link for the stylesheet with the above @import, you'll see a line in there that looks like:

    @import url(highlight_js/github.css);
    

    The SCSS engine translates "highlight_js/github.css" to url(highlight_js/github.css). This will work swimmingly until you decide to try running it in production where assets are precompiled have a hash injected into the file name. The SCSS file will still resolve to a static /assets/highlight_js/github.css that was not precompiled and doesn't exist in production.

    How this solution works:

    Firstly, by moving the .scss file to .scss.erb, we have effectively turned the SCSS into a template for Rails. Now, whenever we use <%= ... %> template tags, the Rails template processor will replace these snippets with the output of the code (just like any other template).

    Stating asset_path("highlight_js/github") in the .scss.erb file does two things:

    1. Triggers the rake assets:precompile task to precompile the appropriate CSS file.
    2. Generates a URL that appropriately reflects the asset regardless of the Rails environment.

    This also means that the SCSS engine isn't even parsing the CSS file; it's just hosting a link to it! So there's no hokey monkey patches or gross workarounds. We're serving a CSS asset via SCSS as intended, and using a URL to said CSS asset as Rails intended. Sweet!

    0 讨论(0)
  • 2020-11-22 06:46

    Simple.

    @import "path/to/file.css";

    0 讨论(0)
  • 2020-11-22 06:47

    It is now possible using:

    @import 'CSS:directory/filename.css';
    
    0 讨论(0)
  • 2020-11-22 06:48

    I can confirm this works:

    class CSSImporter < Sass::Importers::Filesystem
      def extensions
        super.merge('css' => :scss)
      end
    end
    
    view_context = ActionView::Base.new
    css = Sass::Engine.new(
      template,
      syntax:     :scss,
      cache:      false,
      load_paths: Rails.application.assets.paths,
      read_cache: false,
      filesystem_importer: CSSImporter # Relevant option,
    
      sprockets:  {
        context:     view_context,
        environment: Rails.application.assets
      }
    ).render
    

    Credit to Chriss Epstein: https://github.com/sass/sass/issues/193

    0 讨论(0)
  • 2020-11-22 06:49

    If you have a .css file which you don't wish to modify, neither change its extension to .scss (e.g. this file is from a forked project you don't maintain), you can always create a symlink and then import it into your .scss.

    Creates a symlink:

    ln -s path/to/css/file.css path/to/sass/files/_file.scss
    


    Imports symlink file into a target .scss:

    @import "path/to/sass/files/file";
    


    Your target output .css file is going to hold contents from imported symlink .scss file, not a CSS import rule (mentioned by @yaz with highest comment votes). And you don't have duplicated files with different extensions, what means any update made inside initial .css file immediately gets imported into your target output.

    Symbolic link (also symlink or soft link) is a special type of file that contains a reference to another file in the form of an absolute or relative path and that affects pathname resolution.
    – http://en.wikipedia.org/wiki/Symbolic_link

    0 讨论(0)
提交回复
热议问题