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

swc_mut_cjs_exports

v8.0.1

Published

[SWC plugin] mutable CJS exports

Downloads

403,175

Readme

[SWC plugin] mutable CJS exports

Crates.io npm

Test SWC Compat Test with @swc/core@latest with @swc/core@nightly

This is a SWC plugin to emit mutable CJS exports.

This SWC plugin has only been tested for compatibility with jest. It should be used with @swc/jest.

This project was previously called jest_workaround

plugin version

https://swc.rs/docs/plugin/selecting-swc-core

usage

install

npm i -D jest @swc/core @swc/jest swc_mut_cjs_exports
// jest.config.js
const fs = require("node:fs");

const swcrc = JSON.parse(fs.readFileSync(".swcrc", "utf8"));

// If you have other plugins, change this line.
((swcrc.jsc ??= {}).experimental ??= {}).plugins = [
  ["swc_mut_cjs_exports", {}],
];

module.exports = {
  transform: {
    "^.+\\.(t|j)sx?$": ["@swc/jest", swcrc],
  },
};

Alternative implementation without .swcrc file

// jest.config.js

module.exports = {
  transform: {
    "^.+\\.(t|j)sx?$": [
      "@swc/jest",
      {
        jsc: {
          experimental: {
            plugins: [["swc_mut_cjs_exports", {}]],
          },
        },
      },
    ],
  },
};

Make sure that module.type is commonjs in your .swcrc since this plugin does not touch non-workaround parts, such as import statements.

FAQ

1. When do I need this?

If you're using the swc compiler to transform your code to comply with the ESM specification, but you're also using Jest to test it in a CJS environment, you may encounter issues due to the immutable issue of exports.

This plugin can help by transforming the export statements into mutable exports.

2. Do I have a better choice?

You may have other options depending on your specific needs:

  • If you're able to run Jest in an ESM environment, you can use swc to transpile TypeScript/JSX syntax or downgrade JavaScript syntax without module conversion. Simply set the value of module.type to es6 to achieve this.

  • It's possible that some issues related to running Jest in an ESM environment will be resolved over time. Keep an eye on facebook/jest#9430 for updates.

  • If you don't need the behavior of ESM specifically, you can stick with the CJS syntax to get the CJS behavior of exports.

These options may be worth considering before using this plugin.

CJS syntax

exports.foo = function foo() {
  return 42;
};

CTS(CJS in TypeScript) syntax

export = {
  foo: function foo() {
    return 42;
  },
};

Notes:

  • ESM style export means immutable exports when transformed into CJS
  • ESM style import means hoisted require when transformed into CJS

3. After upgrading the plugin version, the changes have not taken effect.

This is a known issue. You could remove the Jest cache by running jest --clearCache as a workaround.