Skip to content

Commit

Permalink
Initial Flow support
Browse files Browse the repository at this point in the history
  • Loading branch information
bryannaegele committed Jun 19, 2016
1 parent 51010a5 commit d76736c
Show file tree
Hide file tree
Showing 40 changed files with 169 additions and 12 deletions.
10 changes: 8 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"presets": ['react', 'es2015', 'stage-0'],
"plugins": []
"presets": [
'react',
'es2015',
'stage-0'
],
"plugins": [
'transform-flow-strip-types'
]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
interfaces
34 changes: 32 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"plugin:ava/recommended"
],
"plugins": [
"ava"
"ava",
"flowtype",
"flow-vars"
],
"rules": {
"indent": [2, 2, {"SwitchCase": 1}],
Expand All @@ -20,9 +22,37 @@
"react/prefer-stateless-function": [0],
"no-underscore-dangle": ["error", { "allow": ["_config"] }],
"import/no-unresolved": [0],
"global-require": [0]
"global-require": [0],
"no-duplicate-imports": [0],
"flowtype/require-parameter-type": 1,
"flowtype/require-return-type": [
1,
"always",
{
"annotateUndefined": "never"
}
],
"flowtype/space-after-type-colon": [
1,
"always"
],
"flowtype/space-before-type-colon": [
1,
"never"
],
"flowtype/type-id-match": [
0,
"^([A-Z][a-z0-9]+)+Type$"
],
"flow-vars/define-flow-type": 1,
"flow-vars/use-flow-type": 1,
},
"globals": {
"__PREFIX_LINKS__": true,
},
"settings": {
"flowtype": {
"onlyFilesWithFlowAnnotation": true
}
}
}
24 changes: 24 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[version]
0.27.0

[ignore]
.*/node_modules/.*
.*/test/.*

[include]
node_modules

[libs]
./interfaces/
node_modules/iflow-debug/index.js.flow
node_modules/iflow-lodash/index.js.flow
node_modules/iflow-react-router/index.js.flow

[options]
esproposal.class_instance_fields=enable
esproposal.class_static_fields=enable

strip_root=true

suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe
suppress_comment= \\(.\\|\n\\)*\\$FlowIssue
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ simple steps:

Test suite can be run via `npm test`.

