loaders

例 加载CSS文件

  1. yarn add css-loader style-loader -D

配置文件

  1. const config = {
  2. entry: './src/index.js',
  3. output: {
  4. filename: 'bundle.js',
  5. path: path.join(__dirname, './dist')
  6. },
  7. module: {
  8. rules: [
  9. {
  10. test: /\.css$/,
  11. use: ['style-loader', 'css-loader']
  12. //处理顺序是从后往前,先使用css-loader处理,然后使用style-loader处理
  13. }
  14. ]
  15. }
  16. }
  17. module.exports = config;

入口文件

  1. require('./index.css');
  2. console.log('hello webpack');

如果想让webpack还能处理scss

  1. yarn add sass-loader sass -D
  2. #这里知道要安装sass是运行webpack报错后,看错误信息知道还需要安装sass

配置文件

  1. const path = require('path');
  2. const config = {
  3. entry: './src/index.js',
  4. output: {
  5. filename: 'bundle.js',
  6. path: path.join(__dirname, './dist')
  7. },
  8. module: {
  9. rules: [
  10. {
  11. test: /\.(scss|sass)$/,
  12. use: ['style-loader', 'css-loader', 'sass-loader']
  13. }
  14. ]
  15. }
  16. }
  17. module.exports = config

入口文件

  1. require('./index.scss');
  2. console.log('hello webpack');

plugins

例如 html-webpack-plugin 的使用

  1. yarn add html-webpack-plugin -D

配置文件

  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const config = {
  4. entry: './src/index.js',
  5. output: {
  6. filename: 'bundle.js',
  7. path: path.join(__dirname, './dist')
  8. },
  9. module: {
  10. rules: [
  11. {
  12. test: /\.(scss|sass)$/,
  13. use: ['style-loader', 'css-loader', 'sass-loader']
  14. }
  15. ]
  16. },
  17. plugins: [
  18. new HtmlWebpackPlugin({
  19. filename: 'index.html', //这是生成的html文件的名字
  20. template: './index.html' //这是引用的html文件的模板
  21. })
  22. ]
  23. }
  24. module.exports = config
  25. /*
  26. 这样,生成的dist目录中除了bundle.js
  27. 还会有一个已经引用了bundle.js文件的html,
  28. 这个html来源于模板index.html
  29. */

现在我们每次修改完都需要运行webpack才能生成新的打包文件,如果能监听变化自动打包就好了
package.json添加新的npm脚本

  1. //...其他代码
  2. "script": {
  3. "build": "webpack",
  4. "watch": "webpack --watch"
  5. }
  6. //...其他代码

这样,每次修改文件都会自动打包生成目标文件,如果修改js能在浏览器里面自动刷新就好了

HMR

  1. yarn add webpack-dev-server

package.json

  1. //...其他代码
  2. "script": {
  3. "build": "webpack",
  4. "watch": "webpack --watch",
  5. "hot": "webpack-dev-server"
  6. }
  7. //...其他代码

webpack.config.js

  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. +const webpack = require('webpack');
  4. const config = {
  5. entry: './src/index.js',
  6. output: {
  7. filename: 'bundle.js',
  8. path: path.join(__dirname, './dist')
  9. },
  10. module: {
  11. rules: [
  12. {
  13. test: /\.(scss|sass)$/,
  14. use: ['style-loader', 'css-loader', 'sass-loader']
  15. }
  16. ]
  17. },
  18. +devServer: {
  19. +contentBase: './dist',
  20. +hot: true
  21. +},
  22. plugins: [
  23. new HtmlWebpackPlugin({
  24. filename: 'index.html',
  25. template: './index.html'
  26. }),
  27. +new webpack.HotModuleReplacementPlugin()
  28. ]
  29. }
  30. module.exports = config