Skip to content

Commit 1545dd6

Browse files
author
yangfan19
committed
webpack-10
1 parent 8c97124 commit 1545dd6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+20478
-1
lines changed

webpack-08/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ loader不能做的事情Plugin都能干
4343

4444
## url-loader 和file-loader的区别?
4545

46-
首先url-loader底层调取也是file-loader,小于limit字节的文件会被转为DataURl,文件大小大于limit,url-loader会调用file-loader进行处理,参数也会直接传给file-loader
46+
首先url-loader底层调取也是file-loader,小于limit字节的文件会被转为DataURl,文件大小大于limit,url-loader会调用file-loader进行处理,参数也会直接传给file-loader,这也是前端优化的构建的重要一环
4747

4848

webpack-09/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

webpack-09/.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/idea
6+
/dist
7+
/es
8+
/lib
9+
10+
# misc
11+
.idea
12+
.vscode
13+
.DS_Store
14+
15+
16+
npm-debug.log*
17+
yarn-debug.log*
18+
yarn-error.log*

webpack-09/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# webpack-09
2+
3+
## webpack文件监听
4+
5+
```shell
6+
1、启动webpack 命令时,带上 --watch 参数
7+
2、在配置webpack.config.js 中设置 watch:true
8+
```
9+
10+
执行以下的命令、生成的文件在dist文件夹中 自己手动创建index.html 进行引入search.js
11+
```shell
12+
npm run build
13+
```
14+
15+
### 需要注意:
16+
watch虽然能动态的监听文件的变化、但是浏览器的更新需要手动刷新才能生效
17+
18+
19+

webpack-09/package-lock.json

+5,906
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webpack-09/package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "webpack-01",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "webpack",
9+
"watch": "webpack --watch"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"devDependencies": {
15+
"@babel/core": "^7.13.10",
16+
"@babel/preset-env": "^7.13.10",
17+
"@babel/preset-react": "^7.12.13",
18+
"babel-loader": "^8.2.2",
19+
"css-loader": "^5.1.2",
20+
"file-loader": "^6.2.0",
21+
"less": "^4.1.1",
22+
"less-loader": "^5.0.0",
23+
"react": "^17.0.1",
24+
"react-dom": "^17.0.1",
25+
"style-loader": "^2.0.0",
26+
"url-loader": "^4.1.1",
27+
"webpack": "^4.31.0",
28+
"webpack-cli": "^3.3.2"
29+
}
30+
}

webpack-09/src/fonts/KsoTouryu_5.otf

22.3 MB
Binary file not shown.
134 KB
Binary file not shown.

webpack-09/src/helloWorld.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function helloworld() {
2+
return 'Hello webpack';
3+
}
134 KB
Binary file not shown.

webpack-09/src/images/im1.jpg

34.1 KB
Loading

webpack-09/src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { helloworld } from './helloWorld';
2+
3+
document.write(helloworld());

webpack-09/src/search.css

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@font-face {
2+
font-family: 'KsoTouryu_5';
3+
src: url('./fonts/KsoTouryu_5.otf') format('truetype');
4+
}
5+
6+
.box {
7+
font-size: 100px;
8+
color: red;
9+
font-family: 'KsoTouryu_5';
10+
}

webpack-09/src/search.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
import React from 'react';
4+
import ReactDOM from 'react-dom';
5+
6+
// import './search.css';
7+
import './search01.less';
8+
import logo from './images/im1.jpg';
9+
10+
const Search = () => {
11+
return (
12+
<div className='box'>
13+
我是search组件 哈哈哈哈哈
14+
<img src={logo} />
15+
</div>
16+
);
17+
};
18+
19+
ReactDOM.render(<Search />, document.getElementById('root'));

webpack-09/src/search01.less

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@font-face {
2+
font-family: 'Ranchers-Regular';
3+
src: url('./fonts/Ranchers-Regular.ttf') format('truetype');
4+
}
5+
.box {
6+
font-size: 200px;
7+
color: blue;
8+
font-family: 'Ranchers-Regular';
9+
}

webpack-09/webpack.config.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
module.exports = {
6+
entry: {
7+
// 入口文件可以用对象的形式来写
8+
index: './src/index.js',
9+
search: './src/search.js',
10+
},
11+
output: {
12+
path: path.join(__dirname, 'dist'),
13+
filename: '[name].js', // 多个入口的情况下 不知道对应的名称、可以用占位符来指定[name]
14+
},
15+
mode: 'production',
16+
module: {
17+
rules: [
18+
{
19+
test: /.js$/,
20+
use: 'babel-loader',
21+
},
22+
{
23+
test: /.css$/, // 配置css的后缀名
24+
use: ['style-loader', 'css-loader'], //tips:执行的顺序是右到左的
25+
},
26+
{
27+
test: /.less$/, // 配置less的后缀名
28+
use: ['style-loader', 'css-loader', 'less-loader'], //tips:执行的顺序是右到左的
29+
},
30+
{
31+
test: /.(png|jpg|gif|jpeg)$/,
32+
use: [
33+
{
34+
loader: 'url-loader',
35+
options: {
36+
limit: 102400, // 100k
37+
},
38+
},
39+
],
40+
},
41+
{
42+
test: /.(woff|woff2|eot|ttf|otf)$/,
43+
use: 'file-loader',
44+
},
45+
],
46+
},
47+
};

webpack-10/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

webpack-10/.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/idea
6+
/dist
7+
/es
8+
/lib
9+
10+
# misc
11+
.idea
12+
.vscode
13+
.DS_Store
14+
15+
16+
npm-debug.log*
17+
yarn-debug.log*
18+
yarn-error.log*

webpack-10/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# webpack-10
2+
3+
## 热更新:webpack-dev-server
4+
5+
6+
执行以下的命令、生成的文件在dist文件夹中 自己手动创建index.html 进行引入search.js
7+
```shell
8+
npm run build
9+
```
10+
11+
## 热更新原理
12+
1、webpack complie :将JS编译成Bundle
13+
2、HMR server: 将热更新文件输出给HMR Runtime
14+
3、Bundle server : 提供文件在浏览器访问
15+
4、HMR Runtime :会注入到浏览器更新文件的变化
16+
5、bundle.js 构建输出文件
17+
18+
19+
## 热更新好处
20+
21+
热更新一般是在开发中使用调试的,输入的文件放在内存、跟watch的区别就是 watch监听是放在电脑磁盘中的、所以从构建的速度上来看的话是有优势的
22+
23+
## 需要注意:
24+
25+
webpack-dev-server 需要手动的安装依赖

0 commit comments

Comments
 (0)