0.1.2 • Published 1 year ago

jotai-react-refresh-ts-plugin v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

jotai-react-refresh-ts-plugin

CI codecov Downloads

This plugin adds support for React Refresh for Jotai atoms. This makes sure that state isn't reset, when developing using React Refresh.

Install

npm install jotai-react-refresh-ts-plugin --save-dev

Usage

With a webpack configuration file:

const { join } = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

const { createJotaiReactRefreshTransformer } = require('jotai-react-refresh-ts-plugin')

module.exports = {
  entry: './tests/fixtures/simple.tsx',

  output: {
    filename: '[name].[hash].js',
    path: join(process.cwd(), 'dist'),
  },

  resolve: {
    extensions: ['.tsx', '.ts', '.js', '.jsx'],
  },

  mode: 'development',

  devtool: 'cheap-module-source-map',

  module: {
    rules: [
      {
        test: /\.(j|t)sx?$/,
        loader: 'ts-loader',
        options: {
          transpileOnly: true,
          getCustomTransformers: () => ({
            before: [
              // <------------------- here
              createJotaiReactRefreshTransformer({
                // this can add debug label to your own custom atom
                customAtomNames: ['myCustomAtom']
              }),
            ],
          }),
          compilerOptions: {
            jsxFactory: 'jsx',
          },
        },
        exclude: /node_modules/,
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader?minimize'],
      },
    ],
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: join(process.cwd(), 'tests', 'fixtures', 'index.html'),
    }),
  ],
}