Unverified Commit 0fd1958a authored by Joris's avatar Joris Committed by GitHub

Disallow re-export all exports from Next.js pages (#14325)

Co-authored-by: 's avatarJoe Haddad <joe.haddad@zeit.co>
parent 3c04111c
# Re-exporting all exports from a page is disallowed
#### Why This Error Occurred
The following export can potentially break Next.js' compilation of pages:
```ts
export * from '...'
```
This is because Node.js code may be leaked to the browser build, causing an error. For example, the following two pages:
```ts
// pages/one.js
import fs from 'fs'
export default function A() {
return <main />
}
export function getStaticProps() {
fs
return { props: {} }
}
```
```ts
// pages/two.js
export * from './one'
```
Would cause cause the following error:
```
Module not found: Can't resolve 'fs' in './pages/two.js'
```
#### Possible Ways to Fix It
Update your page to re-export the default component only:
```ts
export { default } from './other-page'
```
If the other page uses `getServerSideProps` or `getStaticProps`, you can re-export those individually too:
```ts
export { default, getServerSideProps } from './other-page'
// or
export { default, getStaticProps } from './other-page'
// or
export { default, getStaticProps, getStaticPaths } from './other-page/[dynamic]'
```
import { NodePath, PluginObj, types } from '@babel/core'
export default function NextPageDisallowReExportAllExports(): PluginObj<any> {
return {
visitor: {
ExportAllDeclaration(path: NodePath<types.ExportAllDeclaration>) {
const err = new SyntaxError(
`Using \`export * from '...'\` in a page is disallowed. Please use \`export { default } from '...'\` instead.\n` +
`Read more: https://err.sh/next.js/export-all-in-page`
)
;(err as any).code = 'BABEL_PARSE_ERROR'
;(err as any).loc =
path.node.loc?.start ?? path.node.loc?.end ?? path.node.loc
throw err
},
},
}
}
......@@ -172,6 +172,14 @@ module.exports = babelLoader.custom((babel) => {
{ type: 'plugin' }
)
options.plugins.push(pageConfigPlugin)
const diallowExportAll = babel.createConfigItem(
[
require('../../babel/plugins/next-page-disallow-re-export-all-exports'),
],
{ type: 'plugin' }
)
options.plugins.push(diallowExportAll)
}
if (isServer && source.indexOf('next/data') !== -1) {
......
import fs from 'fs'
import findUp from 'find-up'
export default ({ world }) => <h1>About {world}</h1>
export async function getStaticProps() {
const text = fs
.readFileSync(
findUp.sync('world.txt', {
// eslint-disable-next-line no-eval
cwd: eval(`__dirname`),
}),
'utf8'
)
.trim()
return {
props: {
world: text,
},
}
}
import { a } from '../component/child'
export * from './about'
export default () => <h1>contact {a}</h1>
import { a } from '../component/child'
export default () => <h1>Hello world {a}</h1>
/* eslint-env jest */
import { File, nextBuild } from 'next-test-utils'
import { join } from 'path'
const appDir = join(__dirname, '..')
jest.setTimeout(1000 * 60 * 2)
describe('Re-export all exports from page is disallowed', () => {
it('shows error when a page re-export all exports', async () => {
const { code, stderr } = await nextBuild(appDir, undefined, {
stderr: true,
})
expect(code).toBe(1)
expect(stderr).toMatch(/https:\/\/err\.sh\/next\.js\/export-all-in-page/)
expect(stderr.split('\n\n')[1]).toMatchInlineSnapshot(`
"./pages/contact.js:3:1
Syntax error: Using \`export * from '...'\` in a page is disallowed. Please use \`export { default } from '...'\` instead.
Read more: https://err.sh/next.js/export-all-in-page"
`)
})
it('builds without error when no `export * from "..."` is used in pages', async () => {
const f = new File(join(appDir, 'pages', 'contact.js'))
f.replace(/^export \*/gm, '// export *')
try {
const { code, stderr } = await nextBuild(appDir, undefined, {
stderr: true,
})
expect(code).toBe(0)
expect(stderr).not.toMatch(
/https:\/\/err\.sh\/next\.js\/export-all-in-page/
)
} finally {
f.restore()
}
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment