[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | const os = require('os');
|
---|
| 4 | const fs = require('fs');
|
---|
| 5 | const { pathToFileURL } = require('url');
|
---|
| 6 | const path = require('path');
|
---|
| 7 | const {
|
---|
| 8 | extendDefaultPlugins,
|
---|
| 9 | optimize: optimizeAgnostic,
|
---|
| 10 | createContentItem,
|
---|
| 11 | } = require('./svgo.js');
|
---|
| 12 |
|
---|
| 13 | exports.extendDefaultPlugins = extendDefaultPlugins;
|
---|
| 14 | exports.createContentItem = createContentItem;
|
---|
| 15 |
|
---|
| 16 | const importConfig = async (configFile) => {
|
---|
| 17 | let config;
|
---|
| 18 | try {
|
---|
| 19 | // dynamic import expects file url instead of path and may fail
|
---|
| 20 | // when windows path is provided
|
---|
| 21 | const { default: imported } = await import(pathToFileURL(configFile));
|
---|
| 22 | config = imported;
|
---|
| 23 | } catch (importError) {
|
---|
| 24 | // TODO remove require in v3
|
---|
| 25 | try {
|
---|
| 26 | config = require(configFile);
|
---|
| 27 | } catch (requireError) {
|
---|
| 28 | // throw original error if es module is detected
|
---|
| 29 | if (requireError.code === 'ERR_REQUIRE_ESM') {
|
---|
| 30 | throw importError;
|
---|
| 31 | } else {
|
---|
| 32 | throw requireError;
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 | if (config == null || typeof config !== 'object' || Array.isArray(config)) {
|
---|
| 37 | throw Error(`Invalid config file "${configFile}"`);
|
---|
| 38 | }
|
---|
| 39 | return config;
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | const isFile = async (file) => {
|
---|
| 43 | try {
|
---|
| 44 | const stats = await fs.promises.stat(file);
|
---|
| 45 | return stats.isFile();
|
---|
| 46 | } catch {
|
---|
| 47 | return false;
|
---|
| 48 | }
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | const loadConfig = async (configFile, cwd = process.cwd()) => {
|
---|
| 52 | if (configFile != null) {
|
---|
| 53 | if (path.isAbsolute(configFile)) {
|
---|
| 54 | return await importConfig(configFile);
|
---|
| 55 | } else {
|
---|
| 56 | return await importConfig(path.join(cwd, configFile));
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | let dir = cwd;
|
---|
| 60 | // eslint-disable-next-line no-constant-condition
|
---|
| 61 | while (true) {
|
---|
| 62 | const js = path.join(dir, 'svgo.config.js');
|
---|
| 63 | if (await isFile(js)) {
|
---|
| 64 | return await importConfig(js);
|
---|
| 65 | }
|
---|
| 66 | const mjs = path.join(dir, 'svgo.config.mjs');
|
---|
| 67 | if (await isFile(mjs)) {
|
---|
| 68 | return await importConfig(mjs);
|
---|
| 69 | }
|
---|
| 70 | const cjs = path.join(dir, 'svgo.config.cjs');
|
---|
| 71 | if (await isFile(cjs)) {
|
---|
| 72 | return await importConfig(cjs);
|
---|
| 73 | }
|
---|
| 74 | const parent = path.dirname(dir);
|
---|
| 75 | if (dir === parent) {
|
---|
| 76 | return null;
|
---|
| 77 | }
|
---|
| 78 | dir = parent;
|
---|
| 79 | }
|
---|
| 80 | };
|
---|
| 81 | exports.loadConfig = loadConfig;
|
---|
| 82 |
|
---|
| 83 | const optimize = (input, config) => {
|
---|
| 84 | if (config == null) {
|
---|
| 85 | config = {};
|
---|
| 86 | }
|
---|
| 87 | if (typeof config !== 'object') {
|
---|
| 88 | throw Error('Config should be an object');
|
---|
| 89 | }
|
---|
| 90 | return optimizeAgnostic(input, {
|
---|
| 91 | ...config,
|
---|
| 92 | js2svg: {
|
---|
| 93 | // platform specific default for end of line
|
---|
| 94 | eol: os.EOL === '\r\n' ? 'crlf' : 'lf',
|
---|
| 95 | ...config.js2svg,
|
---|
| 96 | },
|
---|
| 97 | });
|
---|
| 98 | };
|
---|
| 99 | exports.optimize = optimize;
|
---|