<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

          使用 VSCode 開發 Gatsby 項目配置

          2020-6-3    seo達人

          初始化

          使用 https://github.com/XYShaoKang... 作為基礎模板

          gatsby new gatsby-project-config https://github.com/XYShaoKang/gatsby-hello-world

          Prettier 配置

          安裝 VSCode 擴展

          按 Ctrl + P (MAC 下: Cmd + P) 輸入以下命令,按回車安裝


          ext install esbenp.prettier-vscode

          安裝依賴

          yarn add -D prettier

          Prettier 配置文件.prettierrc.js

          // .prettierrc.js

          module.exports = {

           trailingComma: 'es5',

           tabWidth: 2,

           semi: false,

           singleQuote: true,

           endOfLine: 'lf',

           printWidth: 50,

           arrowParens: 'avoid',

          }

          ESLint 配置

          安裝 VSCode 擴展

          按 Ctrl + P (MAC 下: Cmd + P) 輸入以下命令,按回車安裝


          ext install dbaeumer.vscode-eslint

          安裝 ESLint 依賴

          yarn add -D eslint babel-eslint eslint-config-google eslint-plugin-react eslint-plugin-filenames

          ESLint 配置文件.eslintrc.js

          使用官方倉庫的配置,之后在根據需要修改


          // https://github.com/gatsbyjs/gatsby/blob/master/.eslintrc.js

          // .eslintrc.js

          module.exports = {

           parser: 'babel-eslint',

           extends: [

             'google',

             'eslint:recommended',

             'plugin:react/recommended',

           ],

           plugins: ['react', 'filenames'],

           parserOptions: {

             ecmaVersion: 2016,

             sourceType: 'module',

             ecmaFeatures: {

               jsx: true,

             },

           },

           env: {

             browser: true,

             es6: true,

             node: true,

             jest: true,

           },

           globals: {

             before: true,

             after: true,

             spyOn: true,

             __PATH_PREFIX__: true,

             __BASE_PATH__: true,

             __ASSET_PREFIX__: true,

           },

           rules: {

             'arrow-body-style': [

               'error',

               'as-needed',

               { requireReturnForObjectLiteral: true },

             ],

             'no-unused-expressions': [

               'error',

               {

                 allowTaggedTemplates: true,

               },

             ],

             'consistent-return': ['error'],

             'filenames/match-regex': [

               'error',

               '^[a-z-\\d\\.]+$',

               true,

             ],

             'no-console': 'off',

             'no-inner-declarations': 'off',

             quotes: ['error', 'backtick'],

             'react/display-name': 'off',

             'react/jsx-key': 'warn',

             'react/no-unescaped-entities': 'off',

             'react/prop-types': 'off',

             'require-jsdoc': 'off',

             'valid-jsdoc': 'off',

           },

           settings: {

             react: {

               version: '16.4.2',

             },

           },

          }

          解決 Prettier ESLint 規則沖突

          推薦配置


          安裝依賴


          yarn add -D eslint-config-prettier eslint-plugin-prettier

          在.eslintrc.js中的extends添加'plugin:prettier/recommended'


          module.exports = {

           extends: ['plugin:prettier/recommended'],

          }

          VSCode 中 Prettier 和 ESLint 協作

          方式一:使用 ESLint 擴展來格式化代碼

          配置.vscode/settings.json


          // .vscode/settings.json

          {

           "eslint.format.enable": true,

           "[javascript]": {

             "editor.defaultFormatter": "dbaeumer.vscode-eslint"

           },

           "[javascriptreact]": {

             "editor.defaultFormatter": "dbaeumer.vscode-eslint"

           }

          }

          ESLint 擴展會默認忽略.開頭的文件,比如.eslintrc.js

          如果需要格式化.開頭的文件,可以在.eslintignore中添加一個否定忽略來啟用對應文件的格式化功能.


          !.eslintrc.js

          或者直接使用!.*,這樣可以開啟所有點文件的格式化功能


          方式二:使用 Prettier 擴展來格式化代碼

          在版prettier-vscode@v5.0.0中已經刪除了直接對linter的集成,所以版沒法像之前那樣,通過prettier-eslint來集成ESLint的修復了(一定要這樣用的話,可以通過降級到prettier-vscode@4來使用了).如果要使用Prettier來格式化的話,就只能按照官方指南中的說的集成方法,讓Prettier來處理格式,通過配置在保存時使用ESlint自動修復代碼.只是這樣必須要保存文件時,才能觸發ESLint的修復了.


          配置 VSCode 使用 Prettier 來格式化 js 和 jsx 文件

          在項目中新建文件.vscode/settings.json


          // .vscode/settings.json

          {

           "[javascript]": {

             "editor.defaultFormatter": "esbenp.prettier-vscode"

           },

           "[javascriptreact]": {

             "editor.defaultFormatter": "esbenp.prettier-vscode"

           },

           "editor.codeActionsOnSave": {

             "source.fixAll.eslint": true

           }

          }

          說實話這個體驗很糟糕,之前直接一鍵格式化代碼并且修復 ESLint 錯誤,可以對比格式化之前和格式化之后的代碼,如果感覺不對可以直接撤銷更改就好了.現在必須要通過保存,才能觸發修復 ESlint 錯誤.而在開發過程中,通過監聽文件改變來觸發熱加載或者重新編譯是很常見的操作.這樣之后每次想要去修復 ESLint 錯誤,還是只是想看看修復錯誤之后的樣子,都必須要去觸發熱加載或重新編譯,每次操作的成本就太高了.

          我更推薦第一種方式使用 ESLint 擴展來對代碼進行格式化.


          調試 Gatsby 配置

          調試構建過程

          添加配置文件.vscode/launch.json


          // .vscode/launch.json

          {

           // 使用 IntelliSense 了解相關屬性。

           // 懸停以查看現有屬性的描述。

           // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387

           "version": "0.2.0",

           "configurations": [

             {

               "name": "Gatsby develop",

               "type": "node",

               "request": "launch",

               "protocol": "inspector",

               "program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby",

               "args": ["develop"],

               "stopOnEntry": false,

               "runtimeArgs": ["--nolazy"],

               "sourceMaps": false,

               "outputCapture": "std"

             }

           ]

          }

          的gatsby@2.22.*版本中調試不能進到斷點,解決辦法是降級到2.21.*,yarn add gatsby@2.21.40,等待官方修復再使用版本的

          調試客戶端

          需要安裝 Debugger for Chrome 擴展


          ext install msjsdiag.debugger-for-chrome

          添加配置文件.vscode/launch.json


          // .vscode/launch.json

          {

           // 使用 IntelliSense 了解相關屬性。

           // 懸停以查看現有屬性的描述。

           // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387

           "version": "0.2.0",

           "configurations": [

             {

               "type": "chrome",

               "request": "launch",

               "name": "Gatsby Client Debug",

               "url": "http://localhost:8000",

               "webRoot": "${workspaceFolder}"

             }

           ]

          }

          先啟動 Gatsby,yarn develop,然后按 F5 開始調試.

          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合