Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dcodeio/esm2umd
Transforms ESM to UMD, i.e. to use ESM by default with UMD as a legacy fallback.
https://github.com/dcodeio/esm2umd
esmodules javascript
Last synced: 2 months ago
JSON representation
Transforms ESM to UMD, i.e. to use ESM by default with UMD as a legacy fallback.
- Host: GitHub
- URL: https://github.com/dcodeio/esm2umd
- Owner: dcodeIO
- Created: 2020-06-04T00:27:12.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-07T12:45:26.000Z (almost 2 years ago)
- Last Synced: 2024-09-14T13:27:26.649Z (4 months ago)
- Topics: esmodules, javascript
- Language: JavaScript
- Homepage:
- Size: 33.2 KB
- Stars: 4
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
esm2umd
=======Transforms ESM to UMD, i.e. to use ESM by default with UMD as a legacy fallback.
[![Build Status](https://img.shields.io/github/workflow/status/dcodeIO/esm2umd/Test/main?label=test&logo=github)](https://github.com/dcodeIO/esm2umd/actions?query=workflow%3ATest) [![Publish Status](https://img.shields.io/github/workflow/status/dcodeIO/esm2umd/Publish/main?label=publish&logo=github)](https://github.com/dcodeIO/esm2umd/actions?query=workflow%3APublish) [![npm](https://img.shields.io/npm/v/esm2umd.svg?label=npm&color=007acc&logo=npm)](https://www.npmjs.com/package/esm2umd)
Usage
-----```
npx esm2umd MyModule esmFile.js > umdFile.js
````MyModule` is used as the name of the vanilla JS global.
If the module has a `default` export, it becomes the value obtained when `require`d.
API
---Installation as a dependency is optional (pulls in megabytes of babel), but if so desired exposes the CLI as an API:
```js
import esm2umd from 'esm2umd'const esmCode = '...'
const umdCode = esm2umd('ModuleName', esmCode)
```Example
-------ESM-first hybrid module with legacy fallback and prepublish build step.
**package.json**
```json
{
"type": "module",
"main": "./umd/index.js",
"types": "index.d.ts",
"exports": {
"import": "./index.js",
"require": "./umd/index.js"
},
"scripts": {
"build": "npx esm2umd MyModule index.js > umd/index.js",
"prepublishOnly": "npm run build"
}
}
```Treat .js files in `umd/` as CommonJS.
**umd/package.json**
```json
{
"type": "commonjs"
}
```Keep the generated artifact out of version control to avoid PRs against it.
**.gitignore**
```
umd/index.js
```For typings, if there is a `default` export, stick to the "old" format for compatibility.
**index.d.ts**
```ts
export = MyModule;
```