Skip to content

flex-development/toggle-pkg-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

toggle-pkg-type

github release npm codecov module type: esm license conventional commits typescript vitest yarn

Toggle type fields in package.json files

Contents

What is this?

This package lets you toggle type fields in package.json files.

When should I use this?

toggle-pkg-type was created as a workaround for evanw/esbuild#2026.

Use this package when you use default exports and also ship code in ES module and CommonJS format.

The original issue was closed as "working as intended", but the solution provided is not suitable for all users:

Setting "type": "module" in your package.json means "I need to use node's behavior" for both Webpack and esbuild. If you don't need node's behavior, then just remove "type": "module" from your package.json.

This logic does not account for needing "type": "module" in development, but not when building CommonJS modules (i.e running tests or using a custom loader).

Install

This package is ESM only.

yarn add -D @flex-development/toggle-pkg-type

From Git:

yarn add -D @flex-development/toggle-pkg-type@flex-development/toggle-pkg-type
See Git - Protocols | Yarn  for details on requesting a specific branch, commit, or tag.

Use

  Usage
    $ toggle-pkg-type [off|on] [options]

  Options
    -i, --id         Module id of package directory or manifest  (default process.env.npm_package_json)
    -v, --version    Displays current version
    -h, --help       Displays this message

  Examples
    $ toggle-pkg-type
    $ toggle-pkg-type --id /path/to/manifest
    $ toggle-pkg-type off
    $ toggle-pkg-type on

API

This package exports no identifiers. The default export is toggle.

toggle([command][, id])

Enable or disable the type field in a package.json file.

The field is disabled by changing the field name to #type. The field value will not be modified.

Command Manifest:

  • NIL (null, undefined): Disable type if enabled, re-enable otherwise
  • off: Disable type field
  • on: Re-enable type field

This is a no-op under any of the following conditions:

  • A package.json file is not found
  • The type field is not defined in the located manifest
  • The #type field is not defined in the located manifest (i.e. the type field was not previously disabled)

Parameters

  • {Nilable<Command>?} [command=null] — Toggle command
  • {mlly.ModuleId?} [id='.'] — Module id of package directory or manifest

Returns

{void} Nothing when complete.

Source

src/toggle.ts

Types

This package is fully typed with TypeScript.

Type Definitions

Contribute

See CONTRIBUTING.md.