Coder Social home page Coder Social logo

vdustr / unplugin-transform-imports Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 1.0 912 KB

An imports transform unplugin.

Home Page: https://www.npmjs.com/package/unplugin-transform-imports

License: MIT License

TypeScript 81.89% HTML 11.66% JavaScript 0.87% Svelte 2.09% Vue 3.49%
unplugin import transform webpack rollup vite tree-shaking lodash icon esbuild

unplugin-transform-imports's Introduction

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

unplugin-transform-imports's People

Contributors

vdustr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

squishyu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.