UNPKG

f2e-server3

Version:

f2e-server 3.0

97 lines (96 loc) 3.89 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 }); const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const _ = __importStar(require("../../utils/misc")); const utils_1 = require("../../utils"); const node_process_1 = require("node:process"); const middleware_postcss = { mode: ["dev", "build"], name: "postcss", execute: async (conf) => { if (!conf.postcss) { return; } const postcss = await (0, utils_1.dynamicImport)('postcss'); const processer = postcss(); const { entryPoints, plugins = [], processOptions = {}, watchFilter } = conf.postcss; plugins.forEach(plugin => { processer.use(plugin); }); const [origin, output] = (typeof entryPoints === 'string' ? [entryPoints, entryPoints] : [entryPoints.in, entryPoints.out]).map(_.pathname_fixer); const realPath = node_path_1.default.join(conf.root, origin); if (!node_fs_1.default.existsSync(realPath)) { utils_1.logger.error(`PostCss file ${realPath} not exists!`); (0, node_process_1.exit)(1); } const build = async function (store) { const data = node_fs_1.default.readFileSync(realPath, 'utf-8'); try { const result = await processer.process(data, { from: origin, map: { inline: false }, ...processOptions, }); if (result.css) { store.save({ originPath: origin, outputPath: output, data: result.css + '', }); } if (result.map) { const map = result.map.toString(); const mapPath = output.replace(/\.css$/, '.css.map'); store.save({ originPath: mapPath, outputPath: mapPath, data: map + '', }); } } catch (e) { utils_1.logger.error(e); if (conf.mode === 'build') { (0, node_process_1.exit)(1); } } }; return { onMemoryLoad: async (store) => { await build(store); }, buildWatcher: (pathname, eventType, __build, store) => { if (pathname === origin || !watchFilter || watchFilter(pathname)) { build(store); } } }; } }; exports.default = middleware_postcss;