Webpack using aliases (Resolve.alias) to resolve problems caused by scss @import relative paths

Source: Internet
Author: User
Tags aliases

Resolve.alias Configuration in Webpack.conf.js

Resolve: {         extensions: ['. js ', '. Vue '],         alias: {             ' @ ': path.resolve (__dirname, ' src ') ),             ' @scss ': Path.resolve (__dirname, ' src ', ' scss '),         }}

After configuring the Resolve.alias, we can use this in JS

// originally this write import Hongalert from '. /src/scss/icon.scss '//  can now write import Hongalert from ' @scss/icon.scss '

In Scss need to write this, note is [email protected]

// It was originally written @import '. /.. /.. /scss/mixin.scss '; // now can be written, note is [email protected]@import ' [email protected]/icon.scss ';

Questions and background

In the Style tab in *.vue, I used the @import to introduce the ICON.SCSS style. Because icon needs to rely on a font/woff/ttf. The Icon.scss and font folders are the same directory.

But Alert.vue and they are different directories. So what happens when you compile? Error ~

The reason and the process may be this: Vue will introduce ICON.SCSS, and then want to introduce the font, but found in the Vue directory can not find the font this folder, so error.

The workaround is to configure the absolute path in conjunction with the Webpack Resolve.alias.

Incidentally, if you refer to Scss in JS, it will not cause this error. But in JS to import CSS always feel strange.

Import ' @scss/icon.scss '

Webpack using aliases (Resolve.alias) to resolve problems caused by scss @import relative paths

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.