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

@lidakai/react-utils

v1.0.0

Published

A webpack5 esm library example package.

Downloads

1

Readme

Getting Started

This is a small example repo on how to bundle a ESM library using webpack5. It was based on this blog post and the webpack documentation. It contains several branches:

How to test out this example

  1. Pick a branch.

  2. Install the node dependencies npm i.

  3. Build the bundle using webpack npm run build.

  4. Execute the local_importer.js node script node local_importer.js to see test that the bundle can be imported from inside the main package.

  5. Go into the CRA demo project. Currently the webpack5-library-example library is dynamically linked inside the package.json. You can, however, also use the npm link command.

  6. Install the node dependencies.

  7. Start the development server using npm start.

How to create a ESM library

The steps below are taken from https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c:

  • Add "type": "module" to your package.json.

  • Replace "main": "index.js" with "exports": "./index.js" in your package.json.

  • Update the "engines" field in package.json to Node.js 12: "node": "^12.20.0 || ^14.13.1 || >=16.0.0".

  • Remove 'use strict'; from all JavaScript files.

  • Replace all require()/module.export with import/export.

  • Use only full relative file paths for imports: import x from '.';import x from './index.js';.

  • If you have a TypeScript type definition (for example, index.d.ts), update it to use ESM imports/exports.

  • Optional but recommended, use the node: protocol for imports.

How to use Webpack to build a ESM library

  • Convert your webpack.config.js to a ESM module.

  • Enable the experiments.outputModule option to make sure webpack outputs ECMASCript module syntax when possible.

  • Enable output.module to make sure that javascript files are outputted as ESM modules.

  • If you want your library to be consumed by others make sure to set the output.library.type flag to module.

  • Point the package.json exports property to your outputted bundle.

  • If you want your ESM library to work with a CRA app you have to also specify the "browser": "./build/index.js", entry in your package.json. One known issue is #10933.

How to exclude externals

To see the example checkout the webpack-react-css-lodash-external branch. A guide on how to add externals can be found in the Webpack documentation. The only thing that you have to keep in mind is that when bundling a ES module the externalstype option is set to 'module' by default. If your external module therefore is a ESM module you simply use the following:

externals: {
    "lodash-es": "lodash-es",
}

If your external module is specified in a different format see the documentation.

Useful Resources