Skip to content

Commit 93349f1

Browse files
committed
feat: add Nx config checks
1 parent db57006 commit 93349f1

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

helpers/checkNxConfig.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const { existsSync } = require('fs')
2+
const { EOL } = require('os')
3+
const path = require('path')
4+
5+
const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBLISH_DIR } }) => {
6+
const errors = []
7+
if (nextConfig.distDir === '.next') {
8+
errors.push(
9+
"- When using Nx you must set a value for 'distDir' in your next.config.js, and the value cannot be '.next'",
10+
)
11+
}
12+
if (!PUBLISH_DIR.startsWith('apps')) {
13+
errors.push(
14+
"Please set the 'publish' value in your Netlify build config to a folder inside your app directory. e.g. 'apps/myapp/out'",
15+
)
16+
}
17+
18+
const expectedConfigFile = path.resolve(netlifyConfig.build.publish, '..', 'next.config.js')
19+
20+
if (expectedConfigFile !== nextConfig.configFile) {
21+
const confName = path.relative(process.cwd(), nextConfig.configFile)
22+
errors.push(
23+
`- Using incorrect config file '${confName}'. Expected to use '${path.relative(
24+
process.cwd(),
25+
expectedConfigFile,
26+
)}'`,
27+
)
28+
29+
if (existsSync(expectedConfigFile)) {
30+
errors.push(
31+
`Please move or delete '${confName}'${confName === 'next.config.js' ? ' from the root of your site' : ''}.`,
32+
)
33+
} else {
34+
errors.push(
35+
`Please move or delete '${confName}'${
36+
confName === 'next.config.js' ? ' from the root of your site' : ''
37+
}, and create '${path.relative(process.cwd(), expectedConfigFile)}' instead.`,
38+
)
39+
}
40+
}
41+
42+
if (errors.length !== 0) {
43+
failBuild(
44+
// TODO: Add ntl.fyi link to docs
45+
['Invalid configuration', ...errors, 'See the docs on using Nx with Netlify for more information'].join(EOL),
46+
)
47+
}
48+
}
49+
50+
module.exports = checkNxConfig

index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const makeDir = require('make-dir')
44

55
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
6+
const checkNxConfig = require('./helpers/checkNxConfig')
67
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
78
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
89
const getNextConfig = require('./helpers/getNextConfig')
@@ -15,7 +16,7 @@ const nextOnNetlify = require('./src')
1516
// - Between the build and postbuild steps, any functions are bundled
1617

1718
module.exports = {
18-
async onPreBuild({ netlifyConfig, packageJson, utils }) {
19+
async onPreBuild({ netlifyConfig, packageJson, utils, constants }) {
1920
const { failBuild } = utils.build
2021

2122
validateNextUsage(failBuild)
@@ -36,6 +37,15 @@ module.exports = {
3637
// Because we memoize nextConfig, we need to do this after the write file
3738
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
3839

40+
const isNx = Boolean(
41+
(packageJson.devDependencies && packageJson.devDependencies['@nrwl/next']) ||
42+
(packageJson.dependencies && packageJson.dependencies['@nrwl/next']),
43+
)
44+
45+
if (isNx) {
46+
checkNxConfig({ netlifyConfig, packageJson, nextConfig, failBuild, constants })
47+
}
48+
3949
if (nextConfig.images.domains.length !== 0 && !process.env.NEXT_IMAGE_ALLOWED_DOMAINS) {
4050
console.log(
4151
`Image domains set in next.config.js are ignored.\nPlease set the env variable NEXT_IMAGE_ALLOWED_DOMAINS to "${nextConfig.images.domains.join(

0 commit comments

Comments
 (0)