目錄
一、設定eslint
二、設定babel
三、設定postcss
總結
首頁 web前端 js教程 Vue前端架構學習(二)

Vue前端架構學習(二)

Feb 02, 2018 pm 01:56 PM
前端 學習 架構

本文我們將接著上篇Vue前端架構學習(一)來完成設定eslint、babel、postcss,希望能幫助大家。

一、設定eslint

我們採用eslint --init的方式來建立eslintrc.js。
對了,前提我們需要全域安裝eslint:npm i -g eslint
安裝完全局eslint以後,我們在專案根目錄使用eslint --init,我選擇自訂的方式來規定eslint規則:

1

2

3

4

5

6

7

8

9

10

11

12

13

➜  vue-construct git:(master) ✗ eslint --init

? How would you like to configure ESLint? Answer questions about your style

? Are you using ECMAScript 6 features? Yes

? Are you using ES6 modules? Yes

? Where will your code run? Browser, Node

? Do you use CommonJS? Yes

? Do you use JSX? No

? What style of indentation do you use? Spaces

? What quotes do you use for strings? Single

? What line endings do you use? Unix

? Do you require semicolons? No

? What format do you want your config file to be in? (Use arrow keys)

❯ JavaScript

登入後複製

當然,你可以按照自己喜歡,選擇自己想要的方式,例如How would you like to configure ESLint? 這個問題的時候,可以選擇popular的規則,有Google、standard等規則,選擇你想要的就好。

我po下我的設定:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

// 创建这个文件的话,本王推荐用eslint --init创建

module.exports = {

    "env": {

        "browser": true,

        "node": true

    },

    // https://stackoverflow.com/questions/38296761/how-to-support-es7-in-eslint

    // 为了让eslint支持es7或更高的语法

    "parser"'babel-eslint',

    "extends""eslint:recommended",

    "parserOptions": {

        "sourceType""module"

    },

    "plugins": [

        // https://github.com/BenoitZugmeyer/eslint-plugin-html

        // 支持 *.vue lint

        "html"

    ],

    // https://eslint.org/docs/rules/

    "rules": {

        "indent": [

            "error",

            2

        ],

        "linebreak-style": [

            "error",

            "unix"

        ],

        "quotes": [

            "error",

            "single"

        ],

        "semi": [

            "error",

            "never"

        ],

        // https://eslint.org/docs/user-guide/configuring#using-configuration-files

        // "off" or 0 - turn the rule off

        // "warn" or 1 - turn the rule on as a warning (doesn’t affect exit code)

        // "error" or 2 - turn the rule on as an error (exit code is 1 when triggered)

        'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,

        'no-console': 0,

    }

};

登入後複製

二、設定babel

建立.babelrc文件,直接上設定:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

{

  "presets": [

    [

      "env",

      {

        "targets": {

          "browsers": [

            "> 1%",

            "last 2 versions",

            "ie >= 10"

          ]

        },

        "modules": false,

        "useBuiltIns": true

      }

    ]

  ],

  "plugins": [

    "transform-object-rest-spread",

    "syntax-dynamic-import"

  ]

}

登入後複製

配合webpack配置:

1

2

3

4

5

6

7

8

{

    test: /\.js$/,

    include: [resolve('app')],

    use: [

      'babel-loader',

      'eslint-loader'

    ]

},

登入後複製

我們使用的是babel-preset-env,我們知道,babel只是轉譯了高級語法,例如lambda,class,async等,並不會支援高級的api,所以需要babel-polyfill的幫忙。方便的是,我們只需要"useBuiltIns": true,然後npm安裝babel-polyfill,再在webpack配置中的entry帶上babel-polyfill就好了。

babel-preset-env的優點:

  1. 透過targets來決定支援到那個哪些版本的語法就夠了,不會過渡轉譯,可控性強

  2. 透過useBuiltIns來支援babel-polyfill的按需加載,而不是一口氣把整個包打入,因為其實我們只用到了很小一部分

transform-object-rest-spread是為了支援const a = {name: kitty, age: 7}; const b = { ... a }這種es7語法。

syntax-dynamic-import是為了支援const Home = () => import('../views/home')這種語法,達到按需分割、載入的目的。

三、設定postcss

建立postcss.config.js文件,上設定:

1

2

3

4

5

6

7

8

9

10

11

module.exports = {

  plugins: [

    require('autoprefixer')

  ],

  // 配置autoprefix

  browsers: [

    "> 1%",

    "last 2 versions",

    "ie >= 10"

  ]

}

登入後複製

總結

這篇不多,就做了三件事,eslint、babel、postcss。

相關推薦:

Vue前端架構學習(一)


##

以上是Vue前端架構學習(二)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Spring Data JPA 的架構和工作原理是什麼? Spring Data JPA 的架構和工作原理是什麼? Apr 17, 2024 pm 02:48 PM

Spring Data JPA 的架構和工作原理是什麼?

1.3ms耗時!清華最新開源行動裝置神經網路架構 RepViT 1.3ms耗時!清華最新開源行動裝置神經網路架構 RepViT Mar 11, 2024 pm 12:07 PM

1.3ms耗時!清華最新開源行動裝置神經網路架構 RepViT

PHP與Vue:完美搭檔的前端開發利器 PHP與Vue:完美搭檔的前端開發利器 Mar 16, 2024 pm 12:09 PM

PHP與Vue:完美搭檔的前端開發利器

前端面試官常問的問題 前端面試官常問的問題 Mar 19, 2024 pm 02:24 PM

前端面試官常問的問題

一起學習word根號輸入方法 一起學習word根號輸入方法 Mar 19, 2024 pm 08:52 PM

一起學習word根號輸入方法

golang框架架構的學習曲線有多陡峭? golang框架架構的學習曲線有多陡峭? Jun 05, 2024 pm 06:59 PM

golang框架架構的學習曲線有多陡峭?

手撕Llama3第1層: 從零開始實現llama3 手撕Llama3第1層: 從零開始實現llama3 Jun 01, 2024 pm 05:45 PM

手撕Llama3第1層: 從零開始實現llama3

綜述!全面概括基礎模型對於推動自動駕駛的重要作用 綜述!全面概括基礎模型對於推動自動駕駛的重要作用 Jun 11, 2024 pm 05:29 PM

綜述!全面概括基礎模型對於推動自動駕駛的重要作用

See all articles