Skip to content

Commit 21566bd

Browse files
committed
update failure points in plugin to do nothing instead
1 parent 19228c4 commit 21566bd

6 files changed

+163
-126
lines changed

helpers/doesNotNeedPlugin.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Checks all the cases for which the plugin should do nothing
2+
const isStaticExportProject = require('./isStaticExportProject')
3+
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
4+
const hasCorrectNextConfig = require('./hasCorrectNextConfig')
5+
6+
const doesNotNeedPlugin = ({ netlifyConfig, packageJson, nextConfigPath }) => {
7+
const { build } = netlifyConfig
8+
const { name, scripts = {}, dependencies = {} } = packageJson
9+
10+
const hasNoPackageJson = Object.keys(packageJson).length === 0
11+
if (hasNoPackageJson) {
12+
console.log('Could not find a package.json for this project')
13+
}
14+
15+
return (
16+
isStaticExportProject({ build, scripts }) ||
17+
hasNoPackageJson ||
18+
doesSiteUseNextOnNetlify({ packageJson }) ||
19+
!hasCorrectNextConfig(nextConfigPath)
20+
)
21+
}
22+
23+
module.exports = doesNotNeedPlugin

helpers/doesSiteUseNextOnNetlify.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Checks if site is already using next-on-netlify
2+
const { name: pluginName } = require('../package.json')
3+
4+
const doesSiteUseNextOnNetlify = ({ packageJson }) => {
5+
const { name, scripts = {}, dependencies = {} } = packageJson
6+
7+
const hasNextOnNetlifyInstalled = dependencies['next-on-netlify'] !== undefined
8+
const hasNextOnNetlifyPostbuildScript =
9+
typeof scripts.postbuild === 'string' && scripts.postbuild.includes('next-on-netlify')
10+
const isUsingNextOnNetlify = (hasNextOnNetlifyInstalled || hasNextOnNetlifyPostbuildScript) && pluginName !== name
11+
if (isUsingNextOnNetlify) {
12+
console.log(
13+
`This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency and/or remove it from your postbuild script to allow this plugin to run.`,
14+
)
15+
}
16+
17+
return isUsingNextOnNetlify
18+
}
19+
20+
module.exports = doesSiteUseNextOnNetlify

helpers/hasCorrectNextConfig.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Checks if site has the correct next.cofig.js
2+
const hasCorrectNextConfig = (nextConfigPath) => {
3+
// In the plugin's case, no config is valid because we'll make it ourselves
4+
if (nextConfigPath === undefined) return true
5+
6+
// We cannot load `next` at the top-level because we validate whether the
7+
// site is using `next` inside `onPreBuild`.
8+
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
9+
const { default: loadConfig } = require('next/dist/next-server/server/config')
10+
11+
// If the next config exists, log warning if target isnt in acceptableTargets
12+
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
13+
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
14+
const isValidTarget = acceptableTargets.includes(nextConfig.target)
15+
if (!isValidTarget) {
16+
console.log(
17+
`Your next.config.js must set the "target" property to one of: ${acceptableTargets.join(', ')}. Update the
18+
target property to allow this plugin to run.`,
19+
)
20+
}
21+
22+
return isValidTarget
23+
}
24+
25+
module.exports = hasCorrectNextConfig

