Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: build .mjs functions with esbuild #3253

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/lib/functions/runtimes/js/builders/zisi.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,18 @@ const getTargetDirectory = async ({ errorExit }) => {
}

module.exports = async ({ config, directory, errorExit, func, projectRoot }) => {
const isTSFunction = path.extname(func.mainFile) === '.ts'
const functionsConfig = addFunctionsConfigDefaults(
normalizeFunctionsConfig({ functionsConfig: config.functions, projectRoot }),
)

// We must use esbuild for certain file extensions.
const mustUseEsbuild = ['.mjs', '.ts'].includes(path.extname(func.mainFile))

// TODO: Resolve functions config globs so that we can check for the bundler
// on a per-function basis.
const isUsingEsbuild = functionsConfig['*'].nodeBundler === 'esbuild_zisi'

if (!isTSFunction && !isUsingEsbuild) {
if (!mustUseEsbuild && !isUsingEsbuild) {
return false
}

Expand Down
35 changes: 35 additions & 0 deletions tests/serving-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,41 @@ export { handler }
})
})
})

test(testName('Serves functions with a `.mjs` extension', args), async (t) => {
await withSiteBuilder('function-mjs', async (builder) => {
const bundlerConfig = args.includes('esbuild') ? { node_bundler: 'esbuild' } : {}

await builder
.withNetlifyToml({
config: {
build: { publish: 'public' },
functions: { directory: 'functions' },
...bundlerConfig,
},
})
.withContentFile({
path: 'functions/hello.mjs',
content: `
const handler = async () => {
return {
statusCode: 200,
body: 'Hello, world!'
}
}

export { handler }
`,
})
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
await tryAndLogOutput(async () => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'Hello, world!')
}, outputBuffer)
})
})
})
})

test('Serves functions that dynamically load files included in the `functions.included_files` config property', async (t) => {
Expand Down