Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-optimal-modules

jaydenseric430MIT3.0.0

An ESLint plugin to enforce optimal JavaScript module design.

eslint, plugin, eslintplugin, optimal, module, design, esm, mjs

readme

eslint-plugin-optimal-modules

An ESLint plugin to enforce optimal JavaScript module design.

Installation

To install eslint-plugin-optimal-modules with npm, run:

npm install eslint-plugin-optimal-modules --save-dev

To use the recommended config, add the following ESLint “flat” config in eslint.config.mjs:

// @ts-check

import eslintPluginOptimalModules from "eslint-plugin-optimal-modules";

/**
 * ESLint config.
 * @satisfies {Array<import("eslint").Linter.Config>}
 */
const eslintConfig = [eslintPluginOptimalModules.configs.recommended];

export default eslintConfig;

Alternatively, manually configure the plugin and the desired rules:

// @ts-check

import eslintPluginOptimalModules from "eslint-plugin-optimal-modules";

/**
 * ESLint config.
 * @satisfies {Array<import("eslint").Linter.Config>}
 */
const eslintConfig = [
  {
    plugins: {
      "optimal-modules": eslintPluginOptimalModules,
    },
    rules: {
      "optimal-modules/no-named-exports": "error",
    },
  },
];

export default eslintConfig;

To allow named exports in Storybook story modules that have a Component Story Format (CSF):

// @ts-check

import eslintPluginOptimalModules from "eslint-plugin-optimal-modules";

/**
 * ESLint config.
 * @satisfies {Array<import("eslint").Linter.Config>}
 */
const eslintConfig = [
  eslintPluginOptimalModules.configs.recommended,
  {
    files: ["**/*.stories.{mjs,cjs,js,mts,cts,ts,tsx}"],
    rules: {
      "optimal-modules/no-named-exports": "off",
    },
  },
];

export default eslintConfig;

Rules

Rule no-named-exports

Prohibits using named exports for optimal module design.

Valid examples:

// No exports.
const a = true;
// Default export.
export default true;
// Default export.
const a = true;
export { a as default };
// TypeScript type default export.
type A = boolean;
export type { A as default };

Invalid examples:

// Named export.
export const a = true;
// Named export.
const a = true;
export { a };
// TypeScript type named export.
export type A = boolean;

To fix the above errors, move the thing being exported to its own module as a default export.

Configs

Enabled rules:

Requirements

Supported runtime environments:

  • Node.js versions ^18.18.0 || ^20.9.0 || >=21.1.0.

Projects must configure TypeScript to use types from the CommonJS modules that have a // @ts-check comment:

Exports

These CommonJS modules are exported via the package.json field exports:

changelog

eslint-plugin-optimal-modules changelog

3.0.0

Major

  • Updated the peer dependency eslint to ^9.11.1.
  • Removed the dependency @types/eslint to rely on the types provided in eslint v9.10+.

Patch

  • Updated dependencies.
  • Added the dependency @types/estree so it’s no longer transitive.
  • Migrate deprecated types in the project and example ESLint configs.

2.0.0

Major

  • Updated Node.js support to ^18.18.0 || ^20.9.0 || >=21.1.0.
  • Updated the peer dependency eslint to ^9.0.0.
  • Updated dev dependencies, some of which require newer Node.js versions than previously supported.
  • The plugin, and its config recommended, is now ESLint v9 format.

Patch

  • Updated dependencies.
  • Updated GitHub Actions CI config:
    • No longer run the workflow on pull request.
    • Enable manual workflow dispatching.
    • Run tests with Node.js v18, v20, v21, v22.
    • Updated actions/checkout to v4.
    • Updated actions/setup-node to v4.
  • Updated the package.json field repository to conform to new npm requirements.
  • Updated the package script eslint, ESLint config, and tests for ESLint v9.
  • Integrated new dev dependency eslint-plugin-jsdoc.
  • Updated the readme instructions for ESLint “flat” config using eslint.config.mjs.

1.0.2

Patch

  • Fixed the readme intro.
  • Added to the readme installation instructions how to configure ESLint to allow named exports in Storybook story modules that have a Component Story Format (CSF).

1.0.1

Patch

  • Fixed broken rule URLs.

1.0.0

Initial release.