Search code examples
react-hooksnpm-publish

Should I compile my react hook library, or publish only the ES6 code?


I have a very simple hook that I want to publish to npm, and I'm struggling to see why I should compile my hook at all. Let me first provide some context.

My hook depends only upon React.useState and React.useEffect. The purpose of this hook is to be used with my existing library: https://npmjs.com/package/simple-shared-state. As you can see, there's very little to this hook:

import { useEffect, useState } from "react";

export default (store, selectors) => {
    const [state, setState] = useState([]);

    useEffect(() => {
        const unwatch = store.watchBatch(selectors, (array) => {
            setState(array.slice());
        });
        return unwatch;
    }, []);

    return state;
};

I've looked at the create-react-hook cli tool, and tried it out, but I don't see how the included dev-dependencies are needed for my project. My hook is already usable in my mono-repo, where I added a folder, react-test-ground/, in which I provide a working app that I bootstrapped using CRA.

Concerning Testing:

If it makes sense to do, I can go the extra step of adding unit tests specifically for this hook, but at the moment, I can't see a strong need for this since the core of the functionality is in simple-shared-state, and all of that logic is already covered with pretty extensive tests.

I had a look at react-hooks-testing-library, and based on the "When to use this library" and "When not to use this library", it seems to me that my situation is one where I don't need to use this library. I think my hook is simple enough that the extra steps of adding more tests are hard to justify. Do you agree? Can you think of a reason why I should use react-hooks-testing-library?

Concerning compilation:

I can't see a strong reason to compile and minify my hook, since react app developers will almost exclusively be compiling their project from JSX anyway.

Summary

Given all of the above, is there any reason to do anything more than simply publish my hook, exactly as shown, on npm? Meaning, the package.json would include "main": "src/index.js", and no dist/ directory. React would go in the package.json under peerDependencies, and that's it. App developers would simply compile the source ES6 code of this hook into their bundle, and that's all.

Thanks in advance for taking the time to read and reply!


Solution

  • You can publish ES6 code.

    The current js spec is ES2020. All runtimes should support all features from this spec. Thus you need compile own code to latest standard. If user want to use your code on older runtimes(e.g. IE11) he can transpile all code in his build process.