Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Return a list of files, like import.meta.glob in Vite, without calling the files.
import glob from 'fast-glob'
import nodeURL from 'url'
import process from 'process'
/** Returns pathnames matching the given pattern. */
const sync = (source: string) => glob.sync(source, options()) as string[]
/** Returns pathnames matching the given pattern. */
const async = (source: string) => glob(source, options()) as Promise<string[]>
/** Metadata context-specific to the current process. */
const meta = {
/** URL href of the current working directory. */
get cwd() {
return nodeURL.pathToFileURL(process.cwd()).href as string
},
/** URL href of the currently running file. */
get url() {
return new URL(
meta.cwd + Error().stack.match(/ eval \((.+?):/)[1].replace(nodeURL.pathToFileURL(process.cwd()).pathname, '')
).href
}
}
/** Internal options used for globbing. */
const options = () => ({
cwd: nodeURL.fileURLToPath(new URL('.', meta.url)),
ignore: [ '**/node_modules/**' ],
})
export { sync as default, meta, sync, async }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment