🌙 Webpack学习笔记之基础
🌙 1.初识webpack
webpack默认配置文件:
webpack.config.js
可以通过webpack --config 文件名
指定配置文件。webapck配置组成:
module.exports = { entry: './src/index.js', ................. 打包的入口文件(默认'./src/index.js') output: './dist/main.js',..................打包的输出文件(默认'./dist/main.js') mode: 'production',........................环境(开发、生产) module: { rules: [...............................Loader配置文件 {test: /\.txt$/, use: 'raw-loader'} ] }, plugins: [.................................插件配置 new HtmlwebpackPlugin({ template: './src/index.html' }) ] }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
🌙 2.安装webpack
先安装
nodejs
和nvm
(下载或配置node版本)全局安装webpack
npm install webpack webpack-cli --savee-dev -g webpack -v
1
2
3创建文件
mkdir webpack-demo cd webpack-demo
1
2初始化
npm init -y npm install webpack webpack-cli --savee-dev
1
2
🌙 3.小试牛刀
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'
},
};
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
package.json
{
"name": "webpeck-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.43.0",
"webpack-cli": "^4.0.0-beta.2"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
定义index.js
,安装依赖之后,执行命令``npm run build之后会在
src/bundle.js`中生成打包之后的js文件。