安装
npm install webpack -g
npm install webpack-cli -g
使用
新建一个文件夹 命名为webpack-study
用idea打开
新建文件夹modules
新建文件hello.js
//暴露方法
exports.sayhi=function () {
document.write("<h1>hello</h1>");
}
新建文件main.js
//引入hello.js
let require1 = require("./hello");
require1.sayhi();
新建webpack.config.js
module.exports={
//程序入口
entry:"./modules/main.js",
output:{
//文件输出地址
filename:"./js/bundle.js"
}
};
调用
在终端输入webpack 进行打包后即会生成dist/js/bundle.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="./dist/js/bundle.js"></script>
</body>
</html>