• Stars
    star
    433
  • Rank 100,464 (Top 2 %)
  • Language
    TypeScript
  • Created over 4 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple integration to run solid-js with vite

Solid Vite Plugin

âš¡ vite-plugin-solid

A simple integration to run solid-js with vite

HMR gif demonstrationdemodemodemo

Got a question? / Need help?

Join solid discord and check the troubleshooting section to see if your question hasn't been already answered.

Features

  • HMR with no configuration needed
  • Drop-in installation as a vite plugin
  • Minimal bundle size
  • Support typescript (.tsx) out of the box
  • Support code splitting out of the box

Requirements

This module 100% ESM compatible and requires NodeJS 14.18.0 or later.

You can check your current version of NodeJS by typing node -v in your terminal. If your version is below that one version I'd encourage you to either do an update globally or use a NodeJS version management tool such as Volta or nvm.

Quickstart

You can use the vite-template-solid starter templates similar to CRA:

$ npx degit solidjs/templates/js my-solid-project
$ cd my-solid-project
$ npm install # or pnpm install or yarn install
$ npm run start # starts dev-server with hot-module-reloading
$ npm run build # builds to /dist

Installation

Install vite, vite-plugin-solid as dev dependencies.

Install solid-js as dependency.

You have to install those so that you are in control to which solid version is used to compile your code.

# with npm
$ npm install -D vite vite-plugin-solid
$ npm install solid-js

# with pnpm
$ pnpm add -D vite vite-plugin-solid
$ pnpm add solid-js

# with yarn
$ yarn add -D vite vite-plugin-solid
$ yarn add solid-js

Add it as plugin to vite.config.js

// vite.config.ts
import { defineConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';

export default defineConfig({
  plugins: [solidPlugin()],
});

Run

Just use regular vite or vite build commands

{
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  }
}

API

options

  • Type: Object
  • Default: {}

options.include

  • Type: (string | RegExp)[] | string | RegExp | null
  • Default: undefined

A picomatch pattern, or array of patterns, which specifies the files the plugin should operate on.

options.exclude

  • Type: (string | RegExp)[] | string | RegExp | null
  • Default: undefined

A picomatch pattern, or array of patterns, which specifies the files to be ignored by the plugin.

options.dev

  • Type: Boolean
  • Default: true

This will inject solid-js/dev in place of solid-js in dev mode. Has no effect in prod. If set to false, it won't inject it in dev. This is useful for extra logs and debug.

options.hot

  • Type: Boolean
  • Default: true

This will inject HMR runtime in dev mode. Has no effect in prod. If set to false, it won't inject the runtime in dev.

options.ssr

  • Type: Boolean
  • Default: false

This will force SSR code in the produced files.

options.babel

  • Type: Babel.TransformOptions
  • Default: {}

Pass any additional babel transform options. Those will be merged with the transformations required by Solid.

options.solid

Pass any additional babel-plugin-jsx-dom-expressions. They will be merged with the defaults sets by babel-preset-solid.

options.typescript

Pass any additional @babel/preset-typescript.

options.extensions

  • Type: (string, [string, { typescript: boolean }])[]
  • Default: []

An array of custom extension that will be passed through the solid compiler. By default, the plugin only transform jsx and tsx files. This is useful if you want to transform mdx files for example.

Note on HMR

Starting from version 1.1.0, this plugin handle automatic HMR via solid-refresh.

At this stage it's still early work but provide basic HMR. In order to get the best out of it there are couple of things to keep in mind:

  • When you modify a file every state below this component will be reset to default state (including the current file). The state in parent component is preserved.

  • The entrypoint can't benefit from HMR yet and will force a hard reload of the entire app. This is still really fast thanks to browser caching.

If at least one of this point is blocking to you, you can revert to the old behavior by opting out the automatic HMR and placing the following snippet in your entry point:

const dispose = render(() => <App />, document.body);

if (import.meta.hot) {
  import.meta.hot.accept();
  import.meta.hot.dispose(dispose);
}

Troubleshooting

  • It appears that Webstorm generate some weird triggers when saving a file. In order to prevent that you can follow this thread and disable the "Safe Write" option in "Settings | Appearance & Behavior | System Settings".

  • If one of your dependency spit out React code instead of Solid that means that they don't expose JSX properly. To get around it, you might want to manually exclude it from the dependencies optimization

  • If you are trying to make directives work and they somehow don't try setting the options.typescript.onlyRemoveTypeImports option to true

Migration from v1

The master branch now target vite 2.

The main breaking change from previous version is that the package has been renamed from @amoutonbrady/vite-plugin-solid to vite-plugin-solid.

For other breaking changes, check the migration guide of vite.

Credits

More Repositories

1

solid

A declarative, efficient, and flexible JavaScript library for building user interfaces.
TypeScript
31,893
star
2

solid-start

SolidStart, the Solid app framework
TypeScript
5,117
star
3

solid-router

A universal router for Solid inspired by Ember and React Router
TypeScript
1,146
star
4

templates

Vite + solid templates
TypeScript
434
star
5

solid-styled-components

A 1kb Styled Components library for Solid
TypeScript
283
star
6

solid-realworld

A Solid Implementation of the Realworld Example App
JavaScript
228
star
7

solid-docs

SolidJS Docs.
MDX
214
star
8

solid-testing-library

Simple and complete Solid testing utilities that encourage good testing practices.
TypeScript
195
star
9

solid-playground

Quickly discover what the solid compiler will generate from your JSX template
TypeScript
195
star
10

react-solid-state

Auto tracking state management for modern React
TypeScript
188
star
11

solid-site

Code that powers the SolidJS.com platform.
TypeScript
159
star
12

solid-hackernews

Solid implementation of Hacker News
JavaScript
158
star
13

signals

TypeScript
134
star
14

solid-meta

Write meta tags to the document head
TypeScript
127
star
15

solid-refresh

TypeScript
84
star
16

solid-workgroup

Workgroup for future Solid Releases
54
star
17

solid-styled-jsx

A Styled JSX wrapper for Solid
JavaScript
47
star
18

solid-jest

Jest preset for SolidJS
JavaScript
34
star
19

create-solid

Set up a modern web app by running one command
JavaScript
32
star
20

solid-todomvc

Solid implementation of TodoMVC
TypeScript
29
star
21

solidhack-submissions

A repository for collecting SolidHack submissions.
JavaScript
20
star
22

solid-repl

A REPL for SolidJS
TypeScript
14
star
23

solid-service-api

Code that powers Solid Service API on Cloudflare Workers.
JavaScript
13
star
24

solidex

Solidex is a list of SolidJS ecosystem resources and packages.
TypeScript
11
star
25

solid-scripts

No configuration CLI tools to bootstrap Solid applications
JavaScript
10
star
26

solid-assets

Access official dynamic and static assets from SolidJS. Made for the community! 🌟
TypeScript
7
star
27

solid-hot-loader

Webpack Loader with Hot Module Reloading for SolidJS
JavaScript
5
star
28

.github

Community health files for the @solidjs organization
3
star
29

solidhack

Website and contest portal for SolidHack.
TypeScript
2
star