1.1 webpack打包入门

一、安装nodejs的环境

二、新建项目

1、在开发工具中新建项目和目录

2、在开发工具的终端执行初始化生成package.json

npm init -y

3、安装yarn

npm install  yarn -g

4、安装webpack

yarn add webpack

5、在js目录下和html目录先增加js和html文件

a.js

document.write("hello webpack");

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>

6、项目根目录下新建webpack配置文件

webpack.config.js

let path = require('path');
let HTMLWebpackPlugin = require('html-webpack-plugin');
let { CleanWebpackPlugin } = require("clean-webpack-plugin");

module.exports = {
    mode: 'development',  // 模式 模式有两种  production development
    entry: './src/js/a.js', // 入口
    output: {
        filename: "bundle.[hash:8].js", // 打包后的文件名
        path: path.resolve(__dirname, 'dist'), // 路径必须是绝对路径
    },
    plugins: [  // 放着所有的webpack插件
        new HTMLWebpackPlugin({
            template: "./src/html/index.html",
            filename: "index.html",
            minify: {
                removeAttributeQuotes: true,	// 移除引号
                collapseWhitespace: true,	// 去除换行
            },
            hash: true
        }),
        new CleanWebpackPlugin(),//清理dist文件夹

    ],


};

7、webpack.config.js配置文件中有2个插件要安装

yarn add html-webpack-plugin  用来打包html文件

yarn add clean-webpack-plugin  用来清除dist中的文件

8、打包

执行webpack,生成dist文件夹,webpack版本高的话,还需要安装webpack cli

9、打包后的效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值