You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 27, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 27, 2026. It is now read-only.
Should output UTF-8 BOM when input contains UTF-8 BOM #821
When integrate with sass-loader, it will output CSS with UTF-8 BOM (i.e. \uFEFF) at the beginning of the CSS when it is run in production mode. But this plugin seems trimmed the UTF-8 BOM in the final output CSS file.
This is a reproducible sample repository (https://github.com/sammyhk/scss-utf8.git).
Execute npm run sass which call sass directly produce the CSS file (./dist/in.scss.bom.min.css) with UTF-8 BOM included.
Execute npm run build which involve webpack, sass-loader and this plugin produce the CSS file (./dist/in.scss.min.css) without UTF-8 BOM.
Actual Behavior
Seems this plugin trimmed the UTF-8 BOM and output CSS file without BOM.
Expected Behavior
Should preserve the UTF-8 BOM if exists and output CSS file with BOM.
How Do We Reproduce?
This is a reproducible sample repository (https://github.com/sammyhk/scss-utf8.git).
Execute npm run sass which call sass directly produce the CSS file (./dist/in.scss.bom.min.css) with UTF-8 BOM included.
Execute npm run build which involve webpack, sass-loader and this plugin produce the CSS file (./dist/in.scss.min.css) without UTF-8 BOM.
Please paste the results of npx webpack-cli info here, and mention other relevant information
Also due logic merging modules in one file using UTF-8 BOM is not safe because you will have BOM in the middle of the file, I strongly recommend do not use BOM
Also due logic merging modules in one file using UTF-8 BOM is not safe because you will have BOM in the middle of the file, I strongly recommend do not use BOM
This is the most frustrated part, as a webpack & sass user we do not have a choice but the BOM is generated by sass. The original SCSS file (./src/in.scss) is plain ASCII with UTF-8 escaped sequence, but sass will un-escape as UTF-8 character and add BOM to the generated output.
For bundling multiple CSS into one, according to CSS specification, either binary sequence of @charset "UTF-8"; or BOM at the beginning of the file should change the CSS charset to be UTF-8. So the bundling tools should properly handle this case (move @charset "UTF-8"; or BOM to the beginning of the final output instead leaving them in the middle of the file).
As a webpack user, seems SASS do its job properly as it can correctly generate the CSS file when not involving webpack. That must be a problem in webpack or webpack plugins which breaks the things.
hm, yes, you are right, just interesting why do you need UTF-8 BOM? Do you have server with other encoding by default? Why do not use @charset "UTF-8";?
hm, yes, you are right, just interesting why do you need UTF-8 BOM? Do you have server with other encoding by default? Why do not use @charset "UTF-8";?
In SCSS even you explicitly write @charset "UTF-8"; it will be erased and SASS will depends on the input source whether it contains UTF-8 character (or UTF-8 character escaped sequence) and the output mode mentioned before to automatically inject either @charset "UTF-8"; or UTF-8 BOM... That is why I'm looking for a way to fix the output CSS as currently there is no way to fix that...
For the server encoding, definitely it can return the CSS by content type text/css; charset=UTF-8 header, but it will depends on the server side. I would consider that is a workaround but not a fix as the standalone CSS is still problematic...
if developer has @charset "UTF-8"; we keep it but output on the top (limitation, if you have multiple files with different @charset "UTF-8"; we can't merge them)
If developer has UTF-8 BOM we keep it but output on the top (here again the same limitation)
if developer has @charset "UTF-8"; we keep it but output on the top (limitation, if you have multiple files with different @charset "UTF-8"; we can't merge them)
If developer has UTF-8 BOM we keep it but output on the top (here again the same limitation)
Ideally css-loader should trim @charset or UTF-8 BOM and keep it as property of modules, so we can handle it for style-loader/mini-css-extract-plugin/etc
dart-sass adds @charset when you have non ascii characters in your file webpack/sass-loader#985 (need update to the latest version), so now we should handle it for css-loader and problems will be solved
After long time we decide don't support UTF-8 with BOM officially, please setup SASS to output @charset "UTF-8"; , there is a special option for this, sorry for long delay with this question
Activity
alexander-akait commentedon Sep 3, 2021
I think oroblem in
postcss, we use it as parser and postcss remove UTF-8 BOM, in this plugin we don't touch your contentalexander-akait commentedon Sep 3, 2021
Also due logic merging modules in one file using
UTF-8 BOMis not safe because you will have BOM in the middle of the file, I strongly recommend do not use BOMsammyhk commentedon Sep 4, 2021
This is the most frustrated part, as a webpack & sass user we do not have a choice but the BOM is generated by sass. The original SCSS file (
./src/in.scss) is plain ASCII with UTF-8 escaped sequence, butsasswill un-escape as UTF-8 character and add BOM to the generated output.For bundling multiple CSS into one, according to CSS specification, either binary sequence of
@charset "UTF-8";orBOMat the beginning of the file should change the CSS charset to be UTF-8. So the bundling tools should properly handle this case (move@charset "UTF-8";orBOMto the beginning of the final output instead leaving them in the middle of the file).Reference:
https://developer.mozilla.org/en-US/docs/Web/CSS/@charset
https://www.w3.org/TR/CSS2/syndata.html#charset
alexander-akait commentedon Sep 4, 2021
@charset "UTF-8";andUTF-8 BOMare different thingsalexander-akait commentedon Sep 4, 2021
Honestly we have here to remove BOM in
webpack, some developers want to keep them, some to remove 😕sammyhk commentedon Sep 4, 2021
At least for CSS it treated as the same thing, according to the specification (https://www.w3.org/TR/CSS2/syndata.html#charset) the initial bytes
BOMbinary form)@charset "UTF-8";binary form)will change the charset of the CSS parse to be UTF-8
For this reason,
SASSitself will generate@charset "UTF-8";orBOMbased on different mode (https://github.com/sass/dart-sass/blob/main/lib/src/visitor/serialize.dart#L64-L68)NOTE:
webpackdevelopment mode mapped toSASSexpanded mode,webpackproduction mode mapped toSASScompressed mode by thesass-loader.As a webpack user, seems
SASSdo its job properly as it can correctly generate the CSS file when not involvingwebpack. That must be a problem inwebpackor webpack plugins which breaks the things.alexander-akait commentedon Sep 4, 2021
hm, yes, you are right, just interesting why do you need
UTF-8 BOM? Do you have server with other encoding by default? Why do not use@charset "UTF-8";?sammyhk commentedon Sep 4, 2021
In SCSS even you explicitly write
@charset "UTF-8";it will be erased andSASSwill depends on the input source whether it contains UTF-8 character (or UTF-8 character escaped sequence) and the output mode mentioned before to automatically inject either@charset "UTF-8";or UTF-8BOM... That is why I'm looking for a way to fix the output CSS as currently there is no way to fix that...For the server encoding, definitely it can return the CSS by content type
text/css; charset=UTF-8header, but it will depends on the server side. I would consider that is a workaround but not a fix as the standalone CSS is still problematic...alexander-akait commentedon Sep 4, 2021
Just clarify, do you want:
@charset "UTF-8";we keep it but output on the top (limitation, if you have multiple files with different@charset "UTF-8";we can't merge them)UTF-8 BOMwe keep it but output on the top (here again the same limitation)sammyhk commentedon Sep 4, 2021
That should be good enough.
alexander-akait commentedon Sep 4, 2021
Related webpack/css-loader#1212
alexander-akait commentedon Sep 4, 2021
Ideally
css-loadershould trim@charsetorUTF-8 BOMand keep it as property of modules, so we can handle it forstyle-loader/mini-css-extract-plugin/etcalexander-akait commentedon Sep 4, 2021
We strip
UTF-8 BOMby default for all modules to avoid problems with merging and to avoid other problems (caching for example), so usingUTF-8 BOMin webpack has limitation https://github.com/webpack/loader-runner/blob/master/lib/LoaderRunner.js#L11alexander-akait commentedon Sep 7, 2021
dart-sassadds@charsetwhen you have non ascii characters in your file webpack/sass-loader#985 (need update to the latest version), so now we should handle it forcss-loaderand problems will be solvedalexander-akait commentedon Feb 6, 2025
After long time we decide don't support UTF-8 with BOM officially, please setup SASS to output
@charset "UTF-8";, there is a special option for this, sorry for long delay with this question