webpack
是一个用于现代 JavaScript
应用程序的 静态模块打包工具。当 webpack
处理应用程序时,它会在内部从一个或多个入口点构建一个 依赖图( dependency graph
),然后将你项目中所需的每一个模块组合成一个或多个 bundles
,它们均为静态资源,用于展示你的内容。
纸上得来终觉浅,绝知此事要躬行。此次目标是:创建一个简单的webpack
工程demo
,来理解webpack
的运行机制。最终的工程目录结构如下所示:
一、创建webpack工程
cmd
窗口中,执行命令 npm init -y
,此时会在webpack
目录下生成文件package.json
D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>npm init -y
Wrote to D:\A_MyWork\M05.MYCode\vscode\javascript\webpack\package.json:
{
"name": "webpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>
webpack
目录下,新建src
源代码目录。src
目录下,新建 index.html
、和 index.js
npm install jquery -S
-S
为--save
的简写D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>npm install jquery -S
added 1 package in 2s
D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>
npm install webpack webpack-cli -D
-D
为 --save-dev
的简写@
符指定:npm install [email protected] [email protected] -D
D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>npm install webpack webpack-cli -D
added 117 packages in 6s
D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>
module.exports = {
mode:'development' //指定构建模式,可选值有:production和development
}
package.json
,新增如下代码 "scripts": {
"dev": "webpack"
},
npm run dev
,对项目进行打包构建,此时构建成功之后会在生成dist
目录和main.js
npm run dev
的时候,会先读取 webpack.config.js
配置文件D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>npm run dev
> webpack@1.0.0 dev
> webpack
asset main.js 319 KiB [emitted] (name: main)
runtime modules 937 bytes 4 modules
cacheable modules 279 KiB
./src/index.js 396 bytes [built] [code generated]
./node_modules/jquery/dist/jquery.js 278 KiB [built] [code generated]
webpack 5.88.2 compiled successfully in 312 ms
D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>
index.html
,查看效果.{
"name": "webpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.7.0"
},
"devDependencies": {
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
}
}
module.exports = {
mode:'development'
}
import $ from 'jquery'
$(function () {
$("li:odd").css('background-color','red')
$("li:even").css('background-color','pink')
})
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="../dist/main.js">script>
head>
<body>
<ul>
<li>1li>
<li>2li>
<li>3li>
<li>4li>
<li>5li>
<li>6li>
<li>7li>
<li>8li>
<li>9li>
ul>
body>
html>
二、webpack打包默认值
在webpack
中,有如下默认约定:
src/index.js
dist/main.js
我们,可以在 webpack.config.js
配置文件中,修改默认值:
// 导入node.js中专门操作路径的模块
const path = require("path")
module.exports = {
mode:'development',
// 打包入口的文件路径,__dirname表示当前文件的存放路径,即工程路径
entry:path.join(__dirname,'./src/index.js'),
output: {
// 输出文件的存放路径
path: path.join(__dirname,'./dist'),
// 输出文件的名称
filename: 'bundle.js'
}
}
我们再次执行命令:npm run dev
,此时生成的文件名就变成了bundle.js
D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>npm run dev
> webpack@1.0.0 dev
> webpack
asset bundle.js 319 KiB [emitted] (name: main)
runtime modules 937 bytes 4 modules
cacheable modules 279 KiB
./src/index.js 396 bytes [built] [code generated]
./node_modules/jquery/dist/jquery.js 278 KiB [built] [code generated]
webpack 5.88.2 compiled successfully in 692 ms
D:\A_MyWork\M05.MYCode\vscode\javascript\webpack>