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

babel-plugin-optimize-helpers

v0.0.1-dev.1

Published

A Babel plugin that replaces known Babel modular runtime helpers with `require()` calls to a single runtime library.

Downloads

3

Readme

babel-plugin-optimize-helpers

A Babel plugin that replaces known Babel modular runtime helpers with require() calls to a single runtime library.

Why?

Many packages in NPM are vended as already transpiled code that either already contain these helper functions or require() them from different runtime libraries (e.g. @babel/{runtime|runtime-corejs2}/helpers/[esm/]). This plugin will transform all of these references to require() from @babel/runtime-corejs2/helpers/esm/ (the one that the default Next.js configuration for @babel/plugin-transform-runtime uses). That way, there should only be a one, canonical copy of these helper functions which can be included in your "common" bundle.

Setup with webpack

In your webpack config, add a module rule that loads your script with a babel-loader with this plugin.

module.exports = {
  // ...
  module: {
    rules: [
      // ...
      {
        test: /\.(js|mjs|jsx)$/,
        exclude: /[\\/]node_modules[\\/]@babel[\\/]runtime-corejs2[\\/]/,
        use: {
          loader: 'babel-loader',
          options: {
            babelrc: false,
            plugins: [
              require.resolve('@babel/plugin-syntax-dynamic-import'),
              require.resolve('@babel/plugin-syntax-class-properties'),
              require.resolve('@babel/plugin-syntax-jsx'),
              // Add other syntax plugins here.

              require('./babel-plugin-optimize-helpers')
            ]
          }
        }
      }
    ]
  }
};

Important Exclude @babel/runtime-corejs2/ from this rule - this would rewrite references to helpers in this library to require() itself and result in circular references.

Next.js

If you're using Next.js, you can use the provided next-plugin:

const withOptimizeHelpers = require('babel-plugin-optimize-helpers/next-plugin');

module.exports = withOptimizeHelpers({
  /* ... */
});

Alternatively, modify the webpack config in next.config.json (e.g. if you need other Babel syntax plugins):

module.exports = {
  webpack(config, { dev, isServer }) {
    const optimize = !dev && !isServer;

    // Only optimize browser code.
    if (optimize && config.optimization.splitChunks) {

      // Include `@babel/runtime-corejs2/` in the commons bundle.
      config.optimization.splitChunks.cacheGroups['corejs'] = {
        name: 'commons',
        chunks: 'all',
        test: /[\\/]node_modules[\\/]@babel[\\/]runtime-corejs2[\\/]/
      };

      // Load script with the plugin.
      config.module.rules.push({
        test: /\.(js|mjs|jsx)$/,
        exclude: /[\\/]node_modules[\\/]@babel[\\/]runtime-corejs2[\\/]/,
        use: {
          loader: 'babel-loader',
          options: {
            babelrc: false,
            plugins: [
              require.resolve('@babel/plugin-syntax-dynamic-import'),
              require.resolve('@babel/plugin-syntax-class-properties'),
              require.resolve('@babel/plugin-syntax-jsx'),
              // Add other syntax plugins here.

              require('./babel-plugin-optimize-helpers')
            ]
          }
        }
      });
    }

    return config;
  }
};

Do I need this plugin?

In an ideal world, libraries will publish a copy of un-transpiled source code and leave transpiling to the app developer (who can decide what the transpile target should be and what helpers to bundle). Unfortunately this is not the case today. This plugin should be considered as short-term gap solution.