Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #40 from zephraph/support-next-ssg
Browse files Browse the repository at this point in the history
Add support for exporting ssg hooks
  • Loading branch information
jescalan authored Apr 23, 2020
2 parents 83870d6 + 960605b commit 0984a65
Show file tree
Hide file tree
Showing 13 changed files with 9,322 additions and 1,217 deletions.
20 changes: 20 additions & 0 deletions __tests__/fixtures/extend-frontmatter-async/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { frontMatter as introData } from '../pages/docs/intro.mdx'
import { frontMatter as advancedData } from '../pages/docs/advanced.mdx'

export default frontMatter => {
return function docsPageLayout({ children }) {
return (
<>
<p>LAYOUT TEMPLATE</p>
<h1>{frontMatter.title}</h1>
<p>
All frontMatter: {JSON.stringify(frontMatter)}
</p>
<p>
Other docs: {introData.title}, {advancedData.title}
</p>
{children}
</>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: 'Advanced Docs'
---

This is some **advanced** docs content!
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
layout: 'docs-page'
title: 'Intro Docs'
---

Here's some _introductory_ docs content
14 changes: 14 additions & 0 deletions __tests__/fixtures/extend-frontmatter-async/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { frontMatter as introData } from './docs/intro.mdx'
import { frontMatter as advancedData } from './docs/advanced.mdx'

export default () => {
return (
<>
<p>Hello world</p>
<ul>
<li>{introData.title}</li>
<li>{advancedData.title}</li>
</ul>
</>
)
}
17 changes: 17 additions & 0 deletions __tests__/fixtures/layout-exports-ssg/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { frontMatter as other} from '../pages/docs/intro.mdx'

export async function getStaticProps() {
return {props: {}}
}

export default (frontMatter) => {
return function docsPageLayout({ children }) {
return (
<>
<p>LAYOUT TEMPLATE</p>
<h1>{frontMatter.title}</h1>
{children}
</>
)
}
}
3 changes: 3 additions & 0 deletions __tests__/fixtures/layout-exports-ssg/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const withMdxEnhanced = require('../../..')

module.exports = withMdxEnhanced()()
6 changes: 6 additions & 0 deletions __tests__/fixtures/layout-exports-ssg/pages/docs/intro.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
layout: 'docs-page'
title: 'Intro Docs'
---

Hello world
25 changes: 13 additions & 12 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const rmfr = require('rmfr')
const nextBuild = require('next/dist/build').default
const nextExport = require('next/dist/export').default
const glob = require('glob')
const execa = require("execa")

// increase timeout since these are integration tests
jest.setTimeout(200000)
Expand Down Expand Up @@ -38,6 +39,12 @@ test('options.layoutPath and options.defaultLayout', async () => {
)
})

test('exports ssg functions from layout', async () => {
const layoutSSGExportFixture = path.join(__dirname, 'fixtures/layout-exports-ssg')
const outPath = await compileNextjs(layoutSSGExportFixture)
return expectContentMatch(outPath, 'docs/intro.html', /Hello world/)
})

describe('options.extendFrontMatter', () => {
it('should work with a sync process fn', async () => {
const extendFmFixture = path.join(__dirname, 'fixtures/extend-frontmatter')
Expand All @@ -47,8 +54,8 @@ describe('options.extendFrontMatter', () => {
})

it('should work with an async process fn', async () => {
const extendFmFixture = path.join(__dirname, 'fixtures/extend-frontmatter')
const outPath = await compileNextjs(extendFmFixture, 'next.config.async.js')
const extendFmFixture = path.join(__dirname, 'fixtures/extend-frontmatter-async')
const outPath = await compileNextjs(extendFmFixture)
expectContentMatch(outPath, 'index.html', /Hello world/)
expectContentMatch(outPath, 'docs/intro.html', /ortni\/scod/)
})
Expand Down Expand Up @@ -100,17 +107,11 @@ afterAll(() => {

// Test Utilities

function compileNextjs(projectPath, configPath = 'next.config.js') {
const config = require(path.join(projectPath, configPath))
async function compileNextjs(projectPath) {
const outPath = path.join(projectPath, 'out')
return nextBuild(projectPath, config)
.then(() => {
return nextExport(projectPath, {
outdir: outPath,
silent: true
})
})
.then(() => outPath)
await execa("next", ["build", projectPath], { preferLocal: true, cwd: projectPath })
await execa("next", ["export", projectPath], { preferLocal: true, cwd: projectPath })
return outPath
}

function expectContentMatch(outPath, filePath, matcher) {
Expand Down
1 change: 1 addition & 0 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ async function processLayout(options, frontMatter, content, resourcePath, scans)
// Import the layout, export the layout-wrapped content, pass front matter into layout
return `import layout from '${normalizeToUnixPath(layoutPath)}'
export * from '${normalizeToUnixPath(layoutPath)}'
export default layout(${stringifyObject(mergedFrontMatter)})
${content}
Expand Down
Loading

0 comments on commit 0984a65

Please sign in to comment.