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

Commit

Permalink
fix: auto-enable zisi_pure_esm, zisi_pure_esm_mjs when v2 api is used (
Browse files Browse the repository at this point in the history
…#1489)

* fix: auto-enable zisi_pure_esm, zisi_pure_esm_mjs when v2 api is used

* refactor: read runtimeVersion besides flags
  • Loading branch information
Skn0tt committed Jul 4, 2023
1 parent e9ab18f commit 25b10eb
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/runtimes/node/bundlers/esbuild/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const bundleJsFile = async function ({
name,
srcDir,
srcFile,
runtimeAPIVersion,
}: {
additionalModulePaths?: string[]
basePath?: string
Expand All @@ -50,6 +51,7 @@ export const bundleJsFile = async function ({
name: string
srcDir: string
srcFile: string
runtimeAPIVersion: number
}) {
// We use a temporary directory as the destination for esbuild files to avoid
// any naming conflicts with files generated by other functions.
Expand Down Expand Up @@ -96,12 +98,13 @@ export const bundleJsFile = async function ({
// Configuring the output format of esbuild. The `includedFiles` array we get
// here contains additional paths to include with the bundle, like the path
// to a `package.json` with {"type": "module"} in case of an ESM function.
const { includedFiles: includedFilesFromModuleDetection, moduleFormat } = await getModuleFormat(
const { includedFiles: includedFilesFromModuleDetection, moduleFormat } = await getModuleFormat({
srcDir,
featureFlags,
extname(mainFile),
config.nodeVersion,
)
extension: extname(mainFile),
runtimeAPIVersion,
configVersion: config.nodeVersion,
})

// The extension of the output file.
const outputExtension = getFileExtensionForFormat(moduleFormat, featureFlags)
Expand Down
23 changes: 15 additions & 8 deletions src/runtimes/node/bundlers/esbuild/bundler_target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ const getBundlerTarget = (suppliedVersion?: string): VersionValues => {
return versionMap[DEFAULT_NODE_VERSION]
}

const getModuleFormat = async (
srcDir: string,
featureFlags: FeatureFlags,
extension: string,
configVersion?: string,
): Promise<{ includedFiles: string[]; moduleFormat: ModuleFormat }> => {
if (featureFlags.zisi_pure_esm_mjs && extension === MODULE_FILE_EXTENSION.MJS) {
const getModuleFormat = async ({
srcDir,
featureFlags,
extension,
runtimeAPIVersion,
configVersion,
}: {
srcDir: string
featureFlags: FeatureFlags
extension: string
runtimeAPIVersion: number
configVersion?: string
}): Promise<{ includedFiles: string[]; moduleFormat: ModuleFormat }> => {
if (extension === MODULE_FILE_EXTENSION.MJS && (runtimeAPIVersion === 2 || featureFlags.zisi_pure_esm_mjs)) {
return {
includedFiles: [],
moduleFormat: MODULE_FORMAT.ESM,
}
}

if (featureFlags.zisi_pure_esm) {
if (runtimeAPIVersion === 2 || featureFlags.zisi_pure_esm) {
const nodeSupport = getNodeSupportMatrix(configVersion)

if (extension.includes('ts') && nodeSupport.esm) {
Expand Down
2 changes: 2 additions & 0 deletions src/runtimes/node/bundlers/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const bundle: BundleFunction = async ({
srcDir,
srcPath,
stat,
runtimeAPIVersion,
}) => {
const { externalModules, ignoredModules } = await getExternalAndIgnoredModules({ config, srcDir })
const {
Expand All @@ -87,6 +88,7 @@ const bundle: BundleFunction = async ({
name,
srcDir,
srcFile: mainFile,
runtimeAPIVersion,
})
const bundlerWarnings = warnings.length === 0 ? undefined : warnings
const { srcFiles, includedFiles } = await getSrcFiles({
Expand Down
1 change: 1 addition & 0 deletions src/runtimes/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const zipFunction: ZipFunction = async function ({

const inSourceConfig = await findISCDeclarationsInPath(mainFile, name, featureFlags)
const runtimeAPIVersion = inSourceConfig.runtimeAPIVersion === 2 ? 2 : 1

const pluginsModulesPath = await getPluginsModulesPath(srcDir)
const bundlerName = await getBundlerName({
config,
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/v2-api-mjs/function.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default async () => new Response("<h1>Hello world</h1>", {
headers: {
"content-type": "text/html"
}
})
1 change: 1 addition & 0 deletions tests/fixtures/v2-api-mjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
27 changes: 24 additions & 3 deletions tests/v2api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,28 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
async (options) => {
const { files } = await zipFixture('v2-api', {
opts: merge(options, {
featureFlags: { zisi_functions_api_v2: true, zisi_pure_esm: true, zisi_pure_esm_mjs: true },
featureFlags: { zisi_functions_api_v2: true },
}),
})
const unzippedFunctions = await unzipFiles(files)

const func = await importFunctionFile(`${unzippedFunctions[0].unzipPath}/${files[0].entryFilename}`)
const { body: bodyStream, headers = {}, statusCode } = await invokeLambda(func)
const body = await readAsBuffer(bodyStream)

expect(body).toBe('<h1>Hello world</h1>')
expect(headers['content-type']).toBe('text/html')
expect(statusCode).toBe(200)
},
)

testMany(
'Handles a .mjs function',
['bundler_default', 'bundler_esbuild', 'bundler_esbuild_zisi', 'bundler_default_nft', 'bundler_nft'],
async (options) => {
const { files } = await zipFixture('v2-api-mjs', {
opts: merge(options, {
featureFlags: { zisi_functions_api_v2: true },
}),
})
const unzippedFunctions = await unzipFiles(files)
Expand All @@ -49,7 +70,7 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
const { files, tmpDir } = await zipFixture('v2-api', {
opts: merge(options, {
archiveFormat: ARCHIVE_FORMAT.NONE,
featureFlags: { zisi_functions_api_v2: true, zisi_pure_esm: true, zisi_pure_esm_mjs: true },
featureFlags: { zisi_functions_api_v2: true },
}),
})

Expand All @@ -71,7 +92,7 @@ describe.runIf(semver.gte(nodeVersion, '18.13.0'))('V2 functions API', () => {
const { files, tmpDir } = await zipFixture('v2-api-ts', {
opts: merge(options, {
archiveFormat: ARCHIVE_FORMAT.NONE,
featureFlags: { zisi_pure_esm: true, zisi_functions_api_v2: true },
featureFlags: { zisi_functions_api_v2: true },
}),
})

Expand Down

1 comment on commit 25b10eb

@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.5s
  • largeDepsNft: 7.8s
  • largeDepsZisi: 15.7s

Please sign in to comment.