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

Babel & next-swc: Fix exporting page config with AsExpression #32702

Merged
merged 7 commits into from Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/next/build/babel/plugins/next-page-config.ts
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
@@ -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
@@ -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;"
)
})
})