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

Commit

Permalink
fix: do not transpile ESM for v2 functions (#1418)
Browse files Browse the repository at this point in the history
* fix: do not transpile ESM for v2 functions

* fix: import user code correctly

* chore: disable complexity rule

* chore: update error
  • Loading branch information
danez authored May 8, 2023
1 parent b5d9751 commit 4ae871e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
sourceType: 'module',
},
rules: {
complexity: 'off',
'import/extensions': ['error', 'ignorePackages'],
'max-lines': 'off',
'n/no-missing-import': 'off',
Expand Down
36 changes: 34 additions & 2 deletions src/runtimes/node/bundlers/nft/es_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { NodeFileTraceReasons } from '@vercel/nft'
import type { FunctionConfig } from '../../../../config.js'
import { FeatureFlags } from '../../../../feature_flags.js'
import type { RuntimeCache } from '../../../../utils/cache.js'
import { FunctionBundlingUserError } from '../../../../utils/error.js'
import { cachedReadFile } from '../../../../utils/fs.js'
import { RUNTIME } from '../../../runtime.js'
import { ModuleFormat, MODULE_FILE_EXTENSION, MODULE_FORMAT } from '../../utils/module_format.js'
import { getNodeSupportMatrix } from '../../utils/node_version.js'
import { getPackageJsonIfAvailable, PackageJson } from '../../utils/package_json.js'
import { NODE_BUNDLER } from '../types.js'

import { transpile } from './transpile.js'

Expand Down Expand Up @@ -72,7 +75,10 @@ export const processESM = async ({

// If this is a .mjs file and we want to output pure ESM files, we don't need
// to transpile anything.
if (extension === MODULE_FILE_EXTENSION.MJS && featureFlags.zisi_pure_esm_mjs) {
if (
extension === MODULE_FILE_EXTENSION.MJS &&
(featureFlags.zisi_pure_esm_mjs || featureFlags.zisi_functions_api_v2)
) {
return {
moduleFormat: MODULE_FORMAT.ESM,
}
Expand All @@ -81,6 +87,17 @@ export const processESM = async ({
const entrypointIsESM = isEntrypointESM({ basePath, esmPaths, mainFile })

if (!entrypointIsESM) {
if (featureFlags.zisi_functions_api_v2) {
throw new FunctionBundlingUserError(
`The function '${name}' must use the ES module syntax. To learn more, visit https://ntl.fyi/esm.`,
{
functionName: name,
runtime: RUNTIME.JAVASCRIPT,
bundler: NODE_BUNDLER.NFT,
},
)
}

return {
moduleFormat: MODULE_FORMAT.COMMONJS,
}
Expand All @@ -89,12 +106,27 @@ export const processESM = async ({
const packageJson = await getPackageJsonIfAvailable(dirname(mainFile))
const nodeSupport = getNodeSupportMatrix(config.nodeVersion)

if (featureFlags.zisi_pure_esm && packageJson.type === 'module' && nodeSupport.esm) {
if (
(featureFlags.zisi_pure_esm || featureFlags.zisi_functions_api_v2) &&
packageJson.type === 'module' &&
nodeSupport.esm
) {
return {
moduleFormat: MODULE_FORMAT.ESM,
}
}

if (featureFlags.zisi_functions_api_v2) {
throw new FunctionBundlingUserError(
`The function '${name}' must use the ES module syntax. To learn more, visit https://ntl.fyi/esm.`,
{
functionName: name,
runtime: RUNTIME.JAVASCRIPT,
bundler: NODE_BUNDLER.NFT,
},
)
}

const rewrites = await transpileESM({ basePath, cache, config, esmPaths, reasons, name })

return {
Expand Down
2 changes: 1 addition & 1 deletion src/runtimes/node/utils/entry_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getEntryFileContents = (mainPath: string, moduleFormat: string, featureFla

if (featureFlags.zisi_functions_api_v2) {
return [
`import func from '${importPath}'`,
`import * as func from '${importPath}'`,
`import { getLambdaHandler } from './${BOOTSTRAP_FILE_NAME}'`,
`export const handler = getLambdaHandler(func)`,
].join(';')
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/v2-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
1 change: 0 additions & 1 deletion tests/list_functions_files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ describe('listFunctionsFiles', () => {
testMany(
'Can list all function files from multiple source directories with listFunctionsFiles()',
[...allBundleConfigs, 'bundler_none'],
// eslint-disable-next-line complexity
async (options) => {
const fixtureDir = `${FIXTURES_DIR}/multiple-src-directories`
const opts = merge(options, {
Expand Down

1 comment on commit 4ae871e

@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: 2.6s
  • largeDepsNft: 9s
  • largeDepsZisi: 17.4s

Please sign in to comment.