Comparing version 3.0.0 to 3.1.0
@@ -1,2 +0,2 @@ | ||
export declare type LilconfigResult = null | { | ||
export type LilconfigResult = null | { | ||
filepath: string; | ||
@@ -13,9 +13,9 @@ config: any; | ||
} | ||
export declare type Transform = TransformSync | ((result: LilconfigResult) => Promise<LilconfigResult>); | ||
export declare type TransformSync = (result: LilconfigResult) => LilconfigResult; | ||
declare type LoaderResult = any; | ||
export declare type LoaderSync = (filepath: string, content: string) => LoaderResult; | ||
export declare type Loader = LoaderSync | ((filepath: string, content: string) => Promise<LoaderResult>); | ||
export declare type Loaders = Record<string, Loader>; | ||
export declare type LoadersSync = Record<string, LoaderSync>; | ||
export type Transform = TransformSync | ((result: LilconfigResult) => Promise<LilconfigResult>); | ||
export type TransformSync = (result: LilconfigResult) => LilconfigResult; | ||
type LoaderResult = any; | ||
export type LoaderSync = (filepath: string, content: string) => LoaderResult; | ||
export type Loader = LoaderSync | ((filepath: string, content: string) => Promise<LoaderResult>); | ||
export type Loaders = Record<string, Loader>; | ||
export type LoadersSync = Record<string, LoaderSync>; | ||
export interface Options extends OptionsBase { | ||
@@ -29,4 +29,5 @@ loaders?: Loaders; | ||
} | ||
export declare const defaultLoaders: LoadersSync; | ||
declare type ClearCaches = { | ||
export declare const defaultLoadersSync: LoadersSync; | ||
export declare const defaultLoaders: Loaders; | ||
type ClearCaches = { | ||
clearLoadCache: () => void; | ||
@@ -36,3 +37,3 @@ clearSearchCache: () => void; | ||
}; | ||
declare type AsyncSearcher = { | ||
type AsyncSearcher = { | ||
search(searchFrom?: string): Promise<LilconfigResult>; | ||
@@ -42,3 +43,3 @@ load(filepath: string): Promise<LilconfigResult>; | ||
export declare function lilconfig(name: string, options?: Partial<Options>): AsyncSearcher; | ||
declare type SyncSearcher = { | ||
type SyncSearcher = { | ||
search(searchFrom?: string): LilconfigResult; | ||
@@ -45,0 +46,0 @@ load(filepath: string): LilconfigResult; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.lilconfigSync = exports.lilconfig = exports.defaultLoaders = void 0; | ||
exports.lilconfigSync = exports.lilconfig = exports.defaultLoaders = exports.defaultLoadersSync = void 0; | ||
const path = require("path"); | ||
@@ -8,3 +8,3 @@ const fs = require("fs"); | ||
const fsReadFileAsync = fs.promises.readFile; | ||
function getDefaultSearchPlaces(name) { | ||
function getDefaultSearchPlaces(name, sync) { | ||
return [ | ||
@@ -15,2 +15,3 @@ 'package.json', | ||
`.${name}rc.cjs`, | ||
...(sync ? [] : [`.${name}rc.mjs`]), | ||
`.config/${name}rc`, | ||
@@ -20,4 +21,6 @@ `.config/${name}rc.json`, | ||
`.config/${name}rc.cjs`, | ||
...(sync ? [] : [`.config/${name}rc.mjs`]), | ||
`${name}.config.js`, | ||
`${name}.config.cjs`, | ||
...(sync ? [] : [`${name}.config.mjs`]), | ||
]; | ||
@@ -28,17 +31,41 @@ } | ||
} | ||
exports.defaultLoaders = Object.freeze({ | ||
const jsonLoader = (_, content) => JSON.parse(content); | ||
exports.defaultLoadersSync = Object.freeze({ | ||
'.js': require, | ||
'.json': require, | ||
'.cjs': require, | ||
noExt(_, content) { | ||
return JSON.parse(content); | ||
}, | ||
noExt: jsonLoader, | ||
}); | ||
function getExtDesc(ext) { | ||
return ext === 'noExt' ? 'files without extensions' : `extension "${ext}"`; | ||
} | ||
function getOptions(name, options = {}) { | ||
const dynamicImport = async (id) => { | ||
try { | ||
const mod = await eval(`import('${id}')`); | ||
return mod.default; | ||
} | ||
catch (e) { | ||
try { | ||
return require(id); | ||
} | ||
catch (requireE) { | ||
if (requireE.code === 'ERR_REQUIRE_ESM' || | ||
(requireE instanceof SyntaxError && | ||
requireE | ||
.toString() | ||
.includes('Cannot use import statement outside a module'))) { | ||
throw e; | ||
} | ||
throw requireE; | ||
} | ||
} | ||
}; | ||
exports.defaultLoaders = Object.freeze({ | ||
'.js': dynamicImport, | ||
'.mjs': dynamicImport, | ||
'.cjs': dynamicImport, | ||
'.json': jsonLoader, | ||
noExt: jsonLoader, | ||
}); | ||
function getOptions(name, options, sync) { | ||
const conf = { | ||
stopDir: os.homedir(), | ||
searchPlaces: getDefaultSearchPlaces(name), | ||
searchPlaces: getDefaultSearchPlaces(name, sync), | ||
ignoreEmptySearchPlaces: true, | ||
@@ -49,3 +76,6 @@ cache: true, | ||
...options, | ||
loaders: { ...exports.defaultLoaders, ...options.loaders }, | ||
loaders: { | ||
...(sync ? exports.defaultLoadersSync : exports.defaultLoaders), | ||
...options.loaders, | ||
}, | ||
}; | ||
@@ -56,6 +86,6 @@ conf.searchPlaces.forEach(place => { | ||
if (!loader) { | ||
throw new Error(`No loader specified for ${getExtDesc(key)}, so searchPlaces item "${place}" is invalid`); | ||
throw new Error(`Missing loader for extension "${place}"`); | ||
} | ||
if (typeof loader !== 'function') { | ||
throw new Error(`loader for ${getExtDesc(key)} is not a function (type provided: "${typeof loader}"), so searchPlaces item "${place}" is invalid`); | ||
throw new Error(`Loader for extension "${place}" is not a function: Received ${typeof loader}.`); | ||
} | ||
@@ -86,3 +116,3 @@ }); | ||
function lilconfig(name, options) { | ||
const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache, } = getOptions(name, options); | ||
const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache, } = getOptions(name, options !== null && options !== void 0 ? options : {}, false); | ||
const searchCache = new Map(); | ||
@@ -209,3 +239,3 @@ const loadCache = new Map(); | ||
function lilconfigSync(name, options) { | ||
const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache, } = getOptions(name, options); | ||
const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache, } = getOptions(name, options !== null && options !== void 0 ? options : {}, true); | ||
const searchCache = new Map(); | ||
@@ -212,0 +242,0 @@ const loadCache = new Map(); |
{ | ||
"name": "lilconfig", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "A zero-dependency alternative to cosmiconfig", | ||
@@ -12,3 +12,3 @@ "main": "dist/index.js", | ||
"clean": "rm -rf ./dist", | ||
"test": "jest --coverage", | ||
"test": "NODE_OPTIONS=--experimental-vm-modules ./node_modules/.bin/jest --coverage", | ||
"lint": "eslint ./src/*.ts" | ||
@@ -33,15 +33,16 @@ }, | ||
"devDependencies": { | ||
"@types/jest": "^27.0.2", | ||
"@types/node": "^14.18.36", | ||
"@typescript-eslint/eslint-plugin": "^5.54.0", | ||
"@typescript-eslint/parser": "^5.54.0", | ||
"cosmiconfig": "^7.1.0", | ||
"eslint": "^8.35.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"jest": "^27.3.1", | ||
"prettier": "^2.8.4", | ||
"ts-jest": "27.0.7", | ||
"typescript": "4.4.4" | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^14.18.63", | ||
"cosmiconfig": "^8.3.6", | ||
"eslint": "^8.56.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-prettier": "^5.1.3", | ||
"jest": "^29.7.0", | ||
"prettier": "^3.2.5", | ||
"ts-jest": "29.1.2", | ||
"typescript": "^5.3.3", | ||
"typescript-eslint": "^7.0.1", | ||
"uvu": "^0.5.6" | ||
}, | ||
"funding": "https://github.com/sponsors/antonk52", | ||
"engines": { | ||
@@ -48,0 +49,0 @@ "node": ">=14" |
@@ -45,2 +45,6 @@ # Lilconfig ⚙️ | ||
## ESM | ||
ESM configs can be loaded with **async API only**. Specifically `js` files in projects with `"type": "module"` in `package.json` or `mjs` files. | ||
## Difference to `cosmiconfig` | ||
@@ -91,25 +95,2 @@ Lilconfig does not intend to be 100% compatible with `cosmiconfig` but tries to mimic it where possible. The key difference is **no** support for yaml files out of the box(`lilconfig` attempts to parse files with no extension as JSON instead of YAML). You can still add the support for YAML files by providing a loader, see an [example](#yaml-loader) below. | ||
### ESM loader | ||
Lilconfig v2 does not support ESM modules out of the box. However, you can support it with a custom a loader. Note that this will only work with the async `lilconfig` function and won't work with the sync `lilconfigSync`. | ||
```js | ||
import {lilconfig} from 'lilconfig'; | ||
const loadEsm = filepath => import(filepath); | ||
lilconfig('myapp', { | ||
loaders: { | ||
'.js': loadEsm, | ||
'.mjs': loadEsm, | ||
} | ||
}) | ||
.search() | ||
.then(result => { | ||
result // {config, filepath} | ||
result.config.default // if config uses `export default` | ||
}); | ||
``` | ||
## Version correlation | ||
@@ -116,0 +97,0 @@ |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
19813
395
1
99
5
1