🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

swc-plugin-another-transform-imports

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swc-plugin-another-transform-imports

Another wasm swc transform imports plugin

1.11.1
latest
npm
Version published
Weekly downloads
6.9K
-3.53%
Maintainers
1
Weekly downloads
 
Created
Source

swc-plugin-another-transform-imports

Another wasm plugin for swc, inspired from babel-plugin-transform-imports.

Installation

npm install --save-dev swc-plugin-another-transform-imports
# or
yarn add -D swc-plugin-another-transform-imports

Usage

It follows resolving rule of node.js, can be use in .swcrcwebpack.config.js or next.config.js and so on. For Example:

{
  "jsc": {
    "experimental": {
      "plugins": [
        [
          "swc-plugin-another-transform-imports",
          {
            "antd": {
              "transform": "antd/lib/${member}",
              "skipDefaultConversion": false,
              "preventFullImport": true,
              "style": "antd/lib/${member}/style",
              "memberTransformers": ["dashed_case"]
            },
            "lodash": {
              "transform": "lodash/${member}",
              "preventFullImport": true
            }
          }
        ]
      ]
    }
  }
}

Can convert the following lines:

import { Button as MyButton, BackTop } from "antd";
import { merge } from "lodash";

To:

import MyButton from "antd/lib/button";
import BackTop from "antd/lib/back-top";
import "antd/lib/back-top/style";
import "antd/lib/button/style";
import merge from "lodash/merge";

For next.js users

Ensure you read next.js compiler docs first!!!!

Since the semantic version association of @swc/core (npm) and swc_core (rust) for next.js is still experimental. next.js has a lot of breaking change in the swc native plugin mechanism between major and even minor versions.

It is possible that as next.js and swc are updated, the current plugin will fail in the new version. Hopefully the new plugin api for swc will be stable soon. Here is the current version correspondence.

next.js versionsswc_versionsThis package version
12.3.x-0.1.5
13.0.x-0.2.1
13.2.4 ~ 13.3.1-not support https://github.com/vercel/next.js/issues/46989#issuecomment-1486989081
13.3.1 ~ 13.4.3-0.2.3
13.4.3-canary.2 ~ 13.4.71.3.58 ~ 1.3.620.2.4
13.4.8 ~ v13.4.10-canary.01.3.63 ~ 1.3.670.2.5
13.4.10-canary.1 ~1.3.68 ~ 1.3.800.2.6
~ 14.1.01.3.81 ~ 1.3.1050.2.7
-1.3.106 ~ 1.3.1070.2.8
14.1.1-canary.52 ~ 15.0.0-canary.281.4.x0.2.9
15.0.0-canary.29 ~ 15.0.0-canary.361.5.x1.5.0
15.0.0-canary.37 ~ 15.0.0-canary.1161.6.x1.6.0
15.0.0-canary.117 ~ v15.0.0-canary.1831.7.0 ~ 1.7.271.7.0
15.0.0 ~ 15.0.21.7.28 ~ 1.7.421.7.42
-1.8.01.8.0
15.0.3 ~ 15.1.71.9.0 ~ 1.9.21.9.0
-1.9.31.9.3
15.2.01.10.0 ~ 1.10.181.10.0
15.2.1 ~ 15.3.21.11.1 ~ 1.11.241.11.1

@swc/core and swc_core version mappings

For antd users

antd/es/xxx/style and antd/lib/xxx/style will introduce less, please add less-loader by yourself.

If you use antd and next.js at the same time, it will be a bit troublesome to work with them, you can use next-plugin-antd-less for convenience, see issue 1 for an example.

Options

NameTypeRequiredDefaultDescription
transformstringyesundefinedThe library name to use instead of the one specified in the import statement. ${member} will be replaced with the member, aka Grid/Row/Col/etc.
preventFullImportbooleannotrueWhether or not to throw when an import is encountered which would cause the entire module to be imported.
skipDefaultConversionbooleannofalseWhen set to true, will preserve import { X } syntax instead of converting to import X.
stylestringnofalseThe style path of the member, ${member} will be replaced with the member, aka Grid/Row/Col/etc.
memberTransformersArray<MemberTransformer>no[]Member transformers

1. type MemberTransformer

type MemberTransformer =
  | "camel_case"
  | "kebab_case"
  | "pascal_case"
  | "snake_case"
  | "upper_case"
  | "upper_first"
  | "lower_case"
  | "lower_first"
  | "dashed_case";

Common Issues

Usually upgrading to the latest version of @swc/core and other swc tools will solve the problem, see the following issue for typical solution:

Fork & Modify

You can simply fork this plugin, modify its source code to suit your custom needs. For fast validation, you don't necessarily have to publish the modified project, but simply require your wasm file to the project. For example:

cargo prepublish

or

npm run prepublish

Then copy your wasm target file, and set your config to:

module.exports = {
  ...,
  "jsc": {
    "experimental": {
      "plugins": [
        [
          require.resolve("./path/to/your-modified-plugin.wasm"),
          {
            ..your options
          }
        ]
      ]
    }
  },
  ...
}

Keywords

swc-plugin

FAQs

Package last updated on 19 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts