https://github.com/flex-development/toggle-pkg-type
Toggle type fields in package.json files
https://github.com/flex-development/toggle-pkg-type
cjs commonjs default-exports esbuild esm interop module package-json toggle type typescript
Last synced: 2 months ago
JSON representation
Toggle type fields in package.json files
- Host: GitHub
- URL: https://github.com/flex-development/toggle-pkg-type
- Owner: flex-development
- License: bsd-3-clause
- Created: 2022-10-01T22:37:47.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-13T18:14:29.000Z (about 1 year ago)
- Last Synced: 2024-09-17T02:33:44.321Z (9 months ago)
- Topics: cjs, commonjs, default-exports, esbuild, esm, interop, module, package-json, toggle, type, typescript
- Language: JavaScript
- Homepage: https://github.com/flex-development/toggle-pkg-type
- Size: 2.53 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/funding.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# toggle-pkg-type
[](https://github.com/flex-development/toggle-pkg-type/releases/latest)
[](https://npmjs.com/package/@flex-development/toggle-pkg-type)
[](https://codecov.io/gh/flex-development/toggle-pkg-type)
[](https://github.com/voxpelli/badges-cjs-esm)
[](LICENSE.md)
[](https://conventionalcommits.org/)
[](https://typescriptlang.org/)
[](https://vitest.dev/)
[](https://yarnpkg.com/)Toggle [`type`][1] fields in `package.json` files
## Contents
- [What is this?](#what-is-this)
- [When should I use this?](#when-should-i-use-this)
- [Install](#install)
- [Use](#use)
- [API](#api)
- [`toggle([command][, id])`](#togglecommand-id)
- [Types](#types)
- [Type Definitions](#type-definitions)
- [Contribute](#contribute)## What is this?
This package lets you toggle [`type`][1] fields in `package.json` files.
## When should I use this?
`toggle-pkg-type` was created as a workaround for [`evanw/esbuild#2026`][2].
Use this package when you use `default` exports and also [ship code in ES module and CommonJS format][3].
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][4]).## Install
This package is [ESM only][5].
```sh
yarn add -D @flex-development/toggle-pkg-type
```From Git:
```sh
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
```sh
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 messageExamples
$ 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`][1] 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` fieldThis 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=null]`** — Toggle command
- `{mlly.ModuleId?}` **`[id='.']`** — Module id of package directory or manifest#### Returns
`{void}` Nothing when complete.
#### Source
> [`src/toggle.ts`](src/toggle.ts)
## Types
This package is fully typed with [TypeScript][6].
### Type Definitions
- [`Command`](src/types/command.ts)
## Contribute
See [`CONTRIBUTING.md`](CONTRIBUTING.md).
[1]: https://nodejs.org/api/packages.html#type
[2]: https://github.com/evanw/esbuild/issues/2026
[3]: https://github.com/flex-development/aggregate-error-ponyfill/blob/main/package.json#L33-L42
[4]: https://nodejs.org/api/esm.html#loaders
[5]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[6]: https://www.typescriptlang.org