Skip to content

Latest commit

 

History

History
 
 

esmodule

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

vite-plugin-esmodule

NPM version NPM Downloads

Build ES module to CommonJs module for Node.js

English | 简体中文

Why

🤔 When ES module such as execa, node-fetch, file-type used in the Node.js project, we should compile them into CommonJs modules to ensure that they can work normally

👉 You can think that this plugin is to solve some NPM Packges released by sindresorhus 😅

🚧 The plugin only work in the vite build phase

Usage

Take execa, node-fetch and file-type as examples

  • vite.config.js
import esmodule from 'vite-plugin-esmodule'

export default {
  plugins: [
    esmodule([
      'execa',
      'node-fetch',

      // `file-type` have exports condition in package.json
      // this means that you have explicit specified the entry file
      // 🌱 the purpose of this design is to eliminate the difference between Vite and Webpack
      { 'file-type': 'file-type/index.js' },
    ]),
  ],
}

By default, the plugin use Webpack as build tools. You can specify Vite by options.vite

esmodule([...some-es-module], {
  vite: true,
  // or
  vite: (config) => config,
})
  • execa.js
import {execa} from 'execa';

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

See the test cases

API

esmodule(modules[,options])

modules: ES module name list

modules: (string | { [module: string]: string })[]

options:

options?: WebpackOptions | ViteOptions

export interface WebpackOptions {
  webpack?: true
    | ((config: Configuration) => Configuration | void | Promise<Configuration | void>);
  vite?: never;
}

export interface ViteOptions {
  vite?: true
    | ((config: UserConfig) => UserConfig | void | Promise<UserConfig | void>);
  webpack?: never;
}

How to work

This plugin just wraps vite-plugin-optimizer