helpers/isStaticExportProject.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Takes 1. Netlify config's build details and
22
// 2. the project's package.json scripts to determine if
33
// the Next.js app uses static HTML export
4-
54
const isStaticExportProject = ({ build, scripts }) => {
65
const NEXT_EXPORT_COMMAND = 'next export'
76

index.js

+10-44
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const fs = require('fs')
22
const path = require('path')
33
const util = require('util')
4-
54
const findUp = require('find-up')
65
const makeDir = require('make-dir')
76

8-
const { name: pluginName } = require('./package.json')
9-
const isStaticExportProject = require('./helpers/isStaticExportProject')
107
const validateNextUsage = require('./helpers/validateNextUsage')
8+
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
119

1210
const pWriteFile = util.promisify(fs.writeFile)
1311

@@ -18,61 +16,29 @@ const pWriteFile = util.promisify(fs.writeFile)
1816
module.exports = {
1917
async onPreBuild({ netlifyConfig, packageJson, utils }) {
2018
const { failBuild } = utils.build
19+
const nextConfigPath = await findUp('next.config.js')
2120

2221
validateNextUsage(failBuild)
2322

24-
if (Object.keys(packageJson).length === 0) {
25-
return failBuild(`Could not find a package.json for this project`)
26-
}
27-
28-
const { build } = netlifyConfig
29-
const { name, scripts = {}, dependencies = {} } = packageJson
30-
31-
if (isStaticExportProject({ build, scripts })) {
32-
return failBuild(
33-
`Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`,
34-
)
23+
if (doesNotNeedPlugin({ netlifyConfig, packageJson, nextConfigPath })) {
24+
return
3525
}
3626

37-
const hasNextOnNetlifyInstalled = dependencies['next-on-netlify'] !== undefined
38-
const hasNextOnNetlifyPostbuildScript =
39-
typeof scripts.postbuild === 'string' && scripts.postbuild.includes('next-on-netlify')
40-
const isAlreadyUsingNextOnNetlify =
41-
(hasNextOnNetlifyInstalled || hasNextOnNetlifyPostbuildScript) && pluginName !== name
42-
if (isAlreadyUsingNextOnNetlify) {
43-
return failBuild(
44-
`This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`,
45-
)
46-
}
47-
48-
const nextConfigPath = await findUp('next.config.js')
49-
if (nextConfigPath !== undefined) {
50-
// We cannot load `next` at the top-level because we validate whether the
51-
// site is using `next` inside `onPreBuild`.
52-
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
53-
const { default: loadConfig } = require('next/dist/next-server/server/config')
54-
55-
// If the next config exists, fail build if target isnt in acceptableTargets
56-
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
57-
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
58-
const isValidTarget = acceptableTargets.includes(nextConfig.target)
59-
if (!isValidTarget) {
60-
return failBuild(
61-
`Your next.config.js must set the "target" property to one of: ${acceptableTargets.join(', ')}`,
62-
)
63-
}
64-
} else {
27+
if (nextConfigPath === undefined) {
6528
// Create the next config file with target set to serverless by default
6629
const nextConfig = `
6730
module.exports = {
6831
target: 'serverless'
6932
}
7033
`
7134
await pWriteFile('next.config.js', nextConfig)
72-
console.log(`** Adding next.config.js with target set to 'serverless' **`)
7335
}
7436
},
75-
async onBuild({ constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } }) {
37+
async onBuild({ netlifyConfig, packageJson, constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } }) {
38+
if (doesNotNeedPlugin({ netlifyConfig, packageJson })) {
39+
return
40+
}
41+
7642
console.log(`** Running Next on Netlify package **`)
7743

7844
await makeDir(PUBLISH_DIR)

test/index.js

+85-81
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
6161
const netlifyConfig = { build: {} }
6262

6363
describe('preBuild()', () => {
64-
test('fail build if the app has static html export in npm script', async () => {
65-
await expect(
66-
plugin.onPreBuild({
67-
netlifyConfig: { build: { command: 'npm run build' } },
68-
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
69-
utils,
70-
constants: { FUNCTIONS_SRC: 'out_functions' },
71-
}),
72-
).rejects.toThrow(
73-
`Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`,
74-
)
75-
})
64+
// test('fail build if the app has static html export in npm script', async () => {
65+
// await expect(
66+
// plugin.onPreBuild({
67+
// netlifyConfig: { build: { command: 'npm run build' } },
68+
// packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
69+
// utils,
70+
// constants: { FUNCTIONS_SRC: 'out_functions' },
71+
// }),
72+
// ).rejects.toThrow(
73+
// `Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`,
74+
// )
75+
// })
7676

7777
test('do not fail build if the app has next export in an unused script', async () => {
7878
await expect(
@@ -85,59 +85,59 @@ describe('preBuild()', () => {
8585
).resolves
8686
})
8787

88-
test('fail build if the app has static html export in toml/ntl config', async () => {
89-
await expect(
90-
plugin.onPreBuild({
91-
netlifyConfig: { build: { command: 'next build && next export' } },
92-
packageJson: DUMMY_PACKAGE_JSON,
93-
utils,
94-
constants: { FUNCTIONS_SRC: 'out_functions' },
95-
}),
96-
).rejects.toThrow(
97-
`Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`,
98-
)
99-
})
100-
101-
test('fail build if app has next-on-netlify installed', async () => {
102-
const packageJson = {
103-
dependencies: { 'next-on-netlify': '123' },
104-
}
105-
await expect(
106-
plugin.onPreBuild({
107-
netlifyConfig,
108-
packageJson,
109-
utils,
110-
}),
111-
).rejects.toThrow(
112-
`This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`,
113-
)
114-
})
115-
116-
test('fail build if app has next-on-netlify postbuild script', async () => {
117-
const packageJson = {
118-
scripts: { postbuild: 'next-on-netlify' },
119-
}
120-
await expect(
121-
plugin.onPreBuild({
122-
netlifyConfig,
123-
packageJson,
124-
utils,
125-
}),
126-
).rejects.toThrow(
127-
`This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`,
128-
)
129-
})
130-
131-
test('fail build if the app has no package.json', async () => {
132-
await expect(
133-
plugin.onPreBuild({
134-
netlifyConfig,
135-
packageJson: {},
136-
utils,
137-
constants: { FUNCTIONS_SRC: 'out_functions' },
138-
}),
139-
).rejects.toThrow(`Could not find a package.json for this project`)
140-
})
88+
// test('fail build if the app has static html export in toml/ntl config', async () => {
89+
// await expect(
90+
// plugin.onPreBuild({
91+
// netlifyConfig: { build: { command: 'next build && next export' } },
92+
// packageJson: DUMMY_PACKAGE_JSON,
93+
// utils,
94+
// constants: { FUNCTIONS_SRC: 'out_functions' },
95+
// }),
96+
// ).rejects.toThrow(
97+
// `Static HTML export Next.js projects do not require this plugin. Check your project's build command for 'next export'.`,
98+
// )
99+
// })
100+
101+
// test('fail build if app has next-on-netlify installed', async () => {
102+
// const packageJson = {
103+
// dependencies: { 'next-on-netlify': '123' },
104+
// }
105+
// await expect(
106+
// plugin.onPreBuild({
107+
// netlifyConfig,
108+
// packageJson,
109+
// utils,
110+
// }),
111+
// ).rejects.toThrow(
112+
// `This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`,
113+
// )
114+
// })
115+
116+
// test('fail build if app has next-on-netlify postbuild script', async () => {
117+
// const packageJson = {
118+
// scripts: { postbuild: 'next-on-netlify' },
119+
// }
120+
// await expect(
121+
// plugin.onPreBuild({
122+
// netlifyConfig,
123+
// packageJson,
124+
// utils,
125+
// }),
126+
// ).rejects.toThrow(
127+
// `This plugin does not support sites that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`,
128+
// )
129+
// })
130+
131+
// test('fail build if the app has no package.json', async () => {
132+
// await expect(
133+
// plugin.onPreBuild({
134+
// netlifyConfig,
135+
// packageJson: {},
136+
// utils,
137+
// constants: { FUNCTIONS_SRC: 'out_functions' },
138+
// }),
139+
// ).rejects.toThrow(`Could not find a package.json for this project`)
140+
// })
141141

142142
test('create next.config.js with correct target if file does not exist', async () => {
143143
await plugin.onPreBuild({
@@ -150,22 +150,22 @@ describe('preBuild()', () => {
150150
expect(await pathExists('next.config.js')).toBeTruthy()
151151
})
152152

153-
test.each(['invalid_next_config', 'deep_invalid_next_config'])(
154-
`fail build if the app's next config has an invalid target`,
155-
async (fixtureName) => {
156-
await useFixture(fixtureName)
157-
await expect(
158-
plugin.onPreBuild({
159-
netlifyConfig,
160-
packageJson: DUMMY_PACKAGE_JSON,
161-
utils,
162-
constants: { FUNCTIONS_SRC: 'out_functions' },
163-
}),
164-
).rejects.toThrow(
165-
`Your next.config.js must set the "target" property to one of: serverless, experimental-serverless-trace`,
166-
)
167-
},
168-
)
153+
// test.each(['invalid_next_config', 'deep_invalid_next_config'])(
154+
// `fail build if the app's next config has an invalid target`,
155+
// async (fixtureName) => {
156+
// await useFixture(fixtureName)
157+
// await expect(
158+
// plugin.onPreBuild({
159+
// netlifyConfig,
160+
// packageJson: DUMMY_PACKAGE_JSON,
161+
// utils,
162+
// constants: { FUNCTIONS_SRC: 'out_functions' },
163+
// }),
164+
// ).rejects.toThrow(
165+
// `Your next.config.js must set the "target" property to one of: serverless, experimental-serverless-trace`,
166+
// )
167+
// },
168+
// )
169169
})
170170

171171
describe('onBuild()', () => {
@@ -174,6 +174,8 @@ describe('onBuild()', () => {
174174
await moveNextDist()
175175
const PUBLISH_DIR = 'publish'
176176
await plugin.onBuild({
177+
netlifyConfig,
178+
packageJson: DUMMY_PACKAGE_JSON,
177179
constants: {
178180
PUBLISH_DIR,
179181
FUNCTIONS_SRC: 'functions',
@@ -191,6 +193,8 @@ describe('onBuild()', () => {
191193
await useFixture('functions_copy_files')
192194
await moveNextDist()
193195
await plugin.onBuild({
196+
netlifyConfig,
197+
packageJson: DUMMY_PACKAGE_JSON,
194198
constants: {
195199
FUNCTIONS_SRC,
196200
PUBLISH_DIR: '.',

0 commit comments

Comments
 (0)