Skip to content

Byte Order Marker (BOM) inserted after external @import affects syntax #1335

Closed

Description

@justin-oh

Have you used AI?

None

Bug Description

A BOM character is inserted after the external @import commands, which are all moved to the top of the CSS file. This character is interpreted as  by a browser and affects the first selector. In my case, it was Bootstrap's :root selector where they define all their global CSS properties/vars. This selector is likely being seen by the browser as  :root.

If I comment out all the external @import lines, the BOM character won't appear.

With the external @import lines restored, I found the line content: "\2014\00A0"; from Bootstrap's code could be commented out and the BOM wouldn't appear. I ruled out Bootstrap by bringing that chunk of code into my Sass file and commenting out the Bootstrap @import.

I ruled out Sass by compiling my file in the CLI with nothing commented out and using the defaults options.

I thought it must be related to the charset, but my @charset "UTF-8"; line was being removed in the compiled CSS file.

I finally tried altering the sass-loader options so that sassOptions.charset=false and the BOM was not being inserted. This is why I think it could be a sass-loader issue.

I do this via Symfony's Webpack Encore package. I don't have much direct experience with Webpack by itself.

Link to Minimal Reproduction and step to reproduce

I don't know how to set up Webpack by itself. One could easily set up a basic Symfony project:

  1. Install symfony CLI (if needed) https://symfony.com/download
  2. symfony new my_project_directory --version="7.4.*" --webapp
  3. composer require symfony/webpack-encore-bundle
  4. npm install sass sass-loader

Alter their assets/app.js:

//import "./stimulus_bootstrap.js";
/*
 * Welcome to your app's main JavaScript file!
 *
 * This file will be included onto the page via the importmap() Twig function,
 * which should already be in your base.html.twig.
 */
import "./styles/app.scss";

console.log("This log comes from assets/app.js - welcome to AssetMapper! 🎉");

Alter their assets/styles/app.css and rename to assets/styles/app.scss:

body {
    background-color: skyblue;
}

// this blockquote code was pulled directly from Bootstrap
// the mixin and variables are just there to get the code to run

@mixin font-size($font-size) {
    font-size: $font-size;
}

$blockquote-margin-y: 1rem;
$blockquote-footer-font-size: 1rem;
$blockquote-footer-color: grey;

.blockquote-footer {
    margin-top: -$blockquote-margin-y;
    margin-bottom: $blockquote-margin-y;
    @include font-size($blockquote-footer-font-size);
    color: $blockquote-footer-color;

    &::before {
        // comment this out by itself to not see the BOM
        content: "\2014\00A0"; // em dash, nbsp
    }
}

// comment this out by itself to not see the BOM
@import url(https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap);

Alter their webpack.config.js:

// uncomment and alter this function call
.enableSassLoader((options) => {
  // comment this line out to not see the BOM
  options.sassOptions.charset = false;
})

Then run npm run build. The resulting CSS file will be in public/build and look like app.588eb710.css.

Expected Behavior

I would expect there to be no extra characters inserted anywhere that affect the resulting CSS syntax in an unexpected way.

Actual Behavior

A BOM character is inserted in such a way that it affects the first CSS selector.

Environment

⬡ System:
────────────────────────────────────────────────────────────────────────
  OS:      macOS 26.4
  CPU:     (10) arm64 Apple M5
  Memory:  87.28 MB / 16.00 GB

⬡ Binaries:
────────────────────────────────────────────────────────────────────────
  Node:  25.9.0
  npm:   11.12.1

⬡ Browsers:
────────────────────────────────────────────────────────────────────────
  Brave Browser:  149.1.91.172
  Chrome:         151.0.7922.174
  Firefox:        154.0
  Safari:         26.4

⬡ Packages:
────────────────────────────────────────────────────────────────────────
  sass-loader:  ^17.0.0 → 17.0.0
  webpack:      ^5.82 → 5.109.2
  webpack-cli:  ^7.0.0 → 7.2.2

Is this a regression?

None

Last Working Version

No response

Additional Context

I updated my Symfony project to get the latest version's of Webpack Encore and Webpack and encountered this issue.

This may also be a Post CSS issue: postcss/postcss#2133

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions