555ff3bcdb
This PR adds some improved `modularizeImports` rules to ensure that some of the popular UI libraries (mostly icons) can be correctly optimized instead of creating thousands of modules. Here's an example of applying this to `lucide-react`: ![CleanShot 2023-07-22 at 19 34 15@2x](https://github.com/vercel/next.js/assets/3676859/cf9ef13f-1d5d-4df6-9097-364983ea7b8b) With https://github.com/swc-project/plugins/pull/196 being landed, we can add different transform rules for specific names in the import. For example, for `lucide-react` there're now 3 transforms: - `'Lucide(.*)': ...` - `'(.*)Icon': ...` - `'*': ...` On top of that, another critical change made in this PR is the introducing of `modularize-import-loader`. With this new loader, we can delegate the task of re-exporting the value to Webpack, where we can adjust the type of the export and the target path. This is very necessary and can't be done in SWC alone because libs like `lucide-react` is using `.mjs` and don't have `exports` values for the individual icons in the package.json files. Because of that, if we simply transform the expression to `import Icon from 'lucide-react/esm/icons/foo` it will fail because: 1. It's missing a `.mjs` extension 2. It doesn't have `/esm/icons/foo.mjs` in package.json `exports` For 1), in fact that they moved to `.mjs` only in a recent upgrade so we can't even hard code it to be `.mjs` in the transform (breaks old versions). Because of all the above, I decided to go with Webpack's own resolution logic with the loader as a _temporary_ solution. It's temporary because it's still assuming that the file structure of these libs doesn't change. |
||
---|---|---|
.. | ||
.stats-app | ||
__mocks__ | ||
development | ||
e2e | ||
examples | ||
integration | ||
lib | ||
production | ||
unit | ||
.gitignore | ||
jest-setup-after-env.ts | ||
jest.d.ts | ||
readme.md | ||
test-file.txt |