devtools icon indicating copy to clipboard operation
devtools copied to clipboard

Importing as ES module, DevTool is included in production build

Open jpa00 opened this issue 2 years ago • 8 comments

As the title says, when a project by default imports and builds everything from ES modules, hookform's DevTool import returns the entire code even in production builds, instead of the noop of the default import.

Depending on the environment, a quick workaround may be to import from /dist/index for now, which returns either the CommonJS-version or a noop DevTool.

jpa00 avatar Sep 08 '22 08:09 jpa00

+1 I just came across this as well (building a React app with Vite)

Made a quick demo to showcase the problem - https://stackblitz.com/edit/github-ei8jpp?file=src/Home/index.tsx

AdiRishi avatar Oct 23 '22 12:10 AdiRishi

Is this going to be addressed or should we use the work around?

steveoh avatar Nov 02 '22 22:11 steveoh

Any updates on this issue?

AdiRishi avatar Dec 13 '22 02:12 AdiRishi

+1 @bluebill1049 may you take a look at this issue please?

timsofteng avatar May 30 '23 11:05 timsofteng

A workaround (that could turn into a solution) inspired by react-query-devtools that worked for me is to create the following file and always import the DevTool from there:

hookform-devtools.ts

import * as hookFormDevTools from "@hookform/devtools";

export const DevTool: (typeof hookFormDevTools)["DevTool"] =
  process.env.NODE_ENV !== "development"
    ? function () {
        return null;
      }
    : hookFormDevTools.DevTool;

then change your import from

- import { DevTool } from "@hookform/devtools";
+ import { DevTool } from "../hookform-devtools";

burnedikt avatar Jun 07 '23 13:06 burnedikt

+1 any updates?

hadson172 avatar Sep 17 '23 13:09 hadson172

My workaround: {import.meta.env.VITE_RHF_DEVTOOLS && <DevTool control={control} />}

edit: it's not working :confused:

RomainMorlevat avatar Jan 05 '24 18:01 RomainMorlevat