Comparing version 0.2.0 to 0.3.0
@@ -5,2 +5,6 @@ declare module 'lib-esm' { | ||
/** | ||
* IIFE name | ||
*/ | ||
window?: string; | ||
/** | ||
* require id | ||
@@ -16,17 +20,7 @@ */ | ||
*/ | ||
conflictId?: string; | ||
/** | ||
* Generate code snippet format | ||
* | ||
* 🌰 e.g. | ||
* ```js | ||
* const _M_ = require("lib") // cjs | ||
* const _M_ = window["lib"] // iife | ||
* ``` | ||
* | ||
* @default "cjs" | ||
*/ | ||
format?: "cjs" | "iife"; | ||
conflict?: string; | ||
}): { | ||
/** `require` snippets. */ | ||
/** `window[iife-name]` snippets. */ | ||
window: string; | ||
/** `require(id)` snippets. */ | ||
require: string; | ||
@@ -33,0 +27,0 @@ /** `export` snippets. */ |
26
index.js
@@ -72,19 +72,13 @@ // https://www.w3schools.com/js/js_reserved.asp | ||
const { | ||
window, | ||
require: require2, | ||
exports: members = [], | ||
conflictId = '', | ||
format = 'cjs', | ||
conflict = '', | ||
} = options; | ||
const _M_ = '_M_' + conflictId; | ||
const requireSnippet = typeof require2 === 'undefined' ? '' : ( | ||
format === 'cjs' | ||
? ` | ||
const _M_ = '_M_' + conflict; | ||
const windowSnippet = window == null ? '' : `const ${_M_} = window["${window}"];`; | ||
const requireSnippet = require2 == null ? '' : ` | ||
import { createRequire } from "node:module"; | ||
const cjs_require = createRequire(import.meta.url); | ||
const ${_M_} = cjs_require("${require2}"); | ||
`.trim() | ||
: ` | ||
const ${_M_} = window["${require2}"]; | ||
`.trim() | ||
); | ||
const ${_M_} = createRequire(import.meta.url)("${require2}"); | ||
`.trim(); | ||
@@ -95,7 +89,8 @@ !members.includes('default') && members.push('default'); | ||
.filter(member => keywords.includes(member)) | ||
.reduce((memo, keyword) => Object.assign(memo, { [keyword]: `keyword_${keyword + conflictId}` }), {}); | ||
.reduce((memo, keyword) => Object.assign(memo, { [keyword]: `keyword_${keyword + conflict}` }), {}); | ||
const exportsSnippet = ` | ||
${members.map(member => { | ||
const LV = alias[member] ? `const ${alias[member]}` : `export const ${member}`; | ||
const RV = member === 'default' ? `${_M_}.default || ${_M_}` : `${_M_}.${member}`; | ||
return `export const ${alias[member] || member} = ${RV};`; | ||
return `${LV} = ${RV};`; | ||
}).join('\n')} | ||
@@ -108,2 +103,3 @@ export { | ||
return { | ||
window: windowSnippet, | ||
require: requireSnippet, | ||
@@ -110,0 +106,0 @@ exports: exportsSnippet, |
{ | ||
"name": "lib-esm", | ||
"version": "0.2.0", | ||
"description": "Lib to ESM code snippet.", | ||
"version": "0.3.0", | ||
"description": "Lib to ESM snippet.", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "exports": { |
# lib-esm | ||
Lib to ESM code snippet. | ||
Lib to ESM snippet. | ||
@@ -22,35 +22,33 @@ [![NPM version](https://img.shields.io/npm/v/lib-esm.svg)](https://npmjs.org/package/lib-esm) | ||
const result = libEsm({ | ||
lib: 'lib-name', | ||
members: [ | ||
window: 'lib-name', | ||
require: 'lib-name', | ||
exports: [ | ||
'foo', | ||
'bar', | ||
], | ||
}); | ||
}) | ||
console.log(result.snippet); | ||
console.log(`${result.window}\n${result.exports}`) | ||
console.log(`${result.require}\n${result.exports}`) | ||
``` | ||
###### Output | ||
**result.window** | ||
CommonJs format | ||
```js | ||
const _M_ = window["lib-name"]; | ||
``` | ||
**result.require** | ||
```js | ||
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, | ||
}; | ||
const _M_ = createRequire(import.meta.url)("lib-name"); | ||
``` | ||
IIFE format | ||
**result.exports** | ||
```js | ||
const _M_ = window["lib-name"]; | ||
export const foo = _M_.foo; | ||
export const bar = _M_.bar; | ||
export const keyword_default = _M_.default || _M_; | ||
const keyword_default = _M_.default || _M_; | ||
export { | ||
@@ -67,2 +65,6 @@ keyword_default as default, | ||
/** | ||
* IIFE name | ||
*/ | ||
window?: string; | ||
/** | ||
* require id | ||
@@ -78,17 +80,7 @@ */ | ||
*/ | ||
conflictId?: string; | ||
/** | ||
* Generate code snippet format | ||
* | ||
* 🌰 e.g. | ||
* ```js | ||
* const _M_ = require("lib") // cjs | ||
* const _M_ = window["lib"] // iife | ||
* ``` | ||
* | ||
* @default "cjs" | ||
*/ | ||
format?: "cjs" | "iife"; | ||
conflict?: string; | ||
}): { | ||
/** `require` snippets. */ | ||
/** `window[iife-name]` snippets. */ | ||
window: string; | ||
/** `require(id)` snippets. */ | ||
require: string; | ||
@@ -95,0 +87,0 @@ /** `export` snippets. */ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5943
138
90