How to use css preprocessor

醉折花枝作酒筹
Release: 2023-01-07 11:45:36
Original
2601 people have browsed it

No preprocessor was selected when creating the project, and the corresponding loader needs to be installed manually. The methods are: 1. Sass, "sass-loader node-sass"; 2. Less, "less-loader less"; 3. Stylus, "stylus-loader stylus".

How to use css preprocessor

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

CSS preprocessor

1. What is css preprocessor

CSS preprocessor is a specialized programming language used to add some programming features to CSS (CSS itself is not a programming language).

There is no need to consider browser compatibility issues, because the final compilation and output of the CSS preprocessor are still standard CSS styles.

You can use basic programming skills such as variables, simple logical judgments, and functions in the CSS preprocessor.

2. Several commonly used css preprocessors

  • sass

  • less

  • stylus

3. How to use css preprocessor

If you do not select the required preprocessor (Sass/Less/Stylus) when creating the project , you need to manually install the corresponding loader

# Sass

npm install -D sass-loader node-sass
Copy after login

# Less

npm install -D less-loader less
Copy after login

# Stylus

npm install -D stylus-loader stylus
Copy after login

Example: App.vue is modified to Sass

$color: #42b983;
a {
color: $color;
}
Copy after login

4. Automatically import styles

To automatically import style files (for colors, variables, mixins, etc.), you can use style-resources-loader.

npm i -D style-resources-loader
Copy after login

Configuration

const path = require('path')
function addStyleResource(rule) {
rule.use('style-resource')
.loader('style-resources-loader')
.options({
patterns: [            ,
path.resolve(__dirname, './src/styles/imports.scss'),
],
})
}
module.exports = {
chainWebpack: config => {
const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
types.forEach(type => addStyleResource(config.module.rule('scss').oneOf(type)))
},
}
Copy after login

Recommended learning: css video tutorial

The above is the detailed content of How to use css preprocessor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template