UNPKG

@codelet/cli-service

Version:
84 lines (83 loc) 3.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getConfig = exports.parseArgv = exports.getOptionValue = exports.parseDir = exports.resolve = exports.cmd = void 0; const fast_glob_1 = __importDefault(require("fast-glob")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const webpack_merge_1 = __importDefault(require("webpack-merge")); const config_1 = require("./config"); exports.cmd = process.cwd(); const resolve = (...args) => path_1.default.resolve(exports.cmd, ...args); exports.resolve = resolve; const parseDir = (entryPath, source) => { entryPath = (0, exports.resolve)(entryPath); const filepaths = fast_glob_1.default.sync(source.map((item) => (0, exports.resolve)(entryPath, item))); const entry = filepaths.reduce((res, filepath) => { const relPath = path_1.default.relative(entryPath, filepath); const { dir, name } = path_1.default.parse(relPath); res[path_1.default.join(dir, name)] = { import: filepath, runtime: 'bundle', }; return res; }, {}); return { entry }; }; exports.parseDir = parseDir; function getOptionValue(argv, option) { const index = argv.findIndex((item) => item === option); if (index > -1) { return argv[index + 1]; } } exports.getOptionValue = getOptionValue; function parseArgv(argv) { const isWatch = argv.includes('--watch'); const isDev = argv.includes('dev'); const pageIndex = getOptionValue(argv, '--pageIndex') || ''; const configRelativePath = getOptionValue(argv, '--config') || 'codelet.config.js'; const configPath = (0, exports.resolve)(configRelativePath); return { configPath, isWatch, pageIndex, isDev }; } exports.parseArgv = parseArgv; function getConfig(options) { const { configPath, isWatch } = options; let config; if (fs_1.default.existsSync(configPath)) { const cfg = require(configPath); if (typeof cfg === 'function') { config = (0, config_1.getDefaultConfig)(options); config = cfg(config); } else { if (options.pageIndex) { cfg.pageIndex = options.pageIndex; } if (options.isDev) { cfg.isDev = options.isDev; } config = (0, config_1.getDefaultConfig)(cfg); config = { ...config, ...cfg, ...(0, webpack_merge_1.default)(config.webpack ?? {}, cfg.webpack ?? {}), }; } } else { config = (0, config_1.getDefaultConfig)(options); } const { entryPath, source, webpack } = config; if (!webpack.entry) { const { entry } = (0, exports.parseDir)(entryPath, source); webpack.entry = entry; } if (isWatch) { webpack.watch = true; } return webpack; } exports.getConfig = getConfig;