Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove unwanted @charset "UTF-8" from CSS (shows up after compiling SASS files)

A quick summary of our problem:

  • We've got a @charset declaration prepended to our compiled CSS files
  • Our SCSS files do not have this declaration so it gets added during compilation
  • One of my co-workers says our site breaks in Safari if we have that declaration

SASS docs say:

Output Encoding

In general, Sass will try to encode the output stylesheet using the same encoding as the input stylesheet. In order for it to do this, though, the input stylesheet must have a @charset declaration; otherwise, Sass will default to encoding the output stylesheet as UTF-8. In addition, it will add a @charset declaration to the output if it’s not plain ASCII.

When other stylesheets with @charset declarations are @imported, Sass will convert them to the same encoding as the main stylesheet.

Source: http://sass-lang.com/documentation/file.SASS_REFERENCE.html

Does anyone know a way to prevent SASS from adding it?
Does anyone know how we can check the "if it’s not plain ASCII" hypothesis on all our SCSS files?
Any other ideas?

like image 788
Andres Diez Avatar asked May 20 '14 12:05

Andres Diez


2 Answers

To add to this, I experienced this issue recently. We use gulp-sass to compile our CSS. From what I understand gulp-sass (which uses node-sass) have not provided any way to stop @charset "UTF-8"; if there are non-ascii characters on your CSS. It was a problem for us because we wanted to use same CSS for our AMP pages. @charet is not allowed on AMP pages. Refer to this issue.

https://github.com/sass/sass/issues/2288

Our Solution

What I did was to add a simple gulp task to remove the encoding attribute from the compiled CSS.

gulp.task('amp-sass-clean-up', ['amp-sass' /*Add gulp-sass task that compiles CSS*/], function() {
    return gulp
        .src(['/public/amp.min.css' /*This is the full path and file name to where your final css is*/], {cwd: '/public' /*This is the full path to where final css file is*/)
        .pipe(replace('@charset "UTF-8";', ''))
        .pipe(gulp.dest('/public' /*This is the full path to where final css file is*/);
});
like image 151
user3106759 Avatar answered Nov 17 '22 19:11

user3106759


As far as i know the @charset is very well support for all browsers, see also: https://developer.mozilla.org/en-US/docs/Web/CSS/@charset. Only "old" issues can be found, which only happen when @charset is not declared at the start of your CSS file. See also: https://github.com/Compass/compass/issues/211.

When your are using grunt to compile your SASS files you can run an additional task after the compile task which removes the @charset declaration. https://github.com/erickrdch/grunt-string-replace can do the replacement for you.

like image 3
Bass Jobsen Avatar answered Nov 17 '22 17:11

Bass Jobsen