【Webpack】編譯scss並優化+導入Bootstrap

style-loader、mini-css-extract-plugin、css-loader、postcss、postcss-loader、sass、sass-loader、bootstrap、@popperjs/core

【Webpack】編譯scss並優化+導入Bootstrap

style-loader、mini-css-extract-plugin、css-loader、postcss、postcss-loader、sass、sass-loader、bootstrap、@popperjs/core


編譯css

  1. 安裝套件style-loadercss-loader套件

$ npm i style-loader css-loader -D

  1. src資料夾底下手動建立css/style.css檔案,並在裡面寫一些樣式:

1
2
3
4
5
6
7
8
9
body{
    background-color: red;
}

::placeholder{
    display: flex;
    flex: 1 1 auto;
    transform: translateX(-30px);
}

you project
├─── dist/
│     │
│     ├─── index.html 
│     │
│     └─── js/ 
│           │
│           └── main.js
│  
├─── node_modules/        
├─── src/
│     │
│     ├─── index.html 
│     │
│     ├─── js/ 
│     │     │
│     │     └── main.js
│     │
│     └─── css/ 
│           │
│           └── style.css
│ 
├─── package.json
└─── webpack.config.js   

  1. webpack.config.js新增:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/js/main.js', //進入點
    output: {
        path: path.resolve(__dirname,'dist'), //設置輸出檔案位置(絕對路徑)
        filename: './js/main.js', //輸出檔案名稱
        clean: true, //是否自動刪除舊檔案
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'style-loader', //後執行,將css以<style>方式直接引入html
                    'css-loader' //先執行,解析並壓縮代碼
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'src/index.html',
        })
    ]
}

在loader裡的執行序是由下往上

  1. main.js裡新增:

1
import '../css/style.css'

  1. 在終端機執行:

$ npm run start

you project
├─── dist/
│     │
│     ├─── js/ 
│     │     │
│     │     └── main.js
│     │
│     └─── css/ 
│           │
│           └── style.css
│  
├─── node_modules/        
├─── src/
│     │
│     ├─── js/ 
│     │     │
│     │     └── main.js
│     │
│     └─── css/ 
│           │
│           └── style.css
│ 
├─── package.json
└─── webpack.config.js   


將css以獨立方式帶入html

  1. 安裝mini-css-extract-plugin套件

$ npm i mini-css-extract-plugin -D

  1. webpack.config.js新增:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); //引入套件

module.exports = {
    entry: './src/js/main.js',
    output: {
        path: path.resolve(__dirname,'dist'),
        filename: './js/main.js', 
        clean: true, 
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader, //將原本style-loader替換掉,改用<link>方式引入html
                    'css-loader'
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'src/index.html',
        }),
        new MiniCssExtractPlugin({
            filename: 'css/style.css' //定義要輸出的名稱
        })
    ]
}

  1. 在終端機執行:

$ npm run start


編譯scss檔案並將樣式帶入html

  1. 安裝sasssass-loaderpostcsspostcss-loaderpostcss-preset-env

$ npm i sass sass-loader postcss postcss-loader postcss-preset-env -D

  1. webpack.config.js新增:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
    entry: './src/js/main.js',
    output: {
        path: path.resolve(__dirname,'dist'),
        filename: './js/main.js', 
        clean: true, 
    },
    module: {
        rules: [
            {
                test: /\.scss$/, //將原本css替換掉,改成scss檔
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    {
                        loader: 'postcss-loader', //使用postcss套件
                        options: {
                            postcssOptions:{
                                plugins:[
                                    ['postcss-preset-env'] //包含autoprefixer
                                ]
                            }
                        }
                    },
                    'sass-loader' //先編譯scss
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'src/index.html',
        }),
        new MiniCssExtractPlugin({
            filename: 'css/style.css' //定義要輸出的名稱
        })
    ]
}

  1. 建立scss資料夾結構,並在裡面新增style.scss檔,簡單寫一些樣式:

1
2
3
4
5
6
7
8
9
body{
    background-color: red;
}

::placeholder{
    display: flex;
    flex: 1 1 auto;
    transform: translateX(-30px);
}

  1. 修改main.js裡的樣式路徑:

1
import '../scss/style.scss'

  1. 在終端機執行:

