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

Commit

Permalink
refactor: ensure exclude patterns are unix paths
Browse files Browse the repository at this point in the history
Also rename `exclude` to `excludePatterns` to clarify these are not os paths
  • Loading branch information
danez committed Jun 3, 2022
1 parent 7009d6a commit cda964a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/runtimes/node/bundlers/esbuild/src_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getDependencyPathsForDependency } from '../zisi/traverse.js'

export const getSrcFiles: GetSrcFilesFunction = async ({ config, mainFile, pluginsModulesPath, srcDir }) => {
const { externalNodeModules = [], includedFiles = [], includedFilesBasePath } = config
const { exclude: excludedPaths, paths: includedFilePaths } = await getPathsOfIncludedFiles(
const { excludePatterns, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
includedFilesBasePath,
)
Expand All @@ -15,8 +15,8 @@ export const getSrcFiles: GetSrcFilesFunction = async ({ config, mainFile, plugi
basedir: srcDir,
pluginsModulesPath,
})
const srcFiles = filterExcludedPaths(dependencyPaths, excludedPaths)
const includedPaths = filterExcludedPaths(includedFilePaths, excludedPaths)
const srcFiles = filterExcludedPaths(dependencyPaths, excludePatterns)
const includedPaths = filterExcludedPaths(includedFilePaths, excludePatterns)

return {
srcFiles: [...srcFiles, ...includedPaths, mainFile],
Expand Down
13 changes: 7 additions & 6 deletions src/runtimes/node/bundlers/nft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const bundle: BundleFunction = async ({
repositoryRoot = basePath,
}) => {
const { includedFiles = [], includedFilesBasePath } = config
const { exclude: excludedPaths, paths: includedFilePaths } = await getPathsOfIncludedFiles(
const { excludePatterns, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
includedFilesBasePath || basePath,
)
Expand All @@ -45,15 +45,16 @@ const bundle: BundleFunction = async ({
pluginsModulesPath,
name,
})
const filteredIncludedPaths = filterExcludedPaths([...dependencyPaths, ...includedFilePaths], excludedPaths)
const includedPaths = filterExcludedPaths(includedFilePaths, excludePatterns)
const filteredIncludedPaths = [...filterExcludedPaths(dependencyPaths, excludePatterns), ...includedPaths]
const dirnames = filteredIncludedPaths.map((filePath) => normalize(dirname(filePath))).sort()

// Sorting the array to make the checksum deterministic.
const srcFiles = [...filteredIncludedPaths].sort()

return {
basePath: getBasePath(dirnames),
includedFiles: filterExcludedPaths(includedFilePaths, excludedPaths),
includedFiles: includedPaths,
inputs: dependencyPaths,
mainFile,
moduleFormat,
Expand Down Expand Up @@ -145,16 +146,16 @@ const traceFilesAndTranspile = async function ({

const getSrcFiles: GetSrcFilesFunction = async function ({ basePath, config, mainFile }) {
const { includedFiles = [], includedFilesBasePath } = config
const { exclude: excludedPaths, paths: includedFilePaths } = await getPathsOfIncludedFiles(
const { excludePatterns, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
includedFilesBasePath,
)
const { fileList: dependencyPaths } = await nodeFileTrace([mainFile], { base: basePath, ignore: ignoreFunction })
const normalizedDependencyPaths = [...dependencyPaths].map((path) =>
basePath ? resolve(basePath, path) : resolve(path),
)
const srcFiles = filterExcludedPaths(normalizedDependencyPaths, excludedPaths)
const includedPaths = filterExcludedPaths(includedFilePaths, excludedPaths)
const srcFiles = filterExcludedPaths(normalizedDependencyPaths, excludePatterns)
const includedPaths = filterExcludedPaths(includedFilePaths, excludePatterns)

return {
srcFiles: [...srcFiles, ...includedPaths],
Expand Down
6 changes: 3 additions & 3 deletions src/runtimes/node/bundlers/zisi/src_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getSrcFiles: GetSrcFilesFunction = async function ({
stat,
}) {
const { includedFiles = [], includedFilesBasePath } = config
const { exclude: excludedPaths, paths: includedFilePaths } = await getPathsOfIncludedFiles(
const { excludePatterns, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
includedFilesBasePath,
)
Expand All @@ -45,8 +45,8 @@ 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 srcFiles = filterExcludedPaths(filteredFiles, excludedPaths)
const includedPaths = filterExcludedPaths(includedFilePaths, excludedPaths)
const srcFiles = filterExcludedPaths(filteredFiles, excludePatterns)
const includedPaths = filterExcludedPaths(includedFilePaths, excludePatterns)

return { srcFiles: [...srcFiles, ...includedPaths], includedFiles: includedPaths }
}
Expand Down
28 changes: 16 additions & 12 deletions src/runtimes/node/utils/included_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,63 @@ import { promisify } from 'util'

import glob from 'glob'
import minimatch from 'minimatch'
import unixify from 'unixify'

const pGlob = promisify(glob)

// Returns the subset of `paths` that don't match any of the glob expressions
// from `exclude`.
export const filterExcludedPaths = (paths: string[], exclude: string[] = []) => {
if (exclude.length === 0) {
export const filterExcludedPaths = (paths: string[], excludePattern: string[] = []) => {
if (excludePattern.length === 0) {
return paths
}

const excludedPaths = paths.filter((path) => !exclude.some((pattern) => minimatch(path, pattern)))
const excludedPaths = paths.filter((path) => !excludePattern.some((pattern) => minimatch(path, pattern)))

return excludedPaths
}

export const getPathsOfIncludedFiles = async (
includedFiles: string[],
basePath?: string,
): Promise<{ exclude: string[]; paths: string[] }> => {
): Promise<{ excludePatterns: string[]; paths: string[] }> => {
if (basePath === undefined) {
return { exclude: [], paths: [] }
return { excludePatterns: [], paths: [] }
}

// Some of the globs in `includedFiles` might be exclusion patterns, which
// means paths that should NOT be included in the bundle. We need to treat
// these differently, so we iterate on the array and put those paths in a
// `exclude` array and the rest of the paths in an `include` array.
const { include, exclude } = includedFiles.reduce<{ include: string[]; exclude: string[] }>(
const { include, excludePatterns } = includedFiles.reduce<{ include: string[]; excludePatterns: string[] }>(
(acc, path) => {
if (path.startsWith('!')) {
const excludePath = resolve(basePath, path.slice(1))
// convert to unix paths, as minimatch does not support windows paths in patterns
const excludePattern = unixify(resolve(basePath, path.slice(1)))

return {
include: acc.include,
exclude: [...acc.exclude, excludePath],
excludePatterns: [...acc.excludePatterns, excludePattern],
}
}

return {
include: [...acc.include, path],
exclude: acc.exclude,
excludePatterns: acc.excludePatterns,
}
},
{ include: [], exclude: [] },
{ include: [], excludePatterns: [] },
)
const pathGroups = await Promise.all(
include.map((expression) => pGlob(expression, { absolute: true, cwd: basePath, ignore: exclude, nodir: true })),
include.map((expression) =>
pGlob(expression, { absolute: true, cwd: basePath, ignore: excludePatterns, nodir: true }),
),
)

// `pathGroups` is an array containing the paths for each expression in the
// `include` array. We flatten it into a single dimension.
const paths = pathGroups.flat()
const normalizedPaths = paths.map(normalize)

return { exclude, paths: [...new Set(normalizedPaths)] }
return { excludePatterns, paths: [...new Set(normalizedPaths)] }
}

0 comments on commit cda964a

Please sign in to comment.