Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack and Aliases within scss @import

Given below project structure:

|- SRC
|-- COMMON
|---- SCSS
|------ GLOBAL.scss
|------ VARIABLES.json (scss colors in json structure)
|-- PAGES
|---- COMPONENTS
|------ COMPONENT A
|-------- SCSS
|---------- componentA.scss
|------ COMPONENT B
|-------- SCSS
|---------- componentB.scss

I want to @import global.scss file into the single component scss files. I don't want to use relative paths, because components structure may have different nesting.

Currently I'm using Webpack Aliases:

resolve: {
  alias: {
    common: path.resolve(__dirname, 'src/common/')
  }
}

and I'm using @import 'common/scss/global.scss'; and @import 'common/scss/variables.json' and it's not working.

  • Module build failed
  • File to import not found or unreadable.

PS. Using json-loader, sass-loader etc.

Could you please help to find working solution of how to import JSON and SCSS without using relative '../../../../../' paths?

like image 544
user3844198 Avatar asked Feb 01 '26 00:02

user3844198


1 Answers

Sass has no special syntax for relative paths, so your import is equivalent to:

@import './common/scss/global.scss';

To inform webpack that it should be resolved as a module you can start the path with ~ and your alias will be applied correctly. See also sass-loader imports.

@import '~common/scss/global.scss';
like image 100
Michael Jungo Avatar answered Feb 04 '26 00:02

Michael Jungo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!