webpack-清理 /dist 文件夹

本文详细介绍Webpack的基本配置与使用流程,包括环境搭建、依赖安装、代码组织及自动化构建过程。通过具体示例,演示如何利用Webpack整合项目资源,实现模块化开发,以及热更新和代码优化等高级功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.环境准备和命令

npm init -y
npm install --save-dev webpack
npm install --save-dev webpack-cli
npm install --save lodash
npm install --save-dev html-webpack-plugin
npm install clean-webpack-plugin --save-dev
npm run build

2.目录

3.代码文件 

dist/index.html(这个文件是自动生成的)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Output Management</title>
  </head>
  <body>
  <script type="text/javascript" src="app.bundle.js"></script>
  <script type="text/javascript" src="print.bundle.js"></script>
</body>
</html>

src/index.js 

import _ from 'lodash';
import printMe from './print.js';

function component() {
  var element = document.createElement('div');
  var btn = document.createElement('button');

  // Lodash(目前通过一个 script 脚本引入)对于执行这一行是必需的
  element.innerHTML = _.join(['Hello', 'webpack'], ' ');

  btn.innerHTML = 'Click me and check the console!';
  btn.onclick = printMe;
  element.appendChild(btn);

  return element;
}

document.body.appendChild(component());

 src/print.js

export default function printMe() {
    //console.log('I get called from print.js!');
    alert('I get called from print.js!');
  }

 package.json

{
  "name": "webpack-out",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "lodash": "^4.17.15"
  }
}

 webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
//const CleanWebpackPlugin = require('clean-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

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

4.运行效果

 

5.代码分析

 

6 npm run build 

aders/next-font-loader/index.js??ruleSet[1].rules[13].oneOf[2].use[2]!/app/node_modules/next/font/google/target.css?{"path":"src/app/layout.tsx","import":"Geist_Mono","arguments":[{"variable":"--font-geist-mono","subsets":["latin"]}],"variableName":"geistMono"}:1:7) 53.43 at /app/node_modules/next/dist/compiled/webpack/bundle5.js:29:962742 53.43 at Hook.eval [as call] (eval at create (/app/node_modules/next/dist/compiled/webpack/bundle5.js:14:9002), <anonymous>:7:1) 53.43 at /app/node_modules/next/dist/compiled/webpack/bundle5.js:29:131581 53.43 at tryRunOrWebpackError (/app/node_modules/next/dist/compiled/webpack/bundle5.js:29:316096) 53.43 at __webpack_require_module__ (/app/node_modules/next/dist/compiled/webpack/bundle5.js:29:131548) 53.43 at __nested_webpack_require_161494__ (/app/node_modules/next/dist/compiled/webpack/bundle5.js:29:130983) 53.43 at /app/node_modules/next/dist/compiled/webpack/bundle5.js:29:131840 53.43 at symbolIterator (/app/node_modules/next/dist/compiled/neo-async/async.js:1:14444) 53.43 at done (/app/node_modules/next/dist/compiled/neo-async/async.js:1:14824) 53.43 53.43 Generated code for /app/node_modules/next/dist/build/webpack/loaders/css-loader/src/index.js??ruleSet[1].rules[13].oneOf[2].use[1]!/app/node_modules/next/dist/build/webpack/loaders/next-font-loader/index.js??ruleSet[1].rules[13].oneOf[2].use[2]!/app/node_modules/next/font/google/target.css?{"path":"src/app/layout.tsx","import":"Geist_Mono","arguments":[{"variable":"--font-geist-mono","subsets":["latin"]}],"variableName":"geistMono"} 53.43 53.48 53.48 > Build failed because of webpack errors ------ failed to solve: process "/bin/sh -c npm run build" did not complete successfully: exit code: 1 [ERROR] Docker镜像构建失败,请检查错误信息
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值