Hướng dẫn html-webpack-plugin - html-webpack-plugin

  • Trang chủ
  • Hướng dẫn học
  • Hướng dẫn học webpack
  • Sắp xếp nội dung HTML - HTMLWebpackPlugin

Cài đặt và setting HtmlWebpackPlugin

  • Plugin
    
    
      
        Webpack - hocwebchuan
      
      
        
        
        
      
    
    7 được dùng để sắp xếp các file html theo một trật tự nhất định, giúp tối ưu nội dung file html hơn.
  • Trước tiên, ta tiến hành cài đặt
    
    
      
        Webpack - hocwebchuan
      
      
        
        
        
      
    
    7 và cấu hình file
    
    
      
        Webpack - hocwebchuan
      
      
        
        
        
      
    
    9 như bên dưới:

npm install --save-dev html-webpack-plugin

Kết quả

Hướng dẫn html-webpack-plugin - html-webpack-plugin

Chỉnh nội dung file webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};

  • Tiến hành chạy lệnh npm để thực thi nội dung trên:
  • Khi này mở file
    
    
      
        
        Output Management
      
      
      
    
    0, chúng ta sẽ thấy nội dung file đã được sắp xếp lại như sau:
TrướcSau khi sắp xếp



  
    Webpack - hocwebchuan
  
  
    
    
    
  



  
    
    Output Management
  
  
  

  • Ta thấy nội dung sau khi được sắp xếp đã được điều chỉnh lại đúng chuẩn HTML hơn, và các đoạn script đã được đưa xuống bên dưới, đảm bảo cho việc chạy scrip sau khi HTML đã được load hoàn toàn.

Các lệnh chính đã sử dụng trong toàn bộ bài học

Bài họcLệnh đã dùng
Cài đặt Webpack

cd Webpack-project
npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli

Webpack bundle

npx webpack

Cấu hình Webpack

npx webpack --config webpack.config.js
npm run build

Webpack quản lý output

npm run build

Cài đặt và setting HtmlWebpackPlugin

npm install --save-dev html-webpack-plugin
npm run build

Hướng dẫn html-webpack-plugin - html-webpack-plugin

Bài hôm nay chúng ta sẽ học về plugin html-webpack-plugin được dùng để sắp xếp các file html theo một trật tự nhất định, giúp tối ưu file html hơn.

1. Cài đặt

Link plugin: https://github.com/jantimon/html-webpack-plugin

Cài cho Webpack 5

npm i --save-dev html-webpack-plugin

Cài cho Webpack 4

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
0

2. Chuẩn bị file

Để bài học được dễ dàng theo dõi chúng ta nên xóa các file trong folder



  
    
    Output Management
  
  
  
1 và thiết lập file theo cấu trúc bên dưới

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
1

3. Cách sử dụng

Plugin sẽ tự động tạo một file HTML, chỉ cần import và thêm plugin vào file cấu hình webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
2

Giờ chạy thử webpack xem thế nào:



  
    
    Output Management
  
  
  
2

Với cấu hình trên webpack sẽ tự động tạo ra một file



  
    
    Output Management
  
  
  
3 chứa các mục sau

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
3

Lưu ý là code trên đã được mình format lại cho dễ nhìn còn thực tế là code HTML đã bị minify lại, loại bỏ tất cả các khoảng trống thừa, bỏ comment, gom các dòng code lại thành một dòng duy nhất, giảm tối đa dung lượng file, tối ưu source code hơn, vân vân và mây mây... Thực tế như này.

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
4

4. Option title

Thay đổi giá trị title của trang HTML như sau

  • Tên thuộc tính:
    
    
      
        
        Output Management
      
      
      
    
    4
  • Kiểu dữ liệu:
    
    
      
        
        Output Management
      
      
      
    
    5
  • Mặc định:
    
    
      
        
        Output Management
      
      
      
    
    6

Ví dụ chúng ta đổi title thành: Webpack từ A đến Á cùng kentrung

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
5

Kết quả

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
6

5. Option filename

