summaryrefslogtreecommitdiffstatshomepage
path: root/core/scripts/css/compile.js
blob: a3794cd618ecca1f6bb88b827fd738b567d31265 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const log = require('./log');
const fs = require('node:fs');
const postcss = require('postcss');
const postcssImport = require('postcss-import');
const postcssHeader = require('postcss-header');
const postcssUrl = require('postcss-url');
const postcssPresetEnv = require('postcss-preset-env');
// cspell:ignore pxtorem
const postcssPixelsToRem = require('postcss-pxtorem');
const prettier = require('prettier');
const removeUnwantedComments = require('./remove-unwanted-comments');

module.exports = (filePath, callback) => {
  // Transform the file.
  fs.readFile(filePath, (err, css) => {
    postcss([
      postcssImport({
        plugins: [
          removeUnwantedComments,
        ],
      }),
      postcssPresetEnv({
        stage: 1,
        preserve: false,
        autoprefixer: {
          cascade: false,
          grid: 'no-autoplace',
        },
        features: {
          'blank-pseudo-class': false,
          'focus-visible-pseudo-class': false,
          'focus-within-pseudo-class': false,
          'has-pseudo-class': false,
          'image-set-function': false,
          'prefers-color-scheme-query': false,
          'content-alt-text': false,
          'nesting-rules': { edition: '2021' },
        }
      }),
      postcssPixelsToRem({
          propList: [
            '*',
            '!background-position',
            '!border',
            '!border-width',
            '!box-shadow',
            '!border-top*',
            '!border-right*',
            '!border-bottom*',
            '!border-left*',
            '!border-start*',
            '!border-end*',
            '!outline*',
          ],
          mediaQuery: true,
          minPixelValue: 3,
          // Prevent converting PX to REM for icon styles. These files have been
          // added to use the `postcssUrl` plugin, but aren't compatible with
          // `postcssPixelsToRem`.
          exclude: (filePath) => filePath.match(/core\/modules.*\.icons\..*\.pcss\.css$/)

      }),
      postcssHeader({
        header: `/*\n * DO NOT EDIT THIS FILE.\n * See the following change record for more information,\n * https://www.drupal.org/node/3084859\n * @preserve\n */\n`,
      }),
      postcssUrl({
        filter: '**/*.svg',
        url: 'inline',
        optimizeSvgEncode: true,
      })
    ])
    .process(css, { from: filePath })
    .then(async result => {
      const config = await prettier.resolveConfig(filePath);
      return await prettier.format(result.css, config);
    })
    .then(callback)
    .catch(error => {
      log(error);
      process.exitCode = 1;
    });
  });
};