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(swc): allow input files to be filtered #1566

Merged
merged 5 commits into from
Aug 29, 2023
Merged
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
22 changes: 21 additions & 1 deletion packages/swc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma
## Options

The plugin accepts an object as input parameter to modify the default behaviour.
This object has an `swc` property to provide library specific [Options](https://swc.rs/docs/configuration/swcrc).

### `swc`

- Type: [Options](https://swc.rs/docs/configuration/swcrc)
- Default: `undefined`

```typescript
import type { Options as SWCOptions } from '@swc/core';
Expand All @@ -58,6 +62,22 @@ declare type Options = {
};
```

### `exclude`

- Type: `String` | `Array[...String]`
- Default: `null`

A [picomatch pattern](https://github.com/micromatch/picomatch),
or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.

### `include`

- Type: `String` | `Array[...String]`
- Default: `null`

A [picomatch pattern](https://github.com/micromatch/picomatch),
or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.

## Alternatives

Alternative transpiler/transformer, which are also officially offered, are [Babel](https://www.npmjs.com/package/@rollup/plugin-babel) and [Sucrase](https://www.npmjs.com/package/@rollup/plugin-sucrase).
Expand Down
5 changes: 3 additions & 2 deletions packages/swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@
}
},
"dependencies": {
"smob": "^1.0.0"
"@rollup/pluginutils": "^5.0.1",
"smob": "^1.4.0"
},
"devDependencies": {
"@swc/core": "^1.3.58",
"@swc/core": "^1.3.78",
"rollup": "^3.0.0-7",
"typescript": "^4.8.3"
},
Expand Down
7 changes: 6 additions & 1 deletion packages/swc/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { Plugin } from 'rollup';
import { createFilter } from '@rollup/pluginutils';
import type { Options as SWCOptions } from '@swc/core';
import { transform } from '@swc/core';
import { merge } from 'smob';

import type { Options } from './type';

export function swc(input: Options = {}): Plugin {
const filter = createFilter(input.include, input.exclude);

const swcOptions: SWCOptions = merge({}, input.swc || {}, {
jsc: {
target: 'es2020',
Expand All @@ -23,7 +26,9 @@ export function swc(input: Options = {}): Plugin {

return {
name: 'swc',
transform(code) {
transform(code, id) {
if (!filter(id)) return null;

return transform(code, {
...swcOptions,
sourceMaps: true
Expand Down
5 changes: 5 additions & 0 deletions packages/swc/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { FilterPattern } from '@rollup/pluginutils';
import type { Options as SWCOptions } from '@swc/core';

export interface Options {
swc?: SWCOptions;

include?: FilterPattern;

exclude?: FilterPattern;
}
Loading
Loading