|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +// Inlines the upstream postcss-preset-infima and the gulp styles and rtl tasks |
| 9 | +// See https://github.com/facebookincubator/infima/blob/87bc1dbfde5dfb2a70b5ff105b5f2f5ee130c2ab/packages/postcss-preset-infima/index.js |
| 10 | + |
| 11 | +import fs from 'node:fs/promises'; |
| 12 | +import path from 'node:path'; |
| 13 | +import postcss from 'postcss'; |
| 14 | +import postcssColorModFunction from 'postcss-color-mod-function'; |
| 15 | +import postcssCombineDuplicatedSelectors from 'postcss-combine-duplicated-selectors'; |
| 16 | +import postcssEach from 'postcss-each'; |
| 17 | +import postcssFor from 'postcss-for'; |
| 18 | +import postcssImport from 'postcss-import'; |
| 19 | +import postcssMixins from 'postcss-mixins'; |
| 20 | +import postcssNested from 'postcss-nested'; |
| 21 | +import postcssNestedAncestors from 'postcss-nested-ancestors'; |
| 22 | +import postcssPresetEnv from 'postcss-preset-env-v7'; |
| 23 | +import postcssSortMediaQueries from 'postcss-sort-media-queries'; |
| 24 | +import rtlcss from 'rtlcss'; |
| 25 | + |
| 26 | +const packageDir = path.dirname(import.meta.dirname); |
| 27 | +const entryFile = path.join(packageDir, 'styles/themes/default/default.pcss'); |
| 28 | +const outputFile = path.join(packageDir, 'dist/infima.css'); |
| 29 | +const rtlOutputFile = path.join(packageDir, 'dist/infima-rtl.css'); |
| 30 | + |
| 31 | +async function main() { |
| 32 | + const source = await fs.readFile(entryFile, 'utf8'); |
| 33 | + const css = await compileInfima(source, entryFile); |
| 34 | + await fs.mkdir(path.dirname(outputFile), {recursive: true}); |
| 35 | + await fs.writeFile(outputFile, css); |
| 36 | + await fs.writeFile(rtlOutputFile, rtlcss.process(css)); |
| 37 | +} |
| 38 | + |
| 39 | +async function compileInfima(source, from) { |
| 40 | + const result = await postcss(getPlugins()).process(source, {from}); |
| 41 | + return result.css; |
| 42 | +} |
| 43 | + |
| 44 | +// The plugin order matters |
| 45 | +function getPlugins() { |
| 46 | + return [ |
| 47 | + postcssImport, |
| 48 | + // TODO postcss-each v1 doesn't seem to be compatible due to color-mod. |
| 49 | + // Need to upgrade later |
| 50 | + postcssEach, |
| 51 | + postcssFor, |
| 52 | + postcssMixins({mixins: {transition: transitionMixin}}), |
| 53 | + postcssNestedAncestors, |
| 54 | + postcssNested, |
| 55 | + // color-mod has been removed from the spec, so it's not included in |
| 56 | + // preset-env. Using a separate plugin instead. We probably would eventually |
| 57 | + // get rid of color-mod |
| 58 | + postcssColorModFunction({unresolved: 'ignore'}), |
| 59 | + postcssPresetEnv({ |
| 60 | + stage: 1, |
| 61 | + features: { |
| 62 | + 'custom-properties': false, |
| 63 | + }, |
| 64 | + }), |
| 65 | + postcssSortMediaQueries, |
| 66 | + postcssCombineDuplicatedSelectors, |
| 67 | + ]; |
| 68 | +} |
| 69 | + |
| 70 | +// @mixin transition opacity visibility, var(--ifm-transition-fast), linear; |
| 71 | +function transitionMixin( |
| 72 | + _mixin, |
| 73 | + properties, |
| 74 | + duration = 'var(--ifm-transition-fast)', |
| 75 | + timing = 'var(--ifm-transition-timing-default)', |
| 76 | +) { |
| 77 | + if (properties.includes(' ')) { |
| 78 | + return { |
| 79 | + 'transition-property': properties.replace(/\s/g, ', '), |
| 80 | + 'transition-duration': duration, |
| 81 | + 'transition-timing-function': timing, |
| 82 | + }; |
| 83 | + } |
| 84 | + return {transition: [properties, duration, timing].join(' ')}; |
| 85 | +} |
| 86 | + |
| 87 | +await main(); |
0 commit comments