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

Commit

Permalink
Fix for next@9.4.0 (#55)
Browse files Browse the repository at this point in the history
* update dependencies
* fix for next 9.4
* abstract plugin injection function
  • Loading branch information
jescalan authored May 15, 2020
1 parent 38ef106 commit effa78a
Show file tree
Hide file tree
Showing 15 changed files with 8,424 additions and 8,006 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ module.exports = withMdxEnhanced({
rehypePlugins: [],
extendFrontMatter: {
process: (mdxContent, frontMatter) => {},
phase: 'prebuild|loader|both'
}
phase: 'prebuild|loader|both',
},
})(/* your normal nextjs config */)
```

Expand Down Expand Up @@ -101,10 +101,10 @@ Array of [rehype plugins](https://mdxjs.com/advanced/plugins#using-remark-and-re

> `object` | optional
| Property | Type | Description |
| --------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Property | Type | Description |
| --------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `process` | `function` | A hook function whose return value will be appended to the processed front matter. This function is given access to the source `.mdx` content as the first parameter and the processed front matter as the second parameter. |
| `phase` | `string` | Used to specify when to run the `process` function. Eligible values are `prebuild`, `loader`, `both`. Defaults to `both` if not specified. |
| `phase` | `string` | Used to specify when to run the `process` function. Eligible values are `prebuild`, `loader`, `both`. Defaults to `both` if not specified. |

### scan

Expand Down Expand Up @@ -160,7 +160,7 @@ The file extension of the template must be one of configured [pageExtensions](ht
The template, defined in `layouts/docs-page.jsx`, looks like the following:

```jsx
export default frontMatter => {
export default function Layout(frontMatter) {
return ({ children: content }) => {
return (
<div>
Expand Down Expand Up @@ -201,14 +201,14 @@ import Link from 'next/link'
import { frontMatter as introData } from './docs/intro.mdx'
import { frontMatter as advancedData } from './docs/advanced.mdx'

export default () => {
export default function DocsPage() {
const docsPages = [introData, advancedData]

return (
<>
<h1>Docs Index</h1>
<ul>
{docsPages.map(page => (
{docsPages.map((page) => (
<li key={page.__resourcePath}>
<Link href={formatPath(page.__resourcePath)}>
<a>{page.title}</a>
Expand Down Expand Up @@ -245,14 +245,14 @@ First, the index page imports the destructured and renamed front matter from eac
Let's examine the code that renders each link:

```jsx
export default () => {
export default function DocsPage() {
const docsPages = [introData, advancedData]

return (
<>
<h1>Docs Index</h1>
<ul>
{docsPages.map(page => (
{docsPages.map((page) => (
<li key={page.__resourcePath}>
<Link href={formatPath(page.__resourcePath)}>
<a>{page.title}</a>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/basic/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { frontMatter as introData } from './docs/intro.mdx'
import { frontMatter as advancedData } from './docs/advanced.mdx'

export default () => {
export default function Page() {
return (
<>
<p>Hello world</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { frontMatter as introData } from './docs/intro.mdx'
import { frontMatter as advancedData } from './docs/advanced.mdx'

export default () => {
export default function Page() {
return (
<>
<p>Hello world</p>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/extend-frontmatter/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { frontMatter as introData } from './docs/intro.mdx'
import { frontMatter as advancedData } from './docs/advanced.mdx'

export default () => {
export default function Page() {
return (
<>
<p>Hello world</p>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/file-extensions/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { frontMatter as mdExtension } from './test.md'
import { frontMatter as mdxExtension } from './test.mdx'

export default () => {
export default function Page() {
return (
<>
<p>
Expand Down
8 changes: 4 additions & 4 deletions __tests__/fixtures/layout-exports-ssg/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { frontMatter as other} from '../pages/docs/intro.mdx'
import { frontMatter as other } from '../pages/docs/intro.mdx'

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

export default (frontMatter) => {
export default function Page(frontMatter) {
return function docsPageLayout({ children }) {
return (
<>
Expand All @@ -14,4 +14,4 @@ export default (frontMatter) => {
</>
)
}
}
}
2 changes: 1 addition & 1 deletion __tests__/fixtures/layouts-path/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { frontMatter as introData } from './docs/intro.mdx'
import { frontMatter as advancedData } from './docs/advanced.mdx'

export default () => {
export default function Page() {
return (
<>
<p>Hello world</p>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/on-content/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default () => {
export default function Page() {
return function docsPageLayout({ children }) {
return <>{children}</>
}
Expand Down
4 changes: 3 additions & 1 deletion __tests__/fixtures/on-content/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default () => <p>Hello world</p>
export default function Page() {
return <p>Hello world</p>
}
2 changes: 1 addition & 1 deletion __tests__/fixtures/scan-mdx-content/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { frontMatter as introData } from './docs/intro.mdx'
import { frontMatter as advancedData } from './docs/advanced.mdx'

export default () => {
export default function Page() {
return (
<>
<p>Hello world</p>
Expand Down
33 changes: 22 additions & 11 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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")
const spawn = require('cross-spawn')

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

test('exports ssg functions from layout', async () => {
const layoutSSGExportFixture = path.join(__dirname, 'fixtures/layout-exports-ssg')
const layoutSSGExportFixture = path.join(
__dirname,
'fixtures/layout-exports-ssg'
)
const outPath = await compileNextjs(layoutSSGExportFixture)
return expectContentMatch(outPath, 'docs/intro.html', /Hello world/)
})
Expand All @@ -54,7 +57,10 @@ describe('options.extendFrontMatter', () => {
})

it('should work with an async process fn', async () => {
const extendFmFixture = path.join(__dirname, 'fixtures/extend-frontmatter-async')
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 @@ -87,7 +93,7 @@ test('options.onContent', async () => {
if (err) throw err
mdxPageCount = files.length
})
const mockCallback = jest.fn(content => console.log(content))
const mockCallback = jest.fn((content) => console.log(content))
const compile = await compileNextjsWithMockFunction(
onContentFixture,
'next.config-mock.js',
Expand All @@ -101,17 +107,22 @@ afterAll(() => {
return Promise.all([
rmfr(path.join(__dirname, 'fixtures/*/out'), { glob: true }),
rmfr(path.join(__dirname, 'fixtures/*/.mdx-data'), { glob: true }),
rmfr(path.join(__dirname, 'fixtures/*/.next'), { glob: true })
rmfr(path.join(__dirname, 'fixtures/*/.next'), { glob: true }),
])
})

// Test Utilities

async function compileNextjs(projectPath) {
const outPath = path.join(projectPath, 'out')
await execa("next", ["build", projectPath], { preferLocal: true, cwd: projectPath })
await execa("next", ["export", projectPath], { preferLocal: true, cwd: projectPath })
return outPath
const nextLocal = path.join(__dirname, '../node_modules/.bin/next')
spawn.sync(nextLocal, ['build', projectPath], {
stdio: 'inherit',
cwd: projectPath,
})
spawn.sync(nextLocal, ['export', projectPath], {
stdio: 'inherit',
cwd: projectPath,
})
return path.join(projectPath, 'out')
}

function expectContentMatch(outPath, filePath, matcher) {
Expand All @@ -129,7 +140,7 @@ function compileNextjsWithMockFunction(
return nextBuild(projectPath, config(mockFn)).then(() => {
return nextExport(projectPath, {
outdir: outPath,
silent: true
silent: true,
}).then(() => mockFn)
})
}
6 changes: 3 additions & 3 deletions babelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function nextBabelWrapper(nextConfig, pluginOptions) {
return

// if there are no "frontMatter" imports, do nothing
const frontMatterSpecifier = _path.node.specifiers.find(s =>
const frontMatterSpecifier = _path.node.specifiers.find((s) =>
importsFrontMatter(s)
)
if (!frontMatterSpecifier) return
Expand Down Expand Up @@ -57,8 +57,8 @@ module.exports = function nextBabelWrapper(nextConfig, pluginOptions) {
}, [])
}
debug(`finish: extracting frontmatter for ${importPath}`)
}
}
},
},
}
}
}
Expand Down
Loading

0 comments on commit effa78a

Please sign in to comment.