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

feat: include resolved included_files in response of zipFunction #1098

Merged
merged 2 commits into from
Jun 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/runtimes/node/bundlers/esbuild/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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,
Copy link
Contributor Author

@danez danez Jun 2, 2022

Choose a reason for hiding this comment

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

I just made some minor adjustments to every bundler, so that we do not iterate over includedFilePaths twice.

Copy link
Member

Choose a reason for hiding this comment

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

Nice! That also makes things a bit clearer, IMO.

}
}

const getSrcFilesForDependencies = async function ({
Expand Down
3 changes: 2 additions & 1 deletion src/runtimes/node/bundlers/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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