$ npm run start

you project
├─── dist/
│     │
│     ├─── js/ 
│     │     │
│     │     └── main.js
│     │
│     └─── css/ 
│           │
│           └── style.css
│  
├─── node_modules/        
├─── src/
│     │
│     ├─── js/ 
│     │     │
│     │     └── main.js
│     │
│     └─── scss/ 
│           │
│           └── style.scss
│ 
├─── package.json
└─── webpack.config.js   

  1. 可以看到在dist裡的`style.css樣式自動增加前綴

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
body {
  background-color: red;
}

/* 這段是自動增加的 */
::-moz-placeholder { 
  display: flex;
  flex: 1 1 auto;
  transform: translateX(-30px);
}

::placeholder {
  display: flex;
  flex: 1 1 auto;
  transform: translateX(-30px);
}


若要使用bootstrap(5.3.3)

  1. 安裝bootstrap套件:
    • bootstrap是css樣板套件
    • @popperjs/core是bootstrap定位套件

$ npm i bootstrap @popperjs/core

  1. 快速取用bootstrap方法,是直接在main.js引入:

1
2
3
import '../../node_modules/bootstrap/scss/bootstrap.scss'; //bootstrap核心
import '../scss/style.scss'; //自定義樣式
import '../../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js'; //bootstrap定位套件

  1. 自訂bootstrap方法,在scss裡的style.scss引入:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//bootstrap
@import "../../node_modules/bootstrap/scss/functions";

//自訂義變數
@import "custom-variables"; //從bootstrap/scss/variables複製過來到本地端修改(切記要刪掉最後的import以免出跳錯)

//bootstrap
@import "../../node_modules/bootstrap/scss/variables";
@import "../../node_modules/bootstrap/scss/variables-dark";
@import "../../node_modules/bootstrap/scss/maps";
@import "../../node_modules/bootstrap/scss/mixins";
@import "../../node_modules/bootstrap/scss/utilities";
@import "../../node_modules/bootstrap/scss/root";
@import "../../node_modules/bootstrap/scss/reboot";
@import "../../node_modules/bootstrap/scss/type";
@import "../../node_modules/bootstrap/scss/images";
@import "../../node_modules/bootstrap/scss/containers";
@import "../../node_modules/bootstrap/scss/grid";
@import "../../node_modules/bootstrap/scss/tables";
@import "../../node_modules/bootstrap/scss/forms";
@import "../../node_modules/bootstrap/scss/buttons";
@import "../../node_modules/bootstrap/scss/transitions";
@import "../../node_modules/bootstrap/scss/dropdown";
@import "../../node_modules/bootstrap/scss/button-group";
@import "../../node_modules/bootstrap/scss/nav";
@import "../../node_modules/bootstrap/scss/navbar";
@import "../../node_modules/bootstrap/scss/card";
@import "../../node_modules/bootstrap/scss/accordion";
@import "../../node_modules/bootstrap/scss/breadcrumb";
@import "../../node_modules/bootstrap/scss/pagination";
@import "../../node_modules/bootstrap/scss/badge";
@import "../../node_modules/bootstrap/scss/alert";
@import "../../node_modules/bootstrap/scss/progress";
@import "../../node_modules/bootstrap/scss/list-group";
@import "../../node_modules/bootstrap/scss/close";
@import "../../node_modules/bootstrap/scss/toasts";
@import "../../node_modules/bootstrap/scss/modal";
@import "../../node_modules/bootstrap/scss/tooltip";
@import "../../node_modules/bootstrap/scss/popover";
@import "../../node_modules/bootstrap/scss/carousel";
@import "../../node_modules/bootstrap/scss/spinners";
@import "../../node_modules/bootstrap/scss/offcanvas";
@import "../../node_modules/bootstrap/scss/placeholders";
@import "../../node_modules/bootstrap/scss/helpers";
@import "../../node_modules/bootstrap/scss/utilities/api";

//自訂樣式
@import "custom"

  1. 重要補充:如果sass出現,關於變數等等的警告錯誤,請將sass版本降低到@1.77.6

More info: https://sass-lang.com/d/color-functions

More info: https://sass-lang.com/d/mixed-declså

$ npm i sass@1.77.6


其他相關