diff --git a/src/runtimes/node/bundlers/esbuild/index.ts b/src/runtimes/node/bundlers/esbuild/index.ts index 1bd9e0946..b222f5a71 100644 --- a/src/runtimes/node/bundlers/esbuild/index.ts +++ b/src/runtimes/node/bundlers/esbuild/index.ts @@ -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, @@ -122,6 +122,7 @@ const bundle: BundleFunction = async ({ cleanupFunction: cleanTempFiles, basePath: functionBasePath, bundlerWarnings, + includedFiles, inputs, mainFile: normalizedMainFile, moduleFormat, diff --git a/src/runtimes/node/bundlers/esbuild/src_files.ts b/src/runtimes/node/bundlers/esbuild/src_files.ts index 578f516f5..323b2d2ee 100644 --- a/src/runtimes/node/bundlers/esbuild/src_files.ts +++ b/src/runtimes/node/bundlers/esbuild/src_files.ts @@ -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 ({ diff --git a/src/runtimes/node/bundlers/index.ts b/src/runtimes/node/bundlers/index.ts index 6bc34a5ea..cf3de349c 100644 --- a/src/runtimes/node/bundlers/index.ts +++ b/src/runtimes/node/bundlers/index.ts @@ -50,6 +50,7 @@ export type BundleFunction = ( basePath: string bundlerWarnings?: BundlerWarning[] cleanupFunction?: CleanupFunction + includedFiles: string[] inputs: string[] mainFile: string moduleFormat: ModuleFormat @@ -66,7 +67,7 @@ export type GetSrcFilesFunction = ( pluginsModulesPath?: string repositoryRoot?: string } & FunctionSource, -) => Promise +) => Promise<{ srcFiles: string[]; includedFiles: string[] }> interface NodeBundler { bundle: BundleFunction diff --git a/src/runtimes/node/bundlers/nft/index.ts b/src/runtimes/node/bundlers/nft/index.ts index 8d703b27e..994f6b1ce 100644 --- a/src/runtimes/node/bundlers/nft/index.ts +++ b/src/runtimes/node/bundlers/nft/index.ts @@ -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' @@ -54,6 +53,7 @@ const bundle: BundleFunction = async ({ return { basePath: getBasePath(dirnames), + includedFiles: filterExcludedPaths(includedFilePaths, excludedPaths), inputs: dependencyPaths, mainFile, moduleFormat, @@ -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 } diff --git a/src/runtimes/node/bundlers/zisi/index.ts b/src/runtimes/node/bundlers/zisi/index.ts index d1e318f0d..ee2999561 100644 --- a/src/runtimes/node/bundlers/zisi/index.ts +++ b/src/runtimes/node/bundlers/zisi/index.ts @@ -19,7 +19,7 @@ const bundle: BundleFunction = async ({ srcPath, stat, }) => { - const srcFiles = await getSrcFiles({ + const { srcFiles, includedFiles } = await getSrcFiles({ basePath, config: { ...config, @@ -40,6 +40,7 @@ const bundle: BundleFunction = async ({ return { basePath: getBasePath(dirnames), + includedFiles, inputs: srcFiles, mainFile, moduleFormat: 'cjs', diff --git a/src/runtimes/node/bundlers/zisi/src_files.ts b/src/runtimes/node/bundlers/zisi/src_files.ts index 1a08ca824..b6da56579 100644 --- a/src/runtimes/node/bundlers/zisi/src_files.ts +++ b/src/runtimes/node/bundlers/zisi/src_files.ts @@ -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. diff --git a/src/runtimes/node/index.ts b/src/runtimes/node/index.ts index ca9fde985..9866a3ab4 100644 --- a/src/runtimes/node/index.ts +++ b/src/runtimes/node/index.ts @@ -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 ({ @@ -61,6 +62,7 @@ const zipFunction: ZipFunction = async function ({ cleanupFunction, basePath: finalBasePath, bundlerWarnings, + includedFiles, inputs, mainFile: finalMainFile = mainFile, moduleFormat, @@ -108,6 +110,7 @@ const zipFunction: ZipFunction = async function ({ bundlerWarnings, config, inputs, + includedFiles, inSourceConfig, nativeNodeModules, nodeModulesWithDynamicImports, diff --git a/src/runtimes/runtime.ts b/src/runtimes/runtime.ts index eec3e5996..48d0608cc 100644 --- a/src/runtimes/runtime.ts +++ b/src/runtimes/runtime.ts @@ -36,6 +36,7 @@ export interface ZipFunctionResult { bundlerWarnings?: object[] config: FunctionConfig inputs?: string[] + includedFiles?: string[] inSourceConfig?: ISCValues nativeNodeModules?: object nodeModulesWithDynamicImports?: string[] diff --git a/tests/fixtures/node-module-next-image/included/abc.js b/tests/fixtures/node-module-next-image/included/abc.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests/main.js b/tests/main.js index 90add1657..8e1996a40 100644 --- a/tests/main.js +++ b/tests/main.js @@ -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`.