0.1.3 • Published 2 years ago
lib-esm-snippet v0.1.3
lib-esm-snippet
Lib to ESM code snippet.
Install
npm i lib-esm-snippet
Usage
import libEsmSnippet from 'lib-esm-snippet'
// or
// const libEsmSnippet = require('lib-esm-snippet')
const result = libEsmSnippet({
lib: 'lib-name',
members: [
'foo',
'bar',
],
});
console.log(result.snippet);
Output
CommonJs format
import { createRequire } from "node:module";
const cjs_require = createRequire(import.meta.url);
const _M_ = cjs_require("lib-name");
export const foo = _M_.foo;
export const bar = _M_.bar;
export const keyword_default = _M_.default || _M_;
export {
keyword_default as default,
};
IIFE format
const _M_ = window["lib-name"];
export const foo = _M_.foo;
export const bar = _M_.bar;
export const keyword_default = _M_.default || _M_;
export {
keyword_default as default,
};
API (Define)
export default function libEsmSnippet(options: {
/**
* lib name
*/
lib: string;
/**
* export members
*/
members?: string[];
/**
* Prevent naming conflicts
*/
conflictId?: string;
/**
* Generate code snippet format
*
* 🌰 e.g.
* ```js
* const _M_ = require("lib") // cjs
* const _M_ = window["lib"] // iife
* ```
*
* @default "cjs"
*/
format?: "cjs" | "iife";
}): {
snippet: string;
/** Only `export` snippets. */
exports: string;
/** Keywords alias */
keywords: Record<string, string>;
}