UNPKG

image-asset-manager

Version:

A comprehensive image asset management tool for frontend projects

190 lines (185 loc) 5.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigManager = exports.DEFAULT_CONFIG = void 0; const fs_1 = require("fs"); const path_1 = require("path"); exports.DEFAULT_CONFIG = { scan: { include: ["**/*.{svg,png,jpg,jpeg,gif,webp,ico,bmp,tiff}"], exclude: [ "node_modules/**", ".git/**", "dist/**", "build/**", ".next/**", ".nuxt/**", ], extensions: [ "svg", "png", "jpg", "jpeg", "gif", "webp", "ico", "bmp", "tiff", ], maxDepth: 10, followSymlinks: false, }, optimize: { png: { quality: 80, progressive: true }, jpg: { quality: 85, progressive: true }, svg: { removeComments: true, minifyStyles: true }, webp: { quality: 80, lossless: false }, keepOriginal: true, }, server: { port: 3000, host: "localhost", cors: true, }, generate: { frameworks: ["react"], outputDir: "src/assets", typescript: true, componentPrefix: "Icon", }, cache: { enabled: true, ttl: 3600, // 1小时 directory: ".image-asset-cache", }, watch: { enabled: true, debounce: 300, ignored: ["node_modules/**", ".git/**"], }, }; class ConfigManager { constructor(configPath) { this.config = this.loadConfig(configPath); } loadConfig(configPath) { let userConfig = {}; // 尝试加载配置文件 const configFiles = [ configPath, "image-asset.config.js", "image-asset.config.json", ".image-asset.json", "package.json", // 从 package.json 的 imageAssetManager 字段读取 ].filter(Boolean); for (const file of configFiles) { if (file && (0, fs_1.existsSync)(file)) { try { if (file === "package.json") { const pkg = JSON.parse((0, fs_1.readFileSync)(file, "utf-8")); if (pkg.imageAssetManager) { userConfig = pkg.imageAssetManager; break; } } else if (file.endsWith(".json")) { userConfig = JSON.parse((0, fs_1.readFileSync)(file, "utf-8")); break; } else if (file.endsWith(".js")) { // 对于 JS 配置文件,需要动态导入 delete require.cache[require.resolve((0, path_1.join)(process.cwd(), file))]; userConfig = require((0, path_1.join)(process.cwd(), file)); break; } } catch (error) { console.warn(`Warning: Failed to load config from ${file}:`, error); } } } // 深度合并配置 return this.mergeConfig(exports.DEFAULT_CONFIG, userConfig); } mergeConfig(defaultConfig, userConfig) { const merged = { ...defaultConfig }; for (const [key, value] of Object.entries(userConfig)) { if (value && typeof value === "object" && !Array.isArray(value)) { merged[key] = { ...merged[key], ...value, }; } else if (value !== undefined) { merged[key] = value; } } return merged; } getConfig() { return this.config; } updateConfig(updates) { this.config = this.mergeConfig(this.config, updates); } // 将配置转换为 CLI 选项 toCLIOptions(overrides = {}) { const config = this.getConfig(); return { projectPath: overrides.projectPath || process.cwd(), port: overrides.port !== undefined && !isNaN(overrides.port) ? overrides.port : config.server.port, config: overrides.config, exclude: overrides.exclude && overrides.exclude.length > 0 ? overrides.exclude : config.scan.exclude, include: overrides.include && overrides.include.length > 0 ? overrides.include : config.scan.include, watch: overrides.watch !== undefined ? overrides.watch : config.watch.enabled, optimize: overrides.optimize || false, ...overrides, }; } // 生成示例配置文件 static generateExampleConfig() { return `// image-asset.config.js module.exports = { scan: { include: ['src/**/*.{svg,png,jpg,jpeg,gif,webp}'], exclude: ['node_modules/**', 'dist/**'], extensions: ['svg', 'png', 'jpg', 'jpeg', 'gif', 'webp'], maxDepth: 10, followSymlinks: false, }, optimize: { png: { quality: 80, progressive: true }, jpg: { quality: 85, progressive: true }, svg: { removeComments: true, minifyStyles: true }, webp: { quality: 80, lossless: false }, keepOriginal: true, }, server: { port: 3000, host: 'localhost', cors: true, }, generate: { frameworks: ['react'], outputDir: 'src/assets', typescript: true, componentPrefix: 'Icon', }, cache: { enabled: true, ttl: 3600, directory: '.image-asset-cache', }, watch: { enabled: true, debounce: 300, ignored: ['node_modules/**', '.git/**'], }, };`; } } exports.ConfigManager = ConfigManager; //# sourceMappingURL=index.js.map