Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support page data declared as named exports #838

Merged
merged 1 commit into from
Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ exports.data = {
export default MyComponent ...
```

You can also use a named export for the data object:

```javascript
export const data = {
title: 'This is a title',
}
```

### Structure of a Gatsby site
* `config.toml` - Core application configuration is stored here. Available via a `require`
or `import` of 'config'. Values:
Expand Down
2 changes: 1 addition & 1 deletion lib/isomorphic/create-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module.exports = (files, pagesReq) => {
handler = wrappers[page.file.ext]
page.data = pagesReq(`./${page.requirePath}`)
} else if (reactComponentFileTypes.indexOf(page.file.ext) !== -1) {
handler = pagesReq(`./${page.requirePath}`)
handler = requireComponent(pagesReq(`./${page.requirePath}`))
page.data = page.data === undefined ? {} : page.data
}

Expand Down
16 changes: 16 additions & 0 deletions lib/utils/build-page/load-frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs'
import path from 'path'
import frontMatter from 'front-matter'
import objectAssign from 'object-assign'
import _ from 'lodash'
import htmlFrontMatter from 'html-frontmatter'
import * as babylon from 'babylon'
import traverse from 'babel-traverse'
Expand Down Expand Up @@ -81,6 +82,21 @@ export default function loadFrontmatter(pagePath: string): {} {
})
}
},
ExportNamedDeclaration: function ExportNamedDeclaration(astPath) {
const { declaration } = astPath.node
if (declaration && declaration.type === 'VariableDeclaration') {
const dataVariableDeclarator = _.find(
declaration.declarations,
d => d.id.name === 'data'
)

if (dataVariableDeclarator && dataVariableDeclarator.init) {
dataVariableDeclarator.init.properties.forEach(node => {
data[node.key.name] = parseData(node.value)
})
}
}
},
})
} catch (e) {
// Ignore errors — we print out parse errors for user elsewhere.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable react/display-name */
import React from 'react'

export const data = {
titles: ['My title', 'My other title'],
}

export default () => (
<div>
<h1>My title</h1>
<h2>My other title</h2>
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable react/display-name */
import React from 'react'

export const data = {
titles: {
main: 'My title',
sub: 'My other title',
},
}

export default () => (
<div>
<h1>My title</h1>
<h2>My other title</h2>
</div>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable react/display-name */
import React from 'react'

export const data = {
title: 'Foo',
}

export default () => <h1>Foo</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable react/display-name */
import React from 'react'

export const data = {
// eslint-disable-next-line
title: `Bar`,
}

export default () => <h1>Bar</h1>
30 changes: 30 additions & 0 deletions test/utils/build-page/load-frontmatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ test('it works for string literal titles', t => {
t.is(data.title, 'Foo')
})

test('it works for string literal titles declared as a named export', t => {
const pagePath =
'../../fixtures/javascript-pages-frontmatter/pages/string-literal-named-export.js'
const data = loadFrontmatter(pagePath)
t.is(data.title, 'Foo')
})

test('it works for template literal titles', t => {
const pagePath =
'../../fixtures/javascript-pages-frontmatter/pages/template-literal.js'
const data = loadFrontmatter(pagePath)
t.is(data.title, 'Bar')
})

test('it works for template literal titles declared as a named export', t => {
const pagePath =
'../../fixtures/javascript-pages-frontmatter/pages/template-literal-named-export.js'
const data = loadFrontmatter(pagePath)
t.is(data.title, 'Bar')
})

test('it works with array literal values', t => {
const pagePath =
'../../fixtures/javascript-pages-frontmatter/pages/array-literal.js'
Expand All @@ -23,10 +37,26 @@ test('it works with array literal values', t => {
t.is(data.titles[1], 'My other title')
})

test('it works with array literal values declared as a named export', t => {
const pagePath =
'../../fixtures/javascript-pages-frontmatter/pages/array-literal-named-export.js'
const data = loadFrontmatter(pagePath)
t.is(data.titles[0], 'My title')
t.is(data.titles[1], 'My other title')
})

test('it works with object literal values', t => {
const pagePath =
'../../fixtures/javascript-pages-frontmatter/pages/object-literal.js'
const data = loadFrontmatter(pagePath)
t.is(data.titles.main, 'My title')
t.is(data.titles.sub, 'My other title')
})

test('it works with object literal values declared as a named export', t => {
const pagePath =
'../../fixtures/javascript-pages-frontmatter/pages/object-literal-named-export.js'
const data = loadFrontmatter(pagePath)
t.is(data.titles.main, 'My title')
t.is(data.titles.sub, 'My other title')
})