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

swc-plugin-transform-import-declaration

v1.0.7

Published

fix the ast assertion in swc ast for Ankit Chouhan's swc-plugin-transform-import

Downloads

8

Readme

swc-plugin-transform-import-declaration

swc-plugin-transform-import declaration is a swc-loader plugin used in webpack for demanding import.

import Button from '@mui/material';
// swc-loader equipped with this plugin will compile the above as follows 
const _button = require('@mui/material/Button')

The example above is much more simplified, BUT it's clear that for most cases' demanding import for UI library, we just use a plugin to compile importing code slightly more flexibly, i.e. transforming the import declaration, whether it's a babel-plugin or swc-plugin. This is because if we don't transform the import declaration, the un-transformed declaration will be mean to import whole stuff, hundreds of dependencies in view of dependency tree.

About this npm package

Forked and revised from Ankit Chouhan's swc-plugin-transform-import changed the use of skipDefaultConversion, now whether transforming named import into export is now controlled by business logics. More specifically, making option property skipDefaultConversion fully CONTROL whether import declaration is named import or default import, because it's more about business logic which should have more flexiblity and even diffs from UI library.

// Ankit Chouhan's version: swc-plugin-transform-import
// while setting skipDefaultConversion to false
import { Button } from '@mui/material';
// swc-loader equipped with this plugin will compile the above as follows 
const _button = require('@mui/material/Button').default

// ----------------------------------------------------

// this package's version: swc-plugin-transform-import-declaration
// while setting skipDefaultConversion to false
import { Button } from '@mui/material';
// swc-loader equipped with this plugin will compile the above as follows 
const _button = require('@mui/material/Button')

❗❗❗ Orginal README is as follows, and the use of the this plugin can also be found here. Just pay more attention of the tricking property skipDefaultConversion, whose implemention is fully changed.

swc-plugin-transform-import

Inspired from babel-plugin-transform-imports

Installation

npm i -D swc-plugin-transform-import

Uses with webpack-config

// webpack.config.js

const PluginTransformImport = require('swc-plugin-transform-import').default;

module: {
    rules: [
      {
        test: /\.(ts|tsx|js|jsx)$/,
        exclude: /node_modules/,
        use: [{
          loader: 'swc-loader',
          options: {
            plugin: (m) => new PluginTransformImport({
              "lodash": {
                "transform": "lodash/${member}",
                "preventFullImport": true
              }
            }).visitProgram(m),
          }
        }]
      },
    ],
};
import { Row, Grid as MyGrid } from 'react-bootstrap';
import { merge } from 'lodash';

...into default style imports:

import Row from 'react-bootstrap/lib/Row';
import MyGrid from 'react-bootstrap/lib/Grid';
import merge from 'lodash/merge';

Note: this plugin is not restricted to the react-bootstrap and lodash libraries. You may use it with any library.

That's stupid, why would you do that?

When SWC encounters a member style import such as:

import { Grid, Row, Col } from 'react-bootstrap';

it will generate something similarish to:

var reactBootstrap = require('react-bootstrap');
var Grid = reactBootstrap.Grid;
var Row = reactBootstrap.Row;
var Col = reactBootstrap.Col;

Some libraries, such as react-bootstrap and lodash, are rather large and pulling in the entire module just to use a few pieces would cause unnecessary bloat to your client optimized (webpack etc.) bundle. The only way around this is to use default style imports:

import Grid from 'react-bootstrap/lib/Grid';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';

But, the more pieces we need, the more this sucks. This plugin will allow you to pull in just the pieces you need, without a separate import for each item. Additionally, it can be configured to throw when somebody accidentally writes an import which would cause the entire module to resolve, such as:

import Bootstrap, { Grid } from 'react-bootstrap';
// -- or --
import * as Bootstrap from 'react-bootstrap';

Installation

npm install --save-dev swc-plugin-transform-import

Advanced Transformations

In cases where the provided default string replacement transformation is not sufficient (for example, needing to execute a RegExp on the import name), you may instead provide a path to a .js file which exports a function to run instead. Keep in mind that the .js file will be required relative from this plugin's path, likely located in /node_modules/babel-plugin-transform-imports. You may provide any filename, as long as it ends with .js.

.babelrc:

{
    "plugins": [
        ["transform-imports", {
            "my-library": {
                "transform": "../../path/to/transform.js",
                "preventFullImport": true
            }
        }]
    ]
}

/path/to/transform.js:

module.exports = function(importName) {
    return 'my-library/etc/' + importName.toUpperCase();
};

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. Alternatively, pass a path to a .js file which exports a function to process the transform (see Advanced Transformations) | | preventFullImport | boolean | no | false | 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 | boolean or function | no | false | When set to true, will add side effect import of transformed path concatenated with /style. When set as a function, receive an argument as the transformed path, return the tramsformed style module path |