0.2.2 • Published 1 year ago

swc-plugin-transform-imports v0.2.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

swc-plugin-transform-imports

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

Installation

npm install --save-dev swc-plugin-transform-imports
# or
yarn add -D swc-plugin-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-transform-imports",
          {
            "antd": {
              "transform": "antd/es/${member}",
              "skipDefaultConversion": false,
              "preventFullImport": true,
              "style": "antd/es/${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/es/button";
import BackTop from "antd/es/back-top";
import "antd/es/back-top/style";
import "antd/es/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 versionsThis package version
12.3.x0.1.5
13.0.x0.2.1

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:

  1. plugin-styled-components crashes with 'Error while importing "env"."__get_transform_plugin_config": unknown import.'