Skip to content

Commit

Permalink
Initial commit [publish]
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Jan 28, 2022
0 parents commit 7ced984
Show file tree
Hide file tree
Showing 9 changed files with 1,053 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Publish to npm
on:
push:
branches:
- main
jobs:
publish:
runs-on: ubuntu-latest
if: ${{ contains(github.event.head_commit.message, '[publish]') }}
steps:
- uses: actions/checkout@v2
- run: yarn install --frozen-lockfile
- run: yarn prettier-ci
- run: yarn build
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v2
with:
node-version: "16.x"
registry-url: "https://registry.npmjs.org"
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea/
node_modules/
*.js
*.d.ts
!src/refresh-runtime.js
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.ts
!*.d.ts
tsconfig.json
.github
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# vite-plugin-swc-react-refresh [![npm](https://img.shields.io/npm/v/vite-plugin-swc-react-refresh)](https://www.npmjs.com/package/vite-plugin-swc-react-refresh)

Use the versatility of [swc](https://swc.rs/) for development and the maturity of [esbuild](https://esbuild.github.io/) for production.

- ✅ A fast Fast Refresh (~20x faster than Babel)
- ✅ Skip `import React`
- ❌ Not compatible with [automatic JSX runtime](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html). See [this section](#jsx-runtime)
- ❌ Don't check for consistent components exports. See [this section](#consistent-components-exports)

## Installation

```sh
npm i -D vite-plugin-swc-react-refresh
```

## Usage

```ts
import { defineConfig } from "vite";
import swcReactRefresh from "vite-plugin-swc-react-refresh";

export default defineConfig({
plugins: [swcReactRefresh()],
esbuild: { jsxInject: `import React from "react"` },
});
```

## JSX Runtime

This plugin is only used in development, and esbuild (which doesn't support the automatic JSX runtime) will be used by Vite for bundling. However, you can omit the default React import with the `esbuild.jsxInject` Vite option.

## Consistent components exports

For React refresh to work, your file should only export React components. The best explanation I've read is the one from the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works).

However, having some components being hot updated and not others is a very frustrating experience as a developer, and I think this rule should be enforced by a linter, so I made an [eslint rule](https://github.com/ArnaudBarre/eslint-plugin-react-refresh) to go along with this plugin.

This plugin expects this rule to be respected and will always fast refresh components. If a file export something that is not a React component (TS types are ok), update to this export would not propagate and require a manual reload.
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "vite-plugin-swc-react-refresh",
"description": "Use the versatility of swc for development and the maturity of esbuild for production",
"version": "0.1.0",
"license": "MIT",
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
"main": "src/swc-react-refresh.js",
"repository": "github:ArnaudBarre/vite-plugin-swc-react-refresh",
"keywords": [
"vite",
"vite-plugin",
"react",
"swc",
"react-refresh",
"fast refresh"
],
"scripts": {
"build": "tsc",
"prettier": "yarn prettier-ci --write",
"prettier-ci": "prettier --check '**/*.{js,ts,json,md,yml}'"
},
"prettier": {
"trailingComma": "all"
},
"dependencies": {
"@swc/core": "^1.2.133"
},
"devDependencies": {
"@types/node": "^17.0.10",
"prettier": "^2.5.1",
"typescript": "^4.5.4",
"vite": "^2.7.13"
}
}
Loading

0 comments on commit 7ced984

Please sign in to comment.