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

Commit

Permalink
refactor: cleanup resolved feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Jun 15, 2022
1 parent 6284742 commit 24c8cda
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 100 deletions.
3 changes: 0 additions & 3 deletions src/feature_flags.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { env } from 'process'

export const defaultFlags: Record<string, boolean> = {
buildGoSource: Boolean(env.NETLIFY_EXPERIMENTAL_BUILD_GO_SOURCE),
buildRustSource: Boolean(env.NETLIFY_EXPERIMENTAL_BUILD_RUST_SOURCE),
parseWithEsbuild: false,
traceWithNft: false,
zisi_detect_esm: false,
zisi_pure_esm: false,
}

export type FeatureFlag = keyof typeof defaultFlags
Expand Down
6 changes: 1 addition & 5 deletions src/runtimes/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,13 @@ const findFunctionsInPaths: FindFunctionsInPathsFunction = async function ({ fea
return functions.filter(nonNullable)
}

const findFunctionInPath: FindFunctionInPathFunction = async function ({ featureFlags, fsCache, path }) {
const findFunctionInPath: FindFunctionInPathFunction = async function ({ fsCache, path }) {
const runtime = await detectBinaryRuntime({ fsCache, path })

if (runtime === 'go') {
return processBinary({ fsCache, path })
}

if (featureFlags.buildGoSource !== true) {
return
}

const goSourceFile = await detectGoFunction({ fsCache, path })

if (goSourceFile) {
Expand Down
4 changes: 0 additions & 4 deletions src/runtimes/node/bundlers/esbuild/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { build, Metafile } from '@netlify/esbuild'
import { tmpName } from 'tmp-promise'

import type { FunctionConfig } from '../../../../config.js'
import { FeatureFlags } from '../../../../feature_flags.js'
import { getPathWithExtension, safeUnlink } from '../../../../utils/fs.js'
import type { RuntimeName } from '../../../runtime.js'
import type { NodeBundlerName } from '../index.js'
Expand All @@ -29,7 +28,6 @@ export const bundleJsFile = async function ({
basePath,
config,
externalModules = [],
featureFlags,
ignoredModules = [],
name,
srcDir,
Expand All @@ -39,7 +37,6 @@ export const bundleJsFile = async function ({
basePath?: string
config: FunctionConfig
externalModules: string[]
featureFlags: FeatureFlags
ignoredModules: string[]
name: string
srcDir: string
Expand Down Expand Up @@ -92,7 +89,6 @@ export const bundleJsFile = async function ({
// to a `package.json` with {"type": "module"} in case of an ESM function.
const { includedFiles: includedFilesFromModuleDetection, moduleFormat } = await getModuleFormat(
srcDir,
featureFlags,
config.nodeVersion,
)

Expand Down
4 changes: 1 addition & 3 deletions src/runtimes/node/bundlers/esbuild/bundler_target.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FeatureFlags } from '../../../../feature_flags'
import { ModuleFormat } from '../../utils/module_format'
import { DEFAULT_NODE_VERSION, getNodeSupportMatrix } from '../../utils/node_version'
import { getClosestPackageJson } from '../../utils/package_json'
Expand All @@ -25,13 +24,12 @@ const getBundlerTarget = (suppliedVersion?: string): VersionValues => {

const getModuleFormat = async (
srcDir: string,
featureFlags: FeatureFlags,
configVersion?: string,
): Promise<{ includedFiles: string[]; moduleFormat: ModuleFormat }> => {
const packageJsonFile = await getClosestPackageJson(srcDir)
const nodeSupport = getNodeSupportMatrix(configVersion)

if (featureFlags.zisi_pure_esm && packageJsonFile?.contents.type === 'module' && nodeSupport.esm) {
if (packageJsonFile?.contents.type === 'module' && nodeSupport.esm) {
return {
includedFiles: [packageJsonFile.path],
moduleFormat: 'esm',
Expand Down
1 change: 0 additions & 1 deletion src/runtimes/node/bundlers/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ const bundle: BundleFunction = async ({
basePath,
config,
externalModules,
featureFlags,
ignoredModules,
name,
srcDir,
Expand Down
10 changes: 2 additions & 8 deletions src/runtimes/node/bundlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,7 @@ export const getDefaultBundler = async ({
return 'nft'
}

if (featureFlags.zisi_detect_esm) {
const functionIsESM = await detectEsModule({ mainFile })
const functionIsESM = await detectEsModule({ mainFile })

if (functionIsESM) {
return 'nft'
}
}

return 'zisi'
return functionIsESM ? 'nft' : 'zisi'
}
5 changes: 1 addition & 4 deletions src/runtimes/node/bundlers/nft/es_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { basename, dirname, resolve } from 'path'
import { NodeFileTraceReasons } from '@vercel/nft'

import type { FunctionConfig } from '../../../../config.js'
import { FeatureFlags } from '../../../../feature_flags.js'
import { cachedReadFile, FsCache } from '../../../../utils/fs.js'
import { ModuleFormat } from '../../utils/module_format.js'
import { getNodeSupportMatrix } from '../../utils/node_version.js'
Expand Down Expand Up @@ -52,7 +51,6 @@ export const processESM = async ({
basePath,
config,
esmPaths,
featureFlags,
fsCache,
mainFile,
reasons,
Expand All @@ -61,7 +59,6 @@ export const processESM = async ({
basePath: string | undefined
config: FunctionConfig
esmPaths: Set<string>
featureFlags: FeatureFlags
fsCache: FsCache
mainFile: string
reasons: NodeFileTraceReasons
Expand All @@ -78,7 +75,7 @@ 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 (packageJson.type === 'module' && nodeSupport.esm) {
return {
moduleFormat: 'esm',
}
Expand Down
6 changes: 0 additions & 6 deletions src/runtimes/node/bundlers/nft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { nodeFileTrace } from '@vercel/nft'
import resolveDependency from '@vercel/nft/out/resolve-dependency.js'

import type { FunctionConfig } from '../../../../config.js'
import { FeatureFlags } from '../../../../feature_flags.js'
import { cachedReadFile, FsCache } from '../../../../utils/fs.js'
import { minimatch } from '../../../../utils/matching.js'
import { getBasePath } from '../../utils/base_path.js'
Expand All @@ -21,7 +20,6 @@ const appearsToBeModuleName = (name: string) => !name.startsWith('.')
const bundle: BundleFunction = async ({
basePath,
config,
featureFlags,
mainFile,
name,
pluginsModulesPath,
Expand All @@ -39,7 +37,6 @@ const bundle: BundleFunction = async ({
} = await traceFilesAndTranspile({
basePath: repositoryRoot,
config,
featureFlags,
mainFile,
pluginsModulesPath,
name,
Expand Down Expand Up @@ -71,14 +68,12 @@ const ignoreFunction = (path: string) => {
const traceFilesAndTranspile = async function ({
basePath,
config,
featureFlags,
mainFile,
pluginsModulesPath,
name,
}: {
basePath?: string
config: FunctionConfig
featureFlags: FeatureFlags
mainFile: string
pluginsModulesPath?: string
name: string
Expand Down Expand Up @@ -128,7 +123,6 @@ const traceFilesAndTranspile = async function ({
basePath,
config,
esmPaths: esmFileList,
featureFlags,
fsCache,
mainFile,
reasons,
Expand Down
70 changes: 4 additions & 66 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ testMany(
nodeVersion: 'nodejs12.x',
},
},
featureFlags: { zisi_detect_esm: true, zisi_pure_esm: false },
})
const { files, tmpDir } = await zipFixture(t, fixtureName, {
length,
Expand Down Expand Up @@ -517,7 +516,6 @@ testMany(
nodeVersion: 'nodejs12.x',
},
},
featureFlags: { zisi_detect_esm: true, zisi_pure_esm: false },
})
const { tmpDir } = await zipFixture(t, fixtureName, {
length,
Expand Down Expand Up @@ -559,11 +557,8 @@ testMany(
testMany(
'Can bundle CJS functions that import ESM files with an `import()` expression',
['bundler_default', 'bundler_esbuild', 'bundler_nft'],
async (options, t) => {
async (opts, t) => {
const fixtureName = 'node-cjs-importing-mjs'
const opts = merge(options, {
featureFlags: { zisi_detect_esm: true },
})
const { files, tmpDir } = await zipFixture(t, fixtureName, {
opts,
})
Expand All @@ -583,14 +578,13 @@ testMany(
)

testMany(
'Can bundle native ESM functions when the Node version is >=14 and the `zisi_pure_esm` flag is on',
'Can bundle native ESM functions when the Node version is >=14',
['bundler_default', 'bundler_nft', 'bundler_esbuild'],
async (options, t) => {
const length = 2
const fixtureName = 'node-esm'
const opts = merge(options, {
basePath: join(FIXTURES_DIR, fixtureName),
featureFlags: { zisi_detect_esm: true, zisi_pure_esm: true },
})
const { files, tmpDir } = await zipFixture(t, fixtureName, {
length,
Expand All @@ -614,38 +608,6 @@ testMany(
},
)

testMany(
'Can bundle ESM functions and transpile them to CJS when the Node version is >=14 and the `zisi_pure_esm` flag is off',
['bundler_default', 'bundler_esbuild', 'bundler_nft'],
async (options, t) => {
const length = 2
const fixtureName = 'node-esm'
const opts = merge(options, {
basePath: join(FIXTURES_DIR, fixtureName),
featureFlags: { zisi_detect_esm: true },
})
const { files, tmpDir } = await zipFixture(t, fixtureName, {
length,
opts,
})

await unzipFiles(files, (path) => `${path}/../${basename(path)}_out`)

const functionPaths = [join(tmpDir, 'func1.zip_out', 'func1.js'), join(tmpDir, 'func2.zip_out', 'func2.js')]
const func1 = await importFunctionFile(functionPaths[0])
const func2 = await importFunctionFile(functionPaths[1])

t.true(func1.handler())
t.true(func2.handler())

const functionsAreESM = await Promise.all(
functionPaths.map((functionPath) => detectEsModule({ mainFile: functionPath })),
)

t.false(functionsAreESM.some(Boolean))
},
)

testMany(
'Can require local files deeply',
['bundler_default', 'bundler_esbuild', 'bundler_esbuild_zisi', 'bundler_default_nft', 'bundler_nft'],
Expand Down Expand Up @@ -2226,9 +2188,6 @@ test.serial('Zips Go functions built from source if the `zipGo` config property
zipGo: true,
},
},
featureFlags: {
buildGoSource: true,
},
},
})
const [func] = files
Expand All @@ -2244,27 +2203,12 @@ test.serial('Zips Go functions built from source if the `zipGo` config property
t.is(mockSource, unzippedBinaryContents)
})

test.serial('Does not build Go functions from source if the `buildGoSource` feature flag is not enabled', async (t) => {
shellUtilsStub.callsFake((...args) => writeFile(args[1][2], ''))

const fixtureName = 'go-source-multiple'
const { files } = await zipFixture(t, fixtureName, { length: 0 })

t.is(files.length, 0)
t.is(shellUtilsStub.callCount, 0)
})

test.serial('Builds Go functions from source if the `buildGoSource` feature flag is enabled', async (t) => {
test.serial('Builds Go functions from source', async (t) => {
shellUtilsStub.callsFake((...args) => writeFile(args[1][2], ''))

const fixtureName = 'go-source-multiple'
const { files } = await zipFixture(t, fixtureName, {
length: 2,
opts: {
featureFlags: {
buildGoSource: true,
},
},
})

t.is(shellUtilsStub.callCount, 2)
Expand Down Expand Up @@ -2301,13 +2245,7 @@ test.serial('Adds `type: "functionsBundling"` to errors resulting from compiling
})

try {
await zipFixture(t, 'go-source', {
opts: {
featureFlags: {
buildGoSource: true,
},
},
})
await zipFixture(t, 'go-source')

t.fail('Expected catch block')
} catch (error) {
Expand Down

0 comments on commit 24c8cda

Please sign in to comment.