Skip to content

Commit

Permalink
Babel & next-swc: Fix exporting page config with AsExpression (vercel…
Browse files Browse the repository at this point in the history
…#32702)

fixes vercel#20626

No changes needed in next-swc, see comments from @kdy1 below.

Co-authored-by: JJ Kasper <[email protected]>
  • Loading branch information
2 people authored and natew committed Feb 16, 2022
1 parent e3e80a9 commit 1cd24d9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/next/build/babel/plugins/next-page-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ export default function nextPageConfig({
continue
}

if (!BabelTypes.isObjectExpression(declaration.init)) {
const got = declaration.init
? declaration.init.type
: 'undefined'
let { init } = declaration
if (BabelTypes.isTSAsExpression(init)) {
init = init.expression
}

if (!BabelTypes.isObjectExpression(init)) {
const got = init ? init.type : 'undefined'
throw new Error(
errorMessage(
exportState,
Expand All @@ -156,7 +159,7 @@ export default function nextPageConfig({
)
}

for (const prop of declaration.init.properties) {
for (const prop of init.properties) {
if (BabelTypes.isSpreadElement(prop)) {
throw new Error(
errorMessage(
Expand Down
17 changes: 17 additions & 0 deletions test/production/typescript-basic/app/pages/page-config.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Link from 'next/link'
import { PageConfig } from 'next'

export const config: PageConfig = {
unstable_runtimeJS: false,
}

export default function Page() {
return (
<>
<p>hello world</p>
<Link href="/another">
<a>to /another</a>
</Link>
</>
)
}
62 changes: 62 additions & 0 deletions test/unit/babel-plugin-next-page-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* eslint-env jest */
import { transformSync } from '@babel/core'

const babel = (code) =>
transformSync(code, {
filename: 'page.tsx',
presets: ['@babel/preset-typescript'],
plugins: [require('next/dist/build/babel/plugins/next-page-config')],
babelrc: false,
configFile: false,
sourceType: 'module',
compact: true,
caller: {
name: 'tests',
isDev: false,
},
} as any).code

describe('babel plugin (next-page-config)', () => {
test('export config with type annotation', () => {
const output = babel('export const config: PageConfig = {};')

expect(output).toMatch(`export const config={};`)
})

test('export config with AsExpression', () => {
const output = babel('export const config = {} as PageConfig;')

expect(output).toMatch('export const config={};')
})

test('amp enabled', () => {
jest.spyOn(Date, 'now').mockReturnValue(1234)
const output = babel(`
export const config = { amp: true }
function About(props) {
return <h3>My AMP About Page!</h3>
}
export default About`)

expect(output).toMatch(
'const __NEXT_DROP_CLIENT_FILE__="__NEXT_DROP_CLIENT_FILE__ 1234";'
)
})

test('amp hybrid enabled', () => {
const output = babel(`
export const config = { amp: 'hybrid' }
function About(props) {
return <h3>My AMP About Page!</h3>
}
export default About`)

expect(output).toMatch(
"export const config={amp:'hybrid'};function About(props){return<h3>My AMP About Page!</h3>;}export default About;"
)
})
})

0 comments on commit 1cd24d9

Please sign in to comment.