# vite-plugin-cjs2esm

> 🤩 A vite plugin that transform CommonJS to ESModule in Development stage.

Latest version **1.0.10** (published 2023-08-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install vite-plugin-cjs2esm
pnpm add vite-plugin-cjs2esm
yarn add vite-plugin-cjs2esm
bun add vite-plugin-cjs2esm
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2023-08-05 |
| First published | 2023-05-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^14.18.0 \|\| >=16.0.0 |
| Dependencies | 3 |
| Unpacked size | 22 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | haijie-x |
| Maintainers | xiehaijie |
| Keywords | vite, commonjs, plugin |

## Links

- npm: https://www.npmjs.com/package/vite-plugin-cjs2esm
- Repository: https://github.com/haijie-x/vite-plugin-cjs2esm
- Issues: https://github.com/haijie-x/vite-plugin-cjs2esm/issues
- npm.io page: https://npm.io/package/vite-plugin-cjs2esm

## Dependencies (3)

- [acorn](https://npm.io/package/acorn.md) ^8.8.2
- [acorn-walk](https://npm.io/package/acorn-walk.md) ^8.2.0
- [magic-string](https://npm.io/package/magic-string.md) ^0.30.0

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 1.0.10 (latest) — 2023-08-05
- 1.0.9 — 2023-06-13
- 1.0.8 — 2023-06-09
- 1.0.7 — 2023-06-09
- 1.0.6 — 2023-06-06
- 1.0.5 — 2023-06-06
- 1.0.4 — 2023-06-01
- 1.0.3 — 2023-06-01
- 1.0.2 — 2023-06-01
- 1.0.1 — 2023-05-28
- 1.0.0 — 2023-05-28
- 0.0.5 — 2023-05-27
- 0.0.4 — 2023-05-27
- 0.0.3 — 2023-05-26
- 0.0.2 — 2023-05-26
- … 1 more at https://npm.io/package/vite-plugin-cjs2esm/versions

## README

# vite-plugin-cjs2esm

🤩 A vite plugin that transform CommonJS to ESModule in Development stage.

# Installation

```bash
yarn add vite-plugin-cjs2esm --save-dev
# or
npm install vite-plugin-cjs2esm --save-dev
# or
pnpm install vite-plugin-cjs2esm --save-dev
```

# Usage

In your vite.config.ts:

```js
import { defineConfig } from 'vite'
import viteCjsToEsmPlugin from 'vite-plugin-cjs2esm'

export default defineConfig({
  plugins: [viteCjsToEsmPlugin.vitePlugin()],
  optimizeDeps: {
    esbuildOptions: {
      plugins: [viteCjsToEsmPlugin.esbuildPlugin()]
    }
  }
})
```

# Options

### filter

Type: `RegExp`<br> Default: `/.*/`<br>

Files to include in this plugin (default all).

# Features

1. `static require`

```js
// input
const foo = require('./foo')
// ↓ ouput
import * as __IMPORTER_0__ from './foo'
const foo = __IMPORTER_0__.default || __IMPORTER_0__
```

2. `exports`

```js
// input
exports.foo = foo
// ↓ ouput
const __EXPORTER_0__ = foo
export { __EXPORTER_0__ as foo }
```

```js
// input
module.exports = foo
// ↓ ouput
const __EXPORTER_0__ = foo
export { __EXPORTER_0__ as default }
```

3.`dynamic require`

```js
// input
function bar(file) {
  const foo = require(`./foo/${file}`)
  console.log(foo)
}
bar('bar')
// ↓ output
function bar(file) {
  const foo = import(`./foo/${file}`).then(foo => {
    console.log(foo)
  })
}
bar('foo')
```

---
_Source: https://npm.io/package/vite-plugin-cjs2esm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
