Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
6d1261b3
C
Coding Tree
项目概览
檀越@新空间
/
Coding Tree
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coding Tree
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
6d1261b3
编写于
5月 20, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
1718f365
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
202 addition
and
1 deletion
+202
-1
blog/webpack/index.md
blog/webpack/index.md
+202
-1
未找到文件。
blog/webpack/index.md
浏览文件 @
6d1261b3
...
@@ -1436,4 +1436,205 @@ module.exports = {
...
@@ -1436,4 +1436,205 @@ module.exports = {
```
```
https://www.bilibili.com/video/BV1e7411j7T5?p=15&spm_id_from=pageDriver
\ No newline at end of file
### 5.4、js语法检查eslint
ESLint:
-
[
https://eslint.org/
](
https://eslint.org/
)
-
[
https://github.com/eslint/eslint
](
https://github.com/eslint/eslint
)
Airbnb:
-
[
https://github.com/airbnb/javascript
](
https://github.com/airbnb/javascript
)
-
Airbnb中文版:
[
https://github.com/lin-123/javascript
](
https://github.com/lin-123/javascript
)
-
[
https://www.npmjs.com/package/eslint-config-airbnb-base
](
https://www.npmjs.com/package/eslint-config-airbnb-base
)
```
npm i eslint eslint-config-airbnb-base eslint-plugin-import -D
```
设置检查规则
package.json
```
json
{
"eslintConfig"
:
{
"extends"
:
"airbnb-base"
}
}
```
webpack4
```
cnpm i eslint eslint-loader -D
```
```
js
// webpack.config.js
// webpack配置
module
.
exports
=
{
// loader配置
module
:
{
rules
:
[
{
test
:
/
\.
js$/
,
// 注意:只检查自己写的代码,不检查第三方库
exlude
:
/node_modules/
,
loader
:
'
eslint-loader
'
,
options
:
{
// 自动修复
fix
:
true
}
}
]
},
};
```
webpack5
```
bash
npm
install
eslint eslint-webpack-plugin
--save-dev
```
eslint-webpack-plugin:
-
中文文档:
[
https://webpack.docschina.org/plugins/eslint-webpack-plugin/
](
https://webpack.docschina.org/plugins/eslint-webpack-plugin/
)
-
github:
[
https://github.com/webpack-contrib/eslint-webpack-plugin
](
https://github.com/webpack-contrib/eslint-webpack-plugin
)
```
js
// webpack.config.js
const
ESLintPlugin
=
require
(
"
eslint-webpack-plugin
"
);
// webpack配置
module
.
exports
=
{
// 插件配置
plugins
:
[
// 使用eslint对代码检查
new
ESLintPlugin
({
fix
:
true
,
}),
],
};
```
```
js
// 下一行不进行eslint规则检查
// eslint-disable-next-line
console
.
log
(
'
hi
'
);
```
### 5.5、js兼容性处理
处理方案:
-
1、
`@babel/preset-env`
基本js兼容性处理
-
问题:只能转换基本js语法。如,不能转换高级语法Promise(IE不支持)
-
2、
`@babel/polyfill`
全部js兼容性处理
-
问题:将所有兼容性代码全部引入,体积太大
-
3、
`core-js`
按需加载:只加载需要处理兼容性的语法
方案一:
```
npm i babel-loader @babel/core @babel/preset-env -D
```
```
js
// webpack.config.js
module
.
exports
=
{
module
:
{
rules
:
[
{
// js兼容性处理
test
:
/
\.
js$/
,
exclude
:
/node_modules/
,
loader
:
'
babel-loader
'
,
options
:
{
// 预设,提示babel怎么样做兼容性处理
// 基本的兼容性处理,
presets
:
[
'
@babel/preset-env
'
]
}
}
]
}
}
```
方案二:
```
npm i @babel/polyfill -S
```
使用方式
```
js
// 直接引入即可
import
'
@babel/polyfill
'
;
```
方案三:
```
npm i core-js -D
```
```
js
// webpack.config.js
module
.
exports
=
{
module
:
{
rules
:
[
{
// js兼容性处理
test
:
/
\.
js$/
,
exclude
:
/node_modules/
,
loader
:
'
babel-loader
'
,
options
:
{
// 预设,提示babel怎么样做兼容性处理
// 基本的兼容性处理,
presets
:
[[
'
@babel/preset-env
'
,
{
// 按需加载
useBuiltIns
:
'
usage
'
,
// 指定core-js版本
corejs
:
{
version
:
3
},
// 指定兼容性到哪个版本浏览器
targets
:
{
chrome
:
'
60
'
,
firefox
:
'
60
'
,
ie
:
'
9
'
,
safari
:
'
10
'
,
edge
:
'
17
'
}
}
]]
}
}
]
}
}
```
https://www.bilibili.com/video/BV1e7411j7T5?p=17&spm_id_from=pageDriver
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录