• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

vue3+ts学习八webpack插件的使用

武飞扬头像
方沐雨
帮助1

CleanWebpackPlugin:重新打包时清空打包目录的插件

npm install clean-webpack-plugin -D

// webpack.config.js
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
    plugins: [
        new CleanWebpackPlugin(),
    ],
}

HtmlWebpackPlugin:打包目录下生成html模板的插件

DefinePlugin:为模板定义可使用的变量,webpack内置不需要下载

npm install html-webpack-plugin -D

// 模板放置在根目录下public文件夹内
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  // BASE_URL需要使用webpack内置的DefinePlugin插件定义变量
  <link rel="icon" href="<%= BASE_URL %>favicon.ico">
  // 这里需要在htmlWebpackPlugin的配置项配置对应的属性
  <title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
  <noscript>
    <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly
    without JavaScript enabled. Please enable it to continue .
    </strong>
    <div id="app"></div>
  </noscript>
</body>
</html>
//webpack.config.js
// 打包目录生成html入口文件
const HtmlWebpackPlugin = require('html-webpack-plugin')
// 允许在编译时创建全局常量,用<%= %>接收,webpack内置的 不需要单独安装
const { DefinePlugin } = require('webpack');

plugins: [
    new HtmlWebpackPlugin({
      template: "./public/index.html",
      title: "这是我的标题"
    }),
    new DefinePlugin({
      // 双引号里面是变量名 如果要传字符串则需要再加个单引号
      BASE_URL: "'./'"
    })
  ],

CopyWebpackPlugin:复制文件的插件,主要是将public的部分文件复制到打包后的目录下,例如favicon.ico npm install copy-webpack-plugin -D

//webpack.config.js
// 复制文件插件
const CopyWebpackPlugin = require('copy-webpack-plugin')
plugins: [
    new CopyWebpackPlugin({
      patterns : [
        {
          from:"public",
          to:"./",   // 可以不配置,默认是在打包的根目录下
          globOptions:{
            ignore:["**/index.html"]  // **/是代表该文件夹下的所有index.html都忽略
          }
        }
      ]
    })
  ],

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgiihek
系列文章
更多 icon
同类精品
更多 icon
继续加载