npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@moneko/transform-imports

v0.6.1

Published

Another wasm swc transform imports plugin

Downloads

318

Readme

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 versions | This 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.7(@swc/[email protected] ~ @swc/[email protected]) | 0.2.4 | | 13.4.8 ~ v13.4.10-canary.0(@swc/[email protected] ~ @swc/[email protected]) | 0.2.5 | | 13.4.10-canary.1 ~ (@swc/[email protected] ~ @swc/[email protected]) | 0.2.6 | | Unpublished (@swc/[email protected] ~ ) | 0.2.7 |

@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

| Name | Type | Required | Default | Description | |-------------------------|----------------------------|----------|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------| | transform | string | yes | undefined | The 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. | | preventFullImport | boolean | no | true | Whether or not to throw when an import is encountered which would cause the entire module to be imported. | | skipDefaultConversion | boolean | no | false | When set to true, will preserve import { X } syntax instead of converting to import X. | | style | string | no | false | The style path of the member, ${member} will be replaced with the member, aka Grid/Row/Col/etc. | | memberTransformers | Array<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.'
  2. about next.js and antd
  3. about next.js 13+

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
          }
        ]
      ]
    }
  },
  ...
}