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.

Sass and global imports

See original GitHub issue

Hi,

I’d like to automatically add imports at the top of scss files just like webpack.

So instead of adding: @import "src/global-styling/variables.scss"; At the top of every scss file, I’d like it if it could be added automatically.

I basically need the functionality of the following PR from this library: https://github.com/hytromo/rollup-plugin-postcss-retain-sass-data#readme

The problem is that that PR doesn’t work anymore and crashes. Could anyone tell me how I could achieve this? Perhaps there is another plugin that can help me?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tirrthcommented, Jan 31, 2022

Hi,

I was a bit hasty celebrating the solution sadly, sorry. You did push me in the correct direction however, as I now understand how to add custom plugins to the postcss plugin.

I’m using rollup together with the rollup-plugin-postcss. I’ve added the code you added to rollup and the code does seem to be called.

    postcss({
      modules: true,
      extract: "dist/styles.css",
      use: ["sass"],
      plugins: [
        () => {
          return root => {
            root.prepend(
              postcss.atrule({
                name: "import",
                params: '"src/global-styling/variables.scss"'
              })
            );
          };
        }
      ]
    }),

However, when I remove the following line from my scss file it still fails and tells me i’m missing the import during bundling. @import "src/global-styling/variables.scss";

I realize this might be a bit outside of the scope of this github project as it involves rollup etc. Is there any documentation I can find on creating my own custom plugins for postcss. I’m assuming that the plugins should work the same in the rollup-plugin-postcss package.

postcss({
      modules: true,
      extract: true,
      use: [['sass', { data: '@import "src/styles/tailwind.scss";' }]],
      plugins: [autoprefixer()],
      minimize: process.env.NODE_ENV === "production",
      extensions: ['.css']
})

This works for me…

2reactions
aicommented, Dec 18, 2019

Got it. You can add postcss-loader with custom PostCSS plugin before sass-loader.

This custom plugin will be like:

module.exports = postcss.plugin('postcss-auto-importer', () => {
  return root => {
    root.prepend(postcss.atrule({ name: 'import', params: '"src/global-styling/variables.scss"' }))
  }
})

Unfortunately, in Parcel you should use 2.0 alpha, since 1.0 doesn’t have this flexibility.

Did I answer your question?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass: @import
Sass imports have the same syntax as CSS imports, except that they allow ... @import makes all variables, mixins, and functions globally accessible....
Read more >
How to import Sass/SCSS mixins global? - DEV Community ‍ ‍
I import the two .scss files in the App.js so I can access the classes/mixins/variables global. But only the classes are accessible.
Read more >
How do I import a global rule in a sass file without it getting ...
And import it via import './global.sass' in my js file, the css doesn't get loaded on the page at all. How do I...
Read more >
Global SCSS Imports · Issue #832 · vitejs/vite - GitHub
I have a scss file with variables I would like to use throughout my app without importing it in every <style> block. Describe...
Read more >
Sass @import and Partials - W3Schools
Sass Importing Files ... Just like CSS, Sass also supports the @import directive. The @import directive allows you to include the content of...
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