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

Rollup filter doesn't match paths located outside base directory #686

Closed
herberttn opened this issue Feb 22, 2022 · 0 comments Β· Fixed by #687
Closed

Rollup filter doesn't match paths located outside base directory #686

herberttn opened this issue Feb 22, 2022 · 0 comments Β· Fixed by #687

Comments

@herberttn
Copy link
Contributor

πŸ› Bug report

Rollup filter doesn't match paths located outside base directory.

To reproduce

root
β”œβ”€β”€ app
β”‚Β Β  β”œβ”€β”€ index.html
β”‚Β Β  β”œβ”€β”€ index.jsx
β”‚Β Β  └── circle.svg
└── outsider
    β”œβ”€β”€ square.svg
 Β Β  └── triangle.svg
/root/app/index.jsx
import Circle from './circle.svg';
import Square from '../outsider/square.svg';

Steps to reproduce the behavior:

  1. Run rollup in /root/app with the @svgr/rollup plugin

Actual behavior

/root/app/circle.svg is correctly picked up and transformed
/root/outsider/square.svg is filtered out and skipped

Expected behavior

Both /root/app/circle.svg and /root/outsider/square.svg should be picked up and transformed

Why it happens

Rollup plugin utils filter is used with **/*.svg to match relevant imports to transform:

const filter = createFilter(options.include || '**/*.svg', options.exclude)

Which is then resolved to /root/app/**/*.svg (considering the example above) here:
https://github.com/rollup/rollup-pluginutils/blob/162b1db4b7b08f314493b53a7b817c086be26af4/src/createFilter.ts#L10

Which then only matches files inside the directory where rollup is running.

The fix

The use of the filter is correct here, and the root problem has already been fixed in rollup/plugins#517. The catch is that rollup-pluginutils has been renamed to @rollup/pluginutils and the fix happened after the move.

So the real fix is to use the renamed module.

The workaround

It's possible to work around this issue by leveraging this condition:
https://github.com/rollup/rollup-pluginutils/blob/162b1db4b7b08f314493b53a7b817c086be26af4/src/createFilter.ts#L17-L18

If the pattern is a regex instead of a string, the resolve is skipped and the filter correctly matches the files it's supposed to match.

// ...

svgrRollupPlugin({
  include: [/\.svg$/],
}),

// ...

Environment info

## System:
 - OS: macOS 12.2.1
 - CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
 - Memory: 47.63 MB / 16.00 GB
 - Shell: 5.8 - /bin/zsh
## Binaries:
 - Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node
 - npm: 8.3.1 - ~/.nvm/versions/node/v16.14.0/bin/npm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant