Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: allow .mjs functions (#619)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
eduardoboucas and kodiakhq[bot] committed Aug 24, 2021
1 parent 452f7ee commit 73456ff
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/runtimes/node/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ const getMainFile = function (srcPath, filename, stat) {
[
join(srcPath, `${filename}.js`),
join(srcPath, 'index.js'),
join(srcPath, `${filename}.mjs`),
join(srcPath, 'index.mjs'),
join(srcPath, `${filename}.ts`),
join(srcPath, 'index.ts'),
],
Expand Down
7 changes: 4 additions & 3 deletions src/runtimes/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const { getSrcFiles } = require('./src_files')
const { zipEsbuild } = require('./zip_esbuild')
const { zipZisi } = require('./zip_zisi')

// We use ZISI as the default bundler until the next major release, with the
// exception of TypeScript files, for which the only option is esbuild.
const getDefaultBundler = ({ extension }) => (extension === '.ts' ? JS_BUNDLER_ESBUILD : JS_BUNDLER_ZISI)
// We use ZISI as the default bundler, except for certain extensions, for which
// esbuild is the only option.
const getDefaultBundler = ({ extension }) =>
['.mjs', '.ts'].includes(extension) ? JS_BUNDLER_ESBUILD : JS_BUNDLER_ZISI

// A proxy for the `getSrcFiles` function which adds a default `bundler` using
// the `getDefaultBundler` function.
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/node-mjs/func1.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const handler = () => true
1 change: 1 addition & 0 deletions tests/fixtures/node-mjs/func2/func2.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const handler = () => true
1 change: 1 addition & 0 deletions tests/fixtures/node-mjs/func3/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const handler = () => true
24 changes: 24 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,30 @@ testBundlers('Handles a TypeScript function with imports', [ESBUILD, ESBUILD_ZIS
t.true(typeof require(`${tmpDir}/function.js`).type === 'string')
})

testBundlers(
'Handles a JavaScript function ({name}.mjs, {name}/{name}.mjs, {name}/index.mjs)',
[ESBUILD, ESBUILD_ZISI, DEFAULT],
async (bundler, t) => {
const { files, tmpDir } = await zipFixture(t, 'node-mjs', {
length: 3,
opts: { config: { '*': { nodeBundler: bundler } } },
})

await unzipFiles(files)

t.is(files.length, 3)
files.forEach((file) => {
t.is(file.bundler, 'esbuild')
})

/* eslint-disable import/no-dynamic-require, node/global-require */
t.true(require(`${tmpDir}/func1.js`).handler())
t.true(require(`${tmpDir}/func2.js`).handler())
t.true(require(`${tmpDir}/func3.js`).handler())
/* eslint-enable import/no-dynamic-require, node/global-require */
},
)

testBundlers(
'Loads a tsconfig.json placed in the same directory as the function',
[ESBUILD, ESBUILD_ZISI, DEFAULT],
Expand Down

1 comment on commit 73456ff

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 12.8s

largeDepsZisi: 1m 7.8s

Please sign in to comment.