rollup-plugin-styles icon indicating copy to clipboard operation
rollup-plugin-styles copied to clipboard

scss import call does not work with aliases that has special characters

Open parttee opened this issue 5 years ago • 4 comments

If I create an alias for root folder in the config object and set it to some special character, then the alias does not work in the scss import call but works f.e. in background image.

/* config */
styles({
  ...
  alias: {
    '@': path.join(__dirname),
  }
})

and my folder structure is something like scss/ images/ foo/

/* foo/style.scss */
@import "@/scss/mixins"; /* does not work */

.foo {
  background: url("@/images/foo.png"); /* for some reason this still works */
}

Everything works if I create separate aliases for scss and images folders, like

styles({
  ...
  alias: {
    'scss': path.join(__dirname, 'scss'),
    'images': path.join(__dirname, 'images'),
  }
})

parttee avatar Dec 02 '20 07:12 parttee

Hi @parttee,

alias option work only for PostCSS-processed @import and url() statements at the moment, not for SCSS/LESS/Stylus @imports due to high difficulty or even impossibility to implement it consistently across all preprocessors.

Anidetrix avatar Jan 28 '21 07:01 Anidetrix

@Anidetrix I'm having a similar issue.

Is it possible to get this working via the sass configuration option? I tried that, and also tried using @rollup/plugin-alias, but was unable to get alias usage working.

Enteleform avatar Mar 12 '21 19:03 Enteleform

is there any way to use aliases with sass?

I tried to configure import.resolve but it is not even called(

thank you!

aleksanderd avatar Jul 10 '23 14:07 aleksanderd

my solution:

const scssAliases = {
  '@theme': '/app/theme',
  '@': '/',
};

const stylesOptions = {
  ...
  sass: {
    sync: true,
    importer(url) {
      for (const [alias, path] of Object.entries(scssAliases)) {
        if (url.indexOf(alias) === 0) {
          const file = srcPath + url.replace(alias, path);
          return { file };
        }
      }
      return null;
    },
  },
  ...
};

be noted there is different importer formats for dart-sass and node-sass.

node-sass importer must not return value, but use third parameter done callback.

aleksanderd avatar Jul 11 '23 05:07 aleksanderd