This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate components to TypeScript (#753)
* refactor: use RuntimeName type * refactor: unify GetSrcFilesFunction type * refactor: migrate main file * refactor: add FunctionArchive type * refactor: migrate archive_size * refactor: create ZipFunctionResult type * refactor: migrate format_result * refactor: improve typings in zip file * refactor: migrate manifest * refactor: migrate polyfills * refactor: migrate consts * refactor: migrate bin file * chore: add comments * refactor: remove type
- Loading branch information
1 parent
bb9b217
commit d51c860
Showing
23 changed files
with
243 additions
and
214 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { extname } from 'path' | ||
|
||
import './utils/polyfills' | ||
import { Config } from './config' | ||
import { FeatureFlags, getFlags } from './feature_flags' | ||
import { FunctionSource } from './function' | ||
import { getFunctionsFromPaths } from './runtimes' | ||
import { getPluginsModulesPath } from './runtimes/node/utils/plugin_modules_path' | ||
import { GetSrcFilesFunction, RuntimeName } from './runtimes/runtime' | ||
import { listFunctionsDirectories, resolveFunctionsDirectories } from './utils/fs' | ||
import { zipFunction, zipFunctions } from './zip' | ||
|
||
interface ListedFunction { | ||
name: string | ||
mainFile: string | ||
runtime: RuntimeName | ||
extension: string | ||
} | ||
|
||
type ListedFunctionFile = ListedFunction & { | ||
srcFile: string | ||
} | ||
|
||
interface ListFunctionsOptions { | ||
basePath?: string | ||
config?: Config | ||
featureFlags?: FeatureFlags | ||
pluginsModulesPath?: string | ||
} | ||
|
||
// List all Netlify Functions main entry files for a specific directory | ||
const listFunctions = async function ( | ||
relativeSrcFolders: string | string[], | ||
{ featureFlags: inputFeatureFlags }: { featureFlags?: FeatureFlags } = {}, | ||
) { | ||
const featureFlags = getFlags(inputFeatureFlags) | ||
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders) | ||
const paths = await listFunctionsDirectories(srcFolders) | ||
const functions = await getFunctionsFromPaths(paths, { featureFlags }) | ||
const listedFunctions = [...functions.values()].map(getListedFunction) | ||
return listedFunctions | ||
} | ||
|
||
// List all Netlify Functions files for a specific directory | ||
const listFunctionsFiles = async function ( | ||
relativeSrcFolders: string | string[], | ||
{ basePath, config, featureFlags: inputFeatureFlags }: ListFunctionsOptions = {}, | ||
) { | ||
const featureFlags = getFlags(inputFeatureFlags) | ||
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders) | ||
const paths = await listFunctionsDirectories(srcFolders) | ||
const [functions, pluginsModulesPath] = await Promise.all([ | ||
getFunctionsFromPaths(paths, { config, featureFlags }), | ||
getPluginsModulesPath(srcFolders[0]), | ||
]) | ||
const listedFunctionsFiles = await Promise.all( | ||
[...functions.values()].map((func) => getListedFunctionFiles(func, { basePath, featureFlags, pluginsModulesPath })), | ||
) | ||
|
||
return listedFunctionsFiles.flat() | ||
} | ||
|
||
const getListedFunction = function ({ runtime, name, mainFile, extension }: FunctionSource): ListedFunction { | ||
return { name, mainFile, runtime: runtime.name, extension } | ||
} | ||
|
||
const getListedFunctionFiles = async function ( | ||
func: FunctionSource, | ||
options: { basePath?: string; featureFlags: FeatureFlags; pluginsModulesPath?: string }, | ||
): Promise<ListedFunctionFile[]> { | ||
const srcFiles = await getSrcFiles({ ...func, ...options }) | ||
const { name, mainFile, runtime } = func | ||
|
||
return srcFiles.map((srcFile) => ({ srcFile, name, mainFile, runtime: runtime.name, extension: extname(srcFile) })) | ||
} | ||
|
||
const getSrcFiles: GetSrcFilesFunction = async function ({ extension, runtime, srcPath, ...args }) { | ||
const { getSrcFiles: getRuntimeSrcFiles } = runtime | ||
|
||
if (extension === '.zip' || typeof getRuntimeSrcFiles !== 'function') { | ||
return [srcPath] | ||
} | ||
|
||
return await getRuntimeSrcFiles({ | ||
extension, | ||
runtime, | ||
srcPath, | ||
...args, | ||
}) | ||
} | ||
|
||
export { zipFunctions, zipFunction, listFunctions, listFunctionsFiles } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { resolve } from 'path' | ||
import { arch, platform } from 'process' | ||
|
||
import { FunctionResult } from './utils/format_result' | ||
import { writeFile } from './utils/fs' | ||
|
||
const MANIFEST_VERSION = 1 | ||
|
||
const createManifest = async ({ functions, path }: { functions: FunctionResult[]; path: string }) => { | ||
const formattedFunctions = functions.map(formatFunctionForManifest) | ||
const payload = { | ||
functions: formattedFunctions, | ||
system: { arch, platform }, | ||
timestamp: Date.now(), | ||
version: MANIFEST_VERSION, | ||
} | ||
|
||
await writeFile(path, JSON.stringify(payload)) | ||
} | ||
|
||
const formatFunctionForManifest = ({ mainFile, name, path, runtime }: FunctionResult) => ({ | ||
mainFile, | ||
name, | ||
path: resolve(path), | ||
runtime, | ||
}) | ||
|
||
export { createManifest } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
d51c860
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⏱ Benchmark results
largeDepsEsbuild: 8.7s
largeDepsNft: 57.2s
largeDepsZisi: 1m 11.4s