question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose named exports (of top-level pages)

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

We’re using next.js and MDX together to build a simple documentation site, and our page components are .mdx files. MDX supports meta data, which is attached to MDX components via named exports (see https://github.com/mdx-js/mdx#exports), but the next.js renderer doesn’t currently expose anything other than the default export, so there’s no way to access this meta data in _app.js.

Describe the solution you’d like

From looking at the next.js source code I think the easiest solution would be to attach the named exports to the Component function, in server/render.js:

let [ComponentExports, Document, App] = await Promise.all([
  requirePage(page, {dir, dist}),
  require(documentPath),
  require(appPath)
])

const Component = ComponentExports.default || ComponentExports
Component.exports = ComponentExports

…and in server/build/plugins/pages-plugin.js:

source.add(`__NEXT_REGISTER_PAGE('${routeName}', function() {
    var comp =
`)
source.add(modules)
source.add(`
    var page = comp.default
    page.exports = comp
    return { page: page }
  })
`)

Alternatively, instead of attached the named exports to the default export (the component) they could be passed through the renderer independently, but that would be a bigger change.

Describe alternatives you’ve considered

We’ve looked at tackling the issue by modifying the MDX library, but whilst most of our pages will be .mdx files, not all of them will. The solution suggested above would support a consistent approach in both MDX and regular page components.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Timercommented, Dec 23, 2019

Next.js has exposed all exports for a few versions. Closing as solved.

0reactions
balazsorban44commented, Jan 30, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

webpack - Error: Should not import the named export 'version ...
As noted in the comments, there may be cases where you do not want to expose your entire package.json file in the client...
Read more >
exported binding 'jsx' needs to refer to a top-level declared ...
A module can contain both named exports and a default export, and they can be imported together using import defaultExport, { namedExport1, namedExport3,...
Read more >
16. Modules - Exploring JS
Imports and exports must be at the top level; 16.3.4. Imports are hoisted; 16.3.5. ... Having both named exports and a default export...
Read more >
Documentation - Modules - TypeScript
Classes and function declarations can be authored directly as default exports. Default export class and function declaration names are optional.
Read more >
JavaScript modules - MDN Web Docs
The functionality we've exported so far has been comprised of named exports — each item (be it a function, const , etc.) has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found