Skip to content

Commit c58bf2b

Browse files
committed
implement glob-style path resolution
1 parent 67b7797 commit c58bf2b

20 files changed

+1437
-219
lines changed

CHANGELOG.md

+26
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@
3232
}
3333
```
3434

35+
* 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))
36+
37+
This release introduces wildcards in import paths in two places:
38+
39+
* **Entry points**
40+
41+
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:
42+
43+
```sh
44+
esbuild --minify "./src/*.ts" --outdir=out
45+
```
46+
47+
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.
48+
49+
* **Run-time import paths**
50+
51+
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.
52+
53+
```js
54+
// These two forms are equivalent
55+
const json1 = await import('./data/' + kind + '.json')
56+
const json2 = await import(`./data/${kind}.json`)
57+
```
58+
59+
This feature works with `require(...)`, `import(...)`, and `new URL(..., import.meta.url)` because these can all accept run-time expressions. It does not work with `import` and `export` statements because these cannot accept run-time expressions.
60+
3561
* Move all binary executable packages to the `@esbuild/` scope
3662

3763
Binary package executables for esbuild are published as individual packages separate from the main `esbuild` package so you only have to download the relevant one for the current platform when you install esbuild. This release moves all of these packages under the `@esbuild/` scope to avoid collisions with 3rd-party packages. It also changes them to a consistent naming scheme that uses the `os` and `cpu` names from node.

internal/ast/ast.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ func (flags ImportRecordFlags) Has(flag ImportRecordFlags) bool {
128128
}
129129

130130
type ImportRecord struct {
131-
Assertions *[]AssertEntry
132-
Path logger.Path
133-
Range logger.Range
131+
Assertions *[]AssertEntry
132+
GlobPattern *GlobPattern
133+
Path logger.Path
134+
Range logger.Range
134135

135136
// If the "HandlesImportErrors" flag is present, then this is the location
136137
// of the error handler. This is used for error reporting.
@@ -165,6 +166,12 @@ func FindAssertion(assertions []AssertEntry, name string) *AssertEntry {
165166
return nil
166167
}
167168

169+
type GlobPattern struct {
170+
Parts []helpers.GlobPart
171+
ExportAlias string
172+
Kind ImportKind
173+
}
174+
168175
// This stores a 32-bit index where the zero value is an invalid index. This is
169176
// a better alternative to storing the index as a pointer since that has the
170177
// same properties but takes up more space and costs an extra pointer traversal.

0 commit comments

Comments
 (0)