1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-31 07:44:22 +03:00

chore: convert project to us ECMAScript modules (preserving CJS compatibility) (#1905)

This commit is contained in:
Jon Dufresne
2024-01-03 08:17:37 -08:00
committed by GitHub
parent 252b28c4bf
commit 2442f74239
103 changed files with 1147 additions and 1058 deletions

View File

@ -1,24 +1,15 @@
'use strict';
const os = require('os');
const fs = require('fs');
const { pathToFileURL } = require('url');
const path = require('path');
const { optimize: optimizeAgnostic } = require('./svgo.js');
import os from 'os';
import fs from 'fs';
import { pathToFileURL } from 'url';
import path from 'path';
import { optimize as optimizeAgnostic } from './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;
}
// dynamic import expects file url instead of path and may fail
// when windows path is provided
const imported = await import(pathToFileURL(configFile));
const config = imported.default;
if (config == null || typeof config !== 'object' || Array.isArray(config)) {
throw Error(`Invalid config file "${configFile}"`);
}
@ -34,7 +25,7 @@ const isFile = async (file) => {
}
};
const loadConfig = async (configFile, cwd = process.cwd()) => {
export const loadConfig = async (configFile, cwd = process.cwd()) => {
if (configFile != null) {
if (path.isAbsolute(configFile)) {
return await importConfig(configFile);
@ -64,9 +55,8 @@ const loadConfig = async (configFile, cwd = process.cwd()) => {
dir = parent;
}
};
exports.loadConfig = loadConfig;
const optimize = (input, config) => {
export const optimize = (input, config) => {
if (config == null) {
config = {};
}
@ -82,4 +72,3 @@ const optimize = (input, config) => {
},
});
};
exports.optimize = optimize;