-
Notifications
You must be signed in to change notification settings - Fork 88
feat: monorepo handling #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
affd94a
db57006
93349f1
f3e671d
f44e242
08d1bfe
71b5c65
0b3fff6
bdd9bee
74518c8
d34cccd
42a710d
b5c992a
759cd09
ffdf020
09ee497
de068e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const { existsSync } = require('fs') | ||
const { EOL } = require('os') | ||
const path = require('path') | ||
|
||
const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBLISH_DIR } }) => { | ||
const errors = [] | ||
if (nextConfig.distDir === '.next') { | ||
errors.push( | ||
"- When using Nx you must set a value for 'distDir' in your next.config.js, and the value cannot be '.next'", | ||
) | ||
} | ||
if (!PUBLISH_DIR.startsWith('apps')) { | ||
errors.push( | ||
"Please set the 'publish' value in your Netlify build config to a folder inside your app directory. e.g. 'apps/myapp/out'", | ||
) | ||
} | ||
|
||
const expectedConfigFile = path.resolve(netlifyConfig.build.publish, '..', 'next.config.js') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🥴 |
||
|
||
if (expectedConfigFile !== nextConfig.configFile) { | ||
const confName = path.relative(process.cwd(), nextConfig.configFile) | ||
errors.push( | ||
`- Using incorrect config file '${confName}'. Expected to use '${path.relative( | ||
process.cwd(), | ||
expectedConfigFile, | ||
)}'`, | ||
) | ||
|
||
if (existsSync(expectedConfigFile)) { | ||
errors.push( | ||
`Please move or delete '${confName}'${confName === 'next.config.js' ? ' from the root of your site' : ''}.`, | ||
) | ||
} else { | ||
errors.push( | ||
`Please move or delete '${confName}'${ | ||
confName === 'next.config.js' ? ' from the root of your site' : '' | ||
}, and create '${path.relative(process.cwd(), expectedConfigFile)}' instead.`, | ||
) | ||
} | ||
} | ||
|
||
if (errors.length !== 0) { | ||
failBuild( | ||
// TODO: Add ntl.fyi link to docs | ||
['Invalid configuration', ...errors, 'See the docs on using Nx with Netlify for more information'].join(EOL), | ||
) | ||
} | ||
} | ||
|
||
module.exports = checkNxConfig |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { existsSync } = require('fs') | ||
const path = require('path') | ||
|
||
const getNextRoot = ({ netlifyConfig }) => { | ||
let nextRoot = process.cwd() | ||
if (!existsSync(path.join(nextRoot, 'next.config.js')) && netlifyConfig.build.publish) { | ||
nextRoot = path.dirname(netlifyConfig.build.publish) | ||
} | ||
return nextRoot | ||
} | ||
ascorbic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports = getNextRoot |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
const path = require('path') | ||
|
||
const makeDir = require('make-dir') | ||
|
||
const { restoreCache, saveCache } = require('./helpers/cacheBuild') | ||
const checkNxConfig = require('./helpers/checkNxConfig') | ||
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs') | ||
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin') | ||
const getNextConfig = require('./helpers/getNextConfig') | ||
const getNextRoot = require('./helpers/getNextRoot') | ||
const validateNextUsage = require('./helpers/validateNextUsage') | ||
const verifyBuildTarget = require('./helpers/verifyBuildTarget') | ||
const nextOnNetlify = require('./src') | ||
|
||
// * Helpful Plugin Context * | ||
// - Between the prebuild and build steps, the project's build command is run | ||
// - Between the build and postbuild steps, any functions are bundled | ||
|
||
module.exports = { | ||
async onPreBuild({ netlifyConfig, packageJson, utils }) { | ||
async onPreBuild({ netlifyConfig, packageJson, utils, constants }) { | ||
const { failBuild } = utils.build | ||
|
||
validateNextUsage(failBuild) | ||
|
@@ -29,9 +32,19 @@ module.exports = { | |
|
||
// Populates the correct config if needed | ||
await verifyBuildTarget({ netlifyConfig, packageJson, failBuild }) | ||
const nextRoot = getNextRoot({ netlifyConfig }) | ||
|
||
// Because we memoize nextConfig, we need to do this after the write file | ||
const nextConfig = await getNextConfig(utils.failBuild) | ||
const nextConfig = await getNextConfig(utils.failBuild, nextRoot) | ||
|
||
const isNx = Boolean( | ||
(packageJson.devDependencies && packageJson.devDependencies['@nrwl/next']) || | ||
(packageJson.dependencies && packageJson.dependencies['@nrwl/next']), | ||
) | ||
|
||
if (isNx) { | ||
checkNxConfig({ netlifyConfig, packageJson, nextConfig, failBuild, constants }) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clean |
||
|
||
if (nextConfig.images.domains.length !== 0 && !process.env.NEXT_IMAGE_ALLOWED_DOMAINS) { | ||
console.log( | ||
|
@@ -50,6 +63,8 @@ module.exports = { | |
}) { | ||
const { failBuild } = utils.build | ||
|
||
const nextRoot = getNextRoot({ netlifyConfig }) | ||
|
||
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) { | ||
return | ||
} | ||
|
@@ -58,18 +73,24 @@ module.exports = { | |
|
||
await makeDir(PUBLISH_DIR) | ||
|
||
await nextOnNetlify({ functionsDir: FUNCTIONS_SRC, publishDir: PUBLISH_DIR }) | ||
await nextOnNetlify({ | ||
functionsDir: path.resolve(FUNCTIONS_SRC), | ||
publishDir: netlifyConfig.build.publish, | ||
nextRoot, | ||
}) | ||
}, | ||
|
||
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST }, utils }) { | ||
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST = DEFAULT_FUNCTIONS_DIST }, utils }) { | ||
if (doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) { | ||
return | ||
} | ||
const nextRoot = getNextRoot({ netlifyConfig }) | ||
|
||
const nextConfig = await getNextConfig(utils.failBuild) | ||
const nextConfig = await getNextConfig(utils.failBuild, nextRoot) | ||
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir }) | ||
copyUnstableIncludedDirs({ nextConfig, functionsDist: FUNCTIONS_DIST }) | ||
copyUnstableIncludedDirs({ nextConfig, functionsDist: path.resolve(FUNCTIONS_DIST) }) | ||
}, | ||
} | ||
|
||
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions' | ||
const DEFAULT_FUNCTIONS_DIST = '.netlify/functions/' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
[build] | ||
base="demo/" | ||
command = "npm run build:demo" | ||
functions = "demo/netlify/functions" | ||
publish = "demo/out" | ||
base="." | ||
|
||
[dev] | ||
command="npm run dev:demo" | ||
|
||
[[plugins]] | ||
package="." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wowee |
Uh oh!
There was an error while loading. Please reload this page.