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(babel): add filter option #767

Merged
merged 3 commits into from
Feb 10, 2021

Conversation

chengcyber
Copy link
Contributor

@chengcyber chengcyber commented Jan 14, 2021

Rollup Plugin Name: babel

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers:

Description

Since createFilter has a third options, which can pass resolutionBase. It might be better the option can be controlled by rollup-plugin-babel

According to the following discussion, adding filter option to @rollup/plugin-babel.

@shellscape
Copy link
Collaborator

Thanks for the PR. Unfortunately we cannot consider this PR until tests are added for the change. Please add tests or we'll have to close.

@chengcyber
Copy link
Contributor Author

Hi @shellscape ,

As per this issue, rollup/rollup#3013 (comment)

Do you think it's a better solution to add cwd option to rollup self, and change createFilter internally respect the rollup's cwd options as default resolutionBase?

I found it's kind of weird to add test for resolutionBase option to @rollup/plugin-babel 😅

@shellscape shellscape requested a review from Andarist January 20, 2021 01:20
@Andarist
Copy link
Member

It seems that maybe instead of adding more createFilter-related options it would be better to just accept a single filter option and have people pass the createFilter result to the babel plugin instead? @shellscape WDYT?

@shellscape
Copy link
Collaborator

@Andarist if it makes sense for the plugin, then I'm in agreement. we'll definitely want the docs to be clear about why this plugin behaves differently than the other ones, and why it accepts different filtering options.

@chengcyber chengcyber changed the title feat(babel): add resolutionBase option feat(babel): add filter option Feb 2, 2021
@chengcyber
Copy link
Contributor Author

refactor the code and add filter option to @rollup/plugin-babel instead.

@@ -75,6 +75,21 @@ Type: `String | RegExp | Array[...String|RegExp]`<br>

A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. When relying on Babel configuration files you cannot include files already excluded there.

### `filter`

Type: (id: unknown) => boolean<br>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this always receive a string?

Copy link
Contributor Author

@chengcyber chengcyber Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, the type should be ReturnType<typeof createFilter>. As per https://github.com/rollup/plugins/blob/master/packages/pluginutils/types/index.d.ts#L52

The createFilter returns (id: string | unknown) => boolean. and it was id: unknown inferred in IDE when I write the code...

Anyway, I changed the type to (id: string) => boolean. PTAL

skipPreflightCheck,
...babelOptions
} = unpackInputPluginOptions(pluginOptionsWithOverrides, this.meta.rollupVersion));

const extensionRegExp = new RegExp(
`(${extensions.map(escapeRegExpCharacters).join('|')})$`
);
const includeExcludeFilter = createFilter(include, exclude);
filter = (id) => extensionRegExp.test(stripQuery(id).bareId) && includeExcludeFilter(id);
const userDefinedFilter = (id) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not mentioning that before (I would sweat that I've done that but apparently I didn't) - I would not try to compose those things here. If a custom filter gets passed in I would just throw if exclude or include would be passed in as well.

Something along those lines:

if (customFilter && (include || exclude)) {
  throw new Error('...')
}
const configFilter = customFiler || createFilter(include, exclude);
filter = (id) => extensionRegExp.test(stripQuery(id).bareId) && configFilter(id);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the code logic, switch to your design. and add test as well.

@chengcyber chengcyber requested a review from Andarist February 10, 2021 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants