JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2573
  • Score
    100M100P100Q120000F
  • License MIT

Build ES module to CommonJs module for Node.js/Electron

Package Exports

  • vite-plugin-esmodule
  • vite-plugin-esmodule/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vite-plugin-esmodule) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

vite-plugin-esmodule

Build ES module to CommonJs module for Node.js/Electron

NPM version NPM Downloads

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

npm i vite-plugin-esmodule -D

Usage

vite.config.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

import {execa} from 'execa';

const {stdout} = await execa('echo', ['unicorns']);
console.log(stdout);
//=> 'unicorns'

👉 See test

API

export interface Esmodule {
  (
    /**
     * If modules are not passed in, ESM packages will be automatically obtained from package.json in process.cwd path
     */
    modules?: (string | { [module: string]: string })[],
    webpack?: ((config: Configuration) => Configuration | void | Promise<Configuration | void>),
  ): Plugin;
}

How to work

This plugin just wraps vite-plugin-optimizer