Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow customized filter to be passed from external source #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions esm-externals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@ build({
plugins: [EsmExternals({ externals: ['react', 'react-dom'] })],
})
```

If you would like to customize the filter for externals, you can pass a RegExp or a string to the externals option.

```ts

import EsmExternals from '@esbuild-plugins/esm-externals'
import { build } from 'esbuild'

const externalRegExp = new RegExp("^(" + ["react", "react-dom"].join("|") + ")$")
// this will make all react and react-dom packages externals, but not react/abc or react-dom/abc

build({
plugins: [EsmExternals({ externals: externalRegExp })],
})
```
5 changes: 3 additions & 2 deletions esm-externals/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import escapeStringRegexp from 'escape-string-regexp'
const NAME = 'esm-externals'
const NAMESPACE = NAME

export function EsmExternalsPlugin({ externals }: { externals: string[] }) {
export function EsmExternalsPlugin({ externals }: { externals: string[] | RegExp }) {
return {
name: NAME,
setup(build) {
const filter = makeFilter(externals)
const filter = Array.isArray(externals) ? makeFilter(externals) : externals;

build.onResolve({ filter: /.*/, namespace: NAMESPACE }, (args) => {
return {
path: args.path,
Expand Down