yezitoo 2020-06-13
一、你需要准备什么
初始化 npm
npm init -y
F:\web\001js_learn\jQuery\06_commonjs>npm init -y
Wrote to F:\web\001js_learn\jQuery\06_commonjs\package.json:
{
"name": "06_commonjs",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}npm install --save-dev webpack


截止目前,我们就完成了webpack的本地安装,我们来看看我们的项目接口;

我们发现了3个不是我们创建的目录/文件,其中package.json是我们初始化npm是自动生成,另外两个使我们本地安装webpack时自动生成;
接下来,我们随着一个小案例,来学习如何使用webpack打包js文件;
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="bundle.js">
</script>
</body>
</html>main.js
1 var s = require(‘./s‘); 2 console.log(s.s_circle(10));
math.js
var PI = 3.14;
function multile(num1,num2){
return num1 * num2;
}
function square(n){
return n * n;
}
module.exports = {
PI: PI,
multile:multile,
square:square
}s.js
var m =require(‘./math‘);
function circle(r){
return m.multile(m.square(r),m.PI);
}
module.exports = {
s_circle:circle
}通过webpack打包就可以
webpack app.js --output-filename bundle.js --output-path . --mode development


生成了文件bundle.js