6d0150f02e
Adds lint rules to the Next.js ESLint plugin to: - Disallow importing `next/head` inside `pages/_document.js` - Disallow importing `next/document` outside of `pages/_document.js` Both rules will be surfaced as **errors** within the recommended config of the plugin. Fixes #13712 #13958
30 lines
737 B
JavaScript
30 lines
737 B
JavaScript
const path = require('path')
|
|
|
|
module.exports = {
|
|
meta: {
|
|
docs: {
|
|
description:
|
|
'Disallow importing next/document outside of pages/document.js',
|
|
recommended: true,
|
|
},
|
|
},
|
|
create: function (context) {
|
|
return {
|
|
ImportDeclaration(node) {
|
|
if (node.source.value !== 'next/document') {
|
|
return
|
|
}
|
|
|
|
const page = context.getFilename().split('pages')[1]
|
|
if (!page || path.parse(page).name === '_document') {
|
|
return
|
|
}
|
|
|
|
context.report({
|
|
node,
|
|
message: `next/document should not be imported outside of pages/_document.js. See https://nextjs.org/docs/messages/no-document-import-in-page.`,
|
|
})
|
|
},
|
|
}
|
|
},
|
|
}
|