UNPKG

f2e-server3

Version:

f2e-server 3.0

128 lines (127 loc) 5.54 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getConfigEvents = exports.getConfigResult = exports.setConfigPath = exports.F2E_CONFIG = void 0; const _ = __importStar(require("./misc")); const path = __importStar(require("node:path")); const fs = __importStar(require("node:fs")); const node_crypto_1 = __importDefault(require("node:crypto")); const middlewares_1 = require("../middlewares"); const mime_1 = require("./mime"); const templates_1 = require("./templates"); const memory_tree_1 = require("../memory-tree"); let F2E_CONFIG_PATH = ''; exports.F2E_CONFIG = '.f2econfig.js'; const setConfigPath = (path) => F2E_CONFIG_PATH = path; exports.setConfigPath = setConfigPath; const getConfig = (conf = {}) => { let pathname = F2E_CONFIG_PATH.startsWith('/') ? F2E_CONFIG_PATH : path.join(process.cwd(), exports.F2E_CONFIG); if (fs.existsSync(pathname)) { conf = { ...require(pathname), ...conf, }; } if (conf.mimeTypes) (0, mime_1.setMimeTypes)(conf.mimeTypes); return conf; }; /** 保留基础配置 */ const getConfigResult = function (conf = {}) { conf = getConfig(conf); const mode = conf.mode || "prod"; const config = { mode, port: conf.port || 2850, host: conf.host || '0.0.0.0', root: conf.root || process.cwd(), ssl: conf.ssl || false, gzip: conf.gzip || false, gzip_filter: conf.gzip_filter || function (pathname, size) { return _.isText(pathname) && size > 4096; }, cache_filter: conf.cache_filter || function (pathname, size) { return !/\.html?$/.test(pathname); }, watch: typeof conf.watch === 'boolean' ? conf.watch : mode === 'dev', watch_timeout: conf.watch_timeout || 100, onServerCreate: conf.onServerCreate || function (server) { return server; }, namehash: { entries: ['\\.html$'], searchValue: ['\\s(?:src)="([^"]*?)"', '\\s(?:href)="([^"]*?)"'], replacer: (output, hash) => `/${output}?${hash}`, ...(conf.namehash || {}) }, mimeTypes: conf.mimeTypes || {}, dest: conf.dest || path.join(process.cwd(), './output'), range_size: conf.range_size || 1024 * 1024 * 10, headers: conf.headers || {}, page_404: conf.page_404 || templates_1.page_404, page_50x: conf.page_50x || templates_1.page_500, page_dir: conf.page_dir || templates_1.page_dir, // 以下为内置中间件相关配置 try_files: conf.try_files || false, livereload: conf.livereload || (mode === 'dev' && {}) || false, proxies: conf.proxies || [], esbuild: mode != 'prod' ? { esbuildrc: '.esbuildrc.js', build_external: true, with_metafile: false, ...(conf.esbuild || {}) } : false, less: conf.less || false, auth: conf.auth || false, alias: conf.alias || false, postcss: conf.postcss || false, include: conf.include || false, hash_file: conf.hash_file || 'package-lock.json', system_hash: generate_system_hash(conf.root || process.cwd(), conf.hash_file || 'package-lock.json'), }; return config; }; exports.getConfigResult = getConfigResult; const generate_system_hash = (root, hash_file) => { const path_lock = path.join(root, hash_file); if (!fs.existsSync(path_lock)) { return ''; } const package_lock = fs.readFileSync(path_lock); return node_crypto_1.default.createHash('md5').update(package_lock).digest('hex'); }; /** 整理中间件配置 */ const getConfigEvents = (conf = {}) => { conf = getConfig(conf); const { buildFilter = memory_tree_1.defaultOptions.buildFilter, watchFilter = memory_tree_1.defaultOptions.watchFilter, outputFilter, onGet, onSet, onRoute, beforeRoute, buildWatcher, middlewares = [] } = conf; const middlewareBase = { name: "system", mode: ["dev", "build", "prod"], execute: () => { return { buildFilter, watchFilter, outputFilter, onGet, onSet, onRoute, beforeRoute, buildWatcher, }; } }; return (0, middlewares_1.combineMiddleware)((0, exports.getConfigResult)(conf), [middlewareBase, ...middlewares]); }; exports.getConfigEvents = getConfigEvents;