mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
chore: revamp how we export types (#2118)
This commit is contained in:
@ -1,24 +1,11 @@
|
||||
import os from 'os';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import {
|
||||
VERSION,
|
||||
_collections,
|
||||
builtinPlugins,
|
||||
mapNodesToParents,
|
||||
optimize as optimizeAgnostic,
|
||||
querySelector,
|
||||
querySelectorAll,
|
||||
} from './svgo.js';
|
||||
|
||||
/**
|
||||
* @typedef {import('./svgo.js').Config} Config
|
||||
* @typedef {import('./svgo.js').Output} Output
|
||||
*/
|
||||
import * as svgo from './svgo.js';
|
||||
|
||||
/**
|
||||
* @param {string} configFile
|
||||
* @returns {Promise<Config>}
|
||||
* @returns {Promise<import('./types.js').Config>}
|
||||
*/
|
||||
const importConfig = async (configFile) => {
|
||||
const imported = await import(path.resolve(configFile));
|
||||
@ -43,25 +30,21 @@ const isFile = async (file) => {
|
||||
}
|
||||
};
|
||||
|
||||
export {
|
||||
VERSION,
|
||||
builtinPlugins,
|
||||
querySelector,
|
||||
querySelectorAll,
|
||||
_collections,
|
||||
};
|
||||
export * from './svgo.js';
|
||||
|
||||
/**
|
||||
* @param {string} configFile
|
||||
* @param {string} cwd
|
||||
* @returns {Promise<?Config>}
|
||||
* If you write a tool on top of svgo you might need a way to load svgo config.
|
||||
* You can also specify relative or absolute path and customize current working
|
||||
* directory.
|
||||
*
|
||||
* @type {<T extends string>(configFile: T | null, cwd?: string) => Promise<T extends string ? import('./svgo.js').Config : import('./svgo.js').Config | null>}
|
||||
*/
|
||||
export const loadConfig = async (configFile, cwd = process.cwd()) => {
|
||||
if (configFile != null) {
|
||||
if (path.isAbsolute(configFile)) {
|
||||
return await importConfig(configFile);
|
||||
return importConfig(configFile);
|
||||
} else {
|
||||
return await importConfig(path.join(cwd, configFile));
|
||||
return importConfig(path.join(cwd, configFile));
|
||||
}
|
||||
}
|
||||
let dir = cwd;
|
||||
@ -69,18 +52,19 @@ export const loadConfig = async (configFile, cwd = process.cwd()) => {
|
||||
while (true) {
|
||||
const js = path.join(dir, 'svgo.config.js');
|
||||
if (await isFile(js)) {
|
||||
return await importConfig(js);
|
||||
return importConfig(js);
|
||||
}
|
||||
const mjs = path.join(dir, 'svgo.config.mjs');
|
||||
if (await isFile(mjs)) {
|
||||
return await importConfig(mjs);
|
||||
return importConfig(mjs);
|
||||
}
|
||||
const cjs = path.join(dir, 'svgo.config.cjs');
|
||||
if (await isFile(cjs)) {
|
||||
return await importConfig(cjs);
|
||||
return importConfig(cjs);
|
||||
}
|
||||
const parent = path.dirname(dir);
|
||||
if (dir === parent) {
|
||||
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/33912
|
||||
return null;
|
||||
}
|
||||
dir = parent;
|
||||
@ -88,9 +72,11 @@ export const loadConfig = async (configFile, cwd = process.cwd()) => {
|
||||
};
|
||||
|
||||
/**
|
||||
* The core of SVGO.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Config} config
|
||||
* @returns {Output}
|
||||
* @param {import('./svgo.js').Config=} config
|
||||
* @returns {import('./svgo.js').Output}
|
||||
*/
|
||||
export const optimize = (input, config) => {
|
||||
if (config == null) {
|
||||
@ -99,7 +85,7 @@ export const optimize = (input, config) => {
|
||||
if (typeof config !== 'object') {
|
||||
throw Error('Config should be an object');
|
||||
}
|
||||
return optimizeAgnostic(input, {
|
||||
return svgo.optimize(input, {
|
||||
...config,
|
||||
js2svg: {
|
||||
// platform specific default for end of line
|
||||
@ -108,14 +94,3 @@ export const optimize = (input, config) => {
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default {
|
||||
VERSION,
|
||||
builtinPlugins,
|
||||
loadConfig,
|
||||
optimize,
|
||||
mapNodesToParents,
|
||||
querySelector,
|
||||
querySelectorAll,
|
||||
_collections,
|
||||
};
|
||||
|
Reference in New Issue
Block a user