Skip to content

Commit e140d44

Browse files
authored
feat: don't force target (#10)
* chore: don't force target * fix: add warning * fix: update tests
1 parent 8513880 commit e140d44

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

src/helpers/setIncludedFiles.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const setIncludedFiles = ({ netlifyConfig, distDir }) => {
1111
}
1212
netlifyConfig.functions[functionName].included_files.push(
1313
`${distDir}/server/**`,
14+
`${distDir}/serverless/**`,
1415
`${distDir}/*.json`,
1516
`${distDir}/BUILD_ID`,
1617
)

src/helpers/verifyBuildTarget.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const { yellowBright } = require('chalk')
22

33
const verifyBuildTarget = (target) => {
44
if (target !== 'server') {
5-
// NOTE: This will only work for Next > 11.1.2
6-
console.log(yellowBright`Forcing site to build with target: server`)
7-
process.env.NEXT_PRIVATE_TARGET = 'server'
5+
console.log(
6+
yellowBright`Setting target to ${target} is no longer required. You should check if target=server works for you.`,
7+
)
88
}
99
}
1010

test/index.js

+31-16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const getNextConfig = require('../src/helpers/getNextConfig')
1212
// const resolveNextModule = require('../src/helpers/resolveNextModule')
1313
const usesBuildCommand = require('../src/helpers/usesBuildCommand')
1414

15+
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } = require('../src/constants')
16+
1517
const FIXTURES_DIR = `${__dirname}/fixtures`
1618
const SAMPLE_PROJECT_DIR = `${__dirname}/../demo`
1719

@@ -36,6 +38,9 @@ const changeCwd = function (cwd) {
3638
}
3739
}
3840

41+
const prebuildHasRun = (netlifyConfig) =>
42+
netlifyConfig.functions[HANDLER_FUNCTION_NAME].included_files.some((file) => file.includes('BUILD_ID'))
43+
3944
// Move .next from sample project to current directory
4045
const moveNextDist = async function () {
4146
await cpy('.next/**', process.cwd(), { cwd: SAMPLE_PROJECT_DIR, parents: true, overwrite: false, dot: true })
@@ -74,6 +79,8 @@ beforeEach(async () => {
7479
Object.assign(this, { cleanup, restoreCwd })
7580
netlifyConfig.build.publish = path.join(process.cwd(), '.next')
7681
netlifyConfig.redirects = []
82+
netlifyConfig.functions[HANDLER_FUNCTION_NAME] && (netlifyConfig.functions[HANDLER_FUNCTION_NAME].included_files = [])
83+
netlifyConfig.functions[ODB_FUNCTION_NAME] && (netlifyConfig.functions[ODB_FUNCTION_NAME].included_files = [])
7784
await useFixture('serverless_next_config')
7885
})
7986

@@ -106,7 +113,7 @@ describe('preBuild()', () => {
106113
utils,
107114
constants: { IS_LOCAL: true, NETLIFY_BUILD_VERSION: '15.12.0' },
108115
})
109-
expect(process.env.NEXT_PRIVATE_TARGET).toBe('server')
116+
expect(prebuildHasRun(netlifyConfig)).toBeTruthy()
110117
})
111118

112119
test('do nothing if the app has no build command', async () => {
@@ -117,7 +124,7 @@ describe('preBuild()', () => {
117124
constants: { FUNCTIONS_SRC: 'out_functions' },
118125
})
119126

120-
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
127+
expect(prebuildHasRun(netlifyConfig)).toBeFalsy()
121128
})
122129

123130
test('do nothing if the app has static html export in npm script', async () => {
@@ -128,7 +135,7 @@ describe('preBuild()', () => {
128135
constants: { FUNCTIONS_SRC: 'out_functions' },
129136
})
130137

131-
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
138+
expect(prebuildHasRun(netlifyConfig)).toBeFalsy()
132139
})
133140

134141
test('run plugin if `NEXT_PLUGIN_FORCE_RUN` is set to true, even if next export is in script', async () => {
@@ -139,7 +146,7 @@ describe('preBuild()', () => {
139146
utils,
140147
constants: {},
141148
})
142-
expect(process.env.NEXT_PRIVATE_TARGET).toBe('server')
149+
expect(prebuildHasRun(netlifyConfig)).toBeTruthy()
143150
process.env.NEXT_PLUGIN_FORCE_RUN = undefined
144151
})
145152

@@ -151,7 +158,7 @@ describe('preBuild()', () => {
151158
utils,
152159
constants: {},
153160
})
154-
expect(process.env.NEXT_PRIVATE_TARGET).toBe('server')
161+
expect(prebuildHasRun(netlifyConfig)).toBeTruthy()
155162
process.env.NEXT_PLUGIN_FORCE_RUN = undefined
156163
})
157164

