Skip to content

Commit 81fbece

Browse files
authored
feat: enforce zisi when target is not serverless (#22)
* feat: add bundler checks * feat: add tests
1 parent 0ee5b35 commit 81fbece

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

src/helpers/config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,26 @@ exports.setIncludedFiles = ({ netlifyConfig, publish }) => {
124124
)
125125
})
126126
}
127+
128+
exports.setBundler = ({ netlifyConfig, target }) => {
129+
if (target === 'serverless') {
130+
// Any bundler works with serverless
131+
return
132+
}
133+
134+
// Not quite such a simple a check, as we need to check both the default bundler
135+
// and whether any of the individual functions are using the esbuild bundler.
136+
const handlers = [HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME]
137+
const defaultIsEsbuild = Boolean(netlifyConfig.functions['*']?.node_bundler === 'esbuild')
138+
const handlerBundlers = handlers.map((func) => netlifyConfig.functions[func]?.node_bundler)
139+
if (
140+
(defaultIsEsbuild && !handlerBundlers.every((bundler) => bundler === 'zisi')) ||
141+
handlerBundlers.includes('esbuild')
142+
) {
143+
console.warn(`esbuild is not supported for target=${target}. Setting to "zisi"`)
144+
}
145+
handlers.forEach((func) => {
146+
netlifyConfig.functions[func] ||= {}
147+
netlifyConfig.functions[func].node_bundler = 'zisi'
148+
})
149+
}

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { copy } = require('fs-extra')
66

77
const { ODB_FUNCTION_NAME, HANDLER_FUNCTION_NAME } = require('./constants')
88
const { restoreCache, saveCache } = require('./helpers/cache')
9-
const { getNextConfig, setIncludedFiles, generateRedirects } = require('./helpers/config')
9+
const { getNextConfig, setIncludedFiles, generateRedirects, setBundler } = require('./helpers/config')
1010
const { generateFunctions, setupImageFunction } = require('./helpers/functions')
1111
const {
1212
verifyNetlifyBuildVersion,
@@ -44,6 +44,8 @@ module.exports = {
4444

4545
const { images, target, appDir } = await getNextConfig({ publish, failBuild })
4646

47+
setBundler({ netlifyConfig, target })
48+
4749
verifyBuildTarget(target)
4850

4951
setIncludedFiles({ netlifyConfig, publish })

test/index.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { dir: getTmpDir } = require('tmp-promise')
88
const plugin = require('../src')
99

1010
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } = require('../src/constants')
11+
const { setBundler } = require('../src/helpers/config')
1112

1213
const FIXTURES_DIR = `${__dirname}/fixtures`
1314
const SAMPLE_PROJECT_DIR = `${__dirname}/../demo`
@@ -191,3 +192,84 @@ describe('onPostBuild', () => {
191192
})
192193
})
193194
})
195+
196+
describe('target checking', () => {
197+
const warn = jest.spyOn(global.console, 'warn')
198+
beforeEach(() => {
199+
warn.mockReset()
200+
})
201+
test('changes nothing if target is serverless', () => {
202+
const netlifyConfig = {
203+
functions: {
204+
[HANDLER_FUNCTION_NAME]: {
205+
node_bundler: 'esbuild',
206+
},
207+
},
208+
}
209+
setBundler({ target: 'serverless', netlifyConfig })
210+
expect(netlifyConfig.functions[HANDLER_FUNCTION_NAME].node_bundler).toBe('esbuild')
211+
})
212+
213+
test('changes both bundlers if target is server and default is esbuild', () => {
214+
const netlifyConfig = {
215+
functions: {
216+
'*': {
217+
node_bundler: 'esbuild',
218+
},
219+
},
220+
}
221+
setBundler({ target: 'server', netlifyConfig })
222+
expect(warn).toHaveBeenCalledWith(`esbuild is not supported for target=server. Setting to "zisi"`)
223+
expect(netlifyConfig.functions[HANDLER_FUNCTION_NAME].node_bundler).toBe('zisi')
224+
expect(netlifyConfig.functions[ODB_FUNCTION_NAME].node_bundler).toBe('zisi')
225+
})
226+
227+
test('does not warn if default bundler is zisi and target is server', () => {
228+
const netlifyConfig = {
229+
functions: {
230+
'*': {
231+
node_bundler: 'zisi',
232+
},
233+
},
234+
}
235+
setBundler({ target: 'server', netlifyConfig })
236+
expect(warn).not.toHaveBeenCalled()
237+
})
238+
239+
test('changes both bundlers if default is undefined and both are esbuild', () => {
240+
const netlifyConfig = {
241+
functions: {
242+
'*': {},
243+
[HANDLER_FUNCTION_NAME]: {
244+
node_bundler: 'esbuild',
245+
},
246+
[ODB_FUNCTION_NAME]: {
247+
node_bundler: 'esbuild',
248+
},
249+
},
250+
}
251+
setBundler({ target: 'server', netlifyConfig })
252+
expect(warn).toHaveBeenCalledWith(`esbuild is not supported for target=server. Setting to "zisi"`)
253+
254+
expect(netlifyConfig.functions[HANDLER_FUNCTION_NAME].node_bundler).toBe('zisi')
255+
expect(netlifyConfig.functions[ODB_FUNCTION_NAME].node_bundler).toBe('zisi')
256+
})
257+
258+
test('does not warn if default is esbuild but others are both zisi', () => {
259+
const netlifyConfig = {
260+
functions: {
261+
'*': {
262+
node_bundler: 'esbuild',
263+
},
264+
[HANDLER_FUNCTION_NAME]: {
265+
node_bundler: 'zisi',
266+
},
267+
[ODB_FUNCTION_NAME]: {
268+
node_bundler: 'zisi',
269+
},
270+
},
271+
}
272+
setBundler({ target: 'server', netlifyConfig })
273+
expect(warn).not.toHaveBeenCalled()
274+
})
275+
})

0 commit comments

Comments
 (0)