'use strict'; var caniuse = require('caniuse-lite'); var browserslist = require('browserslist'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var browserslist__default = /*#__PURE__*/_interopDefaultLegacy(browserslist); const NO = "No"; const APPLE_SYSTEM = "AppleSystem"; const BLINK_MAC_SYSTEM_FONT = "BlinkMacSystemFont"; function computePolyfillFlagFromStatus(statusString) { const flags = []; const statusArr = Array.from(statusString.split(" ")); for (const status of statusArr) { if (status === "y") { return []; } else if (status.includes("n")) { flags.push(NO); } else if (status.includes("#1")) { // #1: Supported as the -apple-system value (only on Mac) flags.push(APPLE_SYSTEM); } else if (status.includes("#2")) { // #2: Supported as the 'BlinkMacSystemFont' value (only on Mac) flags.push(BLINK_MAC_SYSTEM_FONT); } } return flags; } function generatePolyfillFlags(stats, supportedBrowsers) { const polyfillFlags = { [APPLE_SYSTEM]: false, [BLINK_MAC_SYSTEM_FONT]: false, [NO]: false }; const visitedStatus = {}; for (const browserAndVersion of supportedBrowsers) { const [browser, version] = browserAndVersion.split(" "); const status = stats[browser][version]; if (status === undefined || visitedStatus[status]) { continue; } visitedStatus[status] = true; const shouldPolyfillFlags = computePolyfillFlagFromStatus(status); shouldPolyfillFlags.forEach(flag => { polyfillFlags[flag] = true; }); } return polyfillFlags; } function getSystemUiFamily(supportedBrowsers, preserve) { const { stats } = caniuse.feature(caniuse.features["font-family-system-ui"]); const polyfillFlags = generatePolyfillFlags(stats, supportedBrowsers); const polyfill = [[APPLE_SYSTEM, ["-apple-system" // macOS, Safari >= 9.2 < 11, Firefox >= 43 ]], [BLINK_MAC_SYSTEM_FONT, ["BlinkMacSystemFont" // macOS, Chrome < 56 ]], [NO, ["Segoe UI", // Windows >= Vista "Roboto", // Android >= 4 "Noto Sans", // Plasma >= 5.5 "Ubuntu", // Ubuntu >= 10.10 "Cantarell", // GNOME >= 3 "Helvetica Neue"]]]; const result = [].concat.apply([], polyfill.filter(([flag]) => polyfillFlags[flag]).map(v => v[1])); if (!result.length || preserve) { result.unshift("system-ui"); } return result; } // tooling const creator = opts => { const { family, preserve = true, browsers } = Object(opts); let systemUiFamily = family; if (typeof family === "string") { systemUiFamily = family.trim().split(/\s*,\s*/); } else if (!family) { // browsers supported by the configuration const supportedBrowsers = browserslist__default['default'](browsers, { ignoreUnknownVersions: true }); systemUiFamily = getSystemUiFamily(supportedBrowsers, preserve); } // system-ui and fallbacks match const whitespace = "[\\f\\n\\r\\x09\\x20]"; const systemUiMatch = new RegExp(`(^|,|${whitespace}+)(?:system-ui${whitespace}*)(?:,${whitespace}*(?:${systemUiFamily.join("|")})${whitespace}*)?(,|$)`, "i"); // system-ui fallback replacement const systemUiReplace = `$1${systemUiFamily.join(", ")}$2`; const visited = new WeakSet(); return { postcssPlugin: "postcss-font-family-system-ui", // update font declarations to polyfill system-ui usage Declaration(decl) { if (decl.prop.match(fontPropertyMatch) && !visited.has(decl)) { decl.value = decl.value.replace(systemUiMatch, systemUiReplace); visited.add(decl); } } }; }; // Required by PostCSS 8 creator.postcss = true; /* match the following properties: * - font * - font-family * - custom properties (see https://www.w3.org/TR/css-variables-1/#custom-property) */ const fontPropertyMatch = /(?:^(?:-|\\002d){2})|(?:^font(?:-family)?$)/i; module.exports = creator;