An imports transform unplugin.

Overview

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

You might also like...

a vscode extension for http response data auto transform ts type.

a vscode extension for http response data auto transform ts type.

Api2ts 这是一个自动将 http 的响应数据转化为 ts 的类型,使用 json-to-ts 做的。 Features 框选配置项后,使用快捷键 alt+Q : 请求参数配置文件 在根目录下创建 Api2ts.config.json 文件,配置项如下: { "baseURL": "http

Jun 30, 2022

Transform your icons with trendy animations.

iconate.js A call to transform your existing icons in a cool trendy way iconate.js is a tiny performant library for cross-browser icon transformation

Dec 27, 2022

🍎Transform an SVG icon into multiple themes, and generate React icons,Vue icons,svg icons

🍎Transform an SVG icon into multiple themes, and generate React icons,Vue icons,svg icons

IconPark English | 简体中文 Introduction IconPark gives access to more than 2000 high-quality icons, and introduces an interface for customizing your icon

Jan 5, 2023

Manipulate the AST to transform your code.

unplugin-ast Manipulate the AST to transform your code. Installation npm i unplugin-ast Vite // vite.config.ts import AST from 'unplugin-ast/vite' ex

Dec 3, 2022

An automated tool help you to transform backend json data to TypeScript type.

ohmyts An automated tool help you to transform backend json data to TypeScript type. Quick Start Vite install npm i @ohmyts/vite -D 🤽 playground play

Sep 23, 2022

(Experimenting) Transform CSS Modules to enable Atomic CSS benefits 🌱

(Experimenting) Transform CSS Modules to enable Atomic CSS benefits 🌱

MatoCSS Transform CSS Modules to enable Atomic CSS benefits Please note that it is still experimenting. I do not recommend using this in production. U

Nov 24, 2022

The JavaScript library let’s you transform native tooltip’s into customizable overlays.

iTooltip The JavaScript library let’s you transform native tooltip’s into customizable overlays. Use: script src="/path/to/iTooltip.js"/script sc

Dec 17, 2021

Markdown Transformer. Transform markdown files to different formats

Mdtx Inspired by generative programming and weed :). So I was learning Elm language at home usually in the evening and now I am missing all this gener

Jan 2, 2023

a babel plugin that can transform generator function to state machine, which is a ported version of typescript generator transform

Babel Plugin Lite Regenerator intro This babel plugin is a ported version of TypeScript generator transform. It can transform async and generator func

Jul 8, 2022

Creates ES6 ./index.js file in target directories that imports and exports all sibling files and directories.

create-index create-index program creates (and maintains) ES6 ./index.js file in target directories that imports and exports sibling files and directo

Nov 25, 2022

Further split the React Native code based on Metro build to improve performance, providing `Dll` and `Dynamic Imports` features

Further split the React Native code based on Metro build to improve performance, providing `Dll` and `Dynamic Imports` features

React-Native Code Splitting Further split the React Native code based on Metro build to improve performance, providing Dll and Dynamic Imports feature

Dec 29, 2022

Sheetzapper imports your account value accross Zapper.fi supported wallets and dapps into a Google Sheet

Sheetzapper imports your account value accross Zapper.fi supported wallets and dapps into a Google Sheet

Overview Sheetzapper imports your account value accross Zapper.fi supported wallets and dapps into a Google Sheet. This allows you to chart your net w

Nov 27, 2022

Combine type and value imports using Typescript 4.5 type modifier syntax

type-import-codemod Combines your type and value imports together into a single statement, using Typescript 4.5's type modifier syntax. Before: import

Sep 29, 2022

Tracer for module requires/imports to optimize coldstarts in serverless

treq treq is a library to automatically trace requires/imports in your serverless applications and gives you insights about the most expensive modules

Aug 16, 2022

Sort imports by path - VS Code extension

Import sort by absolute path The sorting algorithm will group each item in the array and sort (alphabetically) its children that starts with the path

Feb 2, 2022

Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Dec 19, 2022

Patches the AssemblyScript compiler to utilize WASI imports instead of Web APIs.

Patches the AssemblyScript compiler to utilize WASI imports instead of Web APIs.

WASI shim for AssemblyScript Patches the AssemblyScript compiler to utilize WASI imports instead of Web APIs. Note that this shim also serves a higher

Dec 23, 2022

Transform SVGs into React components 🦁

Transform SVGs into React components 🦁

Transform SVGs into React components 🦁 Try it out online! Watch the talk at React Europe SVGR transforms SVG into ready to use components. It is part

Jan 3, 2023

JavaScript library to transform coordinates from one coordinate system to another, including datum transformations

PROJ4JS Proj4js is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations. Origina

Dec 28, 2022
Owner
ViPro (京京)
A lazy FE Developer 🐈‍⬛
ViPro (京京)
Combine type and value imports using Typescript 4.5 type modifier syntax

type-import-codemod Combines your type and value imports together into a single statement, using Typescript 4.5's type modifier syntax. Before: import

Ian VanSchooten 4 Sep 29, 2022
Tracer for module requires/imports to optimize coldstarts in serverless

treq treq is a library to automatically trace requires/imports in your serverless applications and gives you insights about the most expensive modules

Serkan ÖZAL 29 Aug 16, 2022
Sort imports by path - VS Code extension

Import sort by absolute path The sorting algorithm will group each item in the array and sort (alphabetically) its children that starts with the path

Richard Bidin 3 Feb 2, 2022
Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Jarred Sumner 19 Dec 19, 2022
Patches the AssemblyScript compiler to utilize WASI imports instead of Web APIs.

WASI shim for AssemblyScript Patches the AssemblyScript compiler to utilize WASI imports instead of Web APIs. Note that this shim also serves a higher

The AssemblyScript Project 37 Dec 23, 2022
Zenload - "Load couple loaders and apply transform one-by-one

Zenload Load couple loaders and apply transforms one-by-one. Install npm i zenload -g How to use? With env vairable ZENLOAD: NODE_OPTIONS='"--loader

coderaiser 1 Jan 25, 2022
A typescript transform that converts exported const enums into object literal.

ts-transformer-optimize-const-enum A typescript transformer that convert exported const enum into object literal. This is just like the one from @babe

Fonger 22 Jul 27, 2022
Transform URLs in strings to actual links.

Transform URLs in strings to actual links. It will find valid links in the given string and create <a> tags for it. Internally, it uses this Regex to

Prince Neil Cedrick Castro 7 Nov 4, 2022
A plugin for Strapi Headless CMS that provides the ability to transform the API request or response.

strapi-plugin-transformer A plugin for Strapi that provides the ability to transform the API request and/or response. Requirements The installation re

daedalus 71 Jan 6, 2023
Transform and compose data for HTTP transactions.

Fragments Fragments is a platform to compose and manage custom data objects for HTTP transactions. Simply put, you can write simple jinja templates to

Artem Golub 15 Sep 6, 2022