npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fix-react-refresh-plugin

v1.0.2

Published

This Vite plugin helps overcome a problem of not reloading some React components after saving changes.

Downloads

1,058

Readme

fix-react-refresh-plugin

This Vite plugin helps overcome a problem of not reloading some React components after saving changes.

Note that this plugin does not fix this issue, but only applies a workaround. Before use, check whether the problem has been resolved at the level of the modules responsible for HMR.

Background

If you are using lazily loaded class components additionally wrapped in HOCs then HMR problem can occur. We faced these types of issues when moving our app from Create React App to Vite. The foundation of our source code was created many years ago and we still use class components in many places and cannot easily rewrite them. While debugging, we noticed that if we add a functional component to a class component module, then reloading works. Here is an example:

🔴 This code does not work.

import { lazy, Suspense, Component } from 'react';

const Tool = lazy(() => import('./Tool');

class MyComponent extends Component {
  render() {
    return (
      <div>
        {someCondition && (
          <Suspense>
            <Tool />
          </Suspense>
        )}
      </div>
    );
  }
);

export default withHoc(MyComponent);

🟢 This code does work.

import { lazy, Suspense, Component } from 'react';

const Tool = lazy(() => import('./Tool');

const NoopFC = () => null; // Here is an empty functional component.

class MyComponent extends Component {
  render() {
    return (
      <div>
        {someCondition && (
          <Suspense>
            <Tool />
          </Suspense>
        )}
      </div>
    );
  }
);

export default withHoc(MyComponent);

Therefore, we came up with an idea for a plugin that will automatically add an empty functional component to class component modules to force refreshing. If you need more information about this error and alternative ways to resolve it, check out this issue.

Usage

Install the plugin via your package manager:

npm i -D fix-react-refresh-plugin

Import and apply it to your vite.config.js file:

import fixReactRefresh from 'fix-react-refresh-plugin';

export default defineConfig({
  plugins: [
    {
      ...fixReactRefresh(),
      enforce: 'pre',
    },
    react()
  ]
});

⚠️ fixReactRefresh() must be set before react() and must have enforce set to pre.

Configuration

The plugin is prepared to work right out of the box, but for more sophisticated cases you may need to change the configuration. Below is a list of configurable options: | Property name | Description | Type | Default value | Required | | ------------- | ----------- | ---- | ------------- | -------- | | componentFileNameRegEx | Match the pattern of React component files. | RegExp | /\.(jsx\|tsx)/ | No | | classComponentDeclarationRegEx | Match the pattern of React class components declarations. | RegExp | /(export(\sdefault)*)*\s*class\s+\w+\s+extends\s+(React.(Component\|PureComponent)\|(Component\|PureComponent))/gm | No | | noopFunctionalComponentName | The name of the added noop function that forces component reloading. You must use the PascalCase notation and avoid special characters. | string | FixReactRefreshFC | No |