Skip to content

Commit ee376a8

Browse files
committed
fix: revert fix force serverless target (#388)
1 parent 68e2b13 commit ee376a8

File tree

6 files changed

+91
-64
lines changed

6 files changed

+91
-64
lines changed

helpers/doesNotNeedPlugin.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
const findUp = require('find-up')
22

33
// Checks all the cases for which the plugin should do nothing
4-
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
54
const isStaticExportProject = require('./isStaticExportProject')
5+
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
6+
const hasCorrectNextConfig = require('./hasCorrectNextConfig')
67

7-
const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
8+
const doesNotNeedPlugin = async ({ netlifyConfig, packageJson, failBuild }) => {
89
const { build } = netlifyConfig
9-
const { scripts = {} } = packageJson
10+
const { name, scripts = {} } = packageJson
11+
const nextConfigPath = await findUp('next.config.js')
1012

11-
return isStaticExportProject({ build, scripts }) || doesSiteUseNextOnNetlify({ packageJson })
13+
return (
14+
isStaticExportProject({ build, scripts }) ||
15+
doesSiteUseNextOnNetlify({ packageJson }) ||
16+
!(await hasCorrectNextConfig({ nextConfigPath, failBuild }))
17+
)
1218
}
1319

1420
module.exports = doesNotNeedPlugin

helpers/getNextConfig.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const { cwd: getCwd } = require('process')
4+
const { resolve } = require('path')
45

56
const moize = require('moize')
67

helpers/hasCorrectNextConfig.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const getNextConfig = require('./getNextConfig')
2+
3+
// Checks if site has the correct next.config.js
4+
const hasCorrectNextConfig = async ({ nextConfigPath, failBuild }) => {
5+
// In the plugin's case, no config is valid because we'll make it ourselves
6+
if (nextConfigPath === undefined) return true
7+
8+
const { target } = await getNextConfig(failBuild)
9+
10+
// If the next config exists, log warning if target isnt in acceptableTargets
11+
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
12+
const isValidTarget = acceptableTargets.includes(target)
13+
if (!isValidTarget) {
14+
console.log(
15+
`Your next.config.js must set the "target" property to one of: ${acceptableTargets.join(', ')}. Update the
16+
target property to allow this plugin to run.`,
17+
)
18+
}
19+
20+
return isValidTarget
21+
}
22+
23+
module.exports = hasCorrectNextConfig

helpers/verifyBuildTarget.js

-38
This file was deleted.

index.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const util = require('util')
4+
5+
const findUp = require('find-up')
16
const makeDir = require('make-dir')
27

38
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
49
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
510
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
611
const getNextConfig = require('./helpers/getNextConfig')
712
const validateNextUsage = require('./helpers/validateNextUsage')
8-
const verifyBuildTarget = require('./helpers/verifyBuildTarget')
913
const nextOnNetlify = require('./src/index.js')
1014

15+
const pWriteFile = util.promisify(fs.writeFile)
16+
1117
// * Helpful Plugin Context *
1218
// - Between the prebuild and build steps, the project's build command is run
1319
// - Between the build and postbuild steps, any functions are bundled
@@ -23,13 +29,22 @@ module.exports = {
2329
return failBuild('Could not find a package.json for this project')
2430
}
2531

26-
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
27-
return
32+
const pluginNotNeeded = await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })
33+
34+
if (!pluginNotNeeded) {
35+
const nextConfigPath = await findUp('next.config.js')
36+
if (nextConfigPath === undefined) {
37+
// Create the next config file with target set to serverless by default
38+
const nextConfig = `
39+
module.exports = {
40+
target: 'serverless'
41+
}
42+
`
43+
await pWriteFile('next.config.js', nextConfig)
44+
}
2845
}
2946

30-
// Populates the correct config if needed
31-
await verifyBuildTarget({ netlifyConfig, packageJson, failBuild })
32-
47+
// Because we memoize nextConfig, we need to do this after the write file
3348
const nextConfig = await getNextConfig(utils.failBuild)
3449

3550
if (nextConfig.images.domains.length !== 0 && !process.env.NEXT_IMAGE_ALLOWED_DOMAINS) {
@@ -49,7 +64,7 @@ module.exports = {
4964
}) {
5065
const { failBuild } = utils.build
5166

52-
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
67+
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
5368
return
5469
}
5570

@@ -61,7 +76,7 @@ module.exports = {
6176
},
6277