Thay đổi tên file HTML được output ra, bạn cũng có thể chỉ định thư mục chứa trang HTML (eg: assets/admin.html)

  • Tên thuộc tính:
    
    
      
        
        Output Management
      
      
      
    
    7
  • Kiểu dữ liệu:
    
    
      
        
        Output Management
      
      
      
    
    5
  • Mặc định:
    
    
      
        
        Output Management
      
      
      
    
    9

Ví dụ chúng ta tạo ra trang:

cd Webpack-project
npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli
0

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
7

6. Option template

Quy định trang mẫu cho trang HTML được output.

  • Tên thuộc tính:
    cd Webpack-project
    npm init -y
    npm install --save-dev webpack
    npm install --save-dev webpack-cli
    1
  • Kiểu dữ liệu:
    
    
      
        
        Output Management
      
      
      
    
    5
  • Mặc định: By default it will use
    cd Webpack-project
    npm init -y
    npm install --save-dev webpack
    npm install --save-dev webpack-cli
    3 if it exists. Please see the docs for details

Ví dụ nếu chúng ta muốn output ra trang

cd Webpack-project
npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli
0 từ trang nguồn
cd Webpack-project
npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli
5

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
8

7. Generating Multiple HTML Files

Để tạo ra được nhiều trang HTML, bạn chỉ cần khai báo thêm giá trị vào trong mảng plugin.

Ví dụ chúng ta tạo ra hai trang



  
    
    Output Management
  
  
  
3 và
cd Webpack-project
npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli
0

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
  /*[name]: [path]*/
    app: './src/index.js',
    print: './src/my-test.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    }
  }
};
9

8. Option chunks

Ở phần trên lúc tạo ra hai trang



  
    
    Output Management
  
  
  
3 và
cd Webpack-project
npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli
0 thì cả hai trang này đều load chung một file
npx webpack
0. Để tách riêng từng trang load từng file JS ta tạo key entry và sử dụng chunks để thêm vào



  
    Webpack - hocwebchuan
  
  
    
    
    
  
0

Với cấu hình trên thì ta có hai trang HTML load hai file JS riêng biệt.

Trang



  
    
    Output Management
  
  
  
3 load file
npx webpack
2 là kết hợp của hai file
npx webpack
3 và
npx webpack
4

Trang

cd Webpack-project
npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli
0 load file
npx webpack
6 là code file
npx webpack
7

9. Custom template

Có bạn hỏi liệu có thể tách code HTML ra thành từng phần nhỏ rồi gọi nó vào được không?

Tại sao lại không nhỉ?

Javascript cũng tách ra các file nhỏ được, css cũng tách ra các file nhỏ được. cũng tách ra các file nhỏ được, css cũng tách ra các file nhỏ được.

=> vậy thì html cũng sẽ làm được. Ngon!Ngon!

Một trong các cách đó là sử dụng html-loader nên các bạn phải cài đặt nó nhé



  
    Webpack - hocwebchuan
  
  
    
    
    
  
1

Ví dụ chúng ta tạo trang

npx webpack
8 và trong này gọi trang
npx webpack
9 thì cấu trúc file như này



  
    Webpack - hocwebchuan
  
  
    
    
    
  
2


  
    Webpack - hocwebchuan
  
  
    
    
    
  
3


  
    Webpack - hocwebchuan
  
  
    
    
    
  
4


  
    Webpack - hocwebchuan
  
  
    
    
    
  
5

Chạy lại webpack



  
    
    Output Management
  
  
  
2 và giờ mở file
npx webpack --config webpack.config.js
npm run build
1 xem kết quả



  
    Webpack - hocwebchuan
  
  
    
    
    
  
6

Quá tuyệt vời phải không? Các bạn thậm chí có thể đặt biến, if-else, vòng lặp, strip tag các kiểu hệt như ngôn ngữ lập trình thực thụ, giờ code HTML cũng đã lên một trình mới rồi nhé.


Bài viết đến đây là hết, hi vọng với bài viết này các bạn đã thêm được nhiều kiến thức bổ ích. Hẹn gặp lại các bạn ở bài viết tiếp theo.

  • Tham khảo HtmlWebpackPlugin
  • Source code github
  • Series webpack
  • Liên hệ: trungnt256