Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/caoxiemeihao/lib-esm
Lib to ESM code snippet.
https://github.com/caoxiemeihao/lib-esm
Last synced: about 1 month ago
JSON representation
Lib to ESM code snippet.
- Host: GitHub
- URL: https://github.com/caoxiemeihao/lib-esm
- Owner: caoxiemeihao
- License: mit
- Created: 2022-10-14T05:25:25.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-06T14:11:19.000Z (10 months ago)
- Last Synced: 2024-09-19T18:18:10.668Z (about 2 months ago)
- Language: JavaScript
- Size: 30.3 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lib-esm
A tiny `esm` snippets interop lib.
[![NPM version](https://img.shields.io/npm/v/lib-esm.svg)](https://npmjs.org/package/lib-esm)
[![NPM Downloads](https://img.shields.io/npm/dm/lib-esm.svg)](https://npmjs.org/package/lib-esm)## Install
```bash
npm i lib-esm
```## Usage
```js
import libEsm from 'lib-esm'
// or
// const libEsm = require('lib-esm').defaultconst snippets = libEsm({
window: 'lib-name',
require: 'lib-name',
exports: [
'foo',
'bar',
],
})console.log(`${snippets.window}\n${snippets.exports}`)
console.log(`${snippets.require}\n${snippets.exports}`)
```**snippets.window**
```js
const _M_ = window["lib-name"];
```**snippets.require**
```js
import _M_node_module from "node:module";
const _M_ = _M_node_module.createRequire(import.meta.url)("lib-name");
```**snippets.exports**
```js
export const foo = _M_.foo;
export const bar = _M_.bar;
const keyword_default = _M_.default || _M_;
export {
keyword_default as default,
};
```## API (Define)
```ts
/** Lib to ESM code snippets */
function libEsm(options: {
/**
* IIFE name
*/
window?: string;
/**
* require id
*/
require?: string;
/**
* export members
*/
exports?: string[];
/**
* Prevent name conflicts
*/
conflict?: string;
}): {
/** `window[iife-name]` snippets */
window: string;
/** `require(id)` snippets */
require: string;
/** `export` snippets */
exports: string;
/** Keywords alias */
keywords: Record;
}
```