若是咱们没有使用配置文件webpack.config.js,那么咱们就须要经过命令来打包
php
咱们首先建立一个webpackTest文件夹,而后在文件夹中再建立2个子文件夹dist和srchtml
接着在src文件夹中建立4个文件,分别是info.js、main.js、mathUtils.js、index.html
info和mathUtils是模块化的js文件,main是主入口,index是首页,总体项目结构以下
代码内容以下:webpack
// info.js const height = 180; const weight = 180 export {height, weight}
// mathUtils.js function add(num1, num2) { return num1 + num2 } function mul(num1, num2) { return num1 * num2 } module.exports = { add, mul }
//main.js // 1.CommonJS模块化 const {add, mul} = require('./mathUtils') console.log(add(20, 30)) console.log(mul(50, 80)) // 2.es6模块化 import {height, weight} from "./info"; console.log(height) console.log(weight)
最后咱们来到webpackTest目录下,输入如下命令:es6
webpack ./src/main.js -o ./dist/bundle.js --mode development
结果以下
咱们会发现webpack会将打包的文件放到了咱们指定的dist目录下
最后只须要在index.html中引入打包后的main.js便可web
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script src="./dist/bundle.js/main.js"></script> </body> </html>
咱们访问index首页,查看控制台,就能看到咱们源代码main.js中写的打印日志了
说明使用webpack打包成功了ide