Home

Awesome

<p> <img width="100%" src="https://raw.githubusercontent.com/solidjs/vite-plugin-solid/master/banner.png" alt="Solid Vite Plugin"> </p>

⚡ vite-plugin-solid

A simple integration to run solid-js with vite

<img alt="HMR gif demonstrationdemodemodemo" src=".github/hmr.gif">

Got a question? / Need help?

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

Features

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

options.include

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

options.exclude

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

options.dev

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

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

This will force SSR code in the produced files.

options.babel

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

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:

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

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.

Testing

If you are using vitest, this plugin already injects the necessary configuration for you. It even automatically detects if you have @testing-library/jest-dom installed in your project and automatically adds it to the setupFiles. All you need to add (if you want) is globals, coverage, and other testing configuration of your choice. If you can live without those, enjoy using vitest without the need to configure it yourself.

Credits