Comparing version 0.1.0 to 0.2.0
@@ -1,33 +0,38 @@ | ||
/** Lib to ESM code snippet. */ | ||
export default function libEsm(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>; | ||
declare module 'lib-esm' { | ||
/** Lib to ESM code snippet. */ | ||
function libEsm(options: { | ||
/** | ||
* require id | ||
*/ | ||
require?: string; | ||
/** | ||
* export members | ||
*/ | ||
exports?: string[]; | ||
/** | ||
* Prevent name 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"; | ||
}): { | ||
/** `require` snippets. */ | ||
require: string; | ||
/** `export` snippets. */ | ||
exports: string; | ||
/** Keywords alias */ | ||
keywords: Record<string, string>; | ||
} | ||
export = libEsm; | ||
} |
22
index.js
@@ -69,7 +69,7 @@ // https://www.w3schools.com/js/js_reserved.asp | ||
/** @type {import('.')['default']} */ | ||
/** @type {import('lib-esm')} */ | ||
module.exports = function libEsm(options) { | ||
const { | ||
lib, | ||
members = [], | ||
require: require2, | ||
exports: members = [], | ||
conflictId = '', | ||
@@ -79,11 +79,13 @@ format = 'cjs', | ||
const _M_ = '_M_' + conflictId; | ||
const importStatement = format === 'cjs' | ||
? ` | ||
const requireSnippet = typeof require2 === 'undefined' ? '' : ( | ||
format === 'cjs' | ||
? ` | ||
import { createRequire } from "node:module"; | ||
const cjs_require = createRequire(import.meta.url); | ||
const ${_M_} = cjs_require("${lib}"); | ||
const ${_M_} = cjs_require("${require2}"); | ||
`.trim() | ||
: ` | ||
const ${_M_} = window["${lib}"]; | ||
`.trim(); | ||
: ` | ||
const ${_M_} = window["${require2}"]; | ||
`.trim() | ||
); | ||
@@ -106,3 +108,3 @@ !members.includes('default') && members.push('default'); | ||
return { | ||
snippet: `${importStatement}\n${exportsSnippet}`, | ||
require: requireSnippet, | ||
exports: exportsSnippet, | ||
@@ -109,0 +111,0 @@ keywords: alias, |
{ | ||
"name": "lib-esm", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Lib to ESM code snippet.", | ||
@@ -16,3 +16,3 @@ "main": "index.js", | ||
"scripts": { | ||
"build": "node build.mjs", | ||
"build": "node build.js", | ||
"test": "node test/index.mjs", | ||
@@ -19,0 +19,0 @@ "prepublishOnly": "npm run build" |
@@ -63,13 +63,14 @@ # lib-esm | ||
```ts | ||
export default function libEsm(options: { | ||
/** Lib to ESM code snippet. */ | ||
function libEsm(options: { | ||
/** | ||
* lib name | ||
* require id | ||
*/ | ||
lib: string; | ||
require?: string; | ||
/** | ||
* export members | ||
*/ | ||
members?: string[]; | ||
exports?: string[]; | ||
/** | ||
* Prevent naming conflicts | ||
* Prevent name conflicts | ||
*/ | ||
@@ -90,4 +91,5 @@ conflictId?: string; | ||
}): { | ||
snippet: string; | ||
/** Only `export` snippets. */ | ||
/** `require` snippets. */ | ||
require: string; | ||
/** `export` snippets. */ | ||
exports: string; | ||
@@ -94,0 +96,0 @@ /** Keywords alias */ |
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
6288
149
98