1.创建一个文件夹
2.进入该文件夹,npm init -y
3.npm install webpack webpack-cli --save-dev
4.创建配置文件 webpack.config.js
'use strict'
const path = require('path')
module.exports = {
mode: 'production',
entry: './src/index.js',//入口文件
output: {
path: path.resolve(__dirname, 'dist'),//打包路径
filename: 'bundle.js'//打包文件
}
}
5.创建文件夹src,在src中创建入口文件index.js
6.在package.json的scripts中加入"start": "webpack"
7.打包测试:
1)src中创建文件test.js
export function test(){
return 'Test!!!'
}
2)修改index.js
import {test} from './src/test.js'
alert(test())
3)输入命令行 npm run start;
4)在生成的dist文件夹中添加index.html,引入bundle.js
5)在浏览器打开index.html