Skip to content

Latest commit

 

History

History
56 lines (39 loc) · 1.26 KB

esm-bundler.md

File metadata and controls

56 lines (39 loc) · 1.26 KB

Enforce ES Module bundler APIs instead of Node APIs (esm-bundler)

Webpack supports Node APIs for features such as module systems, compile-time constants, etc., but most other bundlers do not support these APIs. For example:

  • process.env in webpack, and import.meta.env in Vite
  • module.exports in webpack, and export in esbuild
  • require.context in webpack, and import.meta.glob in Vite

Instead, webpack can be configured or plugged to support more modern ES Module APIs.

This rule completes the following rules:

  • no-process-env: It is deprecated
  • import/no-commonjs: It cannot detect cases such as require.resolve

Options

This rule has an object option:

  • "allowProcessEnv" (default false) allows process.env
  • "allowCommonJs" (default false) allows module.exports or exports.*
  • "allowRequire" (default false) allows require

Fail

const version = process.env.VERSION
module.exports = foo
const { foo } = require('foo')
foo.source = require.resolve('bar')

Pass

const version = import.meta.env.VERSION
export default foo
import { foo } from 'foo'
/* eslint galaxy/esm-bundler: ["error", { "allowProcessEnv": true }] */
const version = process.env.VERSION