Skip to content

Commit

Permalink
feat: include resolved included_files in response of zipFunction (#…
Browse files Browse the repository at this point in the history
…1098)

* feat: include resolved included_files in response of `zipFunction`

* fix: do not iterate multiple times over includedFiles
  • Loading branch information
danez committed Jun 2, 2022
1 parent 0d22eac commit 830f9f2
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/runtimes/node/bundlers/esbuild/index.ts
Expand Up @@ -85,7 +85,7 @@ const bundle: BundleFunction = async ({
srcFile: mainFile,
})
const bundlerWarnings = warnings.length === 0 ? undefined : warnings
const srcFiles = await getSrcFiles({
const { srcFiles, includedFiles } = await getSrcFiles({
basePath,
config: {
...config,
Expand Down Expand Up @@ -122,6 +122,7 @@ const bundle: BundleFunction = async ({
cleanupFunction: cleanTempFiles,
basePath: functionBasePath,
bundlerWarnings,
includedFiles,
inputs,
mainFile: normalizedMainFile,
moduleFormat,
Expand Down
8 changes: 6 additions & 2 deletions src/runtimes/node/bundlers/esbuild/src_files.ts
Expand Up @@ -15,9 +15,13 @@ export const getSrcFiles: GetSrcFilesFunction = async ({ config, mainFile, plugi
basedir: srcDir,
pluginsModulesPath,
})
const includedPaths = filterExcludedPaths([...dependencyPaths, ...includedFilePaths], excludedPaths)
const srcFiles = filterExcludedPaths(dependencyPaths, excludedPaths)
const includedPaths = filterExcludedPaths(includedFilePaths, excludedPaths)

return [...includedPaths, mainFile]
return {
srcFiles: [...srcFiles, ...includedPaths, mainFile],
includedFiles: includedPaths,
}
}

const getSrcFilesForDependencies = async function ({
Expand Down
3 changes: 2 additions & 1 deletion src/runtimes/node/bundlers/index.ts
Expand Up @@ -50,6 +50,7 @@ export type BundleFunction = (
basePath: string
bundlerWarnings?: BundlerWarning[]
cleanupFunction?: CleanupFunction
includedFiles: string[]
inputs: string[]
mainFile: string
moduleFormat: ModuleFormat
Expand All @@ -66,7 +67,7 @@ export type GetSrcFilesFunction = (
pluginsModulesPath?: string
repositoryRoot?: string
} & FunctionSource,
) => Promise<string[]>
) => Promise<{ srcFiles: string[]; includedFiles: string[] }>

interface NodeBundler {
bundle: BundleFunction
Expand Down
12 changes: 8 additions & 4 deletions src/runtimes/node/bundlers/nft/index.ts
Expand Up @@ -8,10 +8,9 @@ import unixify from 'unixify'
import type { FunctionConfig } from '../../../../config.js'
import { FeatureFlags } from '../../../../feature_flags.js'
import { cachedReadFile, FsCache } from '../../../../utils/fs.js'
import type { GetSrcFilesFunction } from '../../../runtime.js'
import { getBasePath } from '../../utils/base_path.js'
import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js'
import type { BundleFunction } from '../index.js'
import type { GetSrcFilesFunction, BundleFunction } from '../index.js'

import { processESM } from './es_modules.js'

Expand Down Expand Up @@ -54,6 +53,7 @@ const bundle: BundleFunction = async ({

return {
basePath: getBasePath(dirnames),
includedFiles: filterExcludedPaths(includedFilePaths, excludedPaths),
inputs: dependencyPaths,
mainFile,
moduleFormat,
Expand Down Expand Up @@ -153,9 +153,13 @@ const getSrcFiles: GetSrcFilesFunction = async function ({ basePath, config, mai
const normalizedDependencyPaths = [...dependencyPaths].map((path) =>
basePath ? resolve(basePath, path) : resolve(path),
)
const includedPaths = filterExcludedPaths([...normalizedDependencyPaths, ...includedFilePaths], excludedPaths)
const srcFiles = filterExcludedPaths(normalizedDependencyPaths, excludedPaths)
const includedPaths = filterExcludedPaths(includedFilePaths, excludedPaths)

return includedPaths
return {
srcFiles: [...srcFiles, ...includedPaths],
includedFiles: includedPaths,
}
}

const bundler = { bundle, getSrcFiles }
Expand Down
3 changes: 2 additions & 1 deletion src/runtimes/node/bundlers/zisi/index.ts
Expand Up @@ -19,7 +19,7 @@ const bundle: BundleFunction = async ({
srcPath,
stat,
}) => {
const srcFiles = await getSrcFiles({
const { srcFiles, includedFiles } = await getSrcFiles({
basePath,
config: {
...config,
Expand All @@ -40,6 +40,7 @@ const bundle: BundleFunction = async ({

return {
basePath: getBasePath(dirnames),
includedFiles,
inputs: srcFiles,
mainFile,
moduleFormat: 'cjs',
Expand Down
5 changes: 3 additions & 2 deletions src/runtimes/node/bundlers/zisi/src_files.ts
Expand Up @@ -45,9 +45,10 @@ export const getSrcFiles: GetSrcFilesFunction = async function ({
// We sort so that the archive's checksum is deterministic.
// Mutating is fine since `Array.filter()` returns a shallow copy
const filteredFiles = uniqueFiles.filter(isNotJunk).sort()
const includedPaths = filterExcludedPaths([...filteredFiles, ...includedFilePaths], excludedPaths)
const srcFiles = filterExcludedPaths(filteredFiles, excludedPaths)
const includedPaths = filterExcludedPaths(includedFilePaths, excludedPaths)

return includedPaths
return { srcFiles: [...srcFiles, ...includedPaths], includedFiles: includedPaths }
}

// Remove temporary files like *~, *.swp, etc.
Expand Down
5 changes: 4 additions & 1 deletion src/runtimes/node/index.ts
Expand Up @@ -24,8 +24,9 @@ const getSrcFilesWithBundler: GetSrcFilesFunction = async (parameters) => {
mainFile: parameters.mainFile,
}))
const bundler = getBundler(bundlerName)
const result = await bundler.getSrcFiles({ ...parameters, pluginsModulesPath })

return bundler.getSrcFiles({ ...parameters, pluginsModulesPath })
return result.srcFiles
}

const zipFunction: ZipFunction = async function ({
Expand Down Expand Up @@ -61,6 +62,7 @@ const zipFunction: ZipFunction = async function ({
cleanupFunction,
basePath: finalBasePath,
bundlerWarnings,
includedFiles,
inputs,
mainFile: finalMainFile = mainFile,
moduleFormat,
Expand Down Expand Up @@ -108,6 +110,7 @@ const zipFunction: ZipFunction = async function ({
bundlerWarnings,
config,
inputs,
includedFiles,
inSourceConfig,
nativeNodeModules,
nodeModulesWithDynamicImports,
Expand Down
1 change: 1 addition & 0 deletions src/runtimes/runtime.ts
Expand Up @@ -36,6 +36,7 @@ export interface ZipFunctionResult {
bundlerWarnings?: object[]
config: FunctionConfig
inputs?: string[]
includedFiles?: string[]
inSourceConfig?: ISCValues
nativeNodeModules?: object
nodeModulesWithDynamicImports?: string[]
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions tests/main.js
Expand Up @@ -381,6 +381,27 @@ testMany(
},
)

testMany(
'Includes includedFiles in the response of zipFunction',
['bundler_default', 'bundler_esbuild', 'bundler_esbuild_zisi', 'bundler_default_nft', 'bundler_nft'],
async (options, t) => {
const { path: tmpDir } = await getTmpDir({ prefix: 'zip-it-test2' })
const mainFile = join(FIXTURES_DIR, 'node-module-next-image', 'function', 'function.js')
const result = await zipFunction(mainFile, tmpDir, {
...options,
basePath: join(FIXTURES_DIR, 'node-module-next-image'),
config: {
'*': {
includedFiles: ['included/*.js'],
},
},
})

t.true(Array.isArray(result.includedFiles))
t.regex(unixify(result.includedFiles[0]), /node-module-next-image\/included\/abc\.js/)
},
)

// We persist `package.json` as `package.json.txt` in git. Otherwise ESLint
// tries to load when linting sibling JavaScript files. In this test, we
// temporarily rename it to an actual `package.json`.
Expand Down

1 comment on commit 830f9f2

@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: 6.3s

largeDepsNft: 28.2s

largeDepsZisi: 38.7s

Please sign in to comment.