postcss-load-config
Advanced tools
Comparing version
{ | ||
"name": "postcss-load-config", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Autoload Config for PostCSS", | ||
@@ -18,5 +18,14 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"cosmiconfig": "^7.0.0", | ||
"import-cwd": "^3.0.0" | ||
"import-cwd": "^3.0.0", | ||
"lilconfig": "^2.0.3", | ||
"yaml": "^1.10.2" | ||
}, | ||
"peerDependencies": { | ||
"ts-node": ">=9.0.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"ts-node": { | ||
"optional": true | ||
} | ||
}, | ||
"keywords": [ | ||
@@ -23,0 +32,0 @@ "postcss", |
// based on @types/postcss-load-config@2.0.1 | ||
// Type definitions for postcss-load-config 2.1 | ||
import Processor from 'postcss/lib/processor' | ||
import { Plugin, ProcessOptions, Transformer } from "postcss"; | ||
import { Options as CosmiconfigOptions } from 'cosmiconfig'; | ||
import Processor from 'postcss/lib/processor'; | ||
import { Plugin, ProcessOptions, Transformer } from 'postcss'; | ||
import { Options as ConfigOptions } from "lilconfig"; | ||
// In the ConfigContext, these three options can be instances of the | ||
// appropriate class, or strings. If they are strings, postcss-load-config will | ||
// require() them and pass the instances along. | ||
interface ProcessOptionsPreload { | ||
declare function postcssrc( | ||
ctx?: postcssrc.ConfigContext, | ||
path?: string, | ||
options?: ConfigOptions | ||
): Promise<postcssrc.Result>; | ||
declare namespace postcssrc { | ||
function sync( | ||
ctx?: ConfigContext, | ||
path?: string, | ||
options?: ConfigOptions | ||
): Result; | ||
// In the ConfigContext, these three options can be instances of the | ||
// appropriate class, or strings. If they are strings, postcss-load-config will | ||
// require() them and pass the instances along. | ||
export interface ProcessOptionsPreload { | ||
parser?: string | ProcessOptions['parser']; | ||
stringifier?: string | ProcessOptions['stringifier']; | ||
syntax?: string | ProcessOptions['syntax']; | ||
} | ||
} | ||
// The remaining ProcessOptions, sans the three above. | ||
type RemainingProcessOptions = | ||
Pick<ProcessOptions, Exclude<keyof ProcessOptions, keyof ProcessOptionsPreload>>; | ||
// The remaining ProcessOptions, sans the three above. | ||
export type RemainingProcessOptions = Pick< | ||
ProcessOptions, | ||
Exclude<keyof ProcessOptions, keyof ProcessOptionsPreload> | ||
>; | ||
// Additional context options that postcss-load-config understands. | ||
interface Context { | ||
// Additional context options that postcss-load-config understands. | ||
export interface Context { | ||
cwd?: string; | ||
env?: string; | ||
} | ||
} | ||
// The full shape of the ConfigContext. | ||
type ConfigContext = Context & ProcessOptionsPreload & RemainingProcessOptions; | ||
// The full shape of the ConfigContext. | ||
export type ConfigContext = Context & | ||
ProcessOptionsPreload & | ||
RemainingProcessOptions; | ||
// Result of postcssrc is a Promise containing the filename plus the options | ||
// and plugins that are ready to pass on to postcss. | ||
type ResultPlugin = Plugin | Transformer | Processor; | ||
// Result of postcssrc is a Promise containing the filename plus the options | ||
// and plugins that are ready to pass on to postcss. | ||
export type ResultPlugin = Plugin | Transformer | Processor; | ||
interface Result { | ||
export interface Result { | ||
file: string; | ||
options: ProcessOptions; | ||
plugins: ResultPlugin[]; | ||
} | ||
} | ||
declare function postcssrc(ctx?: ConfigContext, path?: string, options?: CosmiconfigOptions): Promise<Result>; | ||
export type ConfigPlugin = Transformer | Plugin | Processor; | ||
declare namespace postcssrc { | ||
function sync(ctx?: ConfigContext, path?: string, options?: CosmiconfigOptions): Result; | ||
export interface Config { | ||
parser?: string | ProcessOptions['parser'] | false; | ||
stringifier?: string | ProcessOptions['stringifier'] | false; | ||
syntax?: string | ProcessOptions['syntax'] | false; | ||
map?: string | false; | ||
from?: string; | ||
to?: string; | ||
plugins?: Array<ConfigPlugin | false> | Record<string, object | false>; | ||
} | ||
export type ConfigFn = (ctx: ConfigContext) => Config | Promise<Config>; | ||
} | ||
export = postcssrc; |
@@ -5,3 +5,4 @@ 'use strict' | ||
const config = require('cosmiconfig') | ||
const config = require('lilconfig') | ||
const yaml = require('yaml') | ||
@@ -11,2 +12,5 @@ const loadOptions = require('./options.js') | ||
/* istanbul ignore next */ | ||
const interopRequireDefault = (obj) => obj && obj.__esModule ? obj : { default: obj } | ||
/** | ||
@@ -22,3 +26,3 @@ * Process the result from cosmiconfig | ||
const file = result.filepath || '' | ||
let config = result.config || {} | ||
let config = interopRequireDefault(result.config).default || {} | ||
@@ -68,2 +72,55 @@ if (typeof config === 'function') { | ||
const addTypeScriptLoader = (options = {}, loader) => { | ||
const moduleName = 'postcss' | ||
return { | ||
...options, | ||
searchPlaces: [ | ||
...(options.searchPlaces || []), | ||
'package.json', | ||
`.${moduleName}rc`, | ||
`.${moduleName}rc.json`, | ||
`.${moduleName}rc.yaml`, | ||
`.${moduleName}rc.yml`, | ||
`.${moduleName}rc.ts`, | ||
`.${moduleName}rc.js`, | ||
`.${moduleName}rc.cjs`, | ||
`${moduleName}.config.ts`, | ||
`${moduleName}.config.js`, | ||
`${moduleName}.config.cjs` | ||
], | ||
loaders: { | ||
...options.loaders, | ||
'.yaml': (filepath, content) => yaml.parse(content), | ||
'.yml': (filepath, content) => yaml.parse(content), | ||
'.ts': loader | ||
} | ||
} | ||
} | ||
const withTypeScriptLoader = (rcFunc) => { | ||
return (ctx, path, options) => { | ||
return rcFunc(ctx, path, addTypeScriptLoader(options, (configFile) => { | ||
let registerer = { enabled () {} } | ||
try { | ||
// Register TypeScript compiler instance | ||
registerer = require('ts-node').register() | ||
return require(configFile) | ||
} catch (err) { | ||
if (err.code === 'MODULE_NOT_FOUND') { | ||
throw new Error( | ||
`'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}` | ||
) | ||
} | ||
throw err | ||
} finally { | ||
registerer.enabled(false) | ||
} | ||
})) | ||
} | ||
} | ||
/** | ||
@@ -80,3 +137,3 @@ * Load Config | ||
*/ | ||
const rc = (ctx, path, options) => { | ||
const rc = withTypeScriptLoader((ctx, path, options) => { | ||
/** | ||
@@ -92,3 +149,3 @@ * @type {Object} The full Config Context | ||
return config.cosmiconfig('postcss', options) | ||
return config.lilconfig('postcss', options) | ||
.search(path) | ||
@@ -102,5 +159,5 @@ .then((result) => { | ||
}) | ||
} | ||
}) | ||
rc.sync = (ctx, path, options) => { | ||
rc.sync = withTypeScriptLoader((ctx, path, options) => { | ||
/** | ||
@@ -116,3 +173,3 @@ * @type {Object} The full Config Context | ||
const result = config.cosmiconfigSync('postcss', options).search(path) | ||
const result = config.lilconfigSync('postcss', options).search(path) | ||
@@ -124,3 +181,3 @@ if (!result) { | ||
return processResult(ctx, result) | ||
} | ||
}) | ||
@@ -127,0 +184,0 @@ /** |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
336
27.76%21954
-6.2%4
100%7
-12.5%4
33.33%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed