简体   繁体   中英

Next.js how to use SWC compiler with Material UI and swc-plugin-transform-import

I've been struggling with transforming imports with Next.js using SWC complier.

I'm trying to make use of swc-plugin-transform-import as a replacement of babel-plugin-transform-imports for shorting on Material UI imports.

As documented, I've tried with this settings. It shows experimental warning, but other than that it ignores plugin all together.

// next.config.js

module.exports = {
  experimental: {
    swcPlugins: [
      [
        'swc-plugin-transform-import',
        {
          "@mui/material": {
            transform: "@mui/material/${member}",
            preventFullImport: true
          },
          "@mui/icons-material": {
            transform: "@mui/icons-material/${member}",
            preventFullImport: true
          },
          "@mui/styles": {
            transform: "@mui/styles/${member}",
            preventFullImport: true
          },
          "@mui/lab": {
            transform: "@mui/lab/${member}",
            preventFullImport: true
          }
        }
      ]
    ]
  }
}

Anyone knows how to enable and configure swc-plugin-transform-import for Next.js? Thank you

Oh I've found an answer in Next.js Compiler Docs .

module.exports = {
  swcMinify: true,

  experimental: {
    modularizeImports: {
      "@mui/material": {
        transform: "@mui/material/{{member}}"
      },
      "@mui/icons-material": {
        transform: "@mui/icons-material/{{member}}"
      },
      "@mui/styles": {
        transform: "@mui/styles/{{member}}"
      },
      "@mui/lab": {
        transform: "@mui/lab/{{member}}"
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM