How to upgrade to babel 7

前端 未结 3 695
孤城傲影
孤城傲影 2021-01-17 08:44

I tried to upgrade Webpack and babel to 4, 7 respectively but couldn’t make it work. Also the official doc isn’t helping much with the upgrade

I am getting followin

3条回答
  •  迷失自我
    2021-01-17 09:17

    Use babel-upgrade

    Tested on node@10.15.3, npm@6.4.1 and babel@7.4.0

    You can use following script. (npx on node 5+ only)

    npx babel-upgrade --write
    

    The --write flag writes the updates to your package.json and .babelrc.

    You will end up with following modifications to package.json:

    "devDependencies": {
        "@babel/core": "^7.4.0",
        "@babel/plugin-proposal-class-properties": "^7.0.0",
        "@babel/plugin-proposal-json-strings": "^7.0.0",
        "@babel/plugin-proposal-private-methods": "^7.4.0",
        "@babel/plugin-syntax-dynamic-import": "^7.0.0",
        "@babel/plugin-syntax-import-meta": "^7.0.0"
    }
    

    .babelrc

    {
      "plugins": [
        "@babel/plugin-syntax-dynamic-import",
        "@babel/plugin-syntax-import-meta",
        [
          "@babel/plugin-proposal-class-properties"
        ],
        "@babel/plugin-proposal-json-strings",
        [
          "@babel/plugin-proposal-private-methods"
        ]
      ]
    }
    

    Out of the above plugins you need @babel/plugin-proposal-class-properties @babel/plugin-proposal-private-methods to make private properties work correctly if you choose to implement them.

    If you are using eslint, dont forget to set your parser as babel-eslint like so in your .eslintrc file:

    {
        "parser": "babel-eslint"
    }
    

提交回复
热议问题