@@ -163,7 +170,7 @@ describe('preBuild()', () => {
163170
utils,
164171
constants: {},
165172
})
166-
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
173+
expect(prebuildHasRun(netlifyConfig)).toBeFalsy()
167174
process.env.NEXT_PLUGIN_FORCE_RUN = undefined
168175
})
169176

@@ -174,7 +181,7 @@ describe('preBuild()', () => {
174181
utils,
175182
constants: {},
176183
})
177-
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
184+
expect(prebuildHasRun(netlifyConfig)).toBeFalsy()
178185
})
179186

180187
test('do nothing if build command calls a script that includes "build-storybook"', async () => {
@@ -184,7 +191,7 @@ describe('preBuild()', () => {
184191
utils,
185192
constants: {},
186193
})
187-
expect(process.env.NEXT_PRIVATE_TARGET).toBeUndefined()
194+
expect(prebuildHasRun(netlifyConfig)).toBeFalsy()
188195
})
189196

190197
test('run plugin if app has build-storybook in an unused script', async () => {
@@ -194,7 +201,7 @@ describe('preBuild()', () => {
194201
utils,
195202
constants: {},
196203
})
197-
expect(process.env.NEXT_PRIVATE_TARGET).toBe('server')
204+
expect(prebuildHasRun(netlifyConfig)).toBeTruthy()
198205
})
199206

200207
test('fail build if the app has no package.json', async () => {
@@ -229,7 +236,7 @@ describe('preBuild()', () => {
229236
utils,
230237
})
231238

232-
expect(process.env.NEXT_PRIVATE_TARGET).toBe('server')
239+
expect(prebuildHasRun(netlifyConfig)).toBeTruthy()
233240
})
234241

235242
test('fail build if wrong custom publish dir', async () => {
@@ -241,8 +248,10 @@ describe('preBuild()', () => {
241248
packageJson,
242249
constants: {},
243250
utils,
244-
})
245-
).rejects.toThrow(`You set your publish directory to "${wrongPublishDir}". Your publish directory should be set to your distDir (defaults to .next or is configured in your next.config.js). If your site is rooted in a subdirectory, your publish directory should be {yourSiteRoot}/{distDir}.`)
251+
}),
252+
).rejects.toThrow(
253+
`You set your publish directory to "${wrongPublishDir}". Your publish directory should be set to your distDir (defaults to .next or is configured in your next.config.js). If your site is rooted in a subdirectory, your publish directory should be {yourSiteRoot}/{distDir}.`,
254+
)
246255
})
247256

248257
test('fail build if no publish dir', async () => {
@@ -252,8 +261,10 @@ describe('preBuild()', () => {
252261
packageJson,
253262
constants: {},
254263
utils,
255-
})
256-
).rejects.toThrow('You set your publish directory to "null". Your publish directory should be set to your distDir (defaults to .next or is configured in your next.config.js). If your site is rooted in a subdirectory, your publish directory should be {yourSiteRoot}/{distDir}.')
264+
}),
265+
).rejects.toThrow(
266+
'You set your publish directory to "null". Your publish directory should be set to your distDir (defaults to .next or is configured in your next.config.js). If your site is rooted in a subdirectory, your publish directory should be {yourSiteRoot}/{distDir}.',
267+
)
257268
})
258269

259270
test('restores cache with right paths', async () => {
@@ -286,9 +297,13 @@ describe('onBuild()', () => {
286297
utils,
287298
})
288299

289-
expect(await pathExists(path.resolve(`.netlify/internal-functions/___netlify-handler/___netlify-handler.js`))).toBeTruthy()
300+
expect(
301+
await pathExists(path.resolve(`.netlify/internal-functions/___netlify-handler/___netlify-handler.js`)),
302+
).toBeTruthy()
290303
expect(await pathExists(path.resolve(`.netlify/internal-functions/___netlify-handler/bridge.js`))).toBeTruthy()
291-
expect(await pathExists(path.resolve(`.netlify/internal-functions/___netlify-odb-handler/___netlify-odb-handler.js`))).toBeTruthy()
304+
expect(
305+
await pathExists(path.resolve(`.netlify/internal-functions/___netlify-odb-handler/___netlify-odb-handler.js`)),
306+
).toBeTruthy()
292307
expect(await pathExists(path.resolve(`.netlify/internal-functions/___netlify-odb-handler/bridge.js`))).toBeTruthy()
293308
})
294309

0 commit comments

Comments
 (0)