Skip to content

Commit d1c2d41

Browse files
NMinhNguyenKyleAMathews
authored andcommitted
Support page data declared as named exports (#838)
1 parent 687c43f commit d1c2d41

File tree

8 files changed

+101
-1
lines changed

8 files changed

+101
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ exports.data = {
336336
export default MyComponent ...
337337
```
338338

339+
You can also use a named export for the data object:
340+
341+
```javascript
342+
export const data = {
343+
title: 'This is a title',
344+
}
345+
```
346+
339347
### Structure of a Gatsby site
340348
* `config.toml` - Core application configuration is stored here. Available via a `require`
341349
or `import` of 'config'. Values:

lib/isomorphic/create-routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ module.exports = (files, pagesReq) => {
145145
handler = wrappers[page.file.ext]
146146
page.data = pagesReq(`./${page.requirePath}`)
147147
} else if (reactComponentFileTypes.indexOf(page.file.ext) !== -1) {
148-
handler = pagesReq(`./${page.requirePath}`)
148+
handler = requireComponent(pagesReq(`./${page.requirePath}`))
149149
page.data = page.data === undefined ? {} : page.data
150150
}
151151

lib/utils/build-page/load-frontmatter.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs'
33
import path from 'path'
44
import frontMatter from 'front-matter'
55
import objectAssign from 'object-assign'
6+
import _ from 'lodash'
67
import htmlFrontMatter from 'html-frontmatter'
78
import * as babylon from 'babylon'
89
import traverse from 'babel-traverse'
@@ -81,6 +82,21 @@ export default function loadFrontmatter(pagePath: string): {} {
8182
})
8283
}
8384
},
85+
ExportNamedDeclaration: function ExportNamedDeclaration(astPath) {
86+
const { declaration } = astPath.node
87+
if (declaration && declaration.type === 'VariableDeclaration') {
88+
const dataVariableDeclarator = _.find(
89+
declaration.declarations,
90+
d => d.id.name === 'data'
91+
)
92+
93+
if (dataVariableDeclarator && dataVariableDeclarator.init) {
94+
dataVariableDeclarator.init.properties.forEach(node => {
95+
data[node.key.name] = parseData(node.value)
96+
})
97+
}
98+
}
99+
},
84100
})
85101
} catch (e) {
86102
// Ignore errors — we print out parse errors for user elsewhere.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-disable react/display-name */
2+
import React from 'react'
3+
4+
export const data = {
5+
titles: ['My title', 'My other title'],
6+
}
7+
8+
export default () => (
9+
<div>
10+
<h1>My title</h1>
11+
<h2>My other title</h2>
12+
</div>
13+
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* eslint-disable react/display-name */
2+
import React from 'react'
3+
4+
export const data = {
5+
titles: {
6+
main: 'My title',
7+
sub: 'My other title',
8+
},
9+
}
10+
11+
export default () => (
12+
<div>
13+
<h1>My title</h1>
14+
<h2>My other title</h2>
15+
</div>
16+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* eslint-disable react/display-name */
2+
import React from 'react'
3+
4+
export const data = {
5+
title: 'Foo',
6+
}
7+
8+
export default () => <h1>Foo</h1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable react/display-name */
2+
import React from 'react'
3+
4+
export const data = {
5+
// eslint-disable-next-line
6+
title: `Bar`,
7+
}
8+
9+
export default () => <h1>Bar</h1>

test/utils/build-page/load-frontmatter.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,27 @@ test('it works for string literal titles', t => {
88
t.is(data.title, 'Foo')
99
})
1010

11+
test('it works for string literal titles declared as a named export', t => {
12+
const pagePath =
13+
'../../fixtures/javascript-pages-frontmatter/pages/string-literal-named-export.js'
14+
const data = loadFrontmatter(pagePath)
15+
t.is(data.title, 'Foo')
16+
})
17+
1118
test('it works for template literal titles', t => {
1219
const pagePath =
1320
'../../fixtures/javascript-pages-frontmatter/pages/template-literal.js'
1421
const data = loadFrontmatter(pagePath)
1522
t.is(data.title, 'Bar')
1623
})
1724

25+
test('it works for template literal titles declared as a named export', t => {
26+
const pagePath =
27+
'../../fixtures/javascript-pages-frontmatter/pages/template-literal-named-export.js'
28+
const data = loadFrontmatter(pagePath)
29+
t.is(data.title, 'Bar')
30+
})
31+
1832
test('it works with array literal values', t => {
1933
const pagePath =
2034
'../../fixtures/javascript-pages-frontmatter/pages/array-literal.js'
@@ -23,10 +37,26 @@ test('it works with array literal values', t => {
2337
t.is(data.titles[1], 'My other title')
2438
})
2539

40+
test('it works with array literal values declared as a named export', t => {
41+
const pagePath =
42+
'../../fixtures/javascript-pages-frontmatter/pages/array-literal-named-export.js'
43+
const data = loadFrontmatter(pagePath)
44+
t.is(data.titles[0], 'My title')
45+
t.is(data.titles[1], 'My other title')
46+
})
47+
2648
test('it works with object literal values', t => {
2749
const pagePath =
2850
'../../fixtures/javascript-pages-frontmatter/pages/object-literal.js'
2951
const data = loadFrontmatter(pagePath)
3052
t.is(data.titles.main, 'My title')
3153
t.is(data.titles.sub, 'My other title')
3254
})
55+
56+
test('it works with object literal values declared as a named export', t => {
57+
const pagePath =
58+
'../../fixtures/javascript-pages-frontmatter/pages/object-literal-named-export.js'
59+
const data = loadFrontmatter(pagePath)
60+
t.is(data.titles.main, 'My title')
61+
t.is(data.titles.sub, 'My other title')
62+
})

0 commit comments

Comments
 (0)