美文网首页
webpack 学习笔记

webpack 学习笔记

作者: _旁观者_ | 来源:发表于2019-04-01 18:19 被阅读0次

1 安装环境

npm install --save-dev webpack
npm install --save-dev webpack-cli

2 npm 初始化

npm init -y
npm install webpack webpack-cli --save-dev

3 修改package.json文件

  • 设置 "private": true 使 安装包私有的(private)
  • script 添加 "build": "webpack"
    可以使项目打包时 自动查找 webpack.config.js 文件, 有的话就进行打包
{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0"
  },
  "dependencies": {
    "lodash": "^4.17.10"
  }
}
  • 运行 npm run build 打包

处理 css文件 js 文件 图片等资源


基本的webpack 只能处理 js 文件, 但可以通过在 webpack.config.js 配置 module 来实现对 css js 图片的处理

  • 安装 x-loader 文件
    加载 css文件
npm install --save-dev style-loader css-loader

加载图片

npm install --save-dev file-loader

加载数据

npm install --save-dev csv-loader xml-loader

webpack.config.js配置如下

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.css$/, //css处理
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.(png|svg|jpg|gif)$/,  //图片处理
        use: [
          'file-loader'
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,  //字体处理
        use: [
          'file-loader'
        ]
      },
      // 数据处理
      {
        test: /\.(csv|tsv)$/,
        use: [
          'csv-loader'
        ]
      },
      {
        test: /\.xml$/,
        use: [
          'xml-loader'
        ]
      }
    ]
  }
};

资源管理


  • HtmlWebpackPlugin插件介绍

    • 可以把webpack.config.js 中的 entry 文件处理生成 一个以 xx.html 开头的文件
  • clean-webpack-plugin --save-dev 插件介绍

    • 每次打包都会先删除以前存在的打包资源,重新打包新的资源
  • 安装 插件

npm install --save-dev html-webpack-plugin
npm install clean-webpack-plugin --save-dev
  • webpack.config.js 配置 plugins
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  entry: {
    app: './src/index.js',
    print: './src/print.js'
  },
  plugins: [
    new CleanWebpackPlugin(['dist']), // 删除已存在的打包资源
    new HtmlWebpackPlugin({ // 将资源打包成一个以 'Output Management'开头的html文件
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};

开发


  • 通过 source map 可以定位错误位置
    • webpack 是通过 devtool 来实现定位代码错误位置的
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  entry: {
    app: './src/index.js',
    print: './src/print.js'
  },
  devtool: 'inline-source-map',
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  }
};
  • 使用 webpack-dev-server 实时重新加载
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  entry: {
    app: './src/index.js',
    print: './src/print.js'
  },
  devtool: 'inline-source-map',
+ devServer: {
+    contentBase: './dist'
+  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      title: 'Output Management'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  }
};

以上配置告知 webpack-dev-server,在 localhost:8080 下建立服务,将 dist 目录下的文件,作为可访问文件。

  • 添加一个 script 脚本
    package.json 文件配置
{
    "name": "development",
    "version": "1.0.0",
    "description": "",
    "main": "webpack.config.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
+     "start": "webpack-dev-server --open",
      "build": "webpack"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
      "clean-webpack-plugin": "^0.1.16",
      "css-loader": "^0.28.4",
      "csv-loader": "^2.1.1",
      "file-loader": "^0.11.2",
      "html-webpack-plugin": "^2.29.0",
      "style-loader": "^0.18.2",
      "webpack": "^3.0.0",
      "xml-loader": "^1.2.1"
    }
  }
  • 然后可以运行 npm run start

模块热替换


  • 修改 webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');

module.exports = {
  entry: {
    app: './src/index.js'
  },
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './dist',
+    hot: true // https://www.webpackjs.com/configuration/dev-server/#devserver-hot
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      title: 'Hot Module Replacement'
    }),
+    new webpack.NamedModulesPlugin(),//用于启动HMR时可以显示模块的相对路径
+    new webpack.HotModuleReplacementPlugin() //hot module replacement 启动模块热替换的插件
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};
  • 入口文件添加 条件设置
+ if (module.hot) {
+   module.hot.accept('./print.js', function() {
+     更新代码
+   })
+ }

官网上配置的,好像不写也能运行

  • npm run start

个人github

相关文章

网友评论

      本文标题:webpack 学习笔记

      本文链接:https://www.haomeiwen.com/subject/qrpvbqtx.html