Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Production build and common mistakes

During development the CSS should be readable and have source maps. For publishing it should be as small as possible. Sass produces both from the same sources: the --style flag chooses the output format.

Compressed output

By default Sass uses the expanded style — the readable CSS you have seen throughout the course. The compressed style removes spaces, line breaks and the last semicolons:

// styles/main.scss
$primary: #2563eb;
 
.btn {
  color: white;
  background: $primary;
 
  &:hover {
    background: black;
  }
}
npx sass --style=compressed --no-source-map styles/main.scss css/main.css

The whole css/main.css becomes one line:

.btn{color:#fff;background:#2563eb}.btn:hover{background:#000}

Sass also shortened colours: white became #fff. The browser reads this file exactly like the expanded one.

Scripts for both modes

Put both commands into package.json:

{
  "scripts": {
    "dev": "sass --watch styles/main.scss css/main.css",
    "build": "sass --style=compressed --no-source-map styles/main.scss css/main.css"
  }
}

Work with npm run dev; before uploading the site run npm run build. In a Vite project you do not need this: npm run build already compiles and minifies the styles.

Should css/ go into Git?

The compiled CSS can always be recreated from the sources, so usually the css/ folder is added to .gitignore and built on the server or in CI. Commit it only if the hosting cannot run npm run build — for example, plain static hosting without a build step.

Checklist of common mistakes

MistakeWhat happensHow to avoid
Linking main.scss in HTMLStyles are not appliedLink the compiled .css
Editing css/main.cssChanges are overwrittenEdit only styles/
A partial without _An extra CSS file appearsStart partial names with _
No @use in a componentUndefined variableEach file loads what it uses
Division with /A deprecation warningmath.div()
@extend inside @mediaA compile errorA mixin instead
Deep nestingLong, fragile selectorsUp to three levels
A loop over everythingA heavy CSS fileGenerate only what is used
Sass function on var()A compile errorCalculate before the custom property

Check the size

After the build, look at the file size: ls -l css/main.css on macOS and Linux or the file's properties on Windows. If the CSS suddenly grows after adding a mixin or a loop, look at the expanded output to see what was generated.

Common mistake

Publishing the development build. The expanded CSS with a source map works, but it is larger, and the .map file lets anyone read your original Sass sources. Make npm run build the last step before every upload, and check that no main.css.map is left in the published folder.

Thanks for reading!

Donate to the site — we work hard for you.