summaryrefslogtreecommitdiff
path: root/node_modules/svgo/lib/svgo-node.js
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2023-10-14 15:05:55 +0800
committerlittleblack111 <littleblack11111@gmail.com>2023-10-14 15:05:55 +0800
commitc06f7ee0a40255214c477e13d9b956a7e07c2900 (patch)
treef5982a568ef8fab865d0142e71c7494fea3e4c1b /node_modules/svgo/lib/svgo-node.js
parent3fd9400d4dccc25aa29b5702216732e0b7542073 (diff)
postcss,(plugins): nanocss, postcss-preset-env
Diffstat (limited to 'node_modules/svgo/lib/svgo-node.js')
-rw-r--r--node_modules/svgo/lib/svgo-node.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/node_modules/svgo/lib/svgo-node.js b/node_modules/svgo/lib/svgo-node.js
new file mode 100644
index 00000000..fc3ba8ab
--- /dev/null
+++ b/node_modules/svgo/lib/svgo-node.js
@@ -0,0 +1,85 @@
+'use strict';
+
+const os = require('os');
+const fs = require('fs');
+const { pathToFileURL } = require('url');
+const path = require('path');
+const { optimize: optimizeAgnostic } = require('./svgo.js');
+
+const importConfig = async (configFile) => {
+ let config;
+ // at the moment dynamic import may randomly fail with segfault
+ // to workaround this for some users .cjs extension is loaded
+ // exclusively with require
+ if (configFile.endsWith('.cjs')) {
+ config = require(configFile);
+ } else {
+ // dynamic import expects file url instead of path and may fail
+ // when windows path is provided
+ const { default: imported } = await import(pathToFileURL(configFile));
+ config = imported;
+ }
+ if (config == null || typeof config !== 'object' || Array.isArray(config)) {
+ throw Error(`Invalid config file "${configFile}"`);
+ }
+ return config;
+};
+
+const isFile = async (file) => {
+ try {
+ const stats = await fs.promises.stat(file);
+ return stats.isFile();
+ } catch {
+ return false;
+ }
+};
+
+const loadConfig = async (configFile, cwd = process.cwd()) => {
+ if (configFile != null) {
+ if (path.isAbsolute(configFile)) {
+ return await importConfig(configFile);
+ } else {
+ return await importConfig(path.join(cwd, configFile));
+ }
+ }
+ let dir = cwd;
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const js = path.join(dir, 'svgo.config.js');
+ if (await isFile(js)) {
+ return await importConfig(js);
+ }
+ const mjs = path.join(dir, 'svgo.config.mjs');
+ if (await isFile(mjs)) {
+ return await importConfig(mjs);
+ }
+ const cjs = path.join(dir, 'svgo.config.cjs');
+ if (await isFile(cjs)) {
+ return await importConfig(cjs);
+ }
+ const parent = path.dirname(dir);
+ if (dir === parent) {
+ return null;
+ }
+ dir = parent;
+ }
+};
+exports.loadConfig = loadConfig;
+
+const optimize = (input, config) => {
+ if (config == null) {
+ config = {};
+ }
+ if (typeof config !== 'object') {
+ throw Error('Config should be an object');
+ }
+ return optimizeAgnostic(input, {
+ ...config,
+ js2svg: {
+ // platform specific default for end of line
+ eol: os.EOL === '\r\n' ? 'crlf' : 'lf',
+ ...config.js2svg,
+ },
+ });
+};
+exports.optimize = optimize;