webpack5配置,webpack.config.js

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 用来拼接绝对路径
const {resolve} = require('path');
 
const HtmlWebpackPlugin = require('html-webpack-plugin');
 
module.exports = {
  // 五个核心
  // 1. 入口文件
  entry: './src/main.js',
 
  // 2. 出口
  output: {
    filename: 'static/js/main.js', // 出口文件名,这样可以让js在js文件夹下
    // filename: 'main.js', // 出口文件名
    path: resolve(__dirname, 'dist'), // 出口路径
    clean: true // 自动清空上次打包的内容
  },
 
  // 3. loader的配置
  module: {
    rules: [
      // 详细的loader配置
      {
        test: /\.css$/, // 正则,匹配以.css结尾的文件
        use: [
          // loader执行顺序从下往上
          'style-loader', // 创建style标签,将js中的样式资源插入进去,添加到head中生效
          'css-loader' // 将css文件变成commonjs模块加载到js中,里边内容是样式字符串
        ]
      },
      {
        test: /\.(jpg|png)$/,
        type: 'asset',
        // 这样可以让图片在images文件夹下
        generator: {
          filename: 'static/images/[hash:10][ext][query]'
        }
      },
      {
        test: /\.(ttf|woff|woff2)$/,
        type: 'asset/resource',
        // 这样可以让字体图标在fonts文件夹下
        generator: {
          filename: 'static/fonts/[hash:10][ext][query]'
        }
      }
    ]
  },
 
  // 4. plugins的配置
  plugins: [
    // 打包后创建一个空的html文件(以./src/index.html为模板),会引入打包后的css,js等
    new HtmlWebpackPlugin({
      template: './public/index.html'
    })
  ],
 
  // 5. 模式
  mode: 'development'
}

  

posted @   86727515  阅读(148)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2019-07-12 基于JQUERY BOOTSTRAP 最简单的loading遮罩层
点击右上角即可分享
微信分享提示