question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSS charset rule is ignored in production mode.

See original GitHub issue

When .scss file holds some non-ASCII character, then sass is adding @charset "UTF-8"; row to the begining of output css file. It works fine for “development” webpackge mode, but in “production” mode this css rule is not being added.

  • Operating System: Windows 10
  • Node Version: 14.17.0
  • NPM Version: 6.14.13
  • webpack Version: ^5.11.1
  • sass-loader Version: ^12.1.0
  • sass: ^1.35.1

Expected Behavior

With webpack “production” mode, css charset rule is also added to output css file.

Actual Behavior

With webpack “production” mode, css charset rule is not being added.

Code

// webpack.config.js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const mode = process.env.NODE_ENV || "development";
const target = process.env.NODE_ENV === "production" ? "browserslist" : "web";

module.exports = {
  mode: mode,
  plugins: [new MiniCssExtractPlugin()],
  module: {
    rules: [{
        test: /\.(s[ac]|c)ss$/i,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader",
          "postcss-loader",
          "sass-loader"
        ]
    },{
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
    }],
  },
  target: target,
  devtool: "source-map",
  devServer: { contentBase: "./dist" },
};

How Do We Reproduce?

Follow instructions in this repo - https://github.com/scream87/css-charset-with-sass-loader

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Danoncommented, Mar 10, 2022

@alexander-akait Was the bug reported in dart-sass/sass?

I found this issue: https://github.com/sass/dart-sass/issues/1387 And the author claims removing @charset in compressed isn’t a bug. Can you relate?

CC: @scream87

1reaction
scream87commented, Jul 2, 2021

Ok, I see. Thanks for the operative and quick reaction. Since it is not sass-loader bug, I am closing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

charset - CSS: Cascading Style Sheets - MDN Web Docs
The @charset CSS at-rule specifies the character encoding used in the style sheet. It must be the first element in the style sheet...
Read more >
Allow removing @charset atrule with expanded OutputStyle
We're producing valid CSS that's as compatible as possible. ... it cause warning: "@charset" must be the first rule in the file on...
Read more >
How can I prevent Webpack 5 (or one of its plugins) from ...
If I switch the mode to none or development , then the exported CSS is not minified (i.e., line breaks are there), and...
Read more >
CSS Syntax Module Level 3 - W3C
A number of things you can do in CSS that would produce a valid @charset rule (if one existed), such as using multiple...
Read more >
MiniCssExtractPlugin - webpack
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); module.exports = { plugins: [new MiniCssExtractPlugin()], module: { rules: [ { test: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found