Webpack

7/7/2021

# Description:

Laravel mix giúp Compiling Assets(CSS, JavaScript pre-processors). Tối ưu hóa các file js, css, image,.. Được xây dựng trên Webpack.js.

Thử tích hợp webpack và tailwindcss vào hệ thống.

# Npm Package:

{
  "name": "nin",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "watch": "webpack --watch",
    "start": "webpack-dev-server --open"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^10.2.6",
    "css-loader": "^5.2.6",
    "mini-css-extract-plugin": "^2.1.0",
    "node-sass": "^4.13.1",
    "postcss": "^8.3.5",
    "postcss-loader": "^6.1.1",
    "sass-loader": "^12.1.0",
    "style-loader": "^1.3.0",
    "tailwindcss": "^2.2.4",
    "webpack": "^5.44.0",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3"
  },
  "dependencies": {
    "lodash": "^4.17.15"
  }
}
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

# Webpack config:

const path = require('path');

module.exports = {
    mode: 'production',
    watch: true,
    entry: './resources/js/app.js',
    output: {
        filename: 'app.js',
        path: path.resolve(__dirname, 'public/assets')
    },
    devServer: {
        contentBase: './dist',
    },
    module: {
        rules: [
            {
                test: /\.s[ac]ss$/i,
                use: [
                    // Creates `style` nodes from JS strings
                    'style-loader',
                    // Translates CSS into CommonJS
                    'css-loader',
                    // Compiles Sass to CSS
                    'sass-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                ident: 'postcss',
                                plugins: [
                                    require('tailwindcss'),
                                    require('autoprefixer'),
                                ],
                            },
                        }
                    },
                ],
            },
        ],
    }
};

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

Code javascript tại file /resources/js/app.js và khi run webpack sẽ compile ra file public/assets/app.js

# Resource:

app.js

import "../css/app.scss"

1
2

app.scss

@tailwind base;

@tailwind components;

@tailwind utilities;
1
2
3
4
5

Packagist: https://packagist.org/packages/nin/nin (opens new window)