Core: How can I implement Flow?

Created on 28 Jun 2018  路  7Comments  路  Source: adonisjs/core

I'm trying to use Flow with AdonisJS.

I want my app to work in adonis serve --dev --debug mode, but with Flow type checking.

I have idea to create folder like flowApp write code there, run something like nodemon that will transpile code to app folder, where adonis serve --dev --debug will get code and reload server. What do you think about this idea? Is there better way to use Flow in Adonis?

Most helpful comment

So, that's what I've ended up with:

Stack

  • Babel (minimal require for transpiling Flow)
  • Flow
  • Flow-typed (for dependencies)
  • Eslint
  • Tcomb (optional) (I'm using it for runtime type checking) (https://github.com/gcanti/babel-plugin-tcomb)
  • WebStorm IDE

Folder structer

In the root directory (where app folder is), I've created appFlow folder (you can name it as you wish) and copy everything from app. That's what it looks like:

/app
/appFlow
/config
...

Package.json

{
    ...
    "scripts": {
    ...
    "dev": "adonis serve --dev --debug",
    "flow": "flow",
    "flow:deps": "flow-typed install",
    "babel:build": "babel appFlow -d app",
    "babel:watch": "babel appFlow --watch -d app"
  },
  "dependencies": {
    ....
  },
  "devDependencies": {
    "babel-cli": "6.26.0",
    "babel-eslint": "8.2.5",
    "babel-plugin-tcomb": "0.3.26",
    "babel-preset-env": "1.7.0",
    "babel-preset-flow": "6.23.0",
    "eslint": "4.19.1",
    "eslint-config-airbnb-base": "13.0.0",
    "eslint-plugin-flowtype": "2.49.3",
    "eslint-plugin-import": "2.13.0",
    "flow-bin": "0.75.0",
    "flow-typed": "2.4.0",
    "tcomb": "3.2.27"
  }
  ...
}

.babelrc

{
  "presets": [
    ["env", {
      "targets": {
        "node": "10.5"
      }
    }],
    "flow"
    ],
  "plugins": [
    "syntax-flow",
    "tcomb",
    "transform-flow-strip-types"
  ]
}

.eslintrc.js

module.exports = {
  "extends": ["airbnb-base", 'plugin:flowtype/recommended'],
  plugins: [
    'import',
    'flowtype',
  ],
  parser: 'babel-eslint',
  env: {
    node: true,
  },
};

.flowconfig

[ignore]
.*/node_modules/.*

[include]


[libs]
flow-typed

[lints]

[options]
all=true

[strict]

Start

To start everything working you need to open 2 terminals:

  1. one with npm run dev (or adonis serve --dev --debug)
  2. the other for npm run babel:watch (or babel appFlow --watch -d app).

When you make changes in any appFlow file (like adding new types or something) babel transpile it in to app folder, where adonis serve see changes, grab them and reload itself.

Lifehacks

  1. One of the best thing is that now I can describe props in Lucid models like this:
class User extends Model {

  id:number
  email:string
  password:string
  deleted:boolean

  static boot () {
    ...
  }

  ...

}

P.S.

I'll appreciate and advices or questions.

.

All 7 comments

I think I've discovered answer. I'll write it here in a days.

So, that's what I've ended up with:

Stack

  • Babel (minimal require for transpiling Flow)
  • Flow
  • Flow-typed (for dependencies)
  • Eslint
  • Tcomb (optional) (I'm using it for runtime type checking) (https://github.com/gcanti/babel-plugin-tcomb)
  • WebStorm IDE

Folder structer

In the root directory (where app folder is), I've created appFlow folder (you can name it as you wish) and copy everything from app. That's what it looks like:

/app
/appFlow
/config
...

Package.json

{
    ...
    "scripts": {
    ...
    "dev": "adonis serve --dev --debug",
    "flow": "flow",
    "flow:deps": "flow-typed install",
    "babel:build": "babel appFlow -d app",
    "babel:watch": "babel appFlow --watch -d app"
  },
  "dependencies": {
    ....
  },
  "devDependencies": {
    "babel-cli": "6.26.0",
    "babel-eslint": "8.2.5",
    "babel-plugin-tcomb": "0.3.26",
    "babel-preset-env": "1.7.0",
    "babel-preset-flow": "6.23.0",
    "eslint": "4.19.1",
    "eslint-config-airbnb-base": "13.0.0",
    "eslint-plugin-flowtype": "2.49.3",
    "eslint-plugin-import": "2.13.0",
    "flow-bin": "0.75.0",
    "flow-typed": "2.4.0",
    "tcomb": "3.2.27"
  }
  ...
}

.babelrc

{
  "presets": [
    ["env", {
      "targets": {
        "node": "10.5"
      }
    }],
    "flow"
    ],
  "plugins": [
    "syntax-flow",
    "tcomb",
    "transform-flow-strip-types"
  ]
}

.eslintrc.js

module.exports = {
  "extends": ["airbnb-base", 'plugin:flowtype/recommended'],
  plugins: [
    'import',
    'flowtype',
  ],
  parser: 'babel-eslint',
  env: {
    node: true,
  },
};

.flowconfig

[ignore]
.*/node_modules/.*

[include]


[libs]
flow-typed

[lints]

[options]
all=true

[strict]

Start

To start everything working you need to open 2 terminals:

  1. one with npm run dev (or adonis serve --dev --debug)
  2. the other for npm run babel:watch (or babel appFlow --watch -d app).

When you make changes in any appFlow file (like adding new types or something) babel transpile it in to app folder, where adonis serve see changes, grab them and reload itself.

Lifehacks

  1. One of the best thing is that now I can describe props in Lucid models like this:
class User extends Model {

  id:number
  email:string
  password:string
  deleted:boolean

  static boot () {
    ...
  }

  ...

}

P.S.

I'll appreciate and advices or questions.

.

@Dionid Thanks for sharing. I will close the issue, since it's not actionable on my end

@thetutlage what are your thoughts on implementing flow in the library?

@Dionid did you define global functions (e.g. use())?

For reference: facebook/flow#7014

Also, if you're coming off google reading this, I'd check out adonisjs/discussion#65

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

devcaststudio picture devcaststudio  路  3Comments

themodernpk picture themodernpk  路  3Comments

seanc picture seanc  路  4Comments

GianCastle picture GianCastle  路  3Comments

dezashibi picture dezashibi  路  4Comments