Skip to content

Commit affd94a

Browse files
committed
feat: monorepo handling
1 parent bfb9018 commit affd94a

File tree

7 files changed

+10409
-207
lines changed

7 files changed

+10409
-207
lines changed

helpers/getNextRoot.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { existsSync } = require('fs')
2+
const path = require('path')
3+
4+
const getNextRoot = ({ netlifyConfig }) => {
5+
let nextRoot = process.cwd()
6+
if (!existsSync(path.join(nextRoot, 'next.config.js'))) {
7+
nextRoot = path.dirname(netlifyConfig.build.publish)
8+
}
9+
return nextRoot
10+
}
11+
12+
module.exports = getNextRoot

helpers/validateNextUsage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { lt: ltVersion, gte: gteVersion } = require('semver')
21
const { yellowBright } = require('chalk')
2+
const { lt: ltVersion, gte: gteVersion } = require('semver')
33

44
// Ensure Next.js is available.
55
// We use `peerDependencies` instead of `dependencies` so that users can choose

helpers/verifyBuildTarget.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
const getNextConfig = require('./getNextConfig')
2-
const findUp = require('find-up')
3-
const { writeFile, unlink } = require('fs-extra')
41
const path = require('path')
52

3+
const { writeFile } = require('fs-extra')
4+
5+
const getNextConfig = require('./getNextConfig')
6+
const getNextRoot = require('./getNextRoot')
7+
68
// Checks if site has the correct next.config.js
7-
const verifyBuildTarget = async ({ failBuild }) => {
8-
const { target } = await getNextConfig(failBuild)
9+
const verifyBuildTarget = async ({ failBuild, netlifyConfig }) => {
10+
const nextRoot = getNextRoot({ netlifyConfig })
11+
12+
const { target, configFile, ...rest } = await getNextConfig(failBuild, nextRoot)
13+
console.log({ target, rest, configFile, nextRoot })
914

1015
// If the next config exists, log warning if target isnt in acceptableTargets
1116
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
@@ -19,8 +24,6 @@ const verifyBuildTarget = async ({ failBuild }) => {
1924
)}". Building with "serverless" target.`,
2025
)
2126

22-
/* eslint-disable fp/no-delete, node/no-unpublished-require */
23-
2427
// We emulate Vercel so that we can set target to serverless if needed
2528
process.env.NOW_BUILDER = true
2629
// If no valid target is set, we use an internal Next env var to force it
@@ -29,30 +32,31 @@ const verifyBuildTarget = async ({ failBuild }) => {
2932
// 🐉 We need Next to recalculate "isZeitNow" var so we can set the target, but it's
3033
// set as an import side effect so we need to clear the require cache first. 🐲
3134
// https://github.com/vercel/next.js/blob/canary/packages/next/telemetry/ci-info.ts
35+
/* eslint-disable node/no-unpublished-require */
3236

3337
delete require.cache[require.resolve('next/dist/telemetry/ci-info')]
3438
delete require.cache[require.resolve('next/dist/next-server/server/config')]
3539

40+
/* eslint-enable node/no-unpublished-require */
41+
3642
// Clear memoized cache
3743
getNextConfig.clear()
3844

3945
// Creating a config file, because otherwise Next won't reload the config and pick up the new target
4046

41-
if (!(await findUp('next.config.js'))) {
47+
if (!configFile) {
4248
await writeFile(
4349
path.resolve('next.config.js'),
4450
`module.exports = {
45-
// Supported targets are "serverless" and "experimental-serverless-trace"
46-
target: "serverless"
47-
}`,
51+
// Supported targets are "serverless" and "experimental-serverless-trace"
52+
target: "serverless"
53+
}`,
4854
)
4955
}
5056
// Force the new config to be generated
51-
await getNextConfig(failBuild)
52-
57+
await getNextConfig(failBuild, nextRoot)
5358
// Reset the value in case something else is looking for it
5459
process.env.NOW_BUILDER = false
55-
/* eslint-enable fp/no-delete, node/no-unpublished-require */
5660
}
5761

5862
module.exports = verifyBuildTarget

index.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
const path = require('path')
2+
13
const makeDir = require('make-dir')
24

35
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
46
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
57
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
68
const getNextConfig = require('./helpers/getNextConfig')
9+
const getNextRoot = require('./helpers/getNextRoot')
710
const validateNextUsage = require('./helpers/validateNextUsage')
811
const verifyBuildTarget = require('./helpers/verifyBuildTarget')
912
const nextOnNetlify = require('./src')
10-
1113
// * Helpful Plugin Context *
1214
// - Between the prebuild and build steps, the project's build command is run
1315
// - Between the build and postbuild steps, any functions are bundled
@@ -29,9 +31,10 @@ module.exports = {
2931

3032
// Populates the correct config if needed
3133
await verifyBuildTarget({ netlifyConfig, packageJson, failBuild })
34+
const nextRoot = getNextRoot({ netlifyConfig })
3235

3336
// Because we memoize nextConfig, we need to do this after the write file
34-
const nextConfig = await getNextConfig(utils.failBuild)
37+
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
3538

3639
if (nextConfig.images.domains.length !== 0 && !process.env.NEXT_IMAGE_ALLOWED_DOMAINS) {
3740
console.log(
@@ -50,6 +53,8 @@ module.exports = {
5053
}) {
5154
const { failBuild } = utils.build
5255

56+
const nextRoot = getNextRoot({ netlifyConfig })
57+
5358
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
5459
return
5560
}
@@ -58,17 +63,22 @@ module.exports = {
5863

5964
await makeDir(PUBLISH_DIR)
6065

61-
await nextOnNetlify({ functionsDir: FUNCTIONS_SRC, publishDir: PUBLISH_DIR })
66+
await nextOnNetlify({
67+
functionsDir: path.resolve(FUNCTIONS_SRC),
68+
publishDir: netlifyConfig.build.publish,
69+
nextRoot,
70+
})
6271
},
6372

6473
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST }, utils }) {
6574
if (doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
6675
return
6776
}
77+
const nextRoot = getNextRoot({ netlifyConfig })
6878

69-
const nextConfig = await getNextConfig(utils.failBuild)
70-
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir })
71-
copyUnstableIncludedDirs({ nextConfig, functionsDist: FUNCTIONS_DIST })
79+
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
80+
await saveCache({ cache: utils.cache, distDir: path.join(nextRoot, nextConfig.distDir) })
81+
copyUnstableIncludedDirs({ nextConfig, functionsDist: path.resolve(FUNCTIONS_DIST) })
7282
},
7383
}
7484

0 commit comments

Comments
 (0)