webpack语法分析

在编写js代码的时候容易犯语法错误,IDE提供比较好的语法分析检测工具

js语法分析简明史

JSLint->JSHint->ESLint

webpack中的JSHint(略)

设置ESLint

与另外相比,它允许自定义规则

安装npm i eslint --save-dev

  1. // package.json文件
  2. "scripts": {
  3. ...
  4. "test:lint": "eslint . --ext .js --ext .jsx --cache"
  5. }
  6. ...

执行npm run test:lint

如果要排除某些文件检测,可以在根目录下创建.eslintignore文件,当然在package.json中指定也是可以的

  1. "scripts": {
  2. ...
  3. "test:lint": "eslint . --ext .js --ext .jsx --ignore-path .gitignore --cache"
  4. }
  5. ...

配置ESLint,配置文件为.eslintrc更多配置规则

  1. {
  2. // Extend existing configuration
  3. // from ESlint and eslint-plugin-react defaults.
  4. "extends": [
  5. "eslint:recommended", "plugin:react/recommended"
  6. ],
  7. // Enable ES6 support. If you want to use custom Babel
  8. // features, you will need to enable a custom parser
  9. // as described in a section below.
  10. "parserOptions": {
  11. "ecmaVersion": 6,
  12. "sourceType": "module"
  13. },
  14. "env": {
  15. "browser": true,
  16. "node": true
  17. },
  18. // Enable custom plugin known as eslint-plugin-react
  19. "plugins": [
  20. "react"
  21. ],
  22. "rules": {
  23. // Disable `no-console` rule
  24. "no-console": 0,
  25. // Give a warning if identifiers contain underscores
  26. "no-underscore-dangle": 1,
  27. // Default to single quotes and raise an error if something
  28. // else is used
  29. "quotes": [2, "single"]
  30. }
  31. }

还有更多文件类型检测…,通过eslint --init会生成.eslintrc文件

Babel中使用ESLint

安装npm i babel-eslint --save-dev

更改.eslintrc配置文件,注释为删除部分

  1. {
  2. ...
  3. "parser": "babel-eslint",
  4. //"parserOptions": {
  5. // "ecmaVersion": 6,
  6. // "sourceType": "module"
  7. //},
  8. ...
  9. }

报错等级:0:禁用规则,1:警告,2:错误

处理如果ESLint报错信息输出:

  1. "scripts": {
  2. ...
  3. "test:lint": "eslint . --ext .js --ext .jsx || true"
  4. }
  5. ...

Webpack中使用ESLint

安装加载器npm i eslint-loader --save-dev

通过preLoaders设置

  1. // webpack.config.js文件
  2. const common = {
  3. ...
  4. module: {
  5. preLoaders: [
  6. {
  7. test: /\.jsx?$/,
  8. loaders: ['eslint'],
  9. include: PATHS.app
  10. }
  11. ]
  12. },
  13. ...
  14. };

这样如果有语法错误,在构建过程就会中断

定制ESLint

  • 跳过ESLint规则
  1. // 所有
  2. /* eslint-disable */
  3. ...
  4. /* eslint-enable */
  5. // 指定规则
  6. /* eslint-disable no-unused-vars */
  7. ...
  8. /* eslint-enable no-unused-vars */
  9. // 设定规则
  10. /* eslint no-comma-dangle:1 */
  11. // 指定行
  12. alert('foo'); // eslint-disable-line no-alert

设置环境

  1. // .eslintrc
  2. {
  3. "env": {
  4. "browser": true,
  5. "node": true,
  6. "mocha": true
  7. },
  8. ...
  9. }

自定义规则(略)

CSS语法分析

安装npm i stylelint postcss-loader --save-dev

  1. // webpack.config.js文件
  2. ...
  3. const stylelint = require('stylelint');
  4. ...
  5. const common = {
  6. ...
  7. module: {
  8. preLoaders: [
  9. {
  10. test: /\.css$/,
  11. loaders: ['postcss'],
  12. include: PATHS.app
  13. },
  14. ...
  15. ],
  16. ...
  17. },
  18. postcss: function () {
  19. return [
  20. stylelint({
  21. rules: {
  22. 'color-hex-case': 'lower'
  23. }
  24. })
  25. ];
  26. },
  27. ...
  28. }

16进制颜色小写,更多规则

使用:

  1. const configSuitcss = require('stylelint-config-suitcss');
  2. ...
  3. stylelint(configSuitcss)

EditorConfig

用来统一代码风格~~~(放置在根目录下)

  1. // .editorconfig
  2. root = true
  3. # General settings for whole project
  4. [*]
  5. indent_style = space
  6. indent_size = 4
  7. end_of_line = lf
  8. charset = utf-8
  9. trim_trailing_whitespace = true
  10. insert_final_newline = true
  11. # Format specific overrides
  12. [*.md]
  13. trim_trailing_whitespace = false
  14. [app/**.js]
  15. indent_style = space
  16. indent_size = 2

<<上一节:理解chunks >>下一节:编写package