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

fix: do not transpile ESM for v2 functions #1418

Merged
merged 4 commits into from
May 8, 2023
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
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}'`,
Copy link
Contributor Author

@danez danez May 8, 2023

Choose a reason for hiding this comment

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

getLambdaHandler expects an object with a default property. Previously this seemed to work, because we were still transpilling the code

`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