Skip to content

Commit be9f8e5

Browse files
committed
implement glob-style path resolution
1 parent 8e14e25 commit be9f8e5

File tree

19 files changed

+1371
-184
lines changed

19 files changed

+1371
-184
lines changed

CHANGELOG.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* 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))
6+
7+
This release introduces wildcards in import paths in two places:
8+
9+
* **Entry points**
10+
11+
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:
12+
13+
```sh
14+
esbuild --minify "./src/*.ts" --outdir=out
15+
```
16+
17+
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.
18+
19+
* **Run-time import paths**
20+
21+
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:
22+
23+
```js
24+
// These two forms are equivalent
25+
const json1 = await import('./data/' + kind + '.json')
26+
const json2 = await import(`./data/${kind}.json`)
27+
```
28+
29+
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:
30+
31+
```js
32+
// This will be bundled
33+
const json1 = await import('./data/' + kind + '.json')
34+
35+
// This will not be bundled
36+
const path = './data/' + kind + '.json'
37+
const json2 = await import(path)
38+
```
39+
40+
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).
41+
342
## 0.18.20
443
544
* Support advanced CSS `@import` rules ([#953](https://github.com/evanw/esbuild/issues/953), [#3137](https://github.com/evanw/esbuild/issues/3137))

internal/ast/ast.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ func (flags ImportRecordFlags) Has(flag ImportRecordFlags) bool {
149149
}
150150

151151
type ImportRecord struct {
152-
Assertions *ImportAssertions
153-
Path logger.Path
154-
Range logger.Range
152+
Assertions *ImportAssertions
153+
GlobPattern *GlobPattern
154+
Path logger.Path
155+
Range logger.Range
155156

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

199+
type GlobPattern struct {
200+
Parts []helpers.GlobPart
201+
ExportAlias string
202+
Kind ImportKind
203+
}
204+
198205
// This stores a 32-bit index where the zero value is an invalid index. This is
199206
// a better alternative to storing the index as a pointer since that has the
200207
// same properties but takes up more space and costs an extra pointer traversal.

0 commit comments

Comments
 (0)