Skip to content

Commit

Permalink
implement glob-style path resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 8, 2023
1 parent 8e14e25 commit be9f8e5
Show file tree
Hide file tree
Showing 19 changed files with 1,371 additions and 184 deletions.
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
# Changelog

## Unreleased

* Handle import paths containing wildcards ([#56](https://github.com/evanw/esbuild/issues/56), [#700](https://github.com/evanw/esbuild/issues/700), [#875](https://github.com/evanw/esbuild/issues/875), [#976](https://github.com/evanw/esbuild/issues/976), [#2221](https://github.com/evanw/esbuild/issues/2221), [#2515](https://github.com/evanw/esbuild/issues/2515))

This release introduces wildcards in import paths in two places:

* **Entry points**

You can now pass a string containing glob-style wildcards such as `./src/*.ts` as an entry point and esbuild will search the file system for files that match the pattern. This can be used to easily pass esbuild all files with a certain extension on the command line in a cross-platform way. Previously you had to rely on the shell to perform glob expansion, but that is obviously shell-dependent and didn't work at all on Windows. Note that to use this feature on the command line you will have to quote the pattern so it's passed verbatim to esbuild without any expansion by the shell. Here's an example:

```sh
esbuild --minify "./src/*.ts" --outdir=out
```

Specifically the `*` character will match any character except for the `/` character, and the `/**/` character sequence will match a path separator followed by zero or more path elements. Other wildcard operators found in glob patterns such as `?` and `[...]` are not supported.

* **Run-time import paths**

Import paths that are evaluated at run-time can now be bundled in certain limited situations. The import path expression must be a form of string concatenation and must start with either `./` or `../`. Each non-string expression in the string concatenation chain becomes a wildcard. The `*` wildcard is chosen unless the previous character is a `/`, in which case the `/**/*` character sequence is used. Some examples:

```js
// These two forms are equivalent
const json1 = await import('./data/' + kind + '.json')
const json2 = await import(`./data/${kind}.json`)
```

This feature works with `require(...)` and `import(...)` because these can all accept run-time expressions. It does not work with `import` and `export` statements because these cannot accept run-time expressions. If you want to prevent esbuild from trying to bundle these imports, you should move the string concatenation expression outside of the `require(...)` or `import(...)`. For example:

```js
// This will be bundled
const json1 = await import('./data/' + kind + '.json')
// This will not be bundled
const path = './data/' + kind + '.json'
const json2 = await import(path)
```

Note that using this feature means esbuild will potentially do a lot of file system I/O to find all possible files that might match the pattern. This is by design, and is not a bug. If this is a concern, I recommend either avoiding the `/**/` pattern (e.g. by not putting a `/` before a wildcard) or using this feature only in directory subtrees which do not have many files that don't match the pattern (e.g. making a subdirectory for your JSON files and explicitly including that subdirectory in the pattern).
## 0.18.20
* Support advanced CSS `@import` rules ([#953](https://github.com/evanw/esbuild/issues/953), [#3137](https://github.com/evanw/esbuild/issues/3137))
Expand Down
13 changes: 10 additions & 3 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ func (flags ImportRecordFlags) Has(flag ImportRecordFlags) bool {
}

type ImportRecord struct {
Assertions *ImportAssertions
Path logger.Path
Range logger.Range
Assertions *ImportAssertions
GlobPattern *GlobPattern
Path logger.Path
Range logger.Range

// If the "HandlesImportErrors" flag is present, then this is the location
// of the error handler. This is used for error reporting.
Expand Down Expand Up @@ -195,6 +196,12 @@ func FindAssertion(assertions []AssertEntry, name string) *AssertEntry {
return nil
}

type GlobPattern struct {
Parts []helpers.GlobPart
ExportAlias string
Kind ImportKind
}

// This stores a 32-bit index where the zero value is an invalid index. This is
// a better alternative to storing the index as a pointer since that has the
// same properties but takes up more space and costs an extra pointer traversal.
Expand Down
Loading

0 comments on commit be9f8e5

Please sign in to comment.