GithubHelp home page GithubHelp logo

sidx1024 / vite-plugin-swc-only Goto Github PK

View Code? Open in Web Editor NEW

This project forked from natrim/vite-plugin-swc-only

0.0 0.0 0.0 45 KB

Use the https://swc.rs in https://vitejs.dev for transformation and minification.

License: MIT License

JavaScript 5.12% TypeScript 94.88%

vite-plugin-swc-only's Introduction

vite-plugin-swc-only

Use the swc in vite for transformation and minification.

Hot glued from other plugins.

Only React for now, pulls welcome.

  • Don't need vite-plugin-react

  • Support for React Fast Refresh - faster than the vite-plugin-react with babel

  • Skip import React,React is dynamically injected

  • swc minification instead of esbuild or terser (no css minify as that is hardcoded to esbuild)

  • 3 separate plugins for each mode available (serve, build, minify)

  • serve - applied only in dev (apply: serve), contains React Fast Refresh

  • build - applied only in build mode (apply: build), disables esbuild transform

  • minify - applied only for minification (disables esbuild, terser minify), overrides config.minify

Installation

npm i -D vite-plugin-swc-only @swc/core

Usage

import { defineConfig } from "vite";
import swc from "vite-plugin-swc";

// use all plugins (serve, build, minify)
export default defineConfig({
  plugins: [swc()],
  // or ie. plugins: [swc({minify: false, serve: true, build: false})],
});

// or define each plugin separately
export default defineConfig({
  plugins: [swc.serve(), swc.build(), swc.minify()],
});

Polyfill

To enable polyfill, you would need to

  • install browserlist as a devDependency
  • install core-js as a dependency
npm i browserlist && npm i -D core-js

ES5

If your target browser only supports ES5, you may want to checkout @vitejs/plugin-legacy.

Problems

If you use this plugin only for serve and/or minify and not use vite-plugin-react, then you will need to add extra options for esbuild transformation to support React dynamic import.

import { defineConfig } from "vite";
import swc from "vite-plugin-swc";

// if you use this plugin only in dev mode for fast react refresh
export default defineConfig({
  plugins: [swc.serve()],
  // you will need these settings for automatic react inject in esbuild
  esbuild: {
    jsxFactory: "_jsx",
    jsxFragment: "_jsxFragment",
    jsxInject:
      "import { createElement as _jsx, Fragment as _jsxFragment } from 'react'",
  },
});

Examples

disable HMR

import { defineConfig } from "vite";
import swc from "vite-plugin-swc-only";

export default defineConfig({
  plugins: [
    swc({
      refresh: false,
    }),
  ],
});

Classic JSX runtime

You need to include import React from 'react'; in every tsx file yourself.

import { defineConfig } from "vite";
import swc from "vite-plugin-swc-only";

export default defineConfig({
  plugins: [
    swc({
      runtime: "classic",
    }),
  ],
});

Disable minification

If there is ie. problem with minification on your swc version, it will fallback back to esbuild.

import { defineConfig } from "vite";
import swc from "vite-plugin-swc-only";

export default defineConfig({
  plugins: [
    swc({
      minify: false,
    }),
  ],
});

vite-plugin-swc-only's People

Contributors

natrim avatar sidx1024 avatar

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.