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

unplugin-transform-imports

v0.0.2

Published

A imports transform unplugin.

Downloads

78

Readme

unplugin-transform-imports

NPM Publish

An imports transform unplugin based on babel inspired by babel-plugin-transform-imports.

What unplugin-transform-imports Do?

Transform the imports from:

import { Check as MdiCheck } from "mdi-material-ui";
import { Check as MuiCheck, CheckBox } from "@mui/icons-material";
import { Check as PhCheck } from "phosphor-react";
import { merge } from "lodash";

to:

import MdiCheck from "mdi-material-ui/Check";
import MuiCheck from "@mui/icons-material/Check";
import CheckBox from "@mui/icons-material/CheckBox";
import PhCheck from "phosphor-react/dist/icons/Check";
import merge from "lodash/merge";

Why unplugin-transform-imports?

Better Development Performance

Development bundles can contain the full library which can lead to slower startup times. This is especially noticeable if you import from @mui/icons-material. Startup times can be approximately 6x slower than without named imports from the top-level API.

You can save a lot of time if you use webpack.

There are rough test results with demo-craco on my device:

# without unplugin-transform-imports
$ pnpm start
webpack 5.70.0 compiled successfully in 22427 ms

# with unplugin-transform-imports
$ pnpm start
webpack 5.70.0 compiled successfully in 3313 ms

Tree-shaking Alternative

You can also use it as tree-shaking alternative for modules which is not using ESM like lodash.

demo-craco:

# without unplugin-transform-imports
$ du -h --max-depth=0 build
1.6M    build

# with unplugin-transform-imports
$ du -h --max-depth=0 build
980K   build

demo-vite:

# without unplugin-transform-imports
du -h --max-depth=0 dist
280K    dist

# with unplugin-transform-imports
du -h --max-depth=0 dist
220K    dist

How To Use unplugin-transform-imports

Install:

npm i -D unplugin-transform-imports
yarn add -D unplugin-transform-imports
pnpm i -D unplugin-transform-imports

Usage:

import transformImports from "unplugin-transform-imports";

// webpack
transformImports.webpack(transformImportsOptions);

// vite
transformImports.vite(transformImportsOptions);

// rollup
transformImports.rollup(transformImportsOptions);

// esbuild
transformImports.esbuild(transformImportsOptions);

You can check the demo for craco and vite:

transformImportsOptions

const defaultOptions = {
  enforce = undefined, // "pre" | "post" | undefined
  cwd = defaultCwd, // default: process.cwd()
  modules = [], // See Module
  includes = ["**/*.{tsx,ts,jsx,js,mjs}"],
  excludes = ["node_modules"],
  parseOptions, // Optional. See: https://babeljs.io/docs/en/babel-parser#options
  transformOptions, // Optional. See: https://babeljs.io/docs/en/options
};

Module

transformImports.vite({
  modules: [
    {
      path: "lodash", // the module name to replace
    },

    // You can get the same results with these transform options:
    {
      path: "lodash", // the module name to replace
      transform: `\${moduleName}/\${importName}`,
    },
    {
      path: "lodash",
      transform: (importName, moduleName) => `${moduleName}/${importName}`,
    },
  ],
});

// This will make:
import { merge } from "lodash";

// be transformed to:
import merge from "lodash/merge";

There are three variables for the transform function and the transform template. They are importName, moduleName and constName. It's depends on the original code:

import { [importName] } from "[moduleName]"; // constName === importName
import { [importName] as [constName] } from "[moduleName]";

You can use them in a transform template:

const module = {
  path: "lodash",
  transform: `\${moduleName}/\${importName}/\${constName}`,
};

or in transform function:

const module = {
  path: "lodash",
  transform: (importName, moduleName, constName) =>
    `${moduleName}/${importName}/${constName}`,
};

transformImports()

You can use transformImports() function directly without unplugin:

import transformImports from `unplugin-transform-imports/transformImports`;

const transformedCode:string = transformImports(
  code,
  {
    modules, // See Module
    parseOptions, // Optional. See: https://babeljs.io/docs/en/babel-parser#options
    transformOptions, // Optional. See: https://babeljs.io/docs/en/options
  }
);

Development

# init
pnpm i

# build
pnpm build

# install again to link production
pnpm i

## go to the demo
cd packages/demo-{theDemoPath}

Test

pnpm test

LICENSE

MIT