Skip to content

Commit

Permalink
feat(esbuild): filter option that allows to process only specific fil…
Browse files Browse the repository at this point in the history
…es (#50)
  • Loading branch information
Anber authored Feb 6, 2024
1 parent c1a83e4 commit cef295c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/quick-bulldogs-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@wyw-in-js/esbuild': patch
'@wyw-in-js/website': patch
---

The new `filter` options for the esbuild plugin that allows to process only specific files, e.g. `.styles.ts`.
1 change: 1 addition & 0 deletions apps/website/pages/bundlers/esbuild.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const prod = process.env.NODE_ENV === 'production';

esbuild
.build({
filter: /\.(js|jsx|ts|tsx)$/,
entryPoints: ['src/index.ts'],
outdir: 'dist',
bundle: true,
Expand Down
7 changes: 6 additions & 1 deletion packages/esbuild/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {

type EsbuildPluginOptions = {
esbuildOptions?: TransformOptions;
filter?: RegExp | string;
preprocessor?: Preprocessor;
sourceMap?: boolean;
} & Partial<PluginOptions>;
Expand All @@ -29,6 +30,7 @@ export default function wywInJS({
sourceMap,
preprocessor,
esbuildOptions,
filter = /\.(js|jsx|ts|tsx)$/,
...rest
}: EsbuildPluginOptions = {}): Plugin {
let options = esbuildOptions;
Expand Down Expand Up @@ -73,7 +75,10 @@ export default function wywInJS({
};
});

build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, async (args) => {
const filterRegexp =
typeof filter === 'string' ? new RegExp(filter) : filter;

build.onLoad({ filter: filterRegexp }, async (args) => {
const rawCode = readFileSync(args.path, 'utf8');
const { ext, name: filename } = parse(args.path);
const loader = ext.replace(/^\./, '') as Loader;
Expand Down

0 comments on commit cef295c

Please sign in to comment.