no-document-import-in-page.js 977 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const path = require('path')
  2. const url = 'https://nextjs.org/docs/messages/no-document-import-in-page'
  3. module.exports = {
  4. meta: {
  5. docs: {
  6. description:
  7. 'Prevent importing `next/document` outside of `pages/_document.js`.',
  8. recommended: true,
  9. url,
  10. },
  11. type: 'problem',
  12. schema: [],
  13. },
  14. create: function (context) {
  15. return {
  16. ImportDeclaration(node) {
  17. if (node.source.value !== 'next/document') {
  18. return
  19. }
  20. const paths = context.getFilename().split('pages')
  21. const page = paths[paths.length - 1]
  22. if (
  23. !page ||
  24. page.startsWith(`${path.sep}_document`) ||
  25. page.startsWith(`${path.posix.sep}_document`)
  26. ) {
  27. return
  28. }
  29. context.report({
  30. node,
  31. message: `\`<Document />\` from \`next/document\` should not be imported outside of \`pages/_document.js\`. See: ${url}`,
  32. })
  33. },
  34. }
  35. },
  36. }