# swc-mutable-cjs-exports

> SWC plugin to create mutable ESM exports in transpiled CJS

Latest version **0.0.3** (published 2022-12-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install swc-mutable-cjs-exports
pnpm add swc-mutable-cjs-exports
yarn add swc-mutable-cjs-exports
bun add swc-mutable-cjs-exports
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2022-12-09 |
| First published | 2022-12-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 1.4 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 64 |
| Author | magic-akari |
| Maintainers | hensapir |
| Keywords | swc-plugin, jest |

## Links

- npm: https://www.npmjs.com/package/swc-mutable-cjs-exports
- Repository: https://github.com/magic-akari/jest_workaround
- Homepage: https://github.com/magic-akari/jest_workaround#readme
- Issues: https://github.com/magic-akari/jest_workaround/issues
- npm.io page: https://npm.io/package/swc-mutable-cjs-exports

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2022-12-09
- 0.0.2 — 2022-12-08
- 0.0.1 — 2022-12-08

## README

# [SWC plugin] workaround for jest

[![Crates.io](https://img.shields.io/crates/v/jest_workaround)](https://crates.io/crates/jest_workaround)
[![npm](https://img.shields.io/npm/v/jest_workaround)](https://www.npmjs.com/package/jest_workaround)

[![Test](https://github.com/magic-akari/jest_workaround/actions/workflows/test.yml/badge.svg)](https://github.com/magic-akari/jest_workaround/actions/workflows/test.yml)
[![with @swc/core@latest](https://github.com/magic-akari/jest_workaround/actions/workflows/cron.yml/badge.svg)](https://github.com/magic-akari/jest_workaround/actions/workflows/cron.yml)

This is a SWC plugin to handle jest compatibility issues.

This SWC plugin should be used with `@swc/jest`.

## usage

install

```bash
npm i -D jest @swc/core @swc/jest jest_workaround
```

```js
// 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 = [["jest_workaround", {}]];

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

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?

The swc-transformed CJS is compliant with the ESM specification. This means that exports is immutable.

I need to use swc to get transformed code which conforms to the ESM specification.
But I need to use jest in a CJS environment to test it.

The immutable exports is difficult to use for jest testing.
This plugin will transform the export statement into mutable exports.

2. Do I have a better choice?

Yes.

If I can run jest in an ESM environment, then I don't even need swc, or just use swc to transform TypeScript syntax.

There may be some issues with running jest in ESM, but they will be resolved over time.
Tracked by [facebook/jest#9430](https://github.com/facebook/jest/issues/9430).

Or, I don't need the behavior of ESM.
I can get the CJS behavior of exports by using the CJS syntax.

CJS specific syntax

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

CTS(CJS in TypeScript) syntax

```TypeScript
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

---
_Source: https://npm.io/package/swc-mutable-cjs-exports · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
