Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vite-plugin/vite-plugin-esmodule
Build ES module to CommonJs module for Node.js/Electron
https://github.com/vite-plugin/vite-plugin-esmodule
Last synced: 1 day ago
JSON representation
Build ES module to CommonJs module for Node.js/Electron
- Host: GitHub
- URL: https://github.com/vite-plugin/vite-plugin-esmodule
- Owner: vite-plugin
- License: mit
- Created: 2022-06-14T23:58:38.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-09T09:24:45.000Z (5 months ago)
- Last Synced: 2024-10-30T06:58:26.650Z (11 days ago)
- Language: JavaScript
- Homepage:
- Size: 146 KB
- Stars: 24
- Watchers: 0
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# vite-plugin-esmodule
Build ES module to CommonJs module for Node.js/Electron
[![NPM version](https://img.shields.io/npm/v/vite-plugin-esmodule.svg)](https://npmjs.org/package/vite-plugin-esmodule)
[![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-esmodule.svg)](https://npmjs.org/package/vite-plugin-esmodule)## Why 🤔
When using ES modules(e.g.`node-fetch`) in Node.js/Electron projects, we may need to compile them into CommonJs modules to ensure they work correctly.
*在 Node.js/Electron 项目中使用 ES 模块时(e.g. `node-fetch`),我们可能需要将其编译成 CommonJs 模块,以确保它们能够正常工作*
## Install
```sh
npm i -D vite-plugin-esmodule webpack
```## Usage
vite.config.js
```js
import esmodule from 'vite-plugin-esmodule'export default {
plugins: [
// Take `execa`, `node-fetch` and `file-type` as examples
esmodule([
'execa',
'node-fetch',// 🌱 this means that you have explicit specified the entry file
{ 'file-type': 'file-type/index.js' },
]),
],
}
```execa.js
```js
import {execa} from 'execa';const {stdout} = await execa('echo', ['unicorns']);
console.log(stdout);
//=> 'unicorns'
```## API (Define)
`esmodule(modules[, webpack])`
```ts
import type { Configuration } from 'webpack'type ModuleRecord = string | { [module: string]: string }
function esmodule(
modules: ModuleRecord[] | ((esmPkgs: string[]) => ModuleRecord[]),
webpack?: ((config: Configuration) => Configuration | void | Promise),
): import('vite').Plugin
```## How to work
This plugin just wraps [vite-plugin-optimizer](https://github.com/vite-plugin/vite-plugin-optimizer)