6378
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST }, utils }) {
64-
if (doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
79+
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
6580
return
6681
}
6782

test/index.js

+34-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
const path = require('path')
22
const process = require('process')
3-
4-
const cpy = require('cpy')
53
const pathExists = require('path-exists')
64
const { dir: getTmpDir } = require('tmp-promise')
5+
const cpy = require('cpy')
76

87
const plugin = require('..')
9-
const getNextConfig = require('../helpers/getNextConfig')
108

119
const FIXTURES_DIR = `${__dirname}/fixtures`
1210
const SAMPLE_PROJECT_DIR = `${__dirname}/sample`
@@ -49,12 +47,6 @@ const useFixture = async function (fixtureName) {
4947
// In each test, we change cwd to a temporary directory.
5048
// This allows us not to have to mock filesystem operations.
5149
beforeEach(async () => {
52-
// This is so we can test the target setting code
53-
delete process.env.NEXT_PRIVATE_TARGET
54-
delete require.cache[require.resolve('next/dist/telemetry/ci-info')]
55-
delete require.cache[require.resolve('next/dist/next-server/server/config')]
56-
57-
getNextConfig.clear()
5850
const { path, cleanup } = await getTmpDir({ unsafeCleanup: true })
5951
const restoreCwd = changeCwd(path)
6052
Object.assign(this, { cleanup, restoreCwd })
@@ -74,6 +66,17 @@ const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
7466
const netlifyConfig = { build: {} }
7567

7668
describe('preBuild()', () => {
69+
test('create next.config.js with correct target if file does not exist', async () => {
70+
await plugin.onPreBuild({
71+
netlifyConfig,
72+
packageJson: DUMMY_PACKAGE_JSON,
73+
utils,
74+
constants: { FUNCTIONS_SRC: 'out_functions' },
75+
})
76+
77+
expect(await pathExists('next.config.js')).toBeTruthy()
78+
})
79+
7780
test('do nothing if the app has static html export in npm script', async () => {
7881
await plugin.onPreBuild({
7982
netlifyConfig: { build: { command: 'npm run build' } },
@@ -92,7 +95,8 @@ describe('preBuild()', () => {
9295
utils,
9396
constants: {},
9497
})
95-
expect(process.env.NEXT_PRIVATE_TARGET).toBe('serverless')
98+
99+
expect(await pathExists('next.config.js')).toBeTruthy()
96100
})
97101

98102
test('do nothing if app has static html export in toml/ntl config', async () => {
@@ -103,7 +107,7 @@ describe('preBuild()', () => {
103107
constants: { FUNCTIONS_SRC: 'out_functions' },
104108
})
105109

106-
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
110+
expect(await pathExists('next.config.js')).toBeFalsy()
107111
})
108112

109113
test('do nothing if app has next-on-netlify installed', async () => {
@@ -116,7 +120,7 @@ describe('preBuild()', () => {
116120
utils,
117121
})
118122

119-
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
123+
expect(await pathExists('next.config.js')).toBeFalsy()
120124
})
121125

122126
test('do nothing if app has next-on-netlify postbuild script', async () => {
@@ -129,7 +133,7 @@ describe('preBuild()', () => {
129133
utils,
130134
})
131135

132-
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
136+
expect(await pathExists('next.config.js')).toBeFalsy()
133137
})
134138

135139
test('fail build if the app has no package.json', async () => {
@@ -181,7 +185,6 @@ describe('preBuild()', () => {
181185
})
182186

183187
describe('onBuild()', () => {
184-
// eslint-disable-next-line max-lines
185188
test('does not run onBuild if using next-on-netlify', async () => {
186189
const packageJson = {
187190
scripts: { postbuild: 'next-on-netlify' },
@@ -199,6 +202,23 @@ describe('onBuild()', () => {
199202
expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeFalsy()
200203
})
201204

205+
test.each(['invalid_next_config', 'deep_invalid_next_config'])(
206+
`do nothing if the app's next config has an invalid target`,
207+
async (fixtureName) => {
208+
await useFixture(fixtureName)
209+
const PUBLISH_DIR = 'publish'
210+
await plugin.onBuild({
211+
netlifyConfig,
212+
packageJson: DUMMY_PACKAGE_JSON,
213+
utils,
214+
constants: { FUNCTIONS_SRC: 'out_functions' },
215+
utils,
216+
})
217+
218+
expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeFalsy()
219+
},
220+
)
221+
202222
test('copy files to the publish directory', async () => {
203223
await useFixture('publish_copy_files')
204224
await moveNextDist()

0 commit comments

Comments
 (0)