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 Sep 22, 2022
1 parent 67b7797 commit c58bf2b
Show file tree
Hide file tree
Showing 20 changed files with 1,437 additions and 219 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@
}
```

* 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.

```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(...)`, `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.

* Move all binary executable packages to the `@esbuild/` scope

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.
Expand Down
13 changes: 10 additions & 3 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ func (flags ImportRecordFlags) Has(flag ImportRecordFlags) bool {
}

type ImportRecord struct {
Assertions *[]AssertEntry
Path logger.Path
Range logger.Range
Assertions *[]AssertEntry
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 @@ -165,6 +166,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

0 comments on commit c58bf2b

Please sign in to comment.