Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 5c896ee

Browse files
committed
Add support for function in next.config.js
The configuration for NextJS in next.config.js can also be supplied as a function (in addition to an object). This commit adds support for reading the distDir option when the NextJS config is set by a function. See: #25
1 parent 8cca21a commit 5c896ee

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed

lib/getNextDistDir.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
// Get the NextJS distDir specified in next.config.js
2-
const { pathExistsSync } = require('fs-extra')
3-
const { resolve, join } = require('path')
4-
5-
// The default dist dir used by NextJS, if not specified otherwise by user
6-
const DEFAULT_DIST_DIR = join(".", ".next")
2+
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
3+
const { default: loadConfig } = require('next/dist/next-server/server/config')
4+
const { resolve, join } = require('path')
75

86
const getNextDistDir = ({ nextConfigPath }) => {
9-
// If next.config.js does not exists, default to NextJS' default distDir
10-
hasNextConfig = pathExistsSync(nextConfigPath)
11-
if(!hasNextConfig)
12-
return DEFAULT_DIST_DIR
13-
14-
// Read next.config.js
15-
const resolvedNextConfigPath = resolve(".", nextConfigPath)
16-
const nextConfig = require(resolvedNextConfigPath)
17-
18-
// If distDir is not set, default to NextJS' default distDir
19-
const hasDistDir = 'distDir' in nextConfig
20-
if(!hasDistDir)
21-
return DEFAULT_DIST_DIR
7+
// Load next.config.js
8+
// Use same code as https://github.com/vercel/next.js/blob/25488f4a03db30cade4d086ba49cd9a50a2ac02e/packages/next/build/index.ts#L114
9+
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, resolve("."))
2210

23-
// Return distDir specified by user
2411
return join(".", nextConfig.distDir)
2512
}
2613

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
"cypress:local:testonly": "env CYPRESS_SKIP_DEPLOY=true npm run cypress:local",
3232
"cypress:netlify:testonly": "env CYPRESS_SKIP_DEPLOY=true npm run cypress:netlify"
3333
},
34+
"peerDependencies": {
35+
"next": "9.x"
36+
},
3437
"devDependencies": {
3538
"cypress": "^4.9.0",
3639
"jest": "^26.1.0",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
2+
3+
module.exports = (phase, { defaultConfig }) => {
4+
// next-on-netlify uses settings from PHASE_PRODUCTION_BUILD
5+
// This is the same phase that is used when running `next build`
6+
if (phase === PHASE_PRODUCTION_BUILD) {
7+
return {
8+
target: 'serverless',
9+
distDir: '.myCustomDir'
10+
}
11+
}
12+
13+
// Default options
14+
return {}
15+
}

tests/nextConfigFunction.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Test next-on-netlify when config is set from a function in next.config.js
2+
// See: https://github.com/FinnWoelm/next-on-netlify/issues/25
3+
const { parse, join } = require('path')
4+
const { copySync, emptyDirSync, existsSync,
5+
readdirSync, readFileSync, readJsonSync } = require('fs-extra')
6+
const npmRunBuild = require("./helpers/npmRunBuild")
7+
8+
// The name of this test file (without extension)
9+
const FILENAME = parse(__filename).name
10+
11+
// The directory which will be used for testing.
12+
// We simulate a NextJS app within that directory, with pages, and a
13+
// package.json file.
14+
const PROJECT_PATH = join(__dirname, "builds", FILENAME)
15+
16+
// The directory that contains the fixtures, such as NextJS pages,
17+
// NextJS config, and package.json
18+
const FIXTURE_PATH = join(__dirname, "fixtures")
19+
20+
// Capture the output of `npm run build` to verify successful build
21+
let BUILD_OUTPUT
22+
23+
beforeAll(
24+
async () => {
25+
// Clear project directory
26+
emptyDirSync(PROJECT_PATH)
27+
emptyDirSync(join(PROJECT_PATH, "pages"))
28+
29+
// Copy NextJS pages and config
30+
copySync(
31+
join(FIXTURE_PATH, "pages"),
32+
join(PROJECT_PATH, "pages")
33+
)
34+
copySync(
35+
join(FIXTURE_PATH, "next.config.js-with-function.js"),
36+
join(PROJECT_PATH, "next.config.js")
37+
)
38+
39+
// Copy package.json
40+
copySync(
41+
join(FIXTURE_PATH, "package.json"),
42+
join(PROJECT_PATH, "package.json")
43+
)
44+
45+
// Invoke `npm run build`: Build Next and run next-on-netlify
46+
const { stdout } = await npmRunBuild({ directory: PROJECT_PATH })
47+
BUILD_OUTPUT = stdout
48+
},
49+
// time out after 180 seconds
50+
180 * 1000
51+
)
52+
53+
describe('Next', () => {
54+
test('builds successfully', () => {
55+
// NextJS output
56+
expect(BUILD_OUTPUT).toMatch("Creating an optimized production build...")
57+
expect(BUILD_OUTPUT).toMatch("Automatically optimizing pages...")
58+
expect(BUILD_OUTPUT).toMatch("First Load JS shared by all")
59+
60+
// Next on Netlify output
61+
expect(BUILD_OUTPUT).toMatch("Next on Netlify")
62+
expect(BUILD_OUTPUT).toMatch("Success! All done!")
63+
})
64+
})

0 commit comments

Comments
 (0)