How to add flow (flowtype) support in nuxt?

2019-03-03 00:52发布

问题:

I want to add flow support to a nuxt project (my project uses webpack and babel). Can I find a working example somewhere?

If I run flow check, there are no errors, bun when I run yarn run dev, I get a syntax error.

(I know there are these unanswered questions out there, I raise the same issue again hoping this time it will reach some guys with knowledge on the matter.)

Thanks

回答1:

There a lot of things to configure. Let me guide you trough this process.

TLDR: use wemake-vue-template. It comes with nuxt, flow, and many other goodies.

Babel

First of all, we need to configure babel. That's how your babel configuration should look like:

{
  "presets": [
    ["vue-app", {
      "useBuiltIns": true
    }],
    "flow"
  ]
}

Install these presents with npm install --save-dev babel-preset-flow babel-preset-vue-app.

Eslint

Next, we need to configure eslint to lint our flow files. That's how .eslintrc should look like:

{
  "root": true,
  "plugins": [
    "flowtype-errors"
  ],
  "env": {
    "node": true,
    "browser": true
  },
  "rules": {
    // raise flow errors
    "flowtype-errors/show-errors": 2,
    "flowtype-errors/show-warnings": 1,
    // "flowtype-errors/enforce-min-coverage": [2, 50]
  },
  "parserOptions": {
    "parser": "babel-eslint",
    "ecmaVersion": 2017,
    "sourceType": "module"
  }
}

Make sure that you have installed everything.

.flowconfig

That's the hardest part. You need to specify configuration for flow. It may differ based on your setup.

[ignore]
./nuxt/*
<PROJECT_ROOT>/node_modules/.*config-chain/test/broken.json

[include]
<PROJECT_ROOT>/client
<PROJECT_ROOT>/

[libs]

[lints]
all=warn
untyped-import=off
unsafe-getters-setters=off

[options]
include_warnings=true
esproposal.decorators=ignore
module.name_mapper='^~' -> '<PROJECT_ROOT>/client'
module.system.node.resolve_dirname=node_modules
module.system.node.resolve_dirname=client
module.file_ext=.vue
module.file_ext=.js

nuxt.config.js

The last step. We need to enable linting on each change.

 build: {
    extend (config, { isDev, isClient }) {
      if (isDev && isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
     }
  }

That's it. Now you will have working flow type checking.