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

[docs] Adding tree shaking guide for SWC compiler #36456

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,57 @@ It will perform the following diffs:
+import { Button, TextField } from '@mui/material';
```

### Option three: use a SWC plugin

This option pretty much similar to the option two, except for the developer who is using [SWC Compiler](https://swc.rs/). Use this [plugin](https://github.com/swc-project/plugins/blob/main/packages/transform-imports/README.md):

`yarn add -D @swc/plugin-transform-imports`
Copy link
Member

Choose a reason for hiding this comment

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

Would we also need @swc/core and swc-loader dependencies?


Modify your .swcrc or Webpack configuration:
Copy link
Member

Choose a reason for hiding this comment

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

Below we are showing the config for Webpack, so mentioning .swcrc here may confuse devs. It would be better to show a separate config for .swcrc.

Suggested change
Modify your .swcrc or Webpack configuration:
Modify your webpack configuration as shown below:


```js
/* webpack.config.js */
const webpackConfig = {
// ...
module: {
rules: [
...{
test: /\.tsx?$/,
Copy link
Member

Choose a reason for hiding this comment

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

Include Javascript files as well:

Suggested change
test: /\.tsx?$/,
test: /\.(tsx?|jsx?)$/,

use: [
Comment on lines +225 to +226
Copy link
Member

Choose a reason for hiding this comment

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

Should we exclude node_modules?

Suggested change
test: /\.tsx?$/,
use: [
test: /\.tsx?$/,
exclude: /(node_modules)/,
use: [

{
loader: 'swc-loader',
options: {
jsc: {
experimental: {
plugins: [
[
require.resolve('@swc/plugin-transform-imports'),
{
'@mui/material': {
transform: '@mui/material/{{member}}',
},
'@mui/icons-material': {
transform: '@mui/icons-material/{{member}}',
},
// For styled, useTheme etc
'@mui/system': {
transform: '@mui/system/{{member}}',
},
},
],
],
},
},
},
},
],
},
],
},
};
module.exports = webpackConfig;
```

## Available bundles

The package published on npm is **transpiled**, with [Babel](https://github.com/babel/babel), to take into account the [supported platforms](/material-ui/getting-started/supported-platforms/).
Expand Down