配置文件名称
- webpack默认配置文件:webpackk.config.js
- 可以通过webpack —config指定配置文件
webpack配置组成
module.export = {
entry:'./src/index.js', // 文件打包的入口文件
output: 'dist/main.js', // 打包的输出
mode: 'production', // 环境
module: {
rule: [ // loader 配置
{ test:/\.txt$/, use:'raw-loader' }
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
]
}
零配置的webpack包含的内容
module.export = {
entry:'./src/index.js', // 文件打包的入口文件
output: 'dist/main.js', // 打包的输出
}
webpack安装及配置
创建目录和packgage.json
mkdir my-project
cd my-project
npm init -y
安装webpack和webpack-cli
npm install --save-dev webpack webpack-cli
创建webpack.config.js
module.export = {
mode: 'production', // 环境
entry:'./src/index.js', // 文件打包的入口文件
output: {
path: path.resolve(__dirname,'dist'),
filename: 'bundle.js'
}
}
创建html文件引入打包后的文件 ```html <!DOCTYPE html>
5. 通过npm script 运行webpack
```json
{
"name": "my-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1"
},
}
- 通过npm run build 运行构建
- 原理:模块局部安装会在node_modules/.bin 目录创建软连接