{"id":16590911,"url":"https://github.com/magic-akari/swc_mut_cjs_exports","last_synced_at":"2025-04-09T12:08:21.441Z","repository":{"id":46143498,"uuid":"513986498","full_name":"magic-akari/swc_mut_cjs_exports","owner":"magic-akari","description":"[SWC plugin] mutable CJS exports","archived":false,"fork":false,"pushed_at":"2024-05-01T01:50:05.000Z","size":488,"stargazers_count":52,"open_issues_count":10,"forks_count":13,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-02T05:14:28.519Z","etag":null,"topics":["cjs","commonjs","jest","swc","swc-plugin"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/swc_mut_cjs_exports","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magic-akari.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-14T17:13:51.000Z","updated_at":"2024-05-05T10:27:00.429Z","dependencies_parsed_at":"2023-10-30T01:28:28.163Z","dependency_job_id":"a32f085c-6ee3-4dd7-9346-2323e52ccd2e","html_url":"https://github.com/magic-akari/swc_mut_cjs_exports","commit_stats":null,"previous_names":["magic-akari/swc_mut_cjs_exports","magic-akari/jest_workaround"],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic-akari%2Fswc_mut_cjs_exports","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic-akari%2Fswc_mut_cjs_exports/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic-akari%2Fswc_mut_cjs_exports/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic-akari%2Fswc_mut_cjs_exports/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magic-akari","download_url":"https://codeload.github.com/magic-akari/swc_mut_cjs_exports/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036067,"owners_count":21037092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cjs","commonjs","jest","swc","swc-plugin"],"created_at":"2024-10-11T23:14:42.963Z","updated_at":"2025-04-09T12:08:21.423Z","avatar_url":"https://github.com/magic-akari.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [SWC plugin] mutable CJS exports\n\n[![Crates.io](https://img.shields.io/crates/v/swc_mut_cjs_exports)](https://crates.io/crates/swc_mut_cjs_exports)\n[![npm](https://img.shields.io/npm/v/swc_mut_cjs_exports)](https://www.npmjs.com/package/swc_mut_cjs_exports)\n\n[![Test](https://github.com/magic-akari/swc_mut_cjs_exports/actions/workflows/test.yml/badge.svg)](https://github.com/magic-akari/swc_mut_cjs_exports/actions/workflows/test.yml)\n[![SWC Compat Test](https://github.com/magic-akari/swc_mut_cjs_exports/actions/workflows/swc-compat-test.yml/badge.svg)](https://github.com/magic-akari/swc_mut_cjs_exports/actions/workflows/swc-compat-test.yml)\n[![with @swc/core@latest](https://github.com/magic-akari/swc_mut_cjs_exports/actions/workflows/cron-latest.yml/badge.svg)](https://github.com/magic-akari/swc_mut_cjs_exports/actions/workflows/cron-latest.yml)\n[![with @swc/core@nightly](https://github.com/magic-akari/swc_mut_cjs_exports/actions/workflows/cron-nightly.yml/badge.svg)](https://github.com/magic-akari/swc_mut_cjs_exports/actions/workflows/cron-nightly.yml)\n\nThis is a SWC plugin to emit mutable CJS exports.\n\nThis SWC plugin has only been tested for compatibility with jest. It should be used with `@swc/jest`.\n\nThis project was previously called jest_workaround\n\n## plugin version\n\nhttps://swc.rs/docs/plugin/selecting-swc-core\n\n## usage\n\ninstall\n\n```bash\nnpm i -D jest @swc/core @swc/jest swc_mut_cjs_exports\n```\n\n```js\n// jest.config.js\nconst fs = require(\"node:fs\");\n\nconst swcrc = JSON.parse(fs.readFileSync(\".swcrc\", \"utf8\"));\n\n// If you have other plugins, change this line.\n((swcrc.jsc ??= {}).experimental ??= {}).plugins = [\n  [\"swc_mut_cjs_exports\", {}],\n];\n\nmodule.exports = {\n  transform: {\n    \"^.+\\\\.(t|j)sx?$\": [\"@swc/jest\", swcrc],\n  },\n};\n```\n\nAlternative implementation without .swcrc file\n\n```JavaScript\n// jest.config.js\n\nmodule.exports = {\n  transform: {\n    \"^.+\\\\.(t|j)sx?$\": [\n      \"@swc/jest\",\n      {\n        jsc: {\n          experimental: {\n            plugins: [[\"swc_mut_cjs_exports\", {}]],\n          },\n        },\n      },\n    ],\n  },\n};\n```\n\nMake sure that `module.type` is `commonjs` in your `.swcrc` since this plugin\ndoes not touch non-workaround parts, such as import statements.\n\n## FAQ\n\n#### 1. When do I need this?\n\nIf you're using the swc compiler to transform your code to comply with the ESM\nspecification, but you're also using Jest to test it in a CJS environment, you\nmay encounter issues due to the immutable issue of `exports`.\n\nThis plugin can help by transforming the `export` statements into mutable\n`exports`.\n\n#### 2. Do I have a better choice?\n\nYou may have other options depending on your specific needs:\n\n- If you're able to run Jest in an ESM environment, you can use swc to transpile\n  TypeScript/JSX syntax or downgrade JavaScript syntax without module\n  conversion. Simply set the value of `module.type` to `es6` to achieve this.\n\n- It's possible that some issues related to running Jest in an ESM environment\n  will be resolved over time. Keep an eye on\n  [facebook/jest#9430](https://github.com/facebook/jest/issues/9430) for\n  updates.\n\n- If you don't need the behavior of ESM specifically, you can stick with the CJS\n  syntax to get the CJS behavior of `exports`.\n\nThese options may be worth considering before using this plugin.\n\nCJS syntax\n\n```JavaScript\nexports.foo = function foo() {\n  return 42;\n};\n```\n\nCTS(CJS in TypeScript) syntax\n\n```TypeScript\nexport = {\n  foo: function foo() {\n    return 42;\n  },\n};\n```\n\nNotes:\n\n- ESM style export means immutable exports when transformed into CJS\n- ESM style import means hoisted require when transformed into CJS\n\n#### 3. After upgrading the plugin version, the changes have not taken effect.\n\nThis is a known issue. You could remove the Jest cache by running\n[`jest --clearCache`](https://jestjs.io/docs/cli#--clearcache) as a workaround.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagic-akari%2Fswc_mut_cjs_exports","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagic-akari%2Fswc_mut_cjs_exports","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagic-akari%2Fswc_mut_cjs_exports/lists"}