Skip to content

Commit

Permalink
refactor: replace globby with faster alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev authored Jul 17, 2024
1 parent 809c57a commit 57a372b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@
"debug": "^4.3.5",
"esbuild": "^0.23.0",
"execa": "^5.1.1",
"globby": "^11.1.0",
"fdir": "^6.1.1",
"joycon": "^3.1.1",
"picomatch": "^3.0.1",
"postcss-load-config": "^6.0.1",
"resolve-from": "^5.0.0",
"rollup": "^4.18.1",
Expand All @@ -73,6 +74,7 @@
"@types/debug": "4.1.12",
"@types/fs-extra": "11.0.4",
"@types/node": "20.14.11",
"@types/picomatch": "^3.0.0",
"@types/resolve": "1.20.6",
"colorette": "2.0.20",
"flat": "6.0.1",
Expand Down
35 changes: 32 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import path from 'node:path'
import fs from 'node:fs'
import { Worker } from 'node:worker_threads'
import glob from 'globby'
import { loadTsConfig } from 'bundle-require'
import execa from 'execa'
import { fdir } from 'fdir'
import picomatch from 'picomatch'
import kill from 'tree-kill'
import { version } from '../package.json'
import { PrettyError, handleError } from './errors'
Expand Down Expand Up @@ -117,7 +118,12 @@ const normalizeOptions = async (
}

if (Array.isArray(entry)) {
options.entry = await glob(entry)
const matcher = picomatch(entry)
options.entry = await new fdir()
.withRelativePaths()
.filter((file) => matcher(file))
.crawl(process.cwd())
.withPromise()
// Ensure entry exists
if (!options.entry || options.entry.length === 0) {
throw new PrettyError(`Cannot find ${entry}`)
Expand Down
25 changes: 21 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs'
import path from 'node:path'
import glob from 'globby'
import { fdir } from 'fdir'
import picomatch from 'picomatch'
import resolveFrom from 'resolve-from'
import strip from 'strip-json-comments'
import type { Entry, Format } from './options'
Expand Down Expand Up @@ -65,10 +66,26 @@ export function pathExists(p: string) {
}

export async function removeFiles(patterns: string[], dir: string) {
const files = await glob(patterns, {
cwd: dir,
absolute: true,
const matchPatterns: string[] = []
const ignorePatterns: string[] = []
for (const pattern of patterns) {
if (pattern.startsWith('!') && pattern[1] !== '(') {
ignorePatterns.push(pattern.slice(1))
} else {
matchPatterns.push(pattern)
}
}

const matcher = picomatch(matchPatterns, {
dot: true,
ignore: ignorePatterns,
})

const files = await new fdir()
.withFullPaths()
.filter((file) => matcher(file))
.crawl(dir)
.withPromise()
files.forEach((file) => fs.existsSync(file) && fs.unlinkSync(file))
}

Expand Down
10 changes: 6 additions & 4 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { expect } from 'vitest'
import execa from 'execa'
import { fdir } from 'fdir'
import fs from 'fs-extra'
import glob from 'globby'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const cacheDir = path.resolve(__dirname, '.cache')
Expand Down Expand Up @@ -57,9 +57,11 @@ export async function run(
}

// Get output
const outFiles = await glob('**/*', {
cwd: path.resolve(testDir, 'dist'),
}).then((res) => res.sort())
const outFiles = await new fdir()
.withRelativePaths()
.crawl(path.resolve(testDir, 'dist'))
.withPromise()
.then((res) => res.sort())

return {
get output() {
Expand Down

0 comments on commit 57a372b

Please sign in to comment.