This project uses [FlowType](https://flowtype.org/) for static type checking.

**Note for Windows Users** - Flow is not currently supported on Windows. Please do not let this prevent you from contributing. If you can use a VM for this part, great! Otherwise, feel free to open a PR and ask for assistance.

The usual contributing steps are:

* Fork the [official repository](https://github.com/gatsbyjs/gatsby).
Expand Down
3 changes: 3 additions & 0 deletions interfaces/front-matter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'front-matter' {
declare var exports: (input: string) => { attributes: {}, body: string };
}
3 changes: 3 additions & 0 deletions interfaces/html-frontmatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'html-frontmatter' {
declare var exports: (input: string) => {};
}
3 changes: 3 additions & 0 deletions interfaces/invariant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'invariant' {
declare var exports: (condition: any, message: string) => ?Error
}
3 changes: 3 additions & 0 deletions interfaces/object-assign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'object-assign' {
declare var exports: (target: Object, ...sources?: Array<Object>) => {};
}
24 changes: 24 additions & 0 deletions interfaces/parse-filepath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
declare module 'parse-filepath' {
declare type FileData = {
root: string,
dir: string,
base: string,
ext: string,
name: string,

// aliases
extname: string,
basename: string,
dirname: string,
stem: string,

// original path
path: string,

// getters
absolute: () => string,
isAbsolute: () => Boolean
}

declare var exports: (input: string) => FileData
}
3 changes: 3 additions & 0 deletions interfaces/slash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'slash' {
declare var exports: (input: string) => string;
}
2 changes: 2 additions & 0 deletions lib/isomorphic/create-routes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import filter from 'lodash/filter'
import sortBy from 'lodash/sortBy'
import last from 'lodash/last'
Expand Down Expand Up @@ -96,6 +97,7 @@ module.exports = (files, pagesReq) => {
const wrappers = {}
staticFileTypes.forEach((type) => {
try {
// $FlowIssue - https://github.com/facebook/flow/issues/1975
wrappers[type] = require(`wrappers/${type}`)
} catch (e) {
// Ignore error.
Expand Down
1 change: 1 addition & 0 deletions lib/isomorphic/gatsby-helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import { config } from 'config'
import invariant from 'invariant'
import isString from 'lodash/isString'
Expand Down
2 changes: 2 additions & 0 deletions lib/isomorphic/html.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* @flow weak */

import React, { PropTypes } from 'react'
import { prefixLink } from 'gatsby-helpers'

Expand Down
1 change: 1 addition & 0 deletions lib/isomorphic/pages/_template.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import React, { PropTypes } from 'react'

const defaultMessage = `
Expand Down
1 change: 1 addition & 0 deletions lib/isomorphic/wrappers/html.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import React, { PropTypes } from 'react'

module.exports = React.createClass({
Expand Down
1 change: 1 addition & 0 deletions lib/isomorphic/wrappers/md.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import React, { PropTypes } from 'react'

module.exports = React.createClass({
Expand Down
1 change: 1 addition & 0 deletions lib/loaders/config-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
const toml = require('toml')
const loaderUtils = require('loader-utils')
const path = require('path')
Expand Down
1 change: 1 addition & 0 deletions lib/loaders/html-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import htmlFrontMatter from 'html-frontmatter'
import objectAssign from 'object-assign'

Expand Down
1 change: 1 addition & 0 deletions lib/loaders/markdown-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import frontMatter from 'front-matter'
import markdownIt from 'markdown-it'
import hljs from 'highlight.js'
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/babel-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import resolve from 'babel-core/lib/helpers/resolve'
import fs from 'fs'
import path from 'path'
Expand Down Expand Up @@ -104,6 +105,7 @@ function findBabelrc (directory) {
*/
function findBabelPackage (directory) {
try {
// $FlowIssue - https://github.com/facebook/flow/issues/1975
const packageJson = require(path.join(directory, 'package.json'))
return packageJson.babel
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions lib/utils/build-css.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import webpack from 'webpack'
import webpackConfig from './webpack.config'
import fs from 'fs'
Expand Down
1 change: 1 addition & 0 deletions lib/utils/build-html.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
require('node-cjsx').transform()
import webpack from 'webpack'
import globPages from './glob-pages'
Expand Down
1 change: 1 addition & 0 deletions lib/utils/build-javascript.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import webpack from 'webpack'
import webpackConfig from './webpack.config'

Expand Down
5 changes: 3 additions & 2 deletions lib/utils/build-page/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* @flow */
import path from 'path'
import objectAssign from 'object-assign'
import pathResolver from './path-resolver'
import loadFrontmatter from './load-frontmatter'

export default function buildPage (directory, page) {
export default function buildPage (directory: string, page: string) {
const pageData = loadFrontmatter(page)

const relativePath = path.relative(path.join(directory, 'pages'), page)
const relativePath: string = path.relative(path.join(directory, 'pages'), page)
const pathData = pathResolver(relativePath, pageData)

return objectAssign({}, pathData, { data: pageData })
Expand Down
10 changes: 6 additions & 4 deletions lib/utils/build-page/load-frontmatter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* @flow */
import fs from 'fs'
import path from 'path'
import frontMatter from 'front-matter'
import objectAssign from 'object-assign'
import htmlFrontMatter from 'html-frontmatter'

export default function loadFrontmatter (pagePath) {
const ext = path.extname(pagePath).slice(1)
export default function loadFrontmatter (pagePath: string): {} {
const ext: string = path.extname(pagePath).slice(1)

// Load data for each file type.
// TODO use webpack-require to ensure data loaded
Expand All @@ -14,10 +15,11 @@ export default function loadFrontmatter (pagePath) {

let data
if (ext === 'md') {
const rawData = frontMatter(fs.readFileSync(pagePath, 'utf-8'))
const rawData = frontMatter(fs.readFileSync(pagePath, { encoding: 'utf-8' }))
data = objectAssign({}, rawData.attributes)
} else if (ext === 'html') {
const html = fs.readFileSync(pagePath, 'utf-8')
const html = fs.readFileSync(pagePath, { encoding: 'utf-8' })
// $FlowIssue - https://github.com/facebook/flow/issues/1870
data = objectAssign({}, htmlFrontMatter(html), { body: html })
} else {
data = {}
Expand Down
5 changes: 4 additions & 1 deletion lib/utils/build-page/path-resolver.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* @flow */
import slash from 'slash'
import parsePath from 'parse-filepath'
import urlResolver from './url-resolver'

export default function pathResolver (relativePath, pageData={}) {

export default function pathResolver (relativePath: string, pageData: {} = {}) {
const data = {}

data.file = parsePath(relativePath)
Expand All @@ -26,3 +28,4 @@ export default function pathResolver (relativePath, pageData={}) {

return data
}

2 changes: 2 additions & 0 deletions lib/utils/build-page/url-resolver.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* @flow weak */
import { posix as path } from 'path'
import _, { startsWith } from 'lodash'
import invariant from 'invariant'

let rewritePath
try {
const gatsbyNodeConfig = path.resolve(process.cwd(), './gatsby-node')
// $FlowIssue - https://github.com/facebook/flow/issues/1975
const nodeConfig = require(gatsbyNodeConfig)
rewritePath = nodeConfig.rewritePath
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import toml from 'toml'
import fs from 'fs'
import _ from 'lodash'
Expand All @@ -12,6 +13,7 @@ function customPost (program, callback) {
const directory = program.directory
let customPostBuild
try {
// $FlowIssue - https://github.com/facebook/flow/issues/1975
const gatsbyNodeConfig = require(`${directory}/gatsby-node`)
customPostBuild = gatsbyNodeConfig.postBuild
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions lib/utils/develop.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
require('node-cjsx').transform()
import Hapi from 'hapi'
import Boom from 'boom'
Expand Down
1 change: 1 addition & 0 deletions lib/utils/glob-pages.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import glob from 'glob'
import buildPage from './build-page'
const debug = require('debug')('gatsby:glob')
Expand Down
1 change: 1 addition & 0 deletions lib/utils/init-starter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import { exec } from 'child_process'
import fs from 'fs-extra'
import sysPath from 'path'
Expand Down
1 change: 1 addition & 0 deletions lib/utils/load-context.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
// This file is auto-written and used by Gatsby to require
// files from your pages directory.
module.exports = function (callback) {
Expand Down
1 change: 1 addition & 0 deletions lib/utils/new.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
const logger = require('tracer').colorConsole()

const initStarter = require('./init-starter')
Expand Down
1 change: 1 addition & 0 deletions lib/utils/post-build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import path from 'path'
import glob from 'glob'
import fs from 'fs-extra'
Expand Down
1 change: 1 addition & 0 deletions lib/utils/serve-build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import Hapi from 'hapi'
import opn from 'opn'

Expand Down
1 change: 1 addition & 0 deletions lib/utils/static-entry.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import React from 'react'
import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import { match, RouterContext } from 'react-router'
Expand Down
1 change: 1 addition & 0 deletions lib/utils/web-entry.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow weak */
import React from 'react'
import ReactDOM from 'react-dom'
import { applyRouterMiddleware, browserHistory, Router } from 'react-router'
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,22 @@
"ava-http": "^0.2.1",
"babel-cli": "^6.9.0",
"babel-eslint": "^6.0.2",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-register": "^6.9.0",
"bluebird": "^3.4.0",
"cheerio": "^0.20.0",
"eslint": "^2.11.1",
"eslint-config-airbnb": "^9.0.1",
"eslint-plugin-ava": "^2.5.0",
"eslint-plugin-flow-vars": "^0.4.0",
"eslint-plugin-flowtype": "^2.2.7",
"eslint-plugin-import": "^1.8.1",
"eslint-plugin-jsx-a11y": "^1.5.3",
"eslint-plugin-react": "^5.1.1",
"flow-bin": "^0.27.0",
"iflow-debug": "^1.0.15",
"iflow-lodash": "^1.1.15",
"iflow-react-router": "^1.1.15",
"nyc": "^6.6.1"
},
"engines": {
Expand All @@ -119,7 +126,8 @@
"scripts": {
"build": "babel lib --out-dir dist/",
"lint": "eslint --ext .js,.jsx --ignore-path .gitignore .",
"test": "npm run lint && npm run test-node && npm run test-integration",
"lint:flow": "babel-node scripts/flow-check.js",
"test": "npm run lint && npm run test-node && npm run test-integration && npm run lint:flow",
"test-coverage": "node_modules/.bin/nyc --reporter=lcov --reporter=text npm test",
"test-integration": "node_modules/.bin/ava test/integration",
"test-node": "node_modules/.bin/ava test/utils",
Expand Down
Loading

0 comments on commit d76736c

Please sign